diff options
Diffstat (limited to 'db/post_migrate/20221102090943_create_second_partition_for_builds_metadata.rb')
-rw-r--r-- | db/post_migrate/20221102090943_create_second_partition_for_builds_metadata.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/db/post_migrate/20221102090943_create_second_partition_for_builds_metadata.rb b/db/post_migrate/20221102090943_create_second_partition_for_builds_metadata.rb new file mode 100644 index 00000000000..6923e6f6cba --- /dev/null +++ b/db/post_migrate/20221102090943_create_second_partition_for_builds_metadata.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +class CreateSecondPartitionForBuildsMetadata < Gitlab::Database::Migration[2.0] + TABLE_NAME = 'p_ci_builds_metadata' + BUILDS_TABLE = 'ci_builds' + NEXT_PARTITION_ID = 101 + PARTITION_NAME = 'gitlab_partitions_dynamic.ci_builds_metadata_101' + + disable_ddl_transaction! + + def up + return unless Gitlab.com? + + with_lock_retries(**lock_args) do + connection.execute(<<~SQL) + LOCK TABLE #{BUILDS_TABLE} IN SHARE UPDATE EXCLUSIVE MODE; + LOCK TABLE ONLY #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE; + SQL + + connection.execute(<<~SQL) + CREATE TABLE IF NOT EXISTS #{PARTITION_NAME} + PARTITION OF #{TABLE_NAME} + FOR VALUES IN (#{NEXT_PARTITION_ID}); + SQL + end + end + + def down + return unless Gitlab.com? + return unless table_exists?(PARTITION_NAME) + + with_lock_retries(**lock_args) do + connection.execute(<<~SQL) + LOCK TABLE #{BUILDS_TABLE}, #{TABLE_NAME}, #{PARTITION_NAME} IN ACCESS EXCLUSIVE MODE; + SQL + + connection.execute(<<~SQL) + ALTER TABLE #{TABLE_NAME} DETACH PARTITION #{PARTITION_NAME}; + SQL + + connection.execute(<<~SQL) + DROP TABLE IF EXISTS #{PARTITION_NAME} CASCADE; + SQL + end + end + + private + + def lock_args + { + raise_on_exhaustion: true, + timing_configuration: lock_timing_configuration + } + end + + def lock_timing_configuration + iterations = Gitlab::Database::WithLockRetries::DEFAULT_TIMING_CONFIGURATION + aggressive_iterations = Array.new(5) { [10.seconds, 1.minute] } + + iterations + aggressive_iterations + end +end |