title: How to load multiple schemas into a Rails engine or app?
body: <p>Seeking to move all my shared models to an engine which can be included in each of my micro apps. </p>
  
  <p>This engine should provide a model layer to all our legacy data, including:</p>
  
  <ul>
  <li>Model files</li>
  <li>Schema files</li>
  <li>Migrations (we're following <a href="https://blog.pivotal.io/labs/labs/leave-your-migrations-in-your-rails-engines">Pivotal Labs' pattern</a>, this isn't the issue)</li>
  </ul>
  
  <p>Model files are being patched in automatically, that's fine. </p>
  
  <p>Schema files are being monkey-patched in using <a href="http://blog.nistu.de/2012/03/25/multi-database-setup-with-rails-and-rspec">Nikolay Strum's db.rake</a>:</p>
  
  <pre><code>namespace :db do
    namespace :schema do
      # desc 'Dump additional database schema'
      task :dump =&gt; [: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
  </code></pre>
  
  <p>We need <code>rake db:create</code> and <code>rake db:schema:load</code> to work, </p>
  
  <p>The <code>db.rake</code> patches only affect <code>db:schema:dump</code> and <code>db:test:load_schema</code> (part of tests_prepare, I assume). I've attempted to patch them into <code>db:schema:load</code> using:</p>
  
  <pre><code>namespace :db do
  
    # Helpers
    def mysql_creation_options(config)
      @charset   = ENV['CHARSET']   || 'utf8'
      @collation = ENV['COLLATION'] || 'utf8_unicode_ci'
      {:charset =&gt; (config['charset'] || @charset), :collation =&gt; (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 =&gt; [:environment, :load_config] do
        dump_schema = -&gt; (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 =&gt; [: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
  </code></pre>
  
  <p>But this gives me the error <code>NoMethodError: undefined method 'recreate_database' for #&lt;ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x007feb6bb43558&gt;</code>. Apparently, this only works on Oracle-type databases? </p>
  
  <p>What are the Rails commands for the underlying <code>DROP</code> and <code>CREATE DATABASE</code> sql I'm trying to affect for the extra <code>schema.rb</code>'s?</p>