ruby Camaleon CMS将sqlite3迁移到mysql DB

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Camaleon CMS将sqlite3迁移到mysql DB相关的知识,希望对你有一定的参考价值。

#Take it line by line
pending_line = []
query_end = ');' # take care with insert content's ending with ");"
ARGF.each do |line|
  # fix for: mapping values are not allowed in this context at (content used by rails for serialized model attributes)
  if line.start_with?('INSERT INTO') && !line.strip.end_with?(query_end)
    pending_line = [line.gsub("\n", '')]
    next
  end
  if pending_line.length > 0 && !line.strip.end_with?(query_end)
    pending_line << line.gsub("\n", '')
    next
  end
  if pending_line.length > 0
    line = (pending_line << line).join('\\n')
    pending_line = []
  end
  # end fix

  # Remove lines not included in MySQL
  if line.start_with? "PRAGMA" \
  or line.start_with? "COMMIT" \
  or line.start_with? "BEGIN TRANSACTION" \
  or line.start_with? "CREATE UNIQUE INDEX" \
  or line.start_with? "DELETE FROM sqlite_sequence" \
  or line.start_with? "INSERT INTO \"sqlite_sequence\""
    next
  end

  line = line.gsub(", 't'",", 1")
  line = line.gsub(",'t'",", 1")
  line = line.gsub(", 'f'",", 0")
  line = line.gsub(",'f'",", 0")

  if line.start_with?('CREATE TABLE') # create table rows
    line = line.gsub('autoincrement','auto_increment')
    line = line.gsub('AUTOINCREMENT','AUTO_INCREMENT')

    line = line.gsub("DEFAULT 't'", "DEFAULT '1'")
    line = line.gsub("DEFAULT 'f'", "DEFAULT '0'")

    line = line.gsub(/ varchar(?!\()/, ' varchar(255)')
    line = line.gsub('"',"`")

    #Fix problems with sqlite3 numbers by making (almost) every INTEGER a mysql BIGINT type
    line = line.gsub("INTEGER", "BIGINT") unless line.include? " id " or line.include? " ID "

    #replace datetime('now') function from sqlite with mysql equivalent
    #datetime is sometimes wrapped, so try to remove the extra () first
    line = line.gsub("(datetime('now'))","NOW()")
    #then replace the unwrapped function
    line = line.gsub("datetime('now')","NOW()")

    line = line.gsub("datetime default","TIMESTAMP DEFAULT")
    line = line.gsub("DATETIME DEFAULT","TIMESTAMP DEFAULT")

    # remove default value for text formats
    line = line.gsub(/` text DEFAULT '(.*)'/, '` text')
  end

  if line.start_with?('INSERT INTO ') # insert rows
    tmp = line.split(' VALUES')
    tmp[0] = tmp.first.gsub('"',"`")
    line = tmp.join(' VALUES')
  end

  if line.start_with? 'CREATE INDEX ' # create indexes fix
    line = line.gsub('"',"`")
  end

  #Write line to standard out
  $stdout.write line
end

以上是关于ruby Camaleon CMS将sqlite3迁移到mysql DB的主要内容,如果未能解决你的问题,请参考以下文章

ruby Camaleon CMS创建自己的自定义字段(简单国家选择)

如何在 Windows 上为 Ruby 安装 sqlite3?

Ubuntu上的sqlite3-ruby安装错误

将带有 SQLite3 的 Rails 推送到 Heroku 失败 [重复]

Ruby:SQLite3::BusyException:数据库被锁定:

ruby sqlite3连接数据库