Skip to content

Commit

Permalink
fix: add task_split_type to project when task splitting complete (hot…
Browse files Browse the repository at this point in the history
…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
4 people authored and nischalstha9 committed Dec 8, 2023
1 parent 02d3dbb commit 4f95525
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/backend/app/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Index,
Integer,
LargeBinary,
SmallInteger,
String,
Table,
UniqueConstraint,
Expand All @@ -51,6 +52,7 @@
ProjectStatus,
TaskAction,
TaskCreationMode,
TaskSplitType,
TaskStatus,
TeamVisibility,
UserRole,
Expand Down Expand Up @@ -542,7 +544,11 @@ def tasks_bad(self):
form_config_file = Column(LargeBinary) # Yaml config file if custom xls is uploaded

data_extract_type = Column(String) # Type of data extract (Polygon or Centroid)
task_split_type = Column(String) # Type of split (Grid or Feature)
# Options: divide on square, manual upload, task splitting algo
task_split_type = Column(Enum(TaskSplitType), nullable=True)
task_split_dimension = Column(SmallInteger, nullable=True)
task_num_buildings = Column(SmallInteger, nullable=True)

hashtags = Column(ARRAY(String)) # Project hashtag


Expand Down
8 changes: 8 additions & 0 deletions src/backend/app/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,11 @@ class BackgroundTaskStatus(IntEnum, Enum):

TILES_SOURCE = ["esri", "bing", "google", "topo"]
TILES_FORMATS = ["mbtiles", "sqlitedb", "sqlite3", "sqlite", "pmtiles"]


class TaskSplitType(IntEnum, Enum):
"""Enum describing task splitting type."""

DIVIDE_ON_SQUARE = 0
CHOOSE_AREA_AS_TASK = 1
TASK_SPLITTING_ALGORITHM = 2
6 changes: 6 additions & 0 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ def create_project_with_project_info(
odk_credentials = project_metadata.odk_central
hashtags = project_metadata.hashtags
organisation_id = project_metadata.organisation_id
task_split_type = project_metadata.task_split_type
task_split_dimension = project_metadata.task_split_dimension
task_num_buildings = project_metadata.task_num_buildings

# verify data coming in
if not project_user:
Expand Down Expand Up @@ -344,6 +347,9 @@ def create_project_with_project_info(
odk_central_password=pw,
hashtags=hashtags,
organisation_id=organisation_id,
task_split_type=task_split_type,
task_split_dimension=task_split_dimension,
task_num_buildings=task_num_buildings,
# country=[project_metadata.country],
# location_str=f"{project_metadata.city}, {project_metadata.country}",
)
Expand Down
6 changes: 5 additions & 1 deletion src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pydantic import BaseModel

from ..db import db_models
from ..models.enums import ProjectPriority, ProjectStatus
from ..models.enums import ProjectPriority, ProjectStatus, TaskSplitType
from ..tasks import tasks_schemas
from ..users.user_schemas import User

Expand Down Expand Up @@ -52,6 +52,10 @@ class BETAProjectUpload(BaseModel):
odk_central: ODKCentral
hashtags: Optional[List[str]] = None
organisation_id: Optional[int] = None
task_split_type: Optional[TaskSplitType] = None
task_split_dimension: Optional[int] = None
task_num_buildings: Optional[int] = None

# city: str
# country: str

Expand Down
72 changes: 72 additions & 0 deletions src/backend/migrations/001-project-split-type-fields.sql
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;

0 comments on commit 4f95525

Please sign in to comment.