|
| 1 | +ALTER TYPE work_status RENAME VALUE 'withdrawn-from-sale' TO 'withdrawn'; |
| 2 | + |
| 3 | +-- Assign 1900-01-01 as placeholder publication_date for |
| 4 | +-- Active, withdrawn from sale, out of print, out of stock indefinitely works with no publication date |
| 5 | +-- Required for work_active_publication_date_check constraint below |
| 6 | +-- Affected works in production db with this status, 29-05-2024: 59 works (incl. chapters) |
| 7 | +-- Before running migration, make a list of affected works |
| 8 | +-- After running migration, publishers should be notified to add correct publication_date |
| 9 | +-- !!! This is irreversible |
| 10 | +UPDATE work |
| 11 | + SET |
| 12 | + publication_date = '1900-01-01' |
| 13 | + WHERE |
| 14 | + work_status IN |
| 15 | + ('active', 'withdrawn', 'out-of-print', 'out-of-stock-indefinitely', 'inactive') |
| 16 | + AND publication_date IS NULL; |
| 17 | + |
| 18 | +-- Drop constraints, otherwise it won't be able to cast to text |
| 19 | +ALTER TABLE work |
| 20 | + DROP CONSTRAINT IF EXISTS work_active_withdrawn_date_check, |
| 21 | + DROP CONSTRAINT IF EXISTS work_inactive_no_withdrawn_date_check; |
| 22 | + |
| 23 | +ALTER TABLE work ALTER COLUMN work_status TYPE text; |
| 24 | + |
| 25 | +-- delete unused work_status enum |
| 26 | +DROP TYPE work_status; |
| 27 | + |
| 28 | +-- Assign out of print/inactive/out of stock indefinitely works work_status 'superseded' |
| 29 | +-- current counts in production db as of 29-05-2024: |
| 30 | +-- 145 works (incl. chapters) |
| 31 | +-- Before running migration, make a list of affected works |
| 32 | +-- After running migration, publishers should be notified to add correct work_status |
| 33 | +-- and remove withdrawn_date as necessary. Many OBP "out of print" works are actually first editions |
| 34 | +-- for which superseded is the correct new work_status. |
| 35 | +-- !!! This is irreversible |
| 36 | +UPDATE work |
| 37 | + SET |
| 38 | + work_status = 'superseded', |
| 39 | + -- assign a withdrawn_date, which is required for superseded works |
| 40 | + withdrawn_date = CASE |
| 41 | + WHEN withdrawn_date IS NOT NULL THEN withdrawn_date |
| 42 | + -- + INTERVAL '1 day' is necessary because at least one work has publication_date on |
| 43 | + -- the same day as updated_at, but updated_at has a timestamp, so it's |
| 44 | + -- greater than. Which then throws an error with the |
| 45 | + -- work_withdrawn_date_after_publication_date_check constraint. |
| 46 | + WHEN withdrawn_date IS NULL AND publication_date + INTERVAL '1 day' < updated_at THEN updated_at |
| 47 | + ELSE CURRENT_DATE |
| 48 | + END |
| 49 | + WHERE |
| 50 | + work_status = 'out-of-print' |
| 51 | + OR work_status = 'out-of-stock-indefinitely' |
| 52 | + OR work_status = 'inactive'; |
| 53 | + |
| 54 | +-- Assign unspecified/unkown works work_status 'forthcoming' |
| 55 | +-- current counts in production db as of 29-05-2024: |
| 56 | +-- unspecified, 0 works |
| 57 | +-- unknown, 0 works |
| 58 | +-- !!! This is irreversible |
| 59 | +UPDATE work |
| 60 | + SET work_status = 'forthcoming' |
| 61 | + WHERE work_status = 'unspecified' OR work_status = 'unknown'; |
| 62 | + |
| 63 | +-- Assign no longer our product/remaindered/recalled works work_status 'withdrawn-from-sale' |
| 64 | +-- current counts in production db as of 29-05-2024: |
| 65 | +-- no-longer-our-product, 0 works |
| 66 | +-- remaindered, 0 works |
| 67 | +-- recalled, 0 works |
| 68 | +-- !!! This is irreversible |
| 69 | +UPDATE work |
| 70 | + SET |
| 71 | + work_status = 'withdrawn', |
| 72 | + withdrawn_date = COALESCE(withdrawn_date, updated_at) |
| 73 | + WHERE |
| 74 | + work_status = 'no-longer-our-product' |
| 75 | + OR work_status = 'remaindered' |
| 76 | + OR work_status = 'recalled'; |
| 77 | + |
| 78 | +-- create new work_status enum, adds superseded |
| 79 | +CREATE TYPE work_status AS ENUM ( |
| 80 | + 'cancelled', |
| 81 | + 'forthcoming', |
| 82 | + 'postponed-indefinitely', |
| 83 | + 'active', |
| 84 | + 'withdrawn', |
| 85 | + 'superseded' |
| 86 | +); |
| 87 | +ALTER TABLE work ALTER COLUMN work_status TYPE work_status USING work_status::work_status; |
| 88 | + |
| 89 | +-- add new constraints (with same names as in v0.12.3) to work table |
| 90 | +ALTER TABLE work |
| 91 | + -- withdrawn and superseded works must have withdrawn_date |
| 92 | + -- note that this constraint has the same name as migration from v.0.12.3, |
| 93 | + -- but changes previous constraint by adding superseded alongside withdrawn |
| 94 | + ADD CONSTRAINT work_inactive_no_withdrawn_date_check CHECK |
| 95 | + (((work_status = 'withdrawn' OR work_status = 'superseded') AND withdrawn_date IS NOT NULL) |
| 96 | + OR (work_status NOT IN ('withdrawn', 'superseded'))), |
| 97 | + -- all other work statuses must not have withdrawn_date; see above, adds superseded |
| 98 | + ADD CONSTRAINT work_active_withdrawn_date_check CHECK |
| 99 | + ((work_status = 'withdrawn' OR work_status = 'superseded') |
| 100 | + OR (work_status NOT IN ('withdrawn', 'superseded') AND withdrawn_date IS NULL)), |
| 101 | + -- active, withdrawn-from-sale, and superseded works must have publication_date |
| 102 | + ADD CONSTRAINT work_active_publication_date_check CHECK |
| 103 | + ((work_status IN ('active', 'withdrawn', 'superseded') AND publication_date IS NOT NULL) |
| 104 | + OR (work_status NOT IN ('active', 'withdrawn', 'superseded'))); |
0 commit comments