forked from hotosm/fmtm
-
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.
fix: add task_split_type to project when task splitting complete (hot…
…osm#990) * fix : added task_split_type to project when task splitting complete * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * changed str enum to int enum * altered field type of task_split_type to integer * created migration file alter-field-type-task_split_type * feat(migration): task_split_type, task_split_dimension, task_num_buildings * fix: add task_split_ params to db_modesl & project schema * fix(migration): creating tasksplittype enum if exists, cast to type * fix: make task_split_type Optional for tests * fix: add task_split_dimension & num_buildings if available --------- Co-authored-by: sujanadh <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: spwoodcock <[email protected]>
- Loading branch information
1 parent
02d3dbb
commit 4f95525
Showing
5 changed files
with
98 additions
and
2 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
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
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
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
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,72 @@ | ||
-- ## Migration to: | ||
-- * Change project.task_split_type str --> int. | ||
-- * Add field project.task_split_dimension (int). | ||
-- * Add field project.task_num_buildings (int). | ||
|
||
|
||
|
||
-- ## Apply Migration | ||
-- Start a transaction | ||
BEGIN; | ||
-- Create task_split_type enum if it doesn't exist | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'tasksplittype') THEN | ||
CREATE TYPE public.tasksplittype AS ENUM ( | ||
'DIVIDE_ON_SQUARE', | ||
'CHOOSE_AREA_AS_TASK', | ||
'TASK_SPLITTING_ALGORITHM' | ||
); | ||
END IF; | ||
END $$; | ||
ALTER TYPE public.mappinglevel OWNER TO fmtm; | ||
|
||
-- Update task_split_type | ||
DO $$ | ||
BEGIN | ||
-- Check if the column exists | ||
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'projects' AND column_name = 'task_split_type') THEN | ||
-- Alter the column if it exists | ||
EXECUTE ' | ||
ALTER TABLE public.projects | ||
ALTER COLUMN task_split_type | ||
TYPE public.tasksplittype | ||
USING task_split_type::tasksplittype | ||
'; | ||
END IF; | ||
END $$; | ||
|
||
-- Add extra columns | ||
ALTER TABLE IF EXISTS public.projects | ||
ADD COLUMN IF NOT EXISTS task_split_dimension SMALLINT, | ||
ADD COLUMN IF NOT EXISTS task_num_buildings SMALLINT; | ||
-- Commit the transaction | ||
COMMIT; | ||
|
||
|
||
|
||
-- -- ## Revert Migration (comment above, uncomment below) | ||
-- -- Start a transaction | ||
-- BEGIN; | ||
-- -- Revert task_split_type type | ||
-- DO $$ | ||
-- BEGIN | ||
-- -- Check if the column exists | ||
-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'projects' AND column_name = 'task_split_type') THEN | ||
-- -- Alter the column if it exists | ||
-- EXECUTE ' | ||
-- ALTER TABLE public.projects | ||
-- ALTER COLUMN task_split_type | ||
-- TYPE VARCHAR | ||
-- USING task_split_type::VARCHAR | ||
-- '; | ||
-- END IF; | ||
-- END $$; | ||
-- -- Remove extra fields | ||
-- ALTER TABLE IF EXISTS public.projects | ||
-- DROP COLUMN IF EXISTS task_split_dimension, | ||
-- DROP COLUMN IF EXISTS task_num_buildings; | ||
-- -- Remove enum | ||
-- DROP TYPE IF EXISTS public.tasksplittype; | ||
-- -- Commit the transaction | ||
-- COMMIT; |