Rails testing when using SQL Server

As is pointed out on this page of the Rails wiki, you need to do a few extra steps to run tests if you are using SQL Server.

(1) Create a unique index on each table. For example, if your table name is users, do this:

CREATE UNIQUE INDEX [users_index] ON [dbo].[users] ([id])

(2) [Thanks to the anonymous wiki author]Create a customized rake task for dumping the schema. Copy the following into mydb.rake and place it in appfolder/lib/tasks:

namespace :mydb do
  task :migrate => :environment do
    ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
    Rake::Task["mydb:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
  end

  namespace :schema do
    task :dump => :environment do
      require 'active_record/schema_dumper'
      File.open(ENV['SCHEMA'] || "db/schema.rb", "w") do |file|
        ActiveRecord::Base.connection.instance_variable_get("@connection")["AutoCommit"] = false
        ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
      end
    end
  end
end

Now you can use “rake mydb:schema:dump” to get a correct schema.rb file.

Later: Rails testing when using SQL Server, Part 2 

blog comments powered by Disqus