Skip to content

Commit

Permalink
Added new constraint work_active_publication_date_check to migration,…
Browse files Browse the repository at this point in the history
… few other fixes
  • Loading branch information
brendan-oconnell committed May 17, 2024
1 parent 4a7e313 commit c4dfd37
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions thoth-api/migrations/v0.12.5/up.sql
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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';
Expand All @@ -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'
Expand Down Expand Up @@ -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'))

0 comments on commit c4dfd37

Please sign in to comment.