Skip to content

Use with_lock_retries at the migration level

Andreas Brandl requested to merge ab/lock-retry-migrations into master

What does this MR do?

Use with_lock_retries at the migration level

For transactional migrations, apply with-lock-retries methodology at the
full transaction level. This allows us to remove usage of
with-lock-retries from helpers and we don't need to use it inside
migrations explicitly.

The overall intent here is to remove subtransactions.

See https://gitlab.com/gitlab-org/gitlab/-/issues/339115

How to setup and validate locally (strongly suggested)

create table example (id serial primary key);

Example migration:

class TestMigration < Gitlab::Database::Migration[1.0]
  enable_lock_retries!  # Toggle this on/off

  def up
    execute 'LOCK TABLE example in EXCLUSIVE MODE'
    puts 'locked!'
  end

  def down
    puts 'DOWN'
  end
end

With the enable_lock_retries!, we expect to see control statements for SET lock_timeout... when migrating up.

In another psql session, run this while migrating up. We expect to see with_lock_retries methodology kicking in:

BEGIN;
LOCK TABLE example in EXCLUSIVE MODE;

Does this MR meet the acceptance criteria?

Conformity

Edited by Andreas Brandl

Merge request reports