Commit 232d1336 authored by Tan Le's avatar Tan Le

Remove not-null on type column in audit_events

We are in the process of moving away from STI model in `AuditEvent` and
dropping `type` column. There is only one type (e.g. `SecurityEvent`)
and this redundant column wastes precious storage.

Rollback is an no-op since there would be null values added to the table
after the application stop writing to this field. We will need to create
separate migration to rectify these null values if there is a need to go
back to the original schema.
parent bb7b2a0b
---
title: Remove not-null constraint on type column in audit_events
merge_request: 39192
author:
type: other
# frozen_string_literal: true
class RemoveNotNullConstraintOnTypeFromAuditEvents < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
# rubocop:disable Migration/WithLockRetriesDisallowedMethod
# To avoid deadlock on audit_event and audit_event_part... since there is a trigger to insert record from audit_events
# to audit_events_part..., we need to ensure each ALTER TABLE command run in its own transaction.
def up
with_lock_retries do
change_column_null :audit_events_part_5fc467ac26, :type, true
end
with_lock_retries do
change_column_null :audit_events, :type, true
end
end
# rubocop:enable Migration/WithLockRetriesDisallowedMethod
def down
# no-op -- null values might be added after this constraint is removed.
end
end
b7477fbcba166d848e3b1bd7f4a184f50f3b2fafe80bfcf44fd6f4f9979ffcb7
\ No newline at end of file
...@@ -65,7 +65,7 @@ COMMENT ON FUNCTION public.table_sync_function_2be879775d() IS 'Partitioning mig ...@@ -65,7 +65,7 @@ COMMENT ON FUNCTION public.table_sync_function_2be879775d() IS 'Partitioning mig
CREATE TABLE public.audit_events_part_5fc467ac26 ( CREATE TABLE public.audit_events_part_5fc467ac26 (
id bigint NOT NULL, id bigint NOT NULL,
author_id integer NOT NULL, author_id integer NOT NULL,
type character varying NOT NULL, type character varying,
entity_id integer NOT NULL, entity_id integer NOT NULL,
entity_type character varying NOT NULL, entity_type character varying NOT NULL,
details text, details text,
...@@ -9461,7 +9461,7 @@ CREATE TABLE public.ar_internal_metadata ( ...@@ -9461,7 +9461,7 @@ CREATE TABLE public.ar_internal_metadata (
CREATE TABLE public.audit_events ( CREATE TABLE public.audit_events (
id integer NOT NULL, id integer NOT NULL,
author_id integer NOT NULL, author_id integer NOT NULL,
type character varying NOT NULL, type character varying,
entity_id integer NOT NULL, entity_id integer NOT NULL,
entity_type character varying NOT NULL, entity_type character varying NOT NULL,
details text, details text,
......
...@@ -10,6 +10,13 @@ RSpec.describe AuditEventPartitioned do ...@@ -10,6 +10,13 @@ RSpec.describe AuditEventPartitioned do
expect(partitioned_table.column_names).to match_array(source_table.column_names) expect(partitioned_table.column_names).to match_array(source_table.column_names)
end end
it 'has the same null constraints as the source table' do
constraints_from_source_table = null_constraints(source_table)
constraints_from_partitioned_table = null_constraints(partitioned_table)
expect(constraints_from_partitioned_table.to_a).to match_array(constraints_from_source_table.to_a)
end
it 'inserts the same record as the one in the source table', :aggregate_failures do 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) expect { create(:audit_event) }.to change { partitioned_table.count }.by(1)
...@@ -22,4 +29,13 @@ RSpec.describe AuditEventPartitioned do ...@@ -22,4 +29,13 @@ RSpec.describe AuditEventPartitioned do
expect(event_from_partitioned_table).to eq(event_from_source_table) expect(event_from_partitioned_table).to eq(event_from_source_table)
end end
def null_constraints(table)
table.connection.select_all(<<~SQL)
SELECT c.column_name, c.is_nullable
FROM information_schema.columns c
WHERE c.table_name = '#{table.table_name}'
AND c.column_name != 'created_at'
SQL
end
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