Skip to content

Commit

Permalink
refactor(booking): change induction to an enum (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
chillfox authored Aug 14, 2024
1 parent 6106044 commit c08b739
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
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;
9 changes: 8 additions & 1 deletion src/placeos-models/booking.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ module PlaceOS::Model
MONTHLY
end

enum Induction
TENTATIVE
ACCEPTED
DECLINED
end

attribute booking_type : String
attribute booking_start : Int64
attribute booking_end : Int64
Expand Down Expand Up @@ -106,7 +112,8 @@ module PlaceOS::Model

attribute images : Array(String) = [] of String

attribute induction : Bool = false
attribute induction : Induction = Induction::TENTATIVE, converter: PlaceOS::Model::PGEnumConverter(PlaceOS::Model::Booking::Induction),
description: "The induction status of the booking. Defaults to TENTATIVE."

attribute permission : Permission = Permission::PRIVATE, converter: PlaceOS::Model::PGEnumConverter(PlaceOS::Model::Booking::Permission),
description: "The permission level for the booking. Defaults to private. If set to private, attendees must be invited.If set to open, users in the same tenant can join. If set to public, the booking is open for everyone to join."
Expand Down

0 comments on commit c08b739

Please sign in to comment.