Implement with_lock_retries migration helper
What does this MR do?
Retries SQL statements with built in sleep time and lock timeout.
How to test
- Create a test migration.
rails g migration test_migration
- Change the content to drop
analytics_repository_file_commits
table. (Don't worry, it's not in use)
# frozen_string_literal: true
class TestMigration < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
with_lock_retries do
drop_table :analytics_repository_file_commits
end
end
def down
with_lock_retries do
create_table :analytics_repository_file_commits do |t|
t.references :analytics_repository_file, index: { name: 'index_analytics_repository_file_commits_file_id' }, foreign_key: { on_delete: :cascade }, null: false
t.references :project, index: false, foreign_key: { on_delete: :cascade }, null: false
t.date :committed_date, null: false
t.integer :commit_count, limit: 2, null: false
end
add_index :analytics_repository_file_commits,
[:project_id, :committed_date, :analytics_repository_file_id],
name: 'index_file_commits_on_committed_date_file_id_and_project_id',
unique: true
end
end
end
- Lock the
projects
table.
Notice that the table has a reference to projects
, let's lock it in the dbconsole: rails dbconsole
(separate terminal)
begin;
LOCK TABLE projects in exclusive mode;
- Tail migrations.log (separate terminal)
# cd gitlab rails root
touch log/migrations.log
tail -f log/migrations.log
- Migrate the DB
rake db:migrate
- Notice the retry log messages showing up.
- Finish the transaction in the dbconsole:
END;
- Next iteration should finish the migration.
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
Closes #36859 (closed)
Edited by 🤖 GitLab Bot 🤖