我一直在使用postgres数据库构建rails应用程序,但我当然也被分配了从旧mysql数据库导入数据的任务。Rails 4.1.1使用Mysql遗留数据库的Postgres应用程序导入
的方式我一直在处理这个至今:
# config/database.yml
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 10
username: myuser
password:
legacy_development:
adapter: mysql2
encoding: utf8
reconnect: false
database: myapp_legacy
pool: 10
username: myuser
password:
socket: /tmp/mysql.sock
# app/models/legacy.rb
class Legacy < ActiveRecord::Base
self.abstract_class = true
establish_connection "legacy_#{Rails.env}".to_sym
def self.import
self.find_each do |object|
imported_model = object.class.model.new object.attribute_map
object.report_failures unless imported_model.save
end
end
def self.import_all
Rails.application.eager_load!
self.subclasses.each {|subclass| subclass.import }
end
end
# app/models/legacy/chapter.rb
# a bunch of different subclasses like this
class Legacy::Chapter < Legacy
self.table_name = 'chapters'
def self.model
'Chapter'.constantize
end
def attribute_map
{
id: id,
name: name,
body: chapterBody
}
end
end
然后我有一个运行Legacy.import_all
rake任务。很多这是从this post被盗。
有几件事情不对的:
的主要问题是,当我运行Legacy.import_all
它使得通过大约一半的表的话,我得到这样一个错误:
NoMethodError: undefined method 'import' for Legacy::SomeSubclass(Table doesn't exist):Class
我认为这是因为我们在游泳池中的连接太多。看起来好像它正在从postgres数据库中寻找SomeSubClass table_name
,但它应该在mysql数据库上查找。
这是可能是因为像方法:在上面的子类
def self.model
'Chapter'.constantize
end
。我做这样的代替:
def self.model
Chapter
end
,因为我在我的应用程序也被称为章有一个正常模式(非传统),我行驶到范围的问题有作为。
反正这是一个巨大的混乱和任何想法,我应该挖掘将不胜感激。
感谢
我想用传统阶级和旧式模块 – cvibha