diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
commit | 48aff82709769b098321c738f3444b9bdaa694c6 (patch) | |
tree | e00c7c43e2d9b603a5a6af576b1685e400410dee /db/post_migrate/20200929113254_remove_type_from_audit_events.rb | |
parent | 879f5329ee916a948223f8f43d77fba4da6cd028 (diff) |
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc42
Diffstat (limited to 'db/post_migrate/20200929113254_remove_type_from_audit_events.rb')
-rw-r--r-- | db/post_migrate/20200929113254_remove_type_from_audit_events.rb | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/db/post_migrate/20200929113254_remove_type_from_audit_events.rb b/db/post_migrate/20200929113254_remove_type_from_audit_events.rb new file mode 100644 index 00000000000..000dc0d2865 --- /dev/null +++ b/db/post_migrate/20200929113254_remove_type_from_audit_events.rb @@ -0,0 +1,125 @@ +# frozen_string_literal: true + +class RemoveTypeFromAuditEvents < ActiveRecord::Migration[6.0] + include Gitlab::Database::SchemaHelpers + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + SOURCE_TABLE_NAME = 'audit_events' + PARTITIONED_TABLE_NAME = 'audit_events_part_5fc467ac26' + TRIGGER_FUNCTION_NAME = 'table_sync_function_2be879775d' + + def up + with_lock_retries do + remove_column SOURCE_TABLE_NAME, :type + + create_trigger_function(TRIGGER_FUNCTION_NAME, replace: true) do + <<~SQL + IF (TG_OP = 'DELETE') THEN + DELETE FROM #{PARTITIONED_TABLE_NAME} where id = OLD.id; + ELSIF (TG_OP = 'UPDATE') THEN + UPDATE #{PARTITIONED_TABLE_NAME} + SET author_id = NEW.author_id, + entity_id = NEW.entity_id, + entity_type = NEW.entity_type, + details = NEW.details, + ip_address = NEW.ip_address, + author_name = NEW.author_name, + entity_path = NEW.entity_path, + target_details = NEW.target_details, + target_type = NEW.target_type, + target_id = NEW.target_id, + created_at = NEW.created_at + WHERE #{PARTITIONED_TABLE_NAME}.id = NEW.id; + ELSIF (TG_OP = 'INSERT') THEN + INSERT INTO #{PARTITIONED_TABLE_NAME} (id, + author_id, + entity_id, + entity_type, + details, + ip_address, + author_name, + entity_path, + target_details, + target_type, + target_id, + created_at) + VALUES (NEW.id, + NEW.author_id, + NEW.entity_id, + NEW.entity_type, + NEW.details, + NEW.ip_address, + NEW.author_name, + NEW.entity_path, + NEW.target_details, + NEW.target_type, + NEW.target_id, + NEW.created_at); + END IF; + RETURN NULL; + SQL + end + + remove_column PARTITIONED_TABLE_NAME, :type + end + end + + def down + with_lock_retries do + add_column SOURCE_TABLE_NAME, :type, :string + add_column PARTITIONED_TABLE_NAME, :type, :string + + create_trigger_function(TRIGGER_FUNCTION_NAME, replace: true) do + <<~SQL + IF (TG_OP = 'DELETE') THEN + DELETE FROM #{PARTITIONED_TABLE_NAME} where id = OLD.id; + ELSIF (TG_OP = 'UPDATE') THEN + UPDATE #{PARTITIONED_TABLE_NAME} + SET author_id = NEW.author_id, + type = NEW.type, + entity_id = NEW.entity_id, + entity_type = NEW.entity_type, + details = NEW.details, + ip_address = NEW.ip_address, + author_name = NEW.author_name, + entity_path = NEW.entity_path, + target_details = NEW.target_details, + target_type = NEW.target_type, + target_id = NEW.target_id, + created_at = NEW.created_at + WHERE #{PARTITIONED_TABLE_NAME}.id = NEW.id; + ELSIF (TG_OP = 'INSERT') THEN + INSERT INTO #{PARTITIONED_TABLE_NAME} (id, + author_id, + type, + entity_id, + entity_type, + details, + ip_address, + author_name, + entity_path, + target_details, + target_type, + target_id, + created_at) + VALUES (NEW.id, + NEW.author_id, + NEW.type, + NEW.entity_id, + NEW.entity_type, + NEW.details, + NEW.ip_address, + NEW.author_name, + NEW.entity_path, + NEW.target_details, + NEW.target_type, + NEW.target_id, + NEW.created_at); + END IF; + RETURN NULL; + SQL + end + end + end +end |