Skip to content

Draft: PoC - Finalyze push_event_payloads.event_id conversion to bigint (single migration)

Krasimir Angelov requested to merge spikes/292874-finalize-pk-conversion into master

What does this MR do?

DO NOT MERGE

PoC for finalizing PK conversion for push_event_payloads in the following steps:

  1. Ensure
  2. Prepare
    1. Duplicate indexes
    2. Duplicate FKs
  3. Swap columns
  4. Cleanup

Instead of splitting the steps in separate migrations, as in !64478 (closed), this MR is doing them all (except the column and trigger removal) in one single migration.

Pros

  1. The one migration to do it all is symmetric, and can be executed both in the up and down directions (i.e. it's reversible), as long as we do not actually remove the old int column.

Cons

  1. This migration will take ages, because indexes and FKs have to be copied (created). Splitting this in separate migrations is not making it faster though.

Related to #292874 (closed).

Database schema before the migration

gitlabhq_development=# \d push_event_payloads
                         Table "public.push_event_payloads"
           Column           |         Type          | Collation | Nullable | Default 
----------------------------+-----------------------+-----------+----------+---------
 commit_count               | bigint                |           | not null | 
 event_id                   | integer               |           | not null | 
 action                     | smallint              |           | not null | 
 ref_type                   | smallint              |           | not null | 
 commit_from                | bytea                 |           |          | 
 commit_to                  | bytea                 |           |          | 
 ref                        | text                  |           |          | 
 commit_title               | character varying(70) |           |          | 
 ref_count                  | integer               |           |          | 
 event_id_convert_to_bigint | bigint                |           | not null | 0
Indexes:
    "push_event_payloads_pkey" PRIMARY KEY, btree (event_id)
Foreign-key constraints:
    "fk_36c74129da" FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
Triggers:
    trigger_07c94931164e BEFORE INSERT OR UPDATE ON push_event_payloads FOR EACH ROW EXECUTE PROCEDURE trigger_07c94931164e()

Migration (UP)

$ bundle exec rails db:migrate:up VERSION=20210507014815
== 20210507014815 FinalizePushEventPayloadsEventIdConvertionToBigint: migrating 
-- 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.0012s
-- execute("SET statement_timeout TO 0")
   -> 0.0005s
-- 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.0051s
-- execute("RESET ALL")
   -> 0.0007s
-- transaction_open?()
   -> 0.0000s
-- foreign_keys("push_event_payloads")
   -> 0.0024s
-- 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.0020s
-- execute("ALTER TABLE push_event_payloads VALIDATE CONSTRAINT fk_a5e47ac4c5;")
   -> 0.0019s
-- rename_column("push_event_payloads", :event_id, :event_id_convert_to_bigint_tmp)
   -> 0.0018s
-- rename_column("push_event_payloads", :event_id_convert_to_bigint, :event_id)
   -> 0.0022s
-- rename_column("push_event_payloads", :event_id_convert_to_bigint_tmp, :event_id_convert_to_bigint)
   -> 0.0015s
-- change_column_default("push_event_payloads", :event_id, nil)
   -> 0.0015s
-- change_column_default("push_event_payloads", :event_id_convert_to_bigint, 0)
   -> 0.0015s
-- execute("ALTER TABLE push_event_payloads DROP CONSTRAINT push_event_payloads_pkey")
   -> 0.0007s
-- index_name("push_event_payloads", {:column=>:event_id})
   -> 0.0000s
-- rename_index("push_event_payloads", "index_push_event_payloads_on_event_id", "push_event_payloads_pkey")
   -> 0.0006s
-- execute("ALTER TABLE push_event_payloads ADD CONSTRAINT push_event_payloads_pkey PRIMARY KEY USING INDEX push_event_payloads_pkey")
   -> 0.0007s
-- remove_foreign_key("push_event_payloads", {:name=>"fk_36c74129da"})
   -> 0.0032s
-- execute("ALTER TABLE push_event_payloads\nRENAME CONSTRAINT fk_a5e47ac4c5\nTO fk_36c74129da\n")
   -> 0.0007s
== 20210507014815 FinalizePushEventPayloadsEventIdConvertionToBigint: migrated (0.0406s) 

Database schema after the migration

gitlabhq_development=# \d push_event_payloads
                         Table "public.push_event_payloads"
           Column           |         Type          | Collation | Nullable | Default 
----------------------------+-----------------------+-----------+----------+---------
 commit_count               | bigint                |           | not null | 
 event_id_convert_to_bigint | integer               |           | not null | 0
 action                     | smallint              |           | not null | 
 ref_type                   | smallint              |           | not null | 
 commit_from                | bytea                 |           |          | 
 commit_to                  | bytea                 |           |          | 
 ref                        | text                  |           |          | 
 commit_title               | character varying(70) |           |          | 
 ref_count                  | integer               |           |          | 
 event_id                   | bigint                |           | not null | 
Indexes:
    "push_event_payloads_pkey" PRIMARY KEY, btree (event_id)
Foreign-key constraints:
    "fk_36c74129da" FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
Triggers:
    trigger_07c94931164e BEFORE INSERT OR UPDATE ON push_event_payloads FOR EACH ROW EXECUTE PROCEDURE trigger_07c94931164e()
Edited by Krasimir Angelov

Merge request reports

Loading