Skip to content

Commit

Permalink
refactor: make delete user sql code idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuj-Gupta4 committed Nov 5, 2024
1 parent b361e92 commit a87d027
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/backend/app/tasks/tasks_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
82 changes: 73 additions & 9 deletions src/backend/migrations/009-delete-user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
82 changes: 73 additions & 9 deletions src/backend/migrations/revert/009-delete-user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit a87d027

Please sign in to comment.