| 1 | -- Flyway Migration: Expand TASKS table with description, due_date, priority, and status enum
|
|---|
| 2 | -- This migration adds new columns to support custom tracking tasks with enhanced details.
|
|---|
| 3 | -- Daily discipline tasks will use NULL for these optional fields.
|
|---|
| 4 |
|
|---|
| 5 | SET search_path TO trekr;
|
|---|
| 6 |
|
|---|
| 7 | -- Create ENUM type for task status
|
|---|
| 8 | CREATE TYPE task_status AS ENUM ('NOT_STARTED', 'IN_PROGRESS', 'FINISHED');
|
|---|
| 9 |
|
|---|
| 10 | -- Add new columns to TASKS table
|
|---|
| 11 | ALTER TABLE trekr.tasks
|
|---|
| 12 | ADD COLUMN description TEXT,
|
|---|
| 13 | ADD COLUMN due_date DATE,
|
|---|
| 14 | ADD COLUMN priority VARCHAR(10) DEFAULT 'MEDIUM' CHECK (priority IN ('LOW', 'MEDIUM', 'HIGH')),
|
|---|
| 15 | ADD COLUMN status task_status DEFAULT 'NOT_STARTED';
|
|---|
| 16 |
|
|---|
| 17 | -- For backwards compatibility, migrate is_finished to status
|
|---|
| 18 | -- is_finished = true → status = FINISHED
|
|---|
| 19 | -- is_finished = false → status = NOT_STARTED
|
|---|
| 20 | UPDATE trekr.tasks
|
|---|
| 21 | SET status = CASE
|
|---|
| 22 | WHEN is_finished = true THEN 'FINISHED'::task_status
|
|---|
| 23 | ELSE 'NOT_STARTED'::task_status
|
|---|
| 24 | END;
|
|---|
| 25 |
|
|---|
| 26 | -- Drop the is_finished column (after migration)
|
|---|
| 27 | ALTER TABLE trekr.tasks
|
|---|
| 28 | DROP COLUMN is_finished;
|
|---|
| 29 |
|
|---|
| 30 | -- Add index for filtering tasks by status and custom_tracking_id
|
|---|
| 31 | CREATE INDEX idx_tasks_custom_tracking_status
|
|---|
| 32 | ON trekr.tasks(custom_tracking_id, status)
|
|---|
| 33 | WHERE custom_tracking_id IS NOT NULL;
|
|---|
| 34 |
|
|---|
| 35 | -- Add index for filtering by discipline_user_id and status (for daily completion computation)
|
|---|
| 36 | CREATE INDEX idx_tasks_discipline_status
|
|---|
| 37 | ON trekr.tasks(discipline_user_id, status)
|
|---|
| 38 | WHERE discipline_user_id IS NOT NULL;
|
|---|
| 39 |
|
|---|
| 40 | -- Add index for due_date filtering (useful for reports)
|
|---|
| 41 | CREATE INDEX idx_tasks_due_date
|
|---|
| 42 | ON trekr.tasks(due_date)
|
|---|
| 43 | WHERE due_date IS NOT NULL;
|
|---|
| 44 |
|
|---|