-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add migration file to add enum values 'NEW_GEOM' and 'VALIDATED'
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
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,13 @@ | ||
-- Add new values to the entitystate enum type | ||
BEGIN; | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'NEW_GEOM' AND enumtypid = 'entitystate'::regtype) THEN | ||
ALTER TYPE entitystate ADD VALUE 'NEW_GEOM'; | ||
END IF; | ||
|
||
IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'VALIDATED' AND enumtypid = 'entitystate'::regtype) THEN | ||
ALTER TYPE entitystate ADD VALUE 'VALIDATED'; | ||
END IF; | ||
END$$; | ||
COMMIT; |
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
46 changes: 46 additions & 0 deletions
46
src/backend/migrations/revert/007-add-entitystate-enumtype.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,46 @@ | ||
-- Revert migration: Remove 'NEW_GEOM' and 'VALIDATED' values | ||
-- from the entitystate enum type | ||
BEGIN; | ||
|
||
DO $$ | ||
BEGIN | ||
-- Check if the unwanted enum values exist | ||
IF EXISTS ( | ||
SELECT 1 | ||
FROM pg_enum | ||
WHERE enumlabel IN ('NEW_GEOM', 'VALIDATED') | ||
AND enumtypid = 'entitystate'::regtype | ||
) THEN | ||
-- Step 1: Create a new enum type without the unwanted values | ||
CREATE TYPE public.entitystate_new AS ENUM ( | ||
'READY', | ||
'OPENED_IN_ODK', | ||
'SURVEY_SUBMITTED', | ||
'MARKED_BAD' | ||
); | ||
ALTER TYPE public.entitystate_new OWNER TO fmtm; | ||
|
||
-- Step 2: Update rows with unwanted enum values | ||
-- Map 'NEW_GEOM' to 'READY' and 'VALIDATED' to 'SURVEY_SUBMITTED' | ||
UPDATE public.odk_entities | ||
SET status = 'READY' | ||
WHERE status = 'NEW_GEOM'; | ||
|
||
UPDATE public.odk_entities | ||
SET status = 'SURVEY_SUBMITTED' | ||
WHERE status = 'VALIDATED'; | ||
|
||
-- Step 3: Alter the column to use the new enum type | ||
ALTER TABLE public.odk_entities | ||
ALTER COLUMN status TYPE public.entitystate_new | ||
USING status::text::entitystate_new; | ||
|
||
-- Step 4: Drop the old enum type | ||
DROP TYPE public.entitystate; | ||
|
||
-- Step 5: Rename the new enum type to the original name | ||
ALTER TYPE public.entitystate_new RENAME TO entitystate; | ||
END IF; | ||
END$$; | ||
|
||
COMMIT; |