summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-05-05 06:12:41 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2023-05-05 06:12:41 +0000
commit7c0c3a7dc95668d20ec8f4bbc2d505f373b6032a (patch)
tree2034e645e8aada36db5d816d271d7f2d8728157d /db
parentd8abaef3c6859400b684427ba57aa247b6272cb1 (diff)
downloadgitlab-ce-7c0c3a7dc95668d20ec8f4bbc2d505f373b6032a.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'db')
-rw-r--r--db/post_migrate/20230419094535_ensure_merge_request_metrics_id_bigint_backfill_is_finished_for_self_hosts.rb24
-rw-r--r--db/post_migrate/20230419094939_swap_merge_request_metrics_id_to_bigint_for_self_hosts.rb82
-rw-r--r--db/schema_migrations/202304190945351
-rw-r--r--db/schema_migrations/202304190949391
4 files changed, 108 insertions, 0 deletions
diff --git a/db/post_migrate/20230419094535_ensure_merge_request_metrics_id_bigint_backfill_is_finished_for_self_hosts.rb b/db/post_migrate/20230419094535_ensure_merge_request_metrics_id_bigint_backfill_is_finished_for_self_hosts.rb
new file mode 100644
index 00000000000..decfdf0a3ca
--- /dev/null
+++ b/db/post_migrate/20230419094535_ensure_merge_request_metrics_id_bigint_backfill_is_finished_for_self_hosts.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class EnsureMergeRequestMetricsIdBigintBackfillIsFinishedForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ TABLE_NAME = 'merge_request_metrics'
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: TABLE_NAME,
+ column_name: 'id',
+ job_arguments: [['id'], ['id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230419094939_swap_merge_request_metrics_id_to_bigint_for_self_hosts.rb b/db/post_migrate/20230419094939_swap_merge_request_metrics_id_to_bigint_for_self_hosts.rb
new file mode 100644
index 00000000000..d249ffb9c1b
--- /dev/null
+++ b/db/post_migrate/20230419094939_swap_merge_request_metrics_id_to_bigint_for_self_hosts.rb
@@ -0,0 +1,82 @@
+# frozen_string_literal: true
+
+class SwapMergeRequestMetricsIdToBigintForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ TABLE_NAME = 'merge_request_metrics'
+ TMP_INDEX_NAME = 'tmp_index_mr_metrics_on_target_project_id_merged_at_nulls_last'
+ INDEX_NAME = 'index_mr_metrics_on_target_project_id_merged_at_nulls_last'
+ CONSTRAINT_NAME = 'merge_request_metrics_pkey'
+
+ def up
+ return if should_skip?
+ return if temporary_column_already_dropped?
+ return if columns_alredy_swapped?
+
+ swap
+ end
+
+ def down
+ return if should_skip?
+ return if temporary_column_already_dropped?
+ return unless columns_alredy_swapped?
+
+ swap
+ end
+
+ private
+
+ def swap
+ add_concurrent_index TABLE_NAME, :id_convert_to_bigint, unique: true,
+ name: 'index_merge_request_metrics_on_id_convert_to_bigint'
+ add_concurrent_index TABLE_NAME, 'target_project_id, merged_at DESC NULLS LAST, id_convert_to_bigint DESC',
+ name: TMP_INDEX_NAME
+
+ with_lock_retries(raise_on_exhaustion: true) do
+ execute "LOCK TABLE #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE"
+
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN id TO id_tmp"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN id_convert_to_bigint TO id"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN id_tmp TO id_convert_to_bigint"
+
+ function_name = Gitlab::Database::UnidirectionalCopyTrigger
+ .on_table(TABLE_NAME, connection: connection)
+ .name(:id, :id_convert_to_bigint)
+ execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"
+
+ # Swap defaults of the columns, and change ownership of the sequence to the new id
+ execute "ALTER SEQUENCE merge_request_metrics_id_seq OWNED BY #{TABLE_NAME}.id"
+ change_column_default TABLE_NAME, :id, -> { "nextval('merge_request_metrics_id_seq'::regclass)" }
+ change_column_default TABLE_NAME, :id_convert_to_bigint, 0
+
+ # Swap PK constraint
+ execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT IF EXISTS #{CONSTRAINT_NAME}"
+ rename_index TABLE_NAME, 'index_merge_request_metrics_on_id_convert_to_bigint', CONSTRAINT_NAME
+ execute "ALTER TABLE #{TABLE_NAME} ADD CONSTRAINT #{CONSTRAINT_NAME} PRIMARY KEY USING INDEX #{CONSTRAINT_NAME}"
+
+ # Rename the rest of the indexes (we already hold an exclusive lock, so no need to use DROP INDEX CONCURRENTLY)
+ execute "DROP INDEX IF EXISTS #{INDEX_NAME}"
+ rename_index TABLE_NAME, TMP_INDEX_NAME, INDEX_NAME
+ end
+ end
+
+ def should_skip?
+ com_or_dev_or_test_but_not_jh?
+ end
+
+ def columns_alredy_swapped?
+ table_columns = columns(TABLE_NAME)
+ column_id = table_columns.find { |c| c.name == 'id' }
+ column_id_convert_to_bigint = table_columns.find { |c| c.name == 'id_convert_to_bigint' }
+
+ column_id.sql_type == 'bigint' && column_id_convert_to_bigint.sql_type == 'integer'
+ end
+
+ def temporary_column_already_dropped?
+ table_columns = columns(TABLE_NAME)
+
+ !table_columns.find { |c| c.name == 'id_convert_to_bigint' }
+ end
+end
diff --git a/db/schema_migrations/20230419094535 b/db/schema_migrations/20230419094535
new file mode 100644
index 00000000000..7df3afa8a17
--- /dev/null
+++ b/db/schema_migrations/20230419094535
@@ -0,0 +1 @@
+09e9e754b83635abe717fa686b13488f965ae9678f74e0a47e802941d3501d5a \ No newline at end of file
diff --git a/db/schema_migrations/20230419094939 b/db/schema_migrations/20230419094939
new file mode 100644
index 00000000000..643e94129a2
--- /dev/null
+++ b/db/schema_migrations/20230419094939
@@ -0,0 +1 @@
+a0e01b39610c5c307e34a5215ce4aa47a24fc0f5cff62ebef13172ef6761198b \ No newline at end of file