Skip to content

Commit 707ee0e

Browse files
tarmojeremy
authored andcommitted
Made migrations transactional for PostgreSQL [#834 state:resolved]
Patch originally from http://dev.rubyonrails.org/ticket/5470
1 parent 9dac554 commit 707ee0e

6 files changed

Lines changed: 59 additions & 4 deletions

File tree

‎activerecord/CHANGELOG‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
*Edge*
22

3+
* Transactional migrations for databases which support them. #834 [divoxx, Adam Wiggins, Tarmo Tänav]
4+
35
* Set config.active_record.timestamped_migrations = false to have migrations with numeric prefix instead of UTC timestamp. #446. [Andrew Stone, Nik Wakelin]
46

57
* change_column_default preserves the not-null constraint. #617 [Tarmo Tänav]

‎activerecord/lib/active_record/connection_adapters/abstract_adapter.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ def supports_count_distinct?
5151
true
5252
end
5353

54+
# Does this adapter support DDL rollbacks in transactions? That is, would
55+
# CREATE TABLE or ALTER TABLE get rolled back by a transaction? PostgreSQL,
56+
# SQL Server, and others support this. MySQL and others do not.
57+
def supports_ddl_transactions?
58+
false
59+
end
60+
5461
# Should primary key values be selected from their corresponding
5562
# sequence before the insert statement? If true, next_sequence_value
5663
# is called before each insert to set the record's primary key.

‎activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ def supports_insert_with_returning?
335335
postgresql_version >= 80200
336336
end
337337

338+
def supports_ddl_transactions?
339+
true
340+
end
341+
338342
# Returns the configured supported identifier length supported by PostgreSQL,
339343
# or report the default of 63 on PostgreSQL 7.x.
340344
def table_alias_length

‎activerecord/lib/active_record/migration.rb‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,22 @@ def migrate
461461
Base.logger.info "Migrating to #{migration.name} (#{migration.version})"
462462

463463
# On our way up, we skip migrating the ones we've already migrated
464-
# On our way down, we skip reverting the ones we've never migrated
465464
next if up? && migrated.include?(migration.version.to_i)
466465

466+
# On our way down, we skip reverting the ones we've never migrated
467467
if down? && !migrated.include?(migration.version.to_i)
468468
migration.announce 'never migrated, skipping'; migration.write
469-
else
470-
migration.migrate(@direction)
471-
record_version_state_after_migrating(migration.version)
469+
next
470+
end
471+
472+
begin
473+
ddl_transaction do
474+
migration.migrate(@direction)
475+
record_version_state_after_migrating(migration.version)
476+
end
477+
rescue => e
478+
canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : ""
479+
raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace
472480
end
473481
end
474482
end
@@ -531,5 +539,14 @@ def up?
531539
def down?
532540
@direction == :down
533541
end
542+
543+
# Wrap the migration in a transaction only if supported by the adapter.
544+
def ddl_transaction(&block)
545+
if Base.connection.supports_ddl_transactions?
546+
Base.transaction { block.call }
547+
else
548+
block.call
549+
end
550+
end
534551
end
535552
end

‎activerecord/test/cases/migration_test.rb‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,21 @@ def test_migrator_double_down
937937
assert_equal(0, ActiveRecord::Migrator.current_version)
938938
end
939939

940+
if current_adapter?(:PostgreSQLAdapter)
941+
def test_migrator_one_up_with_exception_and_rollback
942+
assert !Person.column_methods_hash.include?(:last_name)
943+
944+
e = assert_raises(StandardError) do
945+
ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/broken", 100)
946+
end
947+
948+
assert_equal "An error has occurred, this and all later migrations canceled:\n\nSomething broke", e.message
949+
950+
Person.reset_column_information
951+
assert !Person.column_methods_hash.include?(:last_name)
952+
end
953+
end
954+
940955
def test_finds_migrations
941956
migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/valid").migrations
942957
[['1', 'people_have_last_names'],
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class MigrationThatRaisesException < ActiveRecord::Migration
2+
def self.up
3+
add_column "people", "last_name", :string
4+
raise 'Something broke'
5+
end
6+
7+
def self.down
8+
remove_column "people", "last_name"
9+
end
10+
end

0 commit comments

Comments
 (0)