diff options
author | Tarmo Tänav <tarmo@itech.ee> | 2008-08-22 23:53:31 +0300 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-08-22 14:46:25 -0700 |
commit | 707ee0e2695e85186d59aa407f09691ebfcc3125 (patch) | |
tree | 4357234d7cdb191d267e400ea217642124e06cae /activerecord/lib/active_record/migration.rb | |
parent | 9dac5547ad65e82a6fbb6a6243ab5c95d9c44db0 (diff) | |
download | rails-707ee0e2695e85186d59aa407f09691ebfcc3125.tar.gz rails-707ee0e2695e85186d59aa407f09691ebfcc3125.tar.bz2 rails-707ee0e2695e85186d59aa407f09691ebfcc3125.zip |
Made migrations transactional for PostgreSQL [#834 state:resolved]
Patch originally from http://dev.rubyonrails.org/ticket/5470
Diffstat (limited to 'activerecord/lib/active_record/migration.rb')
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index fd77f27b77..c7bc76264d 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -461,14 +461,22 @@ module ActiveRecord Base.logger.info "Migrating to #{migration.name} (#{migration.version})" # On our way up, we skip migrating the ones we've already migrated - # On our way down, we skip reverting the ones we've never migrated next if up? && migrated.include?(migration.version.to_i) + # On our way down, we skip reverting the ones we've never migrated if down? && !migrated.include?(migration.version.to_i) migration.announce 'never migrated, skipping'; migration.write - else - migration.migrate(@direction) - record_version_state_after_migrating(migration.version) + next + end + + begin + ddl_transaction do + migration.migrate(@direction) + record_version_state_after_migrating(migration.version) + end + rescue => e + canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : "" + raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace end end end @@ -531,5 +539,14 @@ module ActiveRecord def down? @direction == :down end + + # Wrap the migration in a transaction only if supported by the adapter. + def ddl_transaction(&block) + if Base.connection.supports_ddl_transactions? + Base.transaction { block.call } + else + block.call + end + end end end |