Skip to content

Commit

Permalink
Add character_type_implies_no_end DB constraint
Browse files Browse the repository at this point in the history
Characters should never have an end date or area; this just enforces it
at the database level like we do with group genders.
  • Loading branch information
reosarevok committed Nov 4, 2024
1 parent 3cb0b5d commit 48f9385
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
13 changes: 13 additions & 0 deletions admin/sql/CreateConstraints.sql
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ ADD CONSTRAINT group_type_implies_null_gender CHECK (
OR type IS NULL
);

ALTER TABLE artist
ADD CONSTRAINT character_type_implies_no_end CHECK (
(
type = 4 AND
end_date_day IS NULL AND
end_date_month IS NULL AND
end_date_year IS NULL AND
end_area IS NULL AND
ended = FALSE
)
OR type != 4
);

ALTER TABLE release_label
ADD CHECK (catalog_number IS NOT NULL OR label IS NOT NULL);

Expand Down
29 changes: 29 additions & 0 deletions admin/sql/updates/20241104-mbs-9885.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
\set ON_ERROR_STOP 1

BEGIN;

UPDATE artist
SET end_date_day = NULL,
end_date_month = NULL,
end_date_year = NULL,
end_area = NULL,
ended = FALSE
WHERE type = 4;

ALTER TABLE artist
DROP CONSTRAINT IF EXISTS character_type_implies_no_end;

ALTER TABLE artist
ADD CONSTRAINT character_type_implies_no_end CHECK (
(
type = 4 AND
end_date_day IS NULL AND
end_date_month IS NULL AND
end_date_year IS NULL AND
end_area IS NULL AND
ended = FALSE
)
OR type != 4
);

COMMIT;
35 changes: 35 additions & 0 deletions admin/sql/updates/schema-change/30.master_and_standalone.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- Generated by CompileSchemaScripts.pl from:
-- 20241104-mbs-9885.sql
\set ON_ERROR_STOP 1
BEGIN;
SET search_path = musicbrainz, public;
SET LOCAL statement_timeout = 0;
--------------------------------------------------------------------------------
SELECT '20241104-mbs-9885.sql';


UPDATE artist
SET end_date_day = NULL,
end_date_month = NULL,
end_date_year = NULL,
end_area = NULL,
ended = FALSE
WHERE type = 4;

ALTER TABLE artist
DROP CONSTRAINT IF EXISTS character_type_implies_no_end;

ALTER TABLE artist
ADD CONSTRAINT character_type_implies_no_end CHECK (
(
type = 4 AND
end_date_day IS NULL AND
end_date_month IS NULL AND
end_date_year IS NULL AND
end_area IS NULL AND
ended = FALSE
)
OR type != 4
);

COMMIT;
5 changes: 5 additions & 0 deletions upgrade.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,10 @@
"20240223-mbs-13421-fks.sql",
"20240319-mbs-13514.sql"
]
},
"30": {
"master_and_standalone": [
"20241104-mbs-9885.sql"
]
}
}

0 comments on commit 48f9385

Please sign in to comment.