Commit 5006578a authored by Sean McGivern's avatar Sean McGivern

Merge branch '217941-drop-updated-at-on-audit-events' into 'master'

Remove updated_at column from audit_events table

See merge request gitlab-org/gitlab!35690
parents ad596914 81158d49
---
title: Remove updated_at column on audit_events table
merge_request: 35690
author:
type: other
# frozen_string_literal: true
class RemoveUpdatedAtFromAuditEvents < 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, :updated_at
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,
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,
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.created_at);
END IF;
RETURN NULL;
SQL
end
remove_column PARTITIONED_TABLE_NAME, :updated_at
end
end
def down
with_lock_retries do
add_column SOURCE_TABLE_NAME, :updated_at, :datetime # rubocop:disable Migration/Datetime
add_column PARTITIONED_TABLE_NAME, :updated_at, :datetime # rubocop:disable Migration/Datetime
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,
updated_at = NEW.updated_at,
ip_address = NEW.ip_address,
author_name = NEW.author_name,
entity_path = NEW.entity_path,
target_details = NEW.target_details,
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,
updated_at,
ip_address,
author_name,
entity_path,
target_details,
created_at)
VALUES (NEW.id,
NEW.author_id,
NEW.type,
NEW.entity_id,
NEW.entity_type,
NEW.details,
NEW.updated_at,
NEW.ip_address,
NEW.author_name,
NEW.entity_path,
NEW.target_details,
NEW.created_at);
END IF;
RETURN NULL;
SQL
end
end
end
end
77601e653f7b4f2740db87a7b19b64bb73fffbe4ce59c0f68b0bb65599da0eb3
\ No newline at end of file
......@@ -25,7 +25,6 @@ ELSIF (TG_OP = 'UPDATE') THEN
entity_id = NEW.entity_id,
entity_type = NEW.entity_type,
details = NEW.details,
updated_at = NEW.updated_at,
ip_address = NEW.ip_address,
author_name = NEW.author_name,
entity_path = NEW.entity_path,
......@@ -39,7 +38,6 @@ ELSIF (TG_OP = 'INSERT') THEN
entity_id,
entity_type,
details,
updated_at,
ip_address,
author_name,
entity_path,
......@@ -51,7 +49,6 @@ ELSIF (TG_OP = 'INSERT') THEN
NEW.entity_id,
NEW.entity_type,
NEW.details,
NEW.updated_at,
NEW.ip_address,
NEW.author_name,
NEW.entity_path,
......@@ -72,7 +69,6 @@ CREATE TABLE public.audit_events_part_5fc467ac26 (
entity_id integer NOT NULL,
entity_type character varying NOT NULL,
details text,
updated_at timestamp without time zone,
ip_address inet,
author_name text,
entity_path text,
......@@ -9470,7 +9466,6 @@ CREATE TABLE public.audit_events (
entity_type character varying NOT NULL,
details text,
created_at timestamp without time zone,
updated_at timestamp without time zone,
ip_address inet,
author_name text,
entity_path text,
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe AuditEventPartitioned do
let(:source_table) { AuditEvent }
let(:partitioned_table) { described_class }
it 'has the same columns as the source table' do
expect(partitioned_table.column_names).to match_array(source_table.column_names)
end
it 'inserts the same record as the one in the source table', :aggregate_failures do
expect { create(:audit_event) }.to change { partitioned_table.count }.by(1)
event_from_source_table = source_table.connection.select_one(
"SELECT * FROM #{source_table.table_name} ORDER BY created_at desc LIMIT 1"
)
event_from_partitioned_table = partitioned_table.connection.select_one(
"SELECT * FROM #{partitioned_table.table_name} ORDER BY created_at desc LIMIT 1"
)
expect(event_from_partitioned_table).to eq(event_from_source_table)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment