Commit da275b6d authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'concurrent_foreign_key_uses_with_lock_retries' into 'master'

Use with_lock_retries when adding concurrent FK

See merge request gitlab-org/gitlab!29663
parents 622400db 62d2029d
...@@ -184,6 +184,7 @@ module Gitlab ...@@ -184,6 +184,7 @@ module Gitlab
# short period of time. The key _is_ enforced for any newly created # short period of time. The key _is_ enforced for any newly created
# data. # data.
with_lock_retries do
execute <<-EOF.strip_heredoc execute <<-EOF.strip_heredoc
ALTER TABLE #{source} ALTER TABLE #{source}
ADD CONSTRAINT #{options[:name]} ADD CONSTRAINT #{options[:name]}
...@@ -193,6 +194,7 @@ module Gitlab ...@@ -193,6 +194,7 @@ module Gitlab
NOT VALID; NOT VALID;
EOF EOF
end end
end
# Validate the existing constraint. This can potentially take a very # Validate the existing constraint. This can potentially take a very
# long time to complete, but fortunately does not lock the source table # long time to complete, but fortunately does not lock the source table
......
...@@ -215,6 +215,7 @@ describe Gitlab::Database::MigrationHelpers do ...@@ -215,6 +215,7 @@ describe Gitlab::Database::MigrationHelpers do
context 'ON DELETE statements' do context 'ON DELETE statements' do
context 'on_delete: :nullify' do context 'on_delete: :nullify' do
it 'appends ON DELETE SET NULL statement' do it 'appends ON DELETE SET NULL statement' do
expect(model).to receive(:with_lock_retries).and_call_original
expect(model).to receive(:disable_statement_timeout).and_call_original expect(model).to receive(:disable_statement_timeout).and_call_original
expect(model).to receive(:execute).with(/statement_timeout/) expect(model).to receive(:execute).with(/statement_timeout/)
expect(model).to receive(:execute).ordered.with(/VALIDATE CONSTRAINT/) expect(model).to receive(:execute).ordered.with(/VALIDATE CONSTRAINT/)
...@@ -230,6 +231,7 @@ describe Gitlab::Database::MigrationHelpers do ...@@ -230,6 +231,7 @@ describe Gitlab::Database::MigrationHelpers do
context 'on_delete: :cascade' do context 'on_delete: :cascade' do
it 'appends ON DELETE CASCADE statement' do it 'appends ON DELETE CASCADE statement' do
expect(model).to receive(:with_lock_retries).and_call_original
expect(model).to receive(:disable_statement_timeout).and_call_original expect(model).to receive(:disable_statement_timeout).and_call_original
expect(model).to receive(:execute).with(/statement_timeout/) expect(model).to receive(:execute).with(/statement_timeout/)
expect(model).to receive(:execute).ordered.with(/VALIDATE CONSTRAINT/) expect(model).to receive(:execute).ordered.with(/VALIDATE CONSTRAINT/)
...@@ -245,6 +247,7 @@ describe Gitlab::Database::MigrationHelpers do ...@@ -245,6 +247,7 @@ describe Gitlab::Database::MigrationHelpers do
context 'on_delete: nil' do context 'on_delete: nil' do
it 'appends no ON DELETE statement' do it 'appends no ON DELETE statement' do
expect(model).to receive(:with_lock_retries).and_call_original
expect(model).to receive(:disable_statement_timeout).and_call_original expect(model).to receive(:disable_statement_timeout).and_call_original
expect(model).to receive(:execute).with(/statement_timeout/) expect(model).to receive(:execute).with(/statement_timeout/)
expect(model).to receive(:execute).ordered.with(/VALIDATE CONSTRAINT/) expect(model).to receive(:execute).ordered.with(/VALIDATE CONSTRAINT/)
...@@ -261,6 +264,7 @@ describe Gitlab::Database::MigrationHelpers do ...@@ -261,6 +264,7 @@ describe Gitlab::Database::MigrationHelpers do
context 'when no custom key name is supplied' do context 'when no custom key name is supplied' do
it 'creates a concurrent foreign key and validates it' do it 'creates a concurrent foreign key and validates it' do
expect(model).to receive(:with_lock_retries).and_call_original
expect(model).to receive(:disable_statement_timeout).and_call_original expect(model).to receive(:disable_statement_timeout).and_call_original
expect(model).to receive(:execute).with(/statement_timeout/) expect(model).to receive(:execute).with(/statement_timeout/)
expect(model).to receive(:execute).ordered.with(/NOT VALID/) expect(model).to receive(:execute).ordered.with(/NOT VALID/)
...@@ -287,6 +291,7 @@ describe Gitlab::Database::MigrationHelpers do ...@@ -287,6 +291,7 @@ describe Gitlab::Database::MigrationHelpers do
context 'when a custom key name is supplied' do context 'when a custom key name is supplied' do
context 'for creating a new foreign key for a column that does not presently exist' do context 'for creating a new foreign key for a column that does not presently exist' do
it 'creates a new foreign key' do it 'creates a new foreign key' do
expect(model).to receive(:with_lock_retries).and_call_original
expect(model).to receive(:disable_statement_timeout).and_call_original expect(model).to receive(:disable_statement_timeout).and_call_original
expect(model).to receive(:execute).with(/statement_timeout/) expect(model).to receive(:execute).with(/statement_timeout/)
expect(model).to receive(:execute).ordered.with(/NOT VALID/) expect(model).to receive(:execute).ordered.with(/NOT VALID/)
...@@ -314,6 +319,7 @@ describe Gitlab::Database::MigrationHelpers do ...@@ -314,6 +319,7 @@ describe Gitlab::Database::MigrationHelpers do
context 'when the supplied key name is different from the existing foreign key name' do context 'when the supplied key name is different from the existing foreign key name' do
it 'creates a new foreign key' do it 'creates a new foreign key' do
expect(model).to receive(:with_lock_retries).and_call_original
expect(model).to receive(:disable_statement_timeout).and_call_original expect(model).to receive(:disable_statement_timeout).and_call_original
expect(model).to receive(:execute).with(/statement_timeout/) expect(model).to receive(:execute).with(/statement_timeout/)
expect(model).to receive(:execute).ordered.with(/NOT VALID/) expect(model).to receive(:execute).ordered.with(/NOT VALID/)
......
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