Search code examples
ruby-on-railssql-serveractiverecordtiny-tds

`rake db:schema:dump` creates schema with empty system tables


I am creating test and development databases that mirror the schema of an existing production database. First, I create db/schema.rb by dumping the production schema:

RAILS_ENV=production rake db:schema:dump

This creates a schema.rb which has an empty table for each system table:

ActiveRecord::Schema.define(version: 0) do

  create_table "MSreplication_objects", id: false, force: :cascade do |t|
  end

  ...    

These empty tables cause an error when I try to create the test and development databases:

$ rake db:reset
-- create_table("MSreplication_objects", {:id=>false, :force=>:cascade})
rake aborted!
ActiveRecord::StatementInvalid: TinyTds::Error: Incorrect syntax near ')'.: CREATE TABLE [MSreplication_objects] ()

If, in schema.rb, I delete the definitions for the system tables, then the databases are created normally:

$ rake db:reset
-- create_table("Org", {:primary_key=>"org_id", :force=>:cascade})
   -> 0.0434s
   -> -1 rows
...

How can I keep rake db:schema:dump from dumping the definitions for empty system tables that cannot be created?


Versions:

  • Microsoft SQL Server 2014 - 12.0.2000.8 (x64)
  • rails (4.2.1)
  • activerecord-sqlserver-adapter (4.2.4)
  • tiny_tds from github:
    • git://github.com/rails-sqlserver/tiny_tds.git
    • Commit c4e59ba82c0cc55a5587cec1b7d5100d1b1ccaf4

Solution

  • There is hope:

    ActiveRecord::SchemaDumper which does most of the heavy lifting supplies a singleton method for ignoring tables:

    ActiveRecord::SchemaDumper.ignore_tables = ['MSreplication_objects', 'MSAnotherStupidSystemTable']
    

    activerecord-sqlserver-adapter does not seem to supply its own rake task for dumping the schema (which it should) where this should have been done.