Skip to content

Commit

Permalink
feat: add migration file to add enum values 'NEW_GEOM' and 'VALIDATED'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sujanadh committed Jan 28, 2025
1 parent 5388d20 commit 4b8437f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/backend/migrations/007-add-entitystate-enumtype.sql
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;
2 changes: 2 additions & 0 deletions src/backend/migrations/init/fmtm_base_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ CREATE TYPE public.entitystate AS ENUM (
'READY',
'OPENED_IN_ODK',
'SURVEY_SUBMITTED',
'NEW_GEOM',
'VALIDATED',
'MARKED_BAD'
);
ALTER TYPE public.entitystate OWNER TO fmtm;
Expand Down
46 changes: 46 additions & 0 deletions src/backend/migrations/revert/007-add-entitystate-enumtype.sql
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;

0 comments on commit 4b8437f

Please sign in to comment.