当前位置:  开发笔记 > 数据库 > 正文

如何将多个模式加载到Rails引擎或应用程序?

如何解决《如何将多个模式加载到Rails引擎或应用程序?》经验,为你挑选了0个好方法。

寻求将我的所有共享模型移动到可以包含在我的每个微应用程序中的引擎.

该引擎应为我们的所有遗留数据提供模型层,包括:

模型文件

架构文件

迁移(我们遵循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:createrake db:schema:load工作,

这些db.rake补丁仅影响db:schema:dumpdb: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类型的数据库?

什么是底层DROPCREATE DATABASEsql 的Rails命令我试图影响额外schema.rb的?

推荐阅读
k78283381
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有