Remove PG warning with with_lock_retries
What does this MR do?
Remove PG warning with with_lock_retries
.
This change fixes the warning message when running a migration with disable_ddl_transaction!
. This MR doesn't alter the behavior of with_lock_retries
, it just makes the warning go away.
WARNING: SET LOCAL can only be used in transaction blocks
More context:
When using with_lock_retries
, the given block might be executed several times with sleep between the attempts (waiting for other processes to release locks).
We are using ruby's sleep
method for delaying the execution. This was a problem when running the code in production because the idle transaction timeout value is configured which caused aborted transactions (failed migration). (already fixed)
The solution was to temporarily disable idle transaction timeout and then re-enable it after the nested transaction block.
There is an edge case: when the migration is executed with disable_ddl_transaction!
, then the migration is not wrapped with a transaction block, thus disabling the idle transaction timeout is not necessary (we're still sleep-ing, but not in a transaction -> cause of the warning message).
How to reproduce it:
On master branch:
- Open a PSQL session.
- Create a migration file with
disable_ddl_transaction!
+with_lock_retries
. - Run the migration.
- When it says: "LOCK NOW", go to the PSQL session and execute:
begin;
LOCK TABLE projects IN ACCESS EXCLUSIVE MODE;
# leave it
- After a few seconds, you should see warning messages in your migration.
Doing the same thing on this branch should not print any warning.
Example migration file:
class TestMigration < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
puts "LOCK NOW"
sleep 20
with_lock_retries do
Project.first.touch
end
end
def down
end
end
Does this MR meet the acceptance criteria?
Conformity
-
Changelog entry -
Documentation (if required) -
Code review guidelines -
Merge request performance guidelines -
Style guides -
Database guides -
Separation of EE specific content
Availability and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. -
Tested in all supported browsers -
Informed Infrastructure department of a default or new setting change, if applicable per definition of done
Security
If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:
-
Label as security and @ mention @gitlab-com/gl-security/appsec
-
The MR includes necessary changes to maintain consistency between UI, API, email, or other methods -
Security reports checked/validated by a reviewer from the AppSec team