Skip to content

Instantly share code, notes, and snippets.

@elucid
Created May 13, 2011 22:04
Show Gist options
  • Save elucid/971399 to your computer and use it in GitHub Desktop.
Save elucid/971399 to your computer and use it in GitHub Desktop.
wrap transactions in a DDL transaction (if your database supports it)
class << ActiveRecord::Base
def ddl_transaction(&block)
if connection.supports_ddl_transactions?
transaction { block.call }
else
block.call
end
end
end
# delete original migrate task.
# note: this is ridiculous
Rake.application.instance_variable_get(:@tasks).delete("db:migrate")
namespace :db do
desc "Migrate the database (options: VERSION=x, VERBOSE=false). wrap all migrations being run in a single ddl transaction so that they either all succeed or you're left back where you started"
task :migrate => :environment do
Rake::Task['db:migrate'].clear # clear the default task
ActiveRecord::Base.ddl_transaction do
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
end
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment