Finalize conversion to bigint for push_event_payloads (take 3)

What does this MR do?

This is same change as !65795 (merged), which had to be reverted because we hit deadlock when deploying to GitLab.com.

This time we changed the lock order to be the same as in EventCreateService#create_push_event, this should avoid the deadlock.

  1. Ensure the migration of event_id to event_id_convert_to_bigint is completed.
  2. Check column types to make sure swapping is not already done
  3. Copy indexes and FKs (this is already executed on GitLab.com)
  4. Swap columns

See #288005 (closed).

Database migrations

Timing

Index and FK are already created on GitLab.com (when we attempted to deploy !65795 (merged)), so this time we have to do just the column swap, which should be very fast. This can be confirmed by the migrations testing pipeline - !67299 (comment 643550375).

Up
$ bundle exec rails db:migrate:up VERSION=20210802043253
== 20210802043253 FinalizePushEventPayloadsBigintConversion3: migrating =======
-- columns("push_event_payloads")
   -> 0.0014s
-- transaction_open?()
   -> 0.0000s
-- index_exists?("push_event_payloads", :event_id_convert_to_bigint, {:unique=>true, :name=>"index_push_event_payloads_on_event_id_convert_to_bigint", :algorithm=>:concurrently})
   -> 0.0018s
-- execute("SET statement_timeout TO 0")
   -> 0.0007s
-- add_index("push_event_payloads", :event_id_convert_to_bigint, {:unique=>true, :name=>"index_push_event_payloads_on_event_id_convert_to_bigint", :algorithm=>:concurrently})
   -> 0.0035s
-- execute("RESET ALL")
   -> 0.0006s
-- transaction_open?()
   -> 0.0000s
-- foreign_keys("push_event_payloads")
   -> 0.0036s
-- execute("LOCK TABLE events, push_event_payloads IN SHARE ROW EXCLUSIVE MODE")
   -> 0.0007s
-- execute("ALTER TABLE push_event_payloads\nADD CONSTRAINT fk_a5e47ac4c5\nFOREIGN KEY (event_id_convert_to_bigint)\nREFERENCES events (id)\nON DELETE CASCADE\nNOT VALID;\n")
   -> 0.0018s
-- execute("ALTER TABLE push_event_payloads VALIDATE CONSTRAINT fk_a5e47ac4c5;")
   -> 0.0058s
-- execute("LOCK TABLE events, push_event_payloads  IN ACCESS EXCLUSIVE MODE")
   -> 0.0007s
-- quote_table_name("push_event_payloads")
   -> 0.0000s
-- quote_column_name(:event_id)
   -> 0.0000s
-- quote_column_name("event_id_tmp")
   -> 0.0000s
-- execute("ALTER TABLE \"push_event_payloads\" RENAME COLUMN \"event_id\" TO \"event_id_tmp\"")
   -> 0.0008s
-- quote_table_name("push_event_payloads")
   -> 0.0000s
-- quote_column_name(:event_id_convert_to_bigint)
   -> 0.0000s
-- quote_column_name(:event_id)
   -> 0.0000s
-- execute("ALTER TABLE \"push_event_payloads\" RENAME COLUMN \"event_id_convert_to_bigint\" TO \"event_id\"")
   -> 0.0007s
-- quote_table_name("push_event_payloads")
   -> 0.0000s
-- quote_column_name("event_id_tmp")
   -> 0.0000s
-- quote_column_name(:event_id_convert_to_bigint)
   -> 0.0000s
-- execute("ALTER TABLE \"push_event_payloads\" RENAME COLUMN \"event_id_tmp\" TO \"event_id_convert_to_bigint\"")
   -> 0.0007s
-- quote_table_name("trigger_07c94931164e")
   -> 0.0000s
-- execute("ALTER FUNCTION \"trigger_07c94931164e\" RESET ALL")
   -> 0.0008s
-- change_column_default("push_event_payloads", :event_id, nil)
   -> 0.0023s
-- change_column_default("push_event_payloads", :event_id_convert_to_bigint, 0)
   -> 0.0023s
-- execute("ALTER TABLE push_event_payloads DROP CONSTRAINT push_event_payloads_pkey")
   -> 0.0010s
-- rename_index("push_event_payloads", "index_push_event_payloads_on_event_id_convert_to_bigint", "push_event_payloads_pkey")
   -> 0.0008s
-- execute("ALTER TABLE push_event_payloads ADD CONSTRAINT push_event_payloads_pkey PRIMARY KEY USING INDEX push_event_payloads_pkey")
   -> 0.0008s
-- remove_foreign_key("push_event_payloads", {:name=>"fk_36c74129da"})
   -> 0.0040s
-- quote_table_name("push_event_payloads")
   -> 0.0000s
-- quote_column_name("fk_a5e47ac4c5")
   -> 0.0000s
-- quote_column_name("fk_36c74129da")
   -> 0.0000s
-- execute("ALTER TABLE \"push_event_payloads\"\nRENAME CONSTRAINT \"fk_a5e47ac4c5\" TO \"fk_36c74129da\"\n")
   -> 0.0009s
== 20210802043253 FinalizePushEventPayloadsBigintConversion3: migrated (0.0687s) 
Down
$ bundle exec rails db:migrate:down VERSION=20210802043253
== 20210802043253 FinalizePushEventPayloadsBigintConversion3: reverting =======
-- transaction_open?()
   -> 0.0000s
-- index_exists?("push_event_payloads", :event_id_convert_to_bigint, {:unique=>true, :name=>"index_push_event_payloads_on_event_id_convert_to_bigint", :algorithm=>:concurrently})
   -> 0.0022s
-- execute("SET statement_timeout TO 0")
   -> 0.0006s
-- add_index("push_event_payloads", :event_id_convert_to_bigint, {:unique=>true, :name=>"index_push_event_payloads_on_event_id_convert_to_bigint", :algorithm=>:concurrently})
   -> 0.0048s
-- execute("RESET ALL")
   -> 0.0014s
-- transaction_open?()
   -> 0.0000s
-- foreign_keys("push_event_payloads")
   -> 0.0042s
-- execute("LOCK TABLE events, push_event_payloads IN SHARE ROW EXCLUSIVE MODE")
   -> 0.0013s
-- execute("ALTER TABLE push_event_payloads\nADD CONSTRAINT fk_a5e47ac4c5\nFOREIGN KEY (event_id_convert_to_bigint)\nREFERENCES events (id)\nON DELETE CASCADE\nNOT VALID;\n")
   -> 0.0677s
-- execute("ALTER TABLE push_event_payloads VALIDATE CONSTRAINT fk_a5e47ac4c5;")
   -> 0.0040s
-- execute("LOCK TABLE events, push_event_payloads  IN ACCESS EXCLUSIVE MODE")
   -> 0.0008s
-- quote_table_name("push_event_payloads")
   -> 0.0000s
-- quote_column_name(:event_id)
   -> 0.0000s
-- quote_column_name("event_id_tmp")
   -> 0.0000s
-- execute("ALTER TABLE \"push_event_payloads\" RENAME COLUMN \"event_id\" TO \"event_id_tmp\"")
   -> 0.0008s
-- quote_table_name("push_event_payloads")
   -> 0.0000s
-- quote_column_name(:event_id_convert_to_bigint)
   -> 0.0000s
-- quote_column_name(:event_id)
   -> 0.0000s
-- execute("ALTER TABLE \"push_event_payloads\" RENAME COLUMN \"event_id_convert_to_bigint\" TO \"event_id\"")
   -> 0.0008s
-- quote_table_name("push_event_payloads")
   -> 0.0000s
-- quote_column_name("event_id_tmp")
   -> 0.0000s
-- quote_column_name(:event_id_convert_to_bigint)
   -> 0.0000s
-- execute("ALTER TABLE \"push_event_payloads\" RENAME COLUMN \"event_id_tmp\" TO \"event_id_convert_to_bigint\"")
   -> 0.0009s
-- quote_table_name("trigger_07c94931164e")
   -> 0.0000s
-- execute("ALTER FUNCTION \"trigger_07c94931164e\" RESET ALL")
   -> 0.0008s
-- change_column_default("push_event_payloads", :event_id, nil)
   -> 0.0024s
-- change_column_default("push_event_payloads", :event_id_convert_to_bigint, 0)
   -> 0.0022s
-- execute("ALTER TABLE push_event_payloads DROP CONSTRAINT push_event_payloads_pkey")
   -> 0.0009s
-- rename_index("push_event_payloads", "index_push_event_payloads_on_event_id_convert_to_bigint", "push_event_payloads_pkey")
   -> 0.0008s
-- execute("ALTER TABLE push_event_payloads ADD CONSTRAINT push_event_payloads_pkey PRIMARY KEY USING INDEX push_event_payloads_pkey")
   -> 0.0008s
-- remove_foreign_key("push_event_payloads", {:name=>"fk_36c74129da"})
   -> 0.0036s
-- quote_table_name("push_event_payloads")
   -> 0.0000s
-- quote_column_name("fk_a5e47ac4c5")
   -> 0.0000s
-- quote_column_name("fk_36c74129da")
   -> 0.0000s
-- execute("ALTER TABLE \"push_event_payloads\"\nRENAME CONSTRAINT \"fk_a5e47ac4c5\" TO \"fk_36c74129da\"\n")
   -> 0.0007s
== 20210802043253 FinalizePushEventPayloadsBigintConversion3: reverted (0.1245s) 

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

Does this MR contain changes to processing or storing of credentials or tokens, authorization and authentication methods or other items described in the security review guidelines? If not, then delete this Security section.

  • 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

Related to #288005 (closed)

Edited by Krasimir Angelov