diff --git a/src/backend/app/tasks/tasks_crud.py b/src/backend/app/tasks/tasks_crud.py index 04be2bc57..c394fc64f 100644 --- a/src/backend/app/tasks/tasks_crud.py +++ b/src/backend/app/tasks/tasks_crud.py @@ -175,7 +175,10 @@ async def update_task_status( async def create_task_history_for_status_change( - db_task: db_models.DbTask, new_status: TaskStatus, db_user: db_models.DbUser, db + db_task: db_models.DbTask, + new_status: TaskStatus, + db_user: db_models.DbUser, + db: Session, ): """Append task status change to task history.""" msg = ( diff --git a/src/backend/migrations/009-delete-user.sql b/src/backend/migrations/009-delete-user.sql index af78a5e43..e414bd182 100644 --- a/src/backend/migrations/009-delete-user.sql +++ b/src/backend/migrations/009-delete-user.sql @@ -5,21 +5,85 @@ BEGIN; -- Allow user_id in task_history to be NULL -ALTER TABLE task_history -ALTER COLUMN user_id DROP NOT NULL; +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'task_history' + AND column_name = 'user_id' + AND is_nullable = 'NO' + ) THEN + ALTER TABLE task_history + ALTER COLUMN user_id DROP NOT NULL; + END IF; +END $$; -- Add a new column username to task_history -ALTER TABLE task_history -ADD COLUMN username VARCHAR; +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'task_history' + AND column_name = 'username' + ) THEN + ALTER TABLE task_history + ADD COLUMN username VARCHAR; + END IF; +END $$; -- Allow author_id in projects to be NULL -ALTER TABLE projects ALTER COLUMN author_id DROP NOT NULL; +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'projects' + AND column_name = 'author_id' + AND is_nullable = 'NO' + ) THEN + ALTER TABLE projects + ALTER COLUMN author_id DROP NOT NULL; + END IF; +END $$; -- Allow locked_by, mapped_by, and validated_by in tasks to be NULL -ALTER TABLE tasks -ALTER COLUMN locked_by DROP NOT NULL, -ALTER COLUMN mapped_by DROP NOT NULL, -ALTER COLUMN validated_by DROP NOT NULL; +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'tasks' + AND column_name = 'locked_by' + AND is_nullable = 'NO' + ) THEN + ALTER TABLE tasks + ALTER COLUMN locked_by DROP NOT NULL; + END IF; + + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'tasks' + AND column_name = 'mapped_by' + AND is_nullable = 'NO' + ) THEN + ALTER TABLE tasks + ALTER COLUMN mapped_by DROP NOT NULL; + END IF; + + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'tasks' + AND column_name = 'validated_by' + AND is_nullable = 'NO' + ) THEN + ALTER TABLE tasks + ALTER COLUMN validated_by DROP NOT NULL; + END IF; +END $$; -- Commit the transaction COMMIT; diff --git a/src/backend/migrations/revert/009-delete-user.sql b/src/backend/migrations/revert/009-delete-user.sql index 67bcc5d21..131b6b064 100644 --- a/src/backend/migrations/revert/009-delete-user.sql +++ b/src/backend/migrations/revert/009-delete-user.sql @@ -5,21 +5,85 @@ BEGIN; -- Disallow user_id in task_history to be NULL -ALTER TABLE task_history -ALTER COLUMN user_id SET NOT NULL; +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'task_history' + AND column_name = 'user_id' + AND is_nullable = 'YES' + ) THEN + ALTER TABLE task_history + ALTER COLUMN user_id SET NOT NULL; + END IF; +END $$; -- Remove the column username from task_history -ALTER TABLE task_history -DROP COLUMN username; +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'task_history' + AND column_name = 'username' + ) THEN + ALTER TABLE task_history + DROP COLUMN username; + END IF; +END $$; -- Disallow author_id in projects to be NULL -ALTER TABLE projects ALTER COLUMN author_id SET NOT NULL; +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'projects' + AND column_name = 'author_id' + AND is_nullable = 'YES' + ) THEN + ALTER TABLE projects + ALTER COLUMN author_id SET NOT NULL; + END IF; +END $$; -- Disallow locked_by, mapped_by, and validated_by in tasks to be NULL -ALTER TABLE tasks -ALTER COLUMN locked_by SET NOT NULL, -ALTER COLUMN mapped_by SET NOT NULL, -ALTER COLUMN validated_by SET NOT NULL; +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'tasks' + AND column_name = 'locked_by' + AND is_nullable = 'YES' + ) THEN + ALTER TABLE tasks + ALTER COLUMN locked_by SET NOT NULL; + END IF; + + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'tasks' + AND column_name = 'mapped_by' + AND is_nullable = 'YES' + ) THEN + ALTER TABLE tasks + ALTER COLUMN mapped_by SET NOT NULL; + END IF; + + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'tasks' + AND column_name = 'validated_by' + AND is_nullable = 'YES' + ) THEN + ALTER TABLE tasks + ALTER COLUMN validated_by SET NOT NULL; + END IF; +END $$; -- Commit the transaction COMMIT;