diff --git a/internal/store/postgres/migrations/20251030120000_convert_null_org_name_to_empty_string.down.sql b/internal/store/postgres/migrations/20251030120000_convert_null_org_name_to_empty_string.down.sql new file mode 100644 index 000000000..f856159c9 --- /dev/null +++ b/internal/store/postgres/migrations/20251030120000_convert_null_org_name_to_empty_string.down.sql @@ -0,0 +1,18 @@ +-- Temporarily drop immutability trigger to allow update +DROP TRIGGER IF EXISTS trg_audit_records_enforce_immutability ON audit_records; + +-- Convert empty string org_name values back to NULL +UPDATE audit_records +SET org_name = NULL +WHERE org_name = ''; + +-- Recreate immutability trigger +CREATE TRIGGER trg_audit_records_enforce_immutability + BEFORE UPDATE ON audit_records + FOR EACH ROW EXECUTE FUNCTION prevent_audit_record_updates(); + +-- Restore partial index condition to original +DROP INDEX IF EXISTS idx_audit_records_org_name; +CREATE INDEX idx_audit_records_org_name + ON audit_records(org_name, occurred_at DESC) + WHERE org_name IS NOT NULL; \ No newline at end of file diff --git a/internal/store/postgres/migrations/20251030120000_convert_null_org_name_to_empty_string.up.sql b/internal/store/postgres/migrations/20251030120000_convert_null_org_name_to_empty_string.up.sql new file mode 100644 index 000000000..de7c7797e --- /dev/null +++ b/internal/store/postgres/migrations/20251030120000_convert_null_org_name_to_empty_string.up.sql @@ -0,0 +1,18 @@ +-- Temporarily drop immutability trigger to allow update +DROP TRIGGER IF EXISTS trg_audit_records_enforce_immutability ON audit_records; + +-- Convert all NULL org_name values to empty strings +UPDATE audit_records +SET org_name = '' +WHERE org_name IS NULL; + +-- Recreate immutability trigger +CREATE TRIGGER trg_audit_records_enforce_immutability + BEFORE UPDATE ON audit_records + FOR EACH ROW EXECUTE FUNCTION prevent_audit_record_updates(); + +-- Update partial index condition to exclude both NULL and empty strings +DROP INDEX IF EXISTS idx_audit_records_org_name; +CREATE INDEX idx_audit_records_org_name + ON audit_records(org_name, occurred_at DESC) + WHERE org_name IS NOT NULL AND org_name != ''; \ No newline at end of file