-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(booking): change induction to an enum (#272)
- Loading branch information
Showing
2 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
migration/db/migrations/202408131534000_alter_bookings_change_induction_to_enum.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
-- +micrate Up | ||
-- SQL in section 'Up' is executed when this migration is applied | ||
|
||
-- +micrate StatementBegin | ||
DO | ||
$$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT * | ||
FROM pg_type typ | ||
INNER JOIN pg_namespace nsp | ||
ON nsp.oid = typ.typnamespace | ||
WHERE nsp.nspname = current_schema() | ||
AND typ.typname = 'booking_induction_type') THEN | ||
CREATE TYPE booking_induction_type AS ENUM ( | ||
'TENTATIVE', | ||
'ACCEPTED', | ||
'DECLINED' | ||
); | ||
END IF; | ||
END; | ||
$$ | ||
LANGUAGE plpgsql; | ||
-- +micrate StatementEnd | ||
|
||
-- Add the new column | ||
ALTER TABLE "bookings" ADD COLUMN IF NOT EXISTS induction_new public.booking_induction_type DEFAULT 'TENTATIVE'::public.booking_induction_type; | ||
|
||
-- Migrate data | ||
UPDATE bookings | ||
SET induction_new = CASE | ||
WHEN induction = true THEN 'ACCEPTED'::public.booking_induction_type | ||
ELSE 'TENTATIVE'::public.booking_induction_type | ||
END; | ||
|
||
-- Drop the old column and rename the new column | ||
ALTER TABLE bookings DROP COLUMN induction; | ||
ALTER TABLE bookings RENAME COLUMN induction_new TO induction; | ||
|
||
-- +micrate Down | ||
-- SQL section 'Down' is executed when this migration is rolled back | ||
|
||
-- Add a temporary boolean column | ||
ALTER TABLE bookings ADD COLUMN induction_old boolean DEFAULT false; | ||
|
||
-- Migrate data back to boolean | ||
UPDATE bookings | ||
SET induction_old = CASE | ||
WHEN induction = 'ACCEPTED' THEN true | ||
ELSE false | ||
END; | ||
|
||
-- Drop the enum column | ||
ALTER TABLE bookings DROP COLUMN induction; | ||
|
||
-- Rename the boolean column back to original name | ||
ALTER TABLE bookings RENAME COLUMN induction_old TO induction; | ||
|
||
-- Drop the enum type | ||
DROP TYPE IF EXISTS public.booking_induction_type; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters