diff --git a/thoth-api/migrations/v0.12.5/up.sql b/thoth-api/migrations/v0.12.5/up.sql index 0fff94b7f..03fb26fb7 100644 --- a/thoth-api/migrations/v0.12.5/up.sql +++ b/thoth-api/migrations/v0.12.5/up.sql @@ -1,4 +1,12 @@ - +-- Assign updated_at as placeholder publication_date for Active works with no publication date +-- works in local db with this status as of 17-05-2024: 1 work +-- Publisher should be notified and add correct publication_date +-- !!! This step of migration is irreversible. +UPDATE work + SET + publication_date = COALESCE(publication_date, updated_at) + WHERE + work_status = 'active'; -- Drop constraints, otherwise it won't be able to cast to text ALTER TABLE work @@ -18,11 +26,14 @@ ALTER TABLE work ALTER COLUMN work_status TYPE text; UPDATE work SET work_status = 'superseded', - withdrawn_date = updated_at + withdrawn_date = COALESCE(withdrawn_date, updated_at) WHERE work_status = 'out-of-print' OR work_status = 'out-of-stock-indefinitely' OR work_status = 'inactive'; + + + -- these work_status currently have no works with this status in production db; -- nonetheless, reassign in case works get assigned these work_status -- before migration is run in production @@ -34,7 +45,7 @@ UPDATE work -- real works with forthcoming work_status that we don't want to -- incorrectly change to 'unspecified' or 'unknown'. However, this doesn't -- seem like a big deal, since there are no works with these work_status --- currently. +-- currently. UPDATE work SET work_status = 'forthcoming' WHERE work_status = 'unspecified' OR work_status = 'unknown'; @@ -45,8 +56,9 @@ UPDATE work -- recalled, 0 works -- !!! see above: this step of the migration is irreversible. UPDATE work - SET work_status = 'withdrawn-from-sale', - withdrawn_date = updated_at + SET + work_status = 'withdrawn-from-sale', + withdrawn_date = COALESCE(withdrawn_date, updated_at) WHERE work_status = 'no-longer-our-product' OR work_status = 'remaindered' @@ -75,6 +87,7 @@ ALTER TABLE work ADD CONSTRAINT work_active_withdrawn_date_check CHECK ((work_status = 'withdrawn-from-sale' OR work_status = 'superseded') OR (work_status NOT IN ('withdrawn-from-sale', 'superseded') AND withdrawn_date IS NULL)), - - - + -- active works must have publication_date + ADD CONSTRAINT work_active_publication_date_check CHECK + ((work_status = 'active' AND publication_date IS NOT NULL) + OR (work_status != 'active')) \ No newline at end of file