Commit 599cc499 authored by Reuben Pereira's avatar Reuben Pereira Committed by Stan Hu

Drop existing trigger before creating new one

- When renaming a column concurrently, drop any existing trigger before
attempting to create a new one.

When running migration specs multiple times (as it happens during
local development), the down method of previous migrations are called.
If any of the called methods contains a call to
rename_column_concurrently, a trigger will be created and not removed.
So, the next time a migration spec is run, if the same down method is
executed again, it will cause an error when attempting to create the
trigger (since it already exists). Dropping the trigger if it already
exists will prevent this problem.
parent 523c619f
...@@ -746,6 +746,11 @@ module Gitlab ...@@ -746,6 +746,11 @@ module Gitlab
VOLATILE VOLATILE
EOF EOF
execute <<-EOF.strip_heredoc
DROP TRIGGER IF EXISTS #{trigger}
ON #{table}
EOF
execute <<-EOF.strip_heredoc execute <<-EOF.strip_heredoc
CREATE TRIGGER #{trigger} CREATE TRIGGER #{trigger}
BEFORE INSERT OR UPDATE BEFORE INSERT OR UPDATE
......
...@@ -618,11 +618,19 @@ describe Gitlab::Database::MigrationHelpers do ...@@ -618,11 +618,19 @@ describe Gitlab::Database::MigrationHelpers do
expect(model).to receive(:execute) expect(model).to receive(:execute)
.with(/CREATE OR REPLACE FUNCTION foo()/m) .with(/CREATE OR REPLACE FUNCTION foo()/m)
expect(model).to receive(:execute)
.with(/DROP TRIGGER IF EXISTS foo/m)
expect(model).to receive(:execute) expect(model).to receive(:execute)
.with(/CREATE TRIGGER foo/m) .with(/CREATE TRIGGER foo/m)
model.install_rename_triggers_for_postgresql('foo', :users, :old, :new) model.install_rename_triggers_for_postgresql('foo', :users, :old, :new)
end end
it 'does not fail if trigger already exists' do
model.install_rename_triggers_for_postgresql('foo', :users, :old, :new)
model.install_rename_triggers_for_postgresql('foo', :users, :old, :new)
end
end end
describe '#remove_rename_triggers_for_postgresql' do describe '#remove_rename_triggers_for_postgresql' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment