fatkodima/online_migrations

Clarity on lock retry behaviour inside and outside transactions

camallen opened this issue · 4 comments

I'm seeking clarification about the lock retry behaviour when running the migration outside of a transaction.

In the docs you explicitly state the retry behaviour when the migration runs inside a migration

When statement within transaction fails - the whole transaction is retried.

However when running the migration without a transaction via disable_ddl_transaction! it's not clear in the docs but my assumption is that the LockRetrier does the same. E.g. in the test code it shows the migration is retried by the LockRetrier mechanism (if set).

Can you please clarify the behaviour is as I assume. I'm happy to get a PR up to update the configuring.md doc as well.

Yes, your assumption is correct. Please do open a PR.

Hmm, I'm not sure if

# Initial run only, then just `add_column` is retried
is as clear as it could be.

$migrate_attempts is only incremented once, here. After that, add_column :users, :name, :string tries, and fails.

The change in #51 implies that now add_column :users, :name, :string will be retried. If that's the case, I would expect this test to have an infinite loop, or to have the lock be removed and the column cleanly added. If the latter is the intended behaviour, I think the test should assert that. But I think what's more likely is that there is no retrying happening at all in this test.

Given that, I think what the docs should say is:

When a statement within transaction fails, the whole transaction is retried. If any statement fails when running outside a transaction (e.g. using disable_ddl_transaction!), that statement will not be retried.

@fatkodima @camallen what am I missing?

The $migrate_attemps is equal to 1, because we actually run the whole migration change method only once. It demonstrates, that we do not retry the whole transaction (because there is no even one).
But we run add_column multiple times.

The runtime of the whole migration looks like this:

def change
  $migrate_attemps += 1
  add_column ... => raises lock error
  add_column ... again.. => raises lock error again
end

Ahhh now I see what's going on.

connection.with_lock_retries { super }
is where each statement is wrapped with a lock retrier. Neat 👍