diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-08-18 13:50:51 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-08-18 13:50:51 +0300 |
commit | db384e6b19af03b4c3c82a5760d83a3fd79f7982 (patch) | |
tree | 34beaef37df5f47ccbcf5729d7583aae093cffa0 /db/post_migrate/20230809090349_ensure_id_uniqueness_for_p_ci_builds_v2.rb | |
parent | 54fd7b1bad233e3944434da91d257fa7f63c3996 (diff) |
Add latest changes from gitlab-org/gitlab@16-3-stable-eev16.3.0-rc42
Diffstat (limited to 'db/post_migrate/20230809090349_ensure_id_uniqueness_for_p_ci_builds_v2.rb')
-rw-r--r-- | db/post_migrate/20230809090349_ensure_id_uniqueness_for_p_ci_builds_v2.rb | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/db/post_migrate/20230809090349_ensure_id_uniqueness_for_p_ci_builds_v2.rb b/db/post_migrate/20230809090349_ensure_id_uniqueness_for_p_ci_builds_v2.rb new file mode 100644 index 00000000000..8170059624c --- /dev/null +++ b/db/post_migrate/20230809090349_ensure_id_uniqueness_for_p_ci_builds_v2.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +class EnsureIdUniquenessForPCiBuildsV2 < Gitlab::Database::Migration[2.1] + include Gitlab::Database::SchemaHelpers + include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum + + enable_lock_retries! + + TABLE_NAME = :p_ci_builds + FUNCTION_NAME = :assign_p_ci_builds_id_value + TRIGGER_NAME = :assign_p_ci_builds_id_trigger + + def up + return unless should_run? + return if trigger_exists?(:ci_builds, TRIGGER_NAME) + + change_column_default(TABLE_NAME, :id, nil) + + create_trigger_function(FUNCTION_NAME) do + <<~SQL + IF NEW."id" IS NOT NULL THEN + RAISE WARNING 'Manually assigning ids is not allowed, the value will be ignored'; + END IF; + NEW."id" := nextval('ci_builds_id_seq'::regclass); + RETURN NEW; + SQL + end + + Gitlab::Database::PostgresPartitionedTable.each_partition(TABLE_NAME) do |partition| + create_trigger(partition.identifier, TRIGGER_NAME, FUNCTION_NAME, fires: 'BEFORE INSERT') + end + end + + def down + return unless should_run? + + execute(<<~SQL.squish) + ALTER TABLE #{TABLE_NAME} + ALTER COLUMN id SET DEFAULT nextval('ci_builds_id_seq'::regclass); + + DROP FUNCTION IF EXISTS #{FUNCTION_NAME} CASCADE; + SQL + end + + private + + def should_run? + can_execute_on?(:ci_builds) + end +end |