寻求将我的所有共享模型移动到可以包含在我的每个微应用程序中的引擎.
该引擎应为我们的所有遗留数据提供模型层,包括:
模型文件
架构文件
迁移(我们遵循Pivotal Labs的模式,这不是问题)
模型文件正在自动修补,没关系.
使用Nikolay Strum的db.rake对模式文件进行了修补:
namespace :db do namespace :schema do # desc 'Dump additional database schema' task :dump => [:environment, :load_config] do filename = "#{Rails.root}/db/foo_schema.rb" File.open(filename, 'w:utf-8') do |file| ActiveRecord::Base.establish_connection("foo_#{Rails.env}") ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file) end end end namespace :test do # desc 'Purge and load foo_test schema' task :load_schema do # like db:test:purge abcs = ActiveRecord::Base.configurations ActiveRecord::Base.connection.recreate_database(abcs['foo_test']['database'], mysql_creation_options(abcs['foo_test'])) # like db:test:load_schema ActiveRecord::Base.establish_connection('foo_test') ActiveRecord::Schema.verbose = false load("#{Rails.root}/db/foo_schema.rb") end end end
我们需要rake db:create
和rake db:schema:load
工作,
这些db.rake
补丁仅影响db:schema:dump
和db:test:load_schema
(我假设为tests_prepare的一部分).我试图修补它们db:schema:load
使用:
namespace :db do # Helpers def mysql_creation_options(config) @charset = ENV['CHARSET'] || 'utf8' @collation = ENV['COLLATION'] || 'utf8_unicode_ci' {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)} end def load_schema(schema_name) abcs = ActiveRecord::Base.configurations ActiveRecord::Base.connection.recreate_database(abcs[schema_name+'_test']['database'], mysql_creation_options(abcs[schema_name+'_test'])) # like db:test:load_schema ActiveRecord::Base.establish_connection(schema_name+'_test') ActiveRecord::Schema.verbose = false load("#{Rails.root}/db/#{schema_name}_schema.rb") end namespace :schema do # desc 'Dump additional database schema' task :dump => [:environment, :load_config] do dump_schema = -> (schema_name) { filename = "#{Rails.root}/db/#{schema_name}_schema.rb" File.open(filename, 'w:utf-8') do |file| ActiveRecord::Base.establish_connection("#{schema_name}_#{Rails.env}") ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file) end } dump_schema.call('kiddom') dump_schema.call('kiddom_warehouse') end # When loading from schema, load these files, too task :load => [:environment, :load_config] do load_schema('kiddom') load_schema('kiddom_warehouse') end end namespace :test do # desc 'Purge and load foo_test schema' task :load_schema do load_schema('kiddom') load_schema('kiddom_warehouse') end end end
但这给了我错误NoMethodError: undefined method 'recreate_database' for #
.显然,这仅适用于Oracle类型的数据库?
什么是底层DROP
和CREATE DATABASE
sql 的Rails命令我试图影响额外schema.rb
的?