Skip to content

Commit

Permalink
Post rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Hareau committed Jul 6, 2023
1 parent e1fa09d commit 0c2e635
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 57 deletions.
29 changes: 4 additions & 25 deletions lib/safe-pg-migrations/plugins/statement_insurer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,42 +85,21 @@ def change_column_null(table_name, column_name, null, default = nil)
remove_check_constraint table_name, "#{column_name} IS NOT NULL"
end


def remove_column(table_name, column_name, *)
foreign_key = foreign_key_for(table_name, column: column_name)

with_setting(:statement_timeout, SafePgMigrations.config.pg_statement_timeout) do
remove_foreign_key(table_name, name: foreign_key.name) if foreign_key
super
end
end

ruby2_keywords def drop_table(table_name, *args)
foreign_keys(table_name).each do |foreign_key|
with_setting(:statement_timeout, SafePgMigrations.config.pg_statement_timeout) do
remove_foreign_key(table_name, name: foreign_key.name)
end
end

Helpers::Logger.say_method_call :drop_table, table_name, *args

with_setting(:statement_timeout, SafePgMigrations.config.pg_statement_timeout) do
super(table_name, *args)
end
remove_foreign_key(table_name, name: foreign_key.name) if foreign_key
super
end

ruby2_keywords def drop_table(table_name, *args)
foreign_keys(table_name).each do |foreign_key|
with_setting(:statement_timeout, SafePgMigrations.config.pg_statement_timeout) do
remove_foreign_key(table_name, name: foreign_key.name)
end
remove_foreign_key(table_name, name: foreign_key.name)
end

Helpers::Logger.say_method_call :drop_table, table_name, *args

with_setting(:statement_timeout, SafePgMigrations.config.pg_statement_timeout) do
super(table_name, *args)
end
super(table_name, *args)
end
end
end
10 changes: 1 addition & 9 deletions test/IdempotentStatements/drop_table_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ def test_first_fk_already_removed
calls = record_calls(@connection, :execute) { run_migration }

assert_calls <<~CALLS.strip.split("\n"), calls
SET statement_timeout TO '5s'
ALTER TABLE "users" DROP CONSTRAINT "fk_rails_d15efa01b1"
SET statement_timeout TO '70s'
SET statement_timeout TO '5s'
DROP TABLE "users"
SET statement_timeout TO '70s'
CALLS
end

Expand All @@ -44,11 +40,7 @@ def test_fks_already_removed

calls = record_calls(@connection, :execute) { run_migration }

assert_calls <<~CALLS.strip.split("\n"), calls
SET statement_timeout TO '5s'
DROP TABLE "users"
SET statement_timeout TO '70s'
CALLS
assert_calls ['DROP TABLE "users"'], calls
end
end
end
33 changes: 12 additions & 21 deletions test/StatementInsurer/drop_table_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def change

calls = record_calls(@connection, :execute) { run_migration }

assert_calls ["SET statement_timeout TO '5s'", 'DROP TABLE "users"', "SET statement_timeout TO '70s'"],
calls
assert_calls <<~CALLS.strip.split("\n"), calls
DROP TABLE "users"
CALLS
end

def test_can_drop_table_with_foreign_key
Expand All @@ -36,14 +37,10 @@ def change

calls = record_calls(@connection, :execute) { run_migration }

assert_calls [
"SET statement_timeout TO '5s'",
'ALTER TABLE "users" DROP CONSTRAINT "fk_rails_253ea793f9"',
"SET statement_timeout TO '70s'",
"SET statement_timeout TO '5s'",
'DROP TABLE "users"',
"SET statement_timeout TO '70s'",
], calls
assert_calls <<~CALLS.strip.split("\n"), calls
ALTER TABLE "users" DROP CONSTRAINT "fk_rails_253ea793f9"
DROP TABLE "users"
CALLS
end

def test_can_drop_table_with_several_foreign_keys
Expand All @@ -64,17 +61,11 @@ def change

calls = record_calls(@connection, :execute) { run_migration }

assert_calls [
"SET statement_timeout TO '5s'",
'ALTER TABLE "users" DROP CONSTRAINT "fk_rails_253ea793f9"',
"SET statement_timeout TO '70s'",
"SET statement_timeout TO '5s'",
'ALTER TABLE "users" DROP CONSTRAINT "fk_rails_d15efa01b1"',
"SET statement_timeout TO '70s'",
"SET statement_timeout TO '5s'",
'DROP TABLE "users"',
"SET statement_timeout TO '70s'",
], calls
assert_calls <<~CALLS.strip.split("\n"), calls
ALTER TABLE "users" DROP CONSTRAINT "fk_rails_253ea793f9"
ALTER TABLE "users" DROP CONSTRAINT "fk_rails_d15efa01b1"
DROP TABLE "users"
CALLS
end
end
end
6 changes: 4 additions & 2 deletions test/StatementInsurer/remove_column_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ def change

calls = record_calls(@connection, :execute) { run_migration }

assert_equal ['ALTER TABLE "users" DROP CONSTRAINT "fk_rails_baad13daec"'], calls[2]
assert_equal ['ALTER TABLE "users" DROP COLUMN "password_id"'], calls[3]
assert_calls <<~CALLS.strip.split("\n"), calls
ALTER TABLE "users" DROP CONSTRAINT "fk_rails_baad13daec"
ALTER TABLE "users" DROP COLUMN "password_id"
CALLS
end

def test_can_remove_column_with_foreign_key_on_other_column
Expand Down

0 comments on commit 0c2e635

Please sign in to comment.