Skip to content

Commit

Permalink
fixing bug to support both string and symbol for constraint name
Browse files Browse the repository at this point in the history
  • Loading branch information
Suraj Mishra committed Jun 26, 2024
1 parent f8a5c78 commit 33b712b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
6 changes: 6 additions & 0 deletions lib/pg_ha_migrations/safe_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,12 @@ def safely_acquire_lock_for_table(table, mode: :access_exclusive, &block)
Thread.current[__method__] = nil unless nested_target_table
end

# since rails versions < 7.1 has bug which does not handle symbol for
# constraint name, we are converting name to string explicitly to solve that.
def unsafe_remove_check_constraint(table, name:, **options)
super(table, name: name.to_s, **options)
end

def adjust_lock_timeout(timeout_seconds = PgHaMigrations::LOCK_TIMEOUT_SECONDS, &block)
_check_postgres_adapter!
original_timeout = ActiveRecord::Base.value_from_sql("SHOW lock_timeout").sub(/s\Z/, '').to_i * 1000
Expand Down
36 changes: 12 additions & 24 deletions spec/safe_statements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1280,32 +1280,20 @@ def up
end

describe "unsafe_remove_check_constraint" do
it "throws error when symbol is used for contraint name" do
migration = Class.new(migration_klass) do
define_method(:up) do
unsafe_create_table :foos
unsafe_add_column :foos, :bar, :text
unsafe_add_check_constraint :foos, "bar IS NOT NULL", :name => :constraint_foo_bar_is_not_null
unsafe_remove_check_constraint :foos, name: :constraint_foo_bar_is_not_null
end
end
expect do
migration.suppress_messages { migration.migrate(:up) }
end.to raise_error(ArgumentError, /Table 'foos' has no check constraint for {:name=>:constraint_foo_bar_is_not_null}/)
end

it "doesn't throw error when string is used for constraint name" do
migration = Class.new(migration_klass) do
define_method(:up) do
unsafe_create_table :foos
unsafe_add_column :foos, :bar, :text
unsafe_add_check_constraint :foos, "bar IS NOT NULL", :name => :constraint_foo_bar_is_not_null
unsafe_remove_check_constraint :foos, name: "constraint_foo_bar_is_not_null"
["constraint_foo_bar_is_not_null", :constraint_foo_bar_is_not_null].each do |constraint_name|
it "does not throw an error when #{constraint_name.is_a?(Symbol) ? "symbol" : "string"} is used for contraint name" do
migration = Class.new(migration_klass) do
define_method(:up) do
unsafe_create_table :foos
unsafe_add_column :foos, :bar, :text
unsafe_add_check_constraint :foos, "bar IS NOT NULL", :name => :constraint_foo_bar_is_not_null
unsafe_remove_check_constraint :foos, name: constraint_name
end
end
expect do
migration.suppress_messages { migration.migrate(:up) }
end.not_to raise_error
end
expect do
migration.suppress_messages { migration.migrate(:up) }
end.not_to raise_error
end
end

Expand Down

0 comments on commit 33b712b

Please sign in to comment.