bigtunacan
bigtunacan

Reputation: 4986

Rails rake db:schema:dump against SQL Server database with multiple schemas

The situation I have is that we have multiple schemas on SQL Server that we need to be able to do schema:dump and migrations against. One schema is for our new Rails application, the other schema is for a legacy system that we have dependencies on.

When running rake db:schema:dump our new schema tables are correctly created in the schema.rb file. The legacy schema tables do not end up in schema.rb. I'm wondering how others are dealing with this issue.

Another consideration I have given to this is since our legacy schema tables are fairly static would be to add these to a separate file once and then create a before hook for rake db:schema:load that would run that file prior to the schema.rb. Is there a before hook for rake db:schema:load; if so what is that?

Upvotes: 3

Views: 2322

Answers (2)

bigtunacan
bigtunacan

Reputation: 4986

Here is how I ended up solving this issue.

I added a before hooks into schema load and schema dump within hooks.rake as described below.

namespace :project do
  namespace :db do
    task :before_schema_load => :environment do
      add_tables
    end

    task :before_schema_dump => :environment do
      add_ignored_tables
    end
  end
end

Rake::Task['db:schema:dump'].enhance(['project:db:before_schema_dump'])
Rake::Task['db:schema:load'].enhance(['project:db:before_schema_load'])

Within the add_tables functionality I've manually created what is essentially a static schema.rb equivalent for my legacy tables since these will change infrequently (possibly never).

Within the add_ignored_tables I've added tables to the ActiveRecord::SchemaDumper.ignore_tables array to indicate tables that are outside of my schema that I don't want dumped to schema.rb. In my case this is everything that isn't under my current app's schema. In my situation everything that I want outside of my app's schema is specified within the add_tables so those tables as well should not end up in the schema.rb.

Upvotes: 1

Jim Deville
Jim Deville

Reputation: 10662

There is some material about multi-tenant databases using Postgres, the one I've referenced before is http://blog.jerodsanto.net/2011/07/building-multi-tenant-rails-apps-with-postgresql-schemas/. There is also a gem (https://github.com/influitive/apartment), which may also be inspiration, or even a solution for you.

Upvotes: 0

Related Questions