From dcbdcb89b4540c360d79912b8fff78be0f00ce38 Mon Sep 17 00:00:00 2001 From: Kithmina Gunasinghe <124335388+kithmina1999@users.noreply.github.com> Date: Fri, 25 Jul 2025 11:19:08 +0530 Subject: [PATCH 1/2] fix(startup): ensure proper line endings and permissions for init script - Convert CRLF to LF in 00_init.sh to prevent execution issues on Unix systems - Set executable permission for 00_init.sh to ensure it can run - Remove unused migration step from init script to clean up startup process --- start.sh | 5 +++++ worklenz-backend/database/00_init.sh | 18 ------------------ 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/start.sh b/start.sh index 47d88baee..033f001d0 100755 --- a/start.sh +++ b/start.sh @@ -108,6 +108,11 @@ for port in "${ports[@]}"; do fi done +# Fix line endings (convert CRLF to LF) and set executable permission for 00_init.sh +dos2unix worklenz-backend/database/00_init.sh 2>/dev/null || true +chmod +x worklenz-backend/database/00_init.sh + + # Start the containers echo -e "${BLUE}Starting Worklenz services...${NC}" $DOCKER_COMPOSE_CMD down diff --git a/worklenz-backend/database/00_init.sh b/worklenz-backend/database/00_init.sh index afd8562a1..b368f31ad 100644 --- a/worklenz-backend/database/00_init.sh +++ b/worklenz-backend/database/00_init.sh @@ -65,24 +65,6 @@ done echo "✅ Base schema SQL execution complete." -# -------------------------------------------- -# 🚀 STEP 3: Apply SQL migrations -# -------------------------------------------- -if [ -d "$MIGRATIONS_DIR" ] && compgen -G "$MIGRATIONS_DIR/*.sql" > /dev/null; then - echo "Applying migrations..." - for f in "$MIGRATIONS_DIR"/*.sql; do - version=$(basename "$f") - if ! psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -tAc "SELECT 1 FROM schema_migrations WHERE version = '$version'" | grep -q 1; then - echo "Applying migration: $version" - psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -f "$f" - psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "INSERT INTO schema_migrations (version) VALUES ('$version');" - else - echo "Skipping already applied migration: $version" - fi - done -else - echo "No migration files found or directory is empty, skipping migrations." -fi echo "🎉 Database initialization completed successfully." From 6f94695828742e268e929724ec7cb2d8592fb8a4 Mon Sep 17 00:00:00 2001 From: Kithmina Gunasinghe <124335388+kithmina1999@users.noreply.github.com> Date: Fri, 25 Jul 2025 11:42:34 +0530 Subject: [PATCH 2/2] feat(database): implement database migrations system Add node-pg-migrate configuration and initial migration files to manage database schema changes. Includes: - Configuration files for migration setup - Dockerfile updates to include migrations - Script updates to run migrations on startup - Initial migration files for task progress, sorting, and performance improvements - Database schema changes to support new features --- start.sh | 22 + update-docker-env.sh | 1 + worklenz-backend/.pgmrc.json | 3 + worklenz-backend/Dockerfile | 2 + .../20250115000000_performance-indexes.js | 161 ++++++ ...0250128000000_fix-window-function-error.js | 184 +++++++ .../20250422132400_manual-task-progress.js | 100 ++++ .../20250423000000-subtask-manual-progress.js | 404 +++++++++++++++ ...-add-progress-and-weight-activity-types.js | 298 +++++++++++ ...250425000000-update-time-based-progress.js | 343 +++++++++++++ ...426000000-update-progress-mode-handlers.js | 188 +++++++ ...mprove-parent-task-progress-calculation.js | 301 +++++++++++ .../20250427000000-fix-progress-mode-type.js | 169 +++++++ ...multilevel-subtask-progress-calculation.js | 220 ++++++++ ...20250715000000-add-grouping-sort-orders.js | 118 +++++ ...20250715000000_add-grouping-sort-orders.js | 135 +++++ .../20250715000001-update-sort-functions.js | 186 +++++++ .../20250715000002-fix-sort-constraint.js | 188 +++++++ .../database/pg-migrations/README.md | 72 --- worklenz-backend/database/sql/1_tables.sql | 7 + worklenz-backend/migrate.json | 22 + worklenz-backend/package-lock.json | 476 ++---------------- worklenz-backend/package.json | 8 +- 23 files changed, 3096 insertions(+), 512 deletions(-) create mode 100644 worklenz-backend/.pgmrc.json create mode 100644 worklenz-backend/database/pg-migrations/20250115000000_performance-indexes.js create mode 100644 worklenz-backend/database/pg-migrations/20250128000000_fix-window-function-error.js create mode 100644 worklenz-backend/database/pg-migrations/20250422132400_manual-task-progress.js create mode 100644 worklenz-backend/database/pg-migrations/20250423000000-subtask-manual-progress.js create mode 100644 worklenz-backend/database/pg-migrations/20250424000000-add-progress-and-weight-activity-types.js create mode 100644 worklenz-backend/database/pg-migrations/20250425000000-update-time-based-progress.js create mode 100644 worklenz-backend/database/pg-migrations/20250426000000-update-progress-mode-handlers.js create mode 100644 worklenz-backend/database/pg-migrations/20250426000000_improve-parent-task-progress-calculation.js create mode 100644 worklenz-backend/database/pg-migrations/20250427000000-fix-progress-mode-type.js create mode 100644 worklenz-backend/database/pg-migrations/20250506000000-fix-multilevel-subtask-progress-calculation.js create mode 100644 worklenz-backend/database/pg-migrations/20250715000000-add-grouping-sort-orders.js create mode 100644 worklenz-backend/database/pg-migrations/20250715000000_add-grouping-sort-orders.js create mode 100644 worklenz-backend/database/pg-migrations/20250715000001-update-sort-functions.js create mode 100644 worklenz-backend/database/pg-migrations/20250715000002-fix-sort-constraint.js delete mode 100644 worklenz-backend/database/pg-migrations/README.md create mode 100644 worklenz-backend/migrate.json diff --git a/start.sh b/start.sh index 033f001d0..134d52776 100755 --- a/start.sh +++ b/start.sh @@ -126,6 +126,28 @@ echo "This may take a minute or two depending on your system..." check_service "Database" "worklenz_db" "" DB_STATUS=$? + +# Wait for database to be healthy before running migrations +echo -e "${BLUE}Waiting for database to be healthy...${NC}" +until [ "$(docker inspect --format='{{.State.Health.Status}}' worklenz_db 2>/dev/null)" == "healthy" ]; do + sleep 2 + echo -n "." +done +echo -e "${GREEN}✓${NC} Database is healthy" + +# Run node-pg-migrate migrations if DB is up +if [ $DB_STATUS -eq 0 ]; then + echo -e "${BLUE}Running database migrations...${NC}" + $DOCKER_COMPOSE_CMD run --rm backend npm run migrate:up + MIGRATE_STATUS=$? + if [ $MIGRATE_STATUS -eq 0 ]; then + echo -e "${GREEN}✓${NC} Database migrations applied successfully" + else + echo -e "${RED}✗${NC} Database migrations failed" + exit 1 + fi +fi + check_service "MinIO" "worklenz_minio" "http://localhost:9000/minio/health/live" MINIO_STATUS=$? diff --git a/update-docker-env.sh b/update-docker-env.sh index 7852e86f7..f5704cec0 100755 --- a/update-docker-env.sh +++ b/update-docker-env.sh @@ -96,6 +96,7 @@ DB_PASSWORD=password DB_NAME=worklenz_db DB_MAX_CLIENTS=50 USE_PG_NATIVE=true +DATABASE_URL=postgres://postgres:password@db:5432/worklenz_db # Storage Configuration STORAGE_PROVIDER=s3 diff --git a/worklenz-backend/.pgmrc.json b/worklenz-backend/.pgmrc.json new file mode 100644 index 000000000..bb1e9c984 --- /dev/null +++ b/worklenz-backend/.pgmrc.json @@ -0,0 +1,3 @@ +{ + "config-file": "migrate.json" +} \ No newline at end of file diff --git a/worklenz-backend/Dockerfile b/worklenz-backend/Dockerfile index 244b7cf89..100c16530 100644 --- a/worklenz-backend/Dockerfile +++ b/worklenz-backend/Dockerfile @@ -32,6 +32,8 @@ COPY --from=builder /usr/src/app/build ./build COPY --from=builder /usr/src/app/node_modules ./node_modules COPY --from=builder /usr/src/app/release ./release COPY --from=builder /usr/src/app/worklenz-email-templates ./worklenz-email-templates +COPY --from=builder /usr/src/app/database/pg-migrations ./pg-migrations +COPY --from=builder /usr/src/app/database/pg-migrations ./migrations EXPOSE 3000 diff --git a/worklenz-backend/database/pg-migrations/20250115000000_performance-indexes.js b/worklenz-backend/database/pg-migrations/20250115000000_performance-indexes.js new file mode 100644 index 000000000..4b1873bab --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250115000000_performance-indexes.js @@ -0,0 +1,161 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = true; + +exports.up = pgm => { + // Since CONCURRENTLY cannot run in transactions, we'll create regular indexes + // For production, run the concurrent version separately + + console.log('Creating performance indexes (non-concurrent for migration compatibility)...'); + + // Composite index for main task filtering + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_project_archived_parent + ON tasks(project_id, archived, parent_task_id) + WHERE archived = FALSE + `); + + // Index for status joins + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_status_project + ON tasks(status_id, project_id) + WHERE archived = FALSE + `); + + // Index for assignees lookup + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_assignees_task_member + ON tasks_assignees(task_id, team_member_id) + `); + + // Index for phase lookup + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_task_phase_task_phase + ON task_phase(task_id, phase_id) + `); + + // Index for subtask counting + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_parent_archived + ON tasks(parent_task_id, archived) + WHERE parent_task_id IS NOT NULL AND archived = FALSE + `); + + // Index for labels + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_task_labels_task_label + ON task_labels(task_id, label_id) + `); + + // Index for comments count + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_task_comments_task + ON task_comments(task_id) + `); + + // Index for attachments count + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_task_attachments_task + ON task_attachments(task_id) + `); + + // Index for work log aggregation + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_task_work_log_task + ON task_work_log(task_id) + `); + + // Index for subscribers check + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_task_subscribers_task + ON task_subscribers(task_id) + `); + + // Index for dependencies check + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_task_dependencies_task + ON task_dependencies(task_id) + `); + + // Additional performance indexes + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_task_dependencies_related + ON task_dependencies(related_task_id) + `); + + // Index for custom column values + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_cc_column_values_task + ON cc_column_values(task_id) + `); + + // Index for project members lookup + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_project_members_team_project + ON project_members(team_member_id, project_id) + `); + + // Index for sorting + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_project_sort + ON tasks(project_id, sort_order) + WHERE archived = FALSE + `); + + // Index for roadmap sorting + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_project_roadmap_sort + ON tasks(project_id, roadmap_sort_order) + WHERE archived = FALSE + `); + + // Index for user lookup in team members + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_team_members_user_team + ON team_members(user_id, team_id) + WHERE active = TRUE + `); + + // Index for task statuses lookup + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_task_statuses_project_category + ON task_statuses(project_id, category_id) + `); + + // Index for task priorities lookup + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_priority + ON tasks(priority_id) + WHERE archived = FALSE + `); + + console.log('Performance indexes created successfully!'); +}; + +exports.down = pgm => { + // Drop indexes in reverse order + pgm.sql('DROP INDEX IF EXISTS idx_tasks_priority'); + pgm.sql('DROP INDEX IF EXISTS idx_task_statuses_project_category'); + pgm.sql('DROP INDEX IF EXISTS idx_team_members_user_team'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_project_roadmap_sort'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_project_sort'); + pgm.sql('DROP INDEX IF EXISTS idx_project_members_team_project'); + pgm.sql('DROP INDEX IF EXISTS idx_cc_column_values_task'); + pgm.sql('DROP INDEX IF EXISTS idx_task_dependencies_related'); + pgm.sql('DROP INDEX IF EXISTS idx_task_dependencies_task'); + pgm.sql('DROP INDEX IF EXISTS idx_task_subscribers_task'); + pgm.sql('DROP INDEX IF EXISTS idx_task_work_log_task'); + pgm.sql('DROP INDEX IF EXISTS idx_task_attachments_task'); + pgm.sql('DROP INDEX IF EXISTS idx_task_comments_task'); + pgm.sql('DROP INDEX IF EXISTS idx_task_labels_task_label'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_parent_archived'); + pgm.sql('DROP INDEX IF EXISTS idx_task_phase_task_phase'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_assignees_task_member'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_status_project'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_project_archived_parent'); +}; + +exports.__migration = { + transaction: false, +}; \ No newline at end of file diff --git a/worklenz-backend/database/pg-migrations/20250128000000_fix-window-function-error.js b/worklenz-backend/database/pg-migrations/20250128000000_fix-window-function-error.js new file mode 100644 index 000000000..4c513513e --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250128000000_fix-window-function-error.js @@ -0,0 +1,184 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = true; + +exports.up = pgm => { + // Replace the optimized sort functions to avoid CTE usage in UPDATE statements + pgm.createFunction( + 'handle_task_list_sort_between_groups_optimized', + [ + { name: '_from_index', type: 'integer' }, + { name: '_to_index', type: 'integer' }, + { name: '_task_id', type: 'uuid' }, + { name: '_project_id', type: 'uuid' }, + { name: '_batch_size', type: 'integer', default: 100 } + ], + { returns: 'void', language: 'plpgsql', replace: true }, + ` +DECLARE + _offset INT := 0; + _affected_rows INT; +BEGIN + -- PERFORMANCE OPTIMIZATION: Use direct updates without CTE in UPDATE + IF (_to_index = -1) + THEN + _to_index = COALESCE((SELECT MAX(sort_order) + 1 FROM tasks WHERE project_id = _project_id), 0); + END IF; + + -- PERFORMANCE OPTIMIZATION: Batch updates for large datasets + IF _to_index > _from_index + THEN + LOOP + UPDATE tasks + SET sort_order = sort_order - 1 + WHERE project_id = _project_id + AND sort_order > _from_index + AND sort_order < _to_index + AND sort_order > _offset + AND sort_order <= _offset + _batch_size; + + GET DIAGNOSTICS _affected_rows = ROW_COUNT; + EXIT WHEN _affected_rows = 0; + _offset := _offset + _batch_size; + END LOOP; + + UPDATE tasks SET sort_order = _to_index - 1 WHERE id = _task_id AND project_id = _project_id; + END IF; + + IF _to_index < _from_index + THEN + _offset := 0; + LOOP + UPDATE tasks + SET sort_order = sort_order + 1 + WHERE project_id = _project_id + AND sort_order > _to_index + AND sort_order < _from_index + AND sort_order > _offset + AND sort_order <= _offset + _batch_size; + + GET DIAGNOSTICS _affected_rows = ROW_COUNT; + EXIT WHEN _affected_rows = 0; + _offset := _offset + _batch_size; + END LOOP; + + UPDATE tasks SET sort_order = _to_index + 1 WHERE id = _task_id AND project_id = _project_id; + END IF; +END + ` + ); + + // Replace the second optimized sort function + pgm.createFunction( + 'handle_task_list_sort_inside_group_optimized', + [ + { name: '_from_index', type: 'integer' }, + { name: '_to_index', type: 'integer' }, + { name: '_task_id', type: 'uuid' }, + { name: '_project_id', type: 'uuid' }, + { name: '_batch_size', type: 'integer', default: 100 } + ], + { returns: 'void', language: 'plpgsql', replace: true }, + ` +DECLARE + _offset INT := 0; + _affected_rows INT; +BEGIN + -- PERFORMANCE OPTIMIZATION: Batch updates for large datasets without CTE in UPDATE + IF _to_index > _from_index + THEN + LOOP + UPDATE tasks + SET sort_order = sort_order - 1 + WHERE project_id = _project_id + AND sort_order > _from_index + AND sort_order <= _to_index + AND sort_order > _offset + AND sort_order <= _offset + _batch_size; + + GET DIAGNOSTICS _affected_rows = ROW_COUNT; + EXIT WHEN _affected_rows = 0; + _offset := _offset + _batch_size; + END LOOP; + END IF; + + IF _to_index < _from_index + THEN + _offset := 0; + LOOP + UPDATE tasks + SET sort_order = sort_order + 1 + WHERE project_id = _project_id + AND sort_order >= _to_index + AND sort_order < _from_index + AND sort_order > _offset + AND sort_order <= _offset + _batch_size; + + GET DIAGNOSTICS _affected_rows = ROW_COUNT; + EXIT WHEN _affected_rows = 0; + _offset := _offset + _batch_size; + END LOOP; + END IF; + + UPDATE tasks SET sort_order = _to_index WHERE id = _task_id AND project_id = _project_id; +END + ` + ); + + // Add simple bulk update function as alternative + pgm.createFunction( + 'update_task_sort_orders_bulk', + [{ name: '_updates', type: 'json' }], + { returns: 'void', language: 'plpgsql', replace: true }, + ` +DECLARE + _update_record RECORD; +BEGIN + -- Simple approach: update each task's sort_order from the provided array + FOR _update_record IN + SELECT + (item->>'task_id')::uuid as task_id, + (item->>'sort_order')::int as sort_order, + (item->>'status_id')::uuid as status_id, + (item->>'priority_id')::uuid as priority_id, + (item->>'phase_id')::uuid as phase_id + FROM json_array_elements(_updates) as item + LOOP + UPDATE tasks + SET + sort_order = _update_record.sort_order, + status_id = COALESCE(_update_record.status_id, status_id), + priority_id = COALESCE(_update_record.priority_id, priority_id) + WHERE id = _update_record.task_id; + + -- Handle phase updates separately since it's in a different table + IF _update_record.phase_id IS NOT NULL THEN + INSERT INTO task_phase (task_id, phase_id) + VALUES (_update_record.task_id, _update_record.phase_id) + ON CONFLICT (task_id) DO UPDATE SET phase_id = _update_record.phase_id; + END IF; + END LOOP; +END + ` + ); +}; + +exports.down = pgm => { + // Drop the functions if needed to rollback + pgm.dropFunction('update_task_sort_orders_bulk', [{ name: '_updates', type: 'json' }], { ifExists: true }); + pgm.dropFunction('handle_task_list_sort_inside_group_optimized', [ + { name: '_from_index', type: 'integer' }, + { name: '_to_index', type: 'integer' }, + { name: '_task_id', type: 'uuid' }, + { name: '_project_id', type: 'uuid' }, + { name: '_batch_size', type: 'integer' } + ], { ifExists: true }); + pgm.dropFunction('handle_task_list_sort_between_groups_optimized', [ + { name: '_from_index', type: 'integer' }, + { name: '_to_index', type: 'integer' }, + { name: '_task_id', type: 'uuid' }, + { name: '_project_id', type: 'uuid' }, + { name: '_batch_size', type: 'integer' } + ], { ifExists: true }); +}; \ No newline at end of file diff --git a/worklenz-backend/database/pg-migrations/20250422132400_manual-task-progress.js b/worklenz-backend/database/pg-migrations/20250422132400_manual-task-progress.js new file mode 100644 index 000000000..f28a631b3 --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250422132400_manual-task-progress.js @@ -0,0 +1,100 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = true; + +exports.up = pgm => { + // Add manual progress fields to tasks table + pgm.addColumns('tasks', { + manual_progress: { + type: 'boolean', + default: false, + notNull: false + }, + progress_value: { + type: 'integer', + default: null, + notNull: false + }, + weight: { + type: 'integer', + default: null, + notNull: false + } + }, { ifNotExists: true }); + + // Update function to consider manual progress + pgm.createFunction( + 'get_task_complete_ratio', + [{ name: '_task_id', type: 'uuid' }], + { returns: 'json', language: 'plpgsql', replace: true }, + ` +DECLARE + _parent_task_done FLOAT = 0; + _sub_tasks_done FLOAT = 0; + _sub_tasks_count FLOAT = 0; + _total_completed FLOAT = 0; + _total_tasks FLOAT = 0; + _ratio FLOAT = 0; + _is_manual BOOLEAN = FALSE; + _manual_value INTEGER = NULL; +BEGIN + -- Check if manual progress is set + SELECT manual_progress, progress_value + FROM tasks + WHERE id = _task_id + INTO _is_manual, _manual_value; + + -- If manual progress is enabled and has a value, use it directly + IF _is_manual IS TRUE AND _manual_value IS NOT NULL THEN + RETURN JSON_BUILD_OBJECT( + 'ratio', _manual_value, + 'total_completed', 0, + 'total_tasks', 0, + 'is_manual', TRUE + ); + END IF; + + -- Otherwise calculate automatically as before + SELECT (CASE + WHEN EXISTS(SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = _task_id + AND is_done IS TRUE) THEN 1 + ELSE 0 END) + INTO _parent_task_done; + SELECT COUNT(*) FROM tasks WHERE parent_task_id = _task_id AND archived IS FALSE INTO _sub_tasks_count; + + SELECT COUNT(*) + FROM tasks_with_status_view + WHERE parent_task_id = _task_id + AND is_done IS TRUE + INTO _sub_tasks_done; + + _total_completed = _parent_task_done + _sub_tasks_done; + _total_tasks = _sub_tasks_count; -- +1 for the parent task + + IF _total_tasks > 0 THEN + _ratio = (_total_completed / _total_tasks) * 100; + ELSE + _ratio = _parent_task_done * 100; + END IF; + + RETURN JSON_BUILD_OBJECT( + 'ratio', _ratio, + 'total_completed', _total_completed, + 'total_tasks', _total_tasks, + 'is_manual', FALSE + ); +END + ` + ); +}; + +exports.down = pgm => { + // Drop the function first (it depends on the columns) + pgm.dropFunction('get_task_complete_ratio', [{ name: '_task_id', type: 'uuid' }], { ifExists: true }); + + // Remove the added columns + pgm.dropColumns('tasks', ['manual_progress', 'progress_value', 'weight'], { ifExists: true }); +}; \ No newline at end of file diff --git a/worklenz-backend/database/pg-migrations/20250423000000-subtask-manual-progress.js b/worklenz-backend/database/pg-migrations/20250423000000-subtask-manual-progress.js new file mode 100644 index 000000000..83d9e577d --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250423000000-subtask-manual-progress.js @@ -0,0 +1,404 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = true; + +exports.up = pgm => { + // Add progress-related columns to projects table if they don't exist + pgm.sql(` + ALTER TABLE projects + ADD COLUMN IF NOT EXISTS use_manual_progress BOOLEAN DEFAULT FALSE, + ADD COLUMN IF NOT EXISTS use_weighted_progress BOOLEAN DEFAULT FALSE, + ADD COLUMN IF NOT EXISTS use_time_progress BOOLEAN DEFAULT FALSE + `); + + // Update get_task_complete_ratio function to consider subtask manual progress + pgm.sql(` + CREATE OR REPLACE FUNCTION get_task_complete_ratio(_task_id uuid) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _parent_task_done FLOAT = 0; + _sub_tasks_done FLOAT = 0; + _sub_tasks_count FLOAT = 0; + _total_completed FLOAT = 0; + _total_tasks FLOAT = 0; + _ratio FLOAT = 0; + _is_manual BOOLEAN = FALSE; + _manual_value INTEGER = NULL; + _project_id UUID; + _use_manual_progress BOOLEAN = FALSE; + _use_weighted_progress BOOLEAN = FALSE; + _use_time_progress BOOLEAN = FALSE; + BEGIN + -- Check if manual progress is set for this task + SELECT manual_progress, progress_value, project_id + FROM tasks + WHERE id = _task_id + INTO _is_manual, _manual_value, _project_id; + + -- Check if the project uses manual progress + IF _project_id IS NOT NULL THEN + SELECT COALESCE(use_manual_progress, FALSE), + COALESCE(use_weighted_progress, FALSE), + COALESCE(use_time_progress, FALSE) + FROM projects + WHERE id = _project_id + INTO _use_manual_progress, _use_weighted_progress, _use_time_progress; + END IF; + + -- Get all subtasks + SELECT COUNT(*) + FROM tasks + WHERE parent_task_id = _task_id AND archived IS FALSE + INTO _sub_tasks_count; + + -- If manual progress is enabled and has a value AND there are no subtasks, use it directly + IF _is_manual IS TRUE AND _manual_value IS NOT NULL AND _sub_tasks_count = 0 THEN + RETURN JSON_BUILD_OBJECT( + 'ratio', _manual_value, + 'total_completed', 0, + 'total_tasks', 0, + 'is_manual', TRUE + ); + END IF; + + -- If there are no subtasks, just use the parent task's status + IF _sub_tasks_count = 0 THEN + SELECT (CASE + WHEN EXISTS(SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = _task_id + AND is_done IS TRUE) THEN 1 + ELSE 0 END) + INTO _parent_task_done; + + _ratio = _parent_task_done * 100; + ELSE + -- If project uses manual progress, calculate based on subtask manual progress values + IF _use_manual_progress IS TRUE THEN + WITH subtask_progress AS ( + SELECT + CASE + -- If subtask has manual progress, use that value + WHEN manual_progress IS TRUE AND progress_value IS NOT NULL THEN + progress_value + -- Otherwise use completion status (0 or 100) + ELSE + CASE + WHEN EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = t.id + AND is_done IS TRUE + ) THEN 100 + ELSE 0 + END + END AS progress_value + FROM tasks t + WHERE t.parent_task_id = _task_id + AND t.archived IS FALSE + ) + SELECT COALESCE(AVG(progress_value), 0) + FROM subtask_progress + INTO _ratio; + -- If project uses weighted progress, calculate based on subtask weights + ELSIF _use_weighted_progress IS TRUE THEN + WITH subtask_progress AS ( + SELECT + CASE + -- If subtask has manual progress, use that value + WHEN manual_progress IS TRUE AND progress_value IS NOT NULL THEN + progress_value + -- Otherwise use completion status (0 or 100) + ELSE + CASE + WHEN EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = t.id + AND is_done IS TRUE + ) THEN 100 + ELSE 0 + END + END AS progress_value, + COALESCE(weight, 100) AS weight + FROM tasks t + WHERE t.parent_task_id = _task_id + AND t.archived IS FALSE + ) + SELECT COALESCE( + SUM(progress_value * weight) / NULLIF(SUM(weight), 0), + 0 + ) + FROM subtask_progress + INTO _ratio; + -- If project uses time-based progress, calculate based on estimated time + ELSIF _use_time_progress IS TRUE THEN + WITH subtask_progress AS ( + SELECT + CASE + -- If subtask has manual progress, use that value + WHEN manual_progress IS TRUE AND progress_value IS NOT NULL THEN + progress_value + -- Otherwise use completion status (0 or 100) + ELSE + CASE + WHEN EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = t.id + AND is_done IS TRUE + ) THEN 100 + ELSE 0 + END + END AS progress_value, + COALESCE(total_minutes, 0) AS estimated_minutes + FROM tasks t + WHERE t.parent_task_id = _task_id + AND t.archived IS FALSE + ) + SELECT COALESCE( + SUM(progress_value * estimated_minutes) / NULLIF(SUM(estimated_minutes), 0), + 0 + ) + FROM subtask_progress + INTO _ratio; + ELSE + -- Traditional calculation based on completion status + SELECT (CASE + WHEN EXISTS(SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = _task_id + AND is_done IS TRUE) THEN 1 + ELSE 0 END) + INTO _parent_task_done; + + SELECT COUNT(*) + FROM tasks_with_status_view + WHERE parent_task_id = _task_id + AND is_done IS TRUE + INTO _sub_tasks_done; + + _total_completed = _parent_task_done + _sub_tasks_done; + _total_tasks = _sub_tasks_count + 1; -- +1 for the parent task + + IF _total_tasks = 0 THEN + _ratio = 0; + ELSE + _ratio = (_total_completed / _total_tasks) * 100; + END IF; + END IF; + END IF; + + -- Ensure ratio is between 0 and 100 + IF _ratio < 0 THEN + _ratio = 0; + ELSIF _ratio > 100 THEN + _ratio = 100; + END IF; + + RETURN JSON_BUILD_OBJECT( + 'ratio', _ratio, + 'total_completed', _total_completed, + 'total_tasks', _total_tasks, + 'is_manual', _is_manual + ); + END + $$ + `); + + // Update update_project function to handle new progress fields + pgm.sql(` + CREATE OR REPLACE FUNCTION update_project(_body json) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _user_id UUID; + _team_id UUID; + _client_id UUID; + _project_id UUID; + _project_manager_team_member_id UUID; + _client_name TEXT; + _project_name TEXT; + BEGIN + -- need a test, can be throw errors + _client_name = TRIM((_body ->> 'client_name')::TEXT); + _project_name = TRIM((_body ->> 'name')::TEXT); + + -- add inside the controller + _user_id = (_body ->> 'user_id')::UUID; + _team_id = (_body ->> 'team_id')::UUID; + _project_manager_team_member_id = (_body ->> 'team_member_id')::UUID; + + -- cache exists client if exists + SELECT id FROM clients WHERE LOWER(name) = LOWER(_client_name) AND team_id = _team_id INTO _client_id; + + -- insert client if not exists + IF is_null_or_empty(_client_id) IS TRUE AND is_null_or_empty(_client_name) IS FALSE + THEN + INSERT INTO clients (name, team_id) VALUES (_client_name, _team_id) RETURNING id INTO _client_id; + END IF; + + -- check whether the project name is already in + IF EXISTS( + SELECT name FROM projects WHERE LOWER(name) = LOWER(_project_name) + AND team_id = _team_id AND id != (_body ->> 'id')::UUID + ) + THEN + RAISE 'PROJECT_EXISTS_ERROR:%', _project_name; + END IF; + + -- update the project + UPDATE projects + SET name = _project_name, + notes = (_body ->> 'notes')::TEXT, + color_code = (_body ->> 'color_code')::TEXT, + status_id = (_body ->> 'status_id')::UUID, + health_id = (_body ->> 'health_id')::UUID, + key = (_body ->> 'key')::TEXT, + start_date = (_body ->> 'start_date')::TIMESTAMPTZ, + end_date = (_body ->> 'end_date')::TIMESTAMPTZ, + client_id = _client_id, + folder_id = (_body ->> 'folder_id')::UUID, + category_id = (_body ->> 'category_id')::UUID, + updated_at = CURRENT_TIMESTAMP, + estimated_working_days = (_body ->> 'working_days')::INTEGER, + estimated_man_days = (_body ->> 'man_days')::INTEGER, + hours_per_day = (_body ->> 'hours_per_day')::INTEGER, + use_manual_progress = COALESCE((_body ->> 'use_manual_progress')::BOOLEAN, FALSE), + use_weighted_progress = COALESCE((_body ->> 'use_weighted_progress')::BOOLEAN, FALSE), + use_time_progress = COALESCE((_body ->> 'use_time_progress')::BOOLEAN, FALSE) + WHERE id = (_body ->> 'id')::UUID + AND team_id = _team_id + RETURNING id INTO _project_id; + + UPDATE project_members SET project_access_level_id = (SELECT id FROM project_access_levels WHERE key = 'MEMBER') WHERE project_id = _project_id; + + IF NOT (_project_manager_team_member_id IS NULL) + THEN + PERFORM update_project_manager(_project_manager_team_member_id, _project_id::UUID); + END IF; + + RETURN JSON_BUILD_OBJECT( + 'id', _project_id, + 'name', (_body ->> 'name')::TEXT, + 'project_manager_id', _project_manager_team_member_id::UUID + ); + END; + $$ + `); + + // Create trigger function to reset parent task manual progress when subtasks are added + pgm.sql(` + CREATE OR REPLACE FUNCTION reset_parent_task_manual_progress() RETURNS TRIGGER AS + $$ + BEGIN + -- When a task gets a new subtask (parent_task_id is set), reset the parent's manual_progress flag + IF NEW.parent_task_id IS NOT NULL THEN + UPDATE tasks + SET manual_progress = false + WHERE id = NEW.parent_task_id + AND manual_progress = true; + END IF; + RETURN NEW; + END; + $$ LANGUAGE plpgsql + `); + + // Create the trigger on the tasks table + pgm.sql(`DROP TRIGGER IF EXISTS reset_parent_manual_progress_trigger ON tasks`); + pgm.sql(` + CREATE TRIGGER reset_parent_manual_progress_trigger + AFTER INSERT OR UPDATE OF parent_task_id ON tasks + FOR EACH ROW + EXECUTE FUNCTION reset_parent_task_manual_progress() + `); +}; + +exports.down = pgm => { + // Drop trigger and function + pgm.sql('DROP TRIGGER IF EXISTS reset_parent_manual_progress_trigger ON tasks'); + pgm.sql('DROP FUNCTION IF EXISTS reset_parent_task_manual_progress()'); + + // Remove progress-related columns from projects table + pgm.sql(` + ALTER TABLE projects + DROP COLUMN IF EXISTS use_time_progress, + DROP COLUMN IF EXISTS use_weighted_progress, + DROP COLUMN IF EXISTS use_manual_progress + `); + + // Revert get_task_complete_ratio function to previous version (simplified) + pgm.sql(` + CREATE OR REPLACE FUNCTION get_task_complete_ratio(_task_id uuid) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _parent_task_done FLOAT = 0; + _sub_tasks_done FLOAT = 0; + _sub_tasks_count FLOAT = 0; + _total_completed FLOAT = 0; + _total_tasks FLOAT = 0; + _ratio FLOAT = 0; + _is_manual BOOLEAN = FALSE; + _manual_value INTEGER = NULL; + BEGIN + -- Check if manual progress is set + SELECT manual_progress, progress_value + FROM tasks + WHERE id = _task_id + INTO _is_manual, _manual_value; + + -- If manual progress is enabled and has a value, use it directly + IF _is_manual IS TRUE AND _manual_value IS NOT NULL THEN + RETURN JSON_BUILD_OBJECT( + 'ratio', _manual_value, + 'total_completed', 0, + 'total_tasks', 0, + 'is_manual', TRUE + ); + END IF; + + -- Otherwise calculate automatically as before + SELECT (CASE + WHEN EXISTS(SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = _task_id + AND is_done IS TRUE) THEN 1 + ELSE 0 END) + INTO _parent_task_done; + SELECT COUNT(*) FROM tasks WHERE parent_task_id = _task_id AND archived IS FALSE INTO _sub_tasks_count; + + SELECT COUNT(*) + FROM tasks_with_status_view + WHERE parent_task_id = _task_id + AND is_done IS TRUE + INTO _sub_tasks_done; + + _total_completed = _parent_task_done + _sub_tasks_done; + _total_tasks = _sub_tasks_count; -- +1 for the parent task + + IF _total_tasks > 0 THEN + _ratio = (_total_completed / _total_tasks) * 100; + ELSE + _ratio = _parent_task_done * 100; + END IF; + + RETURN JSON_BUILD_OBJECT( + 'ratio', _ratio, + 'total_completed', _total_completed, + 'total_tasks', _total_tasks, + 'is_manual', FALSE + ); + END + $$ + `); +}; + +exports.__migration = { + transaction: false, +}; \ No newline at end of file diff --git a/worklenz-backend/database/pg-migrations/20250424000000-add-progress-and-weight-activity-types.js b/worklenz-backend/database/pg-migrations/20250424000000-add-progress-and-weight-activity-types.js new file mode 100644 index 000000000..902bf96b0 --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250424000000-add-progress-and-weight-activity-types.js @@ -0,0 +1,298 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = false; + +exports.up = pgm => { + // Update the get_activity_logs_by_task function to handle progress and weight attribute types + pgm.sql(` + CREATE OR REPLACE FUNCTION get_activity_logs_by_task(_task_id uuid) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _result JSON; + BEGIN + SELECT ROW_TO_JSON(rec) + INTO _result + FROM (SELECT (SELECT tasks.created_at FROM tasks WHERE tasks.id = _task_id), + (SELECT name + FROM users + WHERE id = (SELECT reporter_id FROM tasks WHERE id = _task_id)), + (SELECT avatar_url + FROM users + WHERE id = (SELECT reporter_id FROM tasks WHERE id = _task_id)), + (SELECT COALESCE(ARRAY_TO_JSON(ARRAY_AGG(ROW_TO_JSON(rec2))), '[]'::JSON) + FROM (SELECT task_id, + created_at, + attribute_type, + log_type, + + -- Case for previous value + (CASE + WHEN (attribute_type = 'status') + THEN (SELECT name FROM task_statuses WHERE id = old_value::UUID) + WHEN (attribute_type = 'priority') + THEN (SELECT name FROM task_priorities WHERE id = old_value::UUID) + WHEN (attribute_type = 'phase' AND old_value <> 'Unmapped') + THEN (SELECT name FROM project_phases WHERE id = old_value::UUID) + WHEN (attribute_type = 'progress' OR attribute_type = 'weight') + THEN old_value + ELSE (old_value) END) AS previous, + + -- Case for current value + (CASE + WHEN (attribute_type = 'assignee') + THEN (SELECT name FROM users WHERE id = new_value::UUID) + WHEN (attribute_type = 'label') + THEN (SELECT name FROM team_labels WHERE id = new_value::UUID) + WHEN (attribute_type = 'status') + THEN (SELECT name FROM task_statuses WHERE id = new_value::UUID) + WHEN (attribute_type = 'priority') + THEN (SELECT name FROM task_priorities WHERE id = new_value::UUID) + WHEN (attribute_type = 'phase' AND new_value <> 'Unmapped') + THEN (SELECT name FROM project_phases WHERE id = new_value::UUID) + WHEN (attribute_type = 'progress' OR attribute_type = 'weight') + THEN new_value + ELSE (new_value) END) AS current, + + -- Case for assigned user + (CASE + WHEN (attribute_type = 'assignee') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (CASE + WHEN (new_value IS NOT NULL) + THEN (SELECT name FROM users WHERE users.id = new_value::UUID) + ELSE (next_string) END) AS name, + (SELECT avatar_url FROM users WHERE users.id = new_value::UUID)) rec) + ELSE (NULL) END) AS assigned_user, + + -- Case for label data + (CASE + WHEN (attribute_type = 'label') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM team_labels WHERE id = new_value::UUID), + (SELECT color_code FROM team_labels WHERE id = new_value::UUID)) rec) + ELSE (NULL) END) AS label_data, + + -- Case for previous status + (CASE + WHEN (attribute_type = 'status') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM task_statuses WHERE id = old_value::UUID), + (SELECT color_code + FROM sys_task_status_categories + WHERE id = (SELECT category_id FROM task_statuses WHERE id = old_value::UUID)), + (SELECT color_code_dark + FROM sys_task_status_categories + WHERE id = (SELECT category_id FROM task_statuses WHERE id = old_value::UUID))) rec) + ELSE (NULL) END) AS previous_status, + + -- Case for next status + (CASE + WHEN (attribute_type = 'status') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM task_statuses WHERE id = new_value::UUID), + (SELECT color_code + FROM sys_task_status_categories + WHERE id = (SELECT category_id FROM task_statuses WHERE id = new_value::UUID)), + (SELECT color_code_dark + FROM sys_task_status_categories + WHERE id = (SELECT category_id FROM task_statuses WHERE id = new_value::UUID))) rec) + ELSE (NULL) END) AS next_status, + + -- Case for previous priority + (CASE + WHEN (attribute_type = 'priority') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM task_priorities WHERE id = old_value::UUID), + (SELECT color_code FROM task_priorities WHERE id = old_value::UUID)) rec) + ELSE (NULL) END) AS previous_priority, + + -- Case for next priority + (CASE + WHEN (attribute_type = 'priority') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM task_priorities WHERE id = new_value::UUID), + (SELECT color_code FROM task_priorities WHERE id = new_value::UUID)) rec) + ELSE (NULL) END) AS next_priority, + + -- Case for previous phase + (CASE + WHEN (attribute_type = 'phase' AND old_value <> 'Unmapped') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM project_phases WHERE id = old_value::UUID), + (SELECT color_code FROM project_phases WHERE id = old_value::UUID)) rec) + ELSE (NULL) END) AS previous_phase, + + -- Case for next phase + (CASE + WHEN (attribute_type = 'phase' AND new_value <> 'Unmapped') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM project_phases WHERE id = new_value::UUID), + (SELECT color_code FROM project_phases WHERE id = new_value::UUID)) rec) + ELSE (NULL) END) AS next_phase, + + -- Case for done by + (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM users WHERE users.id = tal.user_id), + (SELECT avatar_url FROM users WHERE users.id = tal.user_id)) rec) AS done_by, + + -- Add log text for progress and weight + (CASE + WHEN (attribute_type = 'progress') + THEN 'updated the progress of' + WHEN (attribute_type = 'weight') + THEN 'updated the weight of' + ELSE '' + END) AS log_text + + FROM task_activity_logs tal + WHERE task_id = _task_id + ORDER BY created_at DESC) rec2) AS logs) rec; + RETURN _result; + END; + $$; + `); +}; + +exports.down = pgm => { + // Revert the function to its previous state (without progress and weight support) + // Note: This assumes the original function didn't handle progress/weight types + pgm.sql(` + CREATE OR REPLACE FUNCTION get_activity_logs_by_task(_task_id uuid) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _result JSON; + BEGIN + SELECT ROW_TO_JSON(rec) + INTO _result + FROM (SELECT (SELECT tasks.created_at FROM tasks WHERE tasks.id = _task_id), + (SELECT name + FROM users + WHERE id = (SELECT reporter_id FROM tasks WHERE id = _task_id)), + (SELECT avatar_url + FROM users + WHERE id = (SELECT reporter_id FROM tasks WHERE id = _task_id)), + (SELECT COALESCE(ARRAY_TO_JSON(ARRAY_AGG(ROW_TO_JSON(rec2))), '[]'::JSON) + FROM (SELECT task_id, + created_at, + attribute_type, + log_type, + + -- Case for previous value (without progress/weight) + (CASE + WHEN (attribute_type = 'status') + THEN (SELECT name FROM task_statuses WHERE id = old_value::UUID) + WHEN (attribute_type = 'priority') + THEN (SELECT name FROM task_priorities WHERE id = old_value::UUID) + WHEN (attribute_type = 'phase' AND old_value <> 'Unmapped') + THEN (SELECT name FROM project_phases WHERE id = old_value::UUID) + ELSE (old_value) END) AS previous, + + -- Case for current value (without progress/weight) + (CASE + WHEN (attribute_type = 'assignee') + THEN (SELECT name FROM users WHERE id = new_value::UUID) + WHEN (attribute_type = 'label') + THEN (SELECT name FROM team_labels WHERE id = new_value::UUID) + WHEN (attribute_type = 'status') + THEN (SELECT name FROM task_statuses WHERE id = new_value::UUID) + WHEN (attribute_type = 'priority') + THEN (SELECT name FROM task_priorities WHERE id = new_value::UUID) + WHEN (attribute_type = 'phase' AND new_value <> 'Unmapped') + THEN (SELECT name FROM project_phases WHERE id = new_value::UUID) + ELSE (new_value) END) AS current, + + -- Case for assigned user + (CASE + WHEN (attribute_type = 'assignee') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (CASE + WHEN (new_value IS NOT NULL) + THEN (SELECT name FROM users WHERE users.id = new_value::UUID) + ELSE (next_string) END) AS name, + (SELECT avatar_url FROM users WHERE users.id = new_value::UUID)) rec) + ELSE (NULL) END) AS assigned_user, + + -- Case for label data + (CASE + WHEN (attribute_type = 'label') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM team_labels WHERE id = new_value::UUID), + (SELECT color_code FROM team_labels WHERE id = new_value::UUID)) rec) + ELSE (NULL) END) AS label_data, + + -- Case for previous status + (CASE + WHEN (attribute_type = 'status') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM task_statuses WHERE id = old_value::UUID), + (SELECT color_code + FROM sys_task_status_categories + WHERE id = (SELECT category_id FROM task_statuses WHERE id = old_value::UUID)), + (SELECT color_code_dark + FROM sys_task_status_categories + WHERE id = (SELECT category_id FROM task_statuses WHERE id = old_value::UUID))) rec) + ELSE (NULL) END) AS previous_status, + + -- Case for next status + (CASE + WHEN (attribute_type = 'status') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM task_statuses WHERE id = new_value::UUID), + (SELECT color_code + FROM sys_task_status_categories + WHERE id = (SELECT category_id FROM task_statuses WHERE id = new_value::UUID)), + (SELECT color_code_dark + FROM sys_task_status_categories + WHERE id = (SELECT category_id FROM task_statuses WHERE id = new_value::UUID))) rec) + ELSE (NULL) END) AS next_status, + + -- Case for previous priority + (CASE + WHEN (attribute_type = 'priority') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM task_priorities WHERE id = old_value::UUID), + (SELECT color_code FROM task_priorities WHERE id = old_value::UUID)) rec) + ELSE (NULL) END) AS previous_priority, + + -- Case for next priority + (CASE + WHEN (attribute_type = 'priority') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM task_priorities WHERE id = new_value::UUID), + (SELECT color_code FROM task_priorities WHERE id = new_value::UUID)) rec) + ELSE (NULL) END) AS next_priority, + + -- Case for previous phase + (CASE + WHEN (attribute_type = 'phase' AND old_value <> 'Unmapped') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM project_phases WHERE id = old_value::UUID), + (SELECT color_code FROM project_phases WHERE id = old_value::UUID)) rec) + ELSE (NULL) END) AS previous_phase, + + -- Case for next phase + (CASE + WHEN (attribute_type = 'phase' AND new_value <> 'Unmapped') + THEN (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM project_phases WHERE id = new_value::UUID), + (SELECT color_code FROM project_phases WHERE id = new_value::UUID)) rec) + ELSE (NULL) END) AS next_phase, + + -- Case for done by + (SELECT ROW_TO_JSON(rec) + FROM (SELECT (SELECT name FROM users WHERE users.id = tal.user_id), + (SELECT avatar_url FROM users WHERE users.id = tal.user_id)) rec) AS done_by + + FROM task_activity_logs tal + WHERE task_id = _task_id + ORDER BY created_at DESC) rec2) AS logs) rec; + RETURN _result; + END; + $$; + `); +}; \ No newline at end of file diff --git a/worklenz-backend/database/pg-migrations/20250425000000-update-time-based-progress.js b/worklenz-backend/database/pg-migrations/20250425000000-update-time-based-progress.js new file mode 100644 index 000000000..a53a00fc1 --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250425000000-update-time-based-progress.js @@ -0,0 +1,343 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = false; + +exports.up = pgm => { + // Update function to use time-based progress for all tasks + pgm.sql(` + CREATE OR REPLACE FUNCTION get_task_complete_ratio(_task_id uuid) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _parent_task_done FLOAT = 0; + _sub_tasks_done FLOAT = 0; + _sub_tasks_count FLOAT = 0; + _total_completed FLOAT = 0; + _total_tasks FLOAT = 0; + _ratio FLOAT = 0; + _is_manual BOOLEAN = FALSE; + _manual_value INTEGER = NULL; + _project_id UUID; + _use_manual_progress BOOLEAN = FALSE; + _use_weighted_progress BOOLEAN = FALSE; + _use_time_progress BOOLEAN = FALSE; + _task_complete BOOLEAN = FALSE; + BEGIN + -- Check if manual progress is set for this task + SELECT manual_progress, progress_value, project_id, + EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = tasks.id + AND is_done IS TRUE + ) AS is_complete + FROM tasks + WHERE id = _task_id + INTO _is_manual, _manual_value, _project_id, _task_complete; + + -- Check if the project uses manual progress + IF _project_id IS NOT NULL THEN + SELECT COALESCE(use_manual_progress, FALSE), + COALESCE(use_weighted_progress, FALSE), + COALESCE(use_time_progress, FALSE) + FROM projects + WHERE id = _project_id + INTO _use_manual_progress, _use_weighted_progress, _use_time_progress; + END IF; + + -- Get all subtasks + SELECT COUNT(*) + FROM tasks + WHERE parent_task_id = _task_id AND archived IS FALSE + INTO _sub_tasks_count; + + -- If task is complete, always return 100% + IF _task_complete IS TRUE THEN + RETURN JSON_BUILD_OBJECT( + 'ratio', 100, + 'total_completed', 1, + 'total_tasks', 1, + 'is_manual', FALSE + ); + END IF; + + -- Use manual progress value in two cases: + -- 1. When task has manual_progress = TRUE and progress_value is set + -- 2. When project has use_manual_progress = TRUE and progress_value is set + IF (_is_manual IS TRUE AND _manual_value IS NOT NULL) OR + (_use_manual_progress IS TRUE AND _manual_value IS NOT NULL) THEN + RETURN JSON_BUILD_OBJECT( + 'ratio', _manual_value, + 'total_completed', 0, + 'total_tasks', 0, + 'is_manual', TRUE + ); + END IF; + + -- If there are no subtasks, just use the parent task's status (unless in time-based mode) + IF _sub_tasks_count = 0 THEN + -- Use time-based estimation for tasks without subtasks if enabled + IF _use_time_progress IS TRUE THEN + -- For time-based tasks without subtasks, we still need some progress calculation + -- If the task is completed, return 100% + -- Otherwise, use the progress value if set manually, or 0 + SELECT + CASE + WHEN _task_complete IS TRUE THEN 100 + ELSE COALESCE(_manual_value, 0) + END + INTO _ratio; + ELSE + -- Traditional calculation for non-time-based tasks + SELECT (CASE WHEN _task_complete IS TRUE THEN 1 ELSE 0 END) + INTO _parent_task_done; + + _ratio = _parent_task_done * 100; + END IF; + ELSE + -- If project uses manual progress, calculate based on subtask manual progress values + IF _use_manual_progress IS TRUE THEN + WITH subtask_progress AS ( + SELECT + t.id, + t.manual_progress, + t.progress_value, + EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = t.id + AND is_done IS TRUE + ) AS is_complete + FROM tasks t + WHERE t.parent_task_id = _task_id + AND t.archived IS FALSE + ), + subtask_with_values AS ( + SELECT + CASE + -- For completed tasks, always use 100% + WHEN is_complete IS TRUE THEN 100 + -- For tasks with progress value set, use it regardless of manual_progress flag + WHEN progress_value IS NOT NULL THEN progress_value + -- Default to 0 for incomplete tasks with no progress value + ELSE 0 + END AS progress_value + FROM subtask_progress + ) + SELECT COALESCE(AVG(progress_value), 0) + FROM subtask_with_values + INTO _ratio; + -- If project uses weighted progress, calculate based on subtask weights + ELSIF _use_weighted_progress IS TRUE THEN + WITH subtask_progress AS ( + SELECT + t.id, + t.manual_progress, + t.progress_value, + EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = t.id + AND is_done IS TRUE + ) AS is_complete, + COALESCE(t.weight, 100) AS weight + FROM tasks t + WHERE t.parent_task_id = _task_id + AND t.archived IS FALSE + ), + subtask_with_values AS ( + SELECT + CASE + -- For completed tasks, always use 100% + WHEN is_complete IS TRUE THEN 100 + -- For tasks with progress value set, use it regardless of manual_progress flag + WHEN progress_value IS NOT NULL THEN progress_value + -- Default to 0 for incomplete tasks with no progress value + ELSE 0 + END AS progress_value, + weight + FROM subtask_progress + ) + SELECT COALESCE( + SUM(progress_value * weight) / NULLIF(SUM(weight), 0), + 0 + ) + FROM subtask_with_values + INTO _ratio; + -- If project uses time-based progress, calculate based on estimated time + ELSIF _use_time_progress IS TRUE THEN + WITH subtask_progress AS ( + SELECT + t.id, + t.manual_progress, + t.progress_value, + EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = t.id + AND is_done IS TRUE + ) AS is_complete, + COALESCE(t.total_minutes, 0) AS estimated_minutes + FROM tasks t + WHERE t.parent_task_id = _task_id + AND t.archived IS FALSE + ), + subtask_with_values AS ( + SELECT + CASE + -- For completed tasks, always use 100% + WHEN is_complete IS TRUE THEN 100 + -- For tasks with progress value set, use it regardless of manual_progress flag + WHEN progress_value IS NOT NULL THEN progress_value + -- Default to 0 for incomplete tasks with no progress value + ELSE 0 + END AS progress_value, + estimated_minutes + FROM subtask_progress + ) + SELECT COALESCE( + SUM(progress_value * estimated_minutes) / NULLIF(SUM(estimated_minutes), 0), + 0 + ) + FROM subtask_with_values + INTO _ratio; + ELSE + -- Traditional calculation based on completion status + SELECT (CASE WHEN _task_complete IS TRUE THEN 1 ELSE 0 END) + INTO _parent_task_done; + + SELECT COUNT(*) + FROM tasks_with_status_view + WHERE parent_task_id = _task_id + AND is_done IS TRUE + INTO _sub_tasks_done; + + _total_completed = _parent_task_done + _sub_tasks_done; + _total_tasks = _sub_tasks_count + 1; -- +1 for the parent task + + IF _total_tasks = 0 THEN + _ratio = 0; + ELSE + _ratio = (_total_completed / _total_tasks) * 100; + END IF; + END IF; + END IF; + + -- Ensure ratio is between 0 and 100 + IF _ratio < 0 THEN + _ratio = 0; + ELSIF _ratio > 100 THEN + _ratio = 100; + END IF; + + RETURN JSON_BUILD_OBJECT( + 'ratio', _ratio, + 'total_completed', _total_completed, + 'total_tasks', _total_tasks, + 'is_manual', _is_manual + ); + END + $$; + `); +}; + +exports.down = pgm => { + // Revert to a simpler version of the function (basic implementation) + // Note: This is a simplified rollback - you may need to adjust based on your previous function version + pgm.sql(` + CREATE OR REPLACE FUNCTION get_task_complete_ratio(_task_id uuid) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _parent_task_done FLOAT = 0; + _sub_tasks_done FLOAT = 0; + _sub_tasks_count FLOAT = 0; + _total_completed FLOAT = 0; + _total_tasks FLOAT = 0; + _ratio FLOAT = 0; + _is_manual BOOLEAN = FALSE; + _manual_value INTEGER = NULL; + _task_complete BOOLEAN = FALSE; + BEGIN + -- Check if manual progress is set for this task + SELECT manual_progress, progress_value, + EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = tasks.id + AND is_done IS TRUE + ) AS is_complete + FROM tasks + WHERE id = _task_id + INTO _is_manual, _manual_value, _task_complete; + + -- Get all subtasks + SELECT COUNT(*) + FROM tasks + WHERE parent_task_id = _task_id AND archived IS FALSE + INTO _sub_tasks_count; + + -- If task is complete, always return 100% + IF _task_complete IS TRUE THEN + RETURN JSON_BUILD_OBJECT( + 'ratio', 100, + 'total_completed', 1, + 'total_tasks', 1, + 'is_manual', FALSE + ); + END IF; + + -- Use manual progress value if set + IF _is_manual IS TRUE AND _manual_value IS NOT NULL THEN + RETURN JSON_BUILD_OBJECT( + 'ratio', _manual_value, + 'total_completed', 0, + 'total_tasks', 0, + 'is_manual', TRUE + ); + END IF; + + -- Traditional calculation based on completion status + SELECT (CASE WHEN _task_complete IS TRUE THEN 1 ELSE 0 END) + INTO _parent_task_done; + + IF _sub_tasks_count > 0 THEN + SELECT COUNT(*) + FROM tasks_with_status_view + WHERE parent_task_id = _task_id + AND is_done IS TRUE + INTO _sub_tasks_done; + + _total_completed = _parent_task_done + _sub_tasks_done; + _total_tasks = _sub_tasks_count + 1; -- +1 for the parent task + ELSE + _total_completed = _parent_task_done; + _total_tasks = 1; + END IF; + + IF _total_tasks = 0 THEN + _ratio = 0; + ELSE + _ratio = (_total_completed / _total_tasks) * 100; + END IF; + + -- Ensure ratio is between 0 and 100 + IF _ratio < 0 THEN + _ratio = 0; + ELSIF _ratio > 100 THEN + _ratio = 100; + END IF; + + RETURN JSON_BUILD_OBJECT( + 'ratio', _ratio, + 'total_completed', _total_completed, + 'total_tasks', _total_tasks, + 'is_manual', _is_manual + ); + END + $$; + `); +}; \ No newline at end of file diff --git a/worklenz-backend/database/pg-migrations/20250426000000-update-progress-mode-handlers.js b/worklenz-backend/database/pg-migrations/20250426000000-update-progress-mode-handlers.js new file mode 100644 index 000000000..a40715db5 --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250426000000-update-progress-mode-handlers.js @@ -0,0 +1,188 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = false; + +exports.up = pgm => { + // Create the enum type first + pgm.sql("CREATE TYPE progress_mode_type AS ENUM ('manual', 'weighted', 'time', 'default');"); + + // Add the progress_mode column if it doesn't exist + pgm.sql(` + ALTER TABLE tasks + ADD COLUMN IF NOT EXISTS progress_mode progress_mode_type; + `); + + // If the column already existed as a different type, convert it + pgm.sql(` + DO $$ + BEGIN + -- Check if column exists and is not the correct type + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'tasks' + AND column_name = 'progress_mode' + AND data_type != 'USER-DEFINED' + ) THEN + -- Column exists but is wrong type, convert it + ALTER TABLE tasks + ALTER COLUMN progress_mode TYPE progress_mode_type + USING CASE + WHEN progress_mode::text = 'manual' THEN 'manual'::progress_mode_type + WHEN progress_mode::text = 'weighted' THEN 'weighted'::progress_mode_type + WHEN progress_mode::text = 'time' THEN 'time'::progress_mode_type + ELSE 'default'::progress_mode_type + END; + END IF; + END $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION on_update_task_progress(_body json) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _task_id UUID; + _progress_value INTEGER; + _parent_task_id UUID; + _project_id UUID; + _current_mode progress_mode_type; + BEGIN + _task_id = (_body ->> 'task_id')::UUID; + _progress_value = (_body ->> 'progress_value')::INTEGER; + _parent_task_id = (_body ->> 'parent_task_id')::UUID; + + -- Get the project ID and determine the current progress mode + SELECT project_id INTO _project_id FROM tasks WHERE id = _task_id; + + IF _project_id IS NOT NULL THEN + SELECT + CASE + WHEN use_manual_progress IS TRUE THEN 'manual' + WHEN use_weighted_progress IS TRUE THEN 'weighted' + WHEN use_time_progress IS TRUE THEN 'time' + ELSE 'default' + END + INTO _current_mode + FROM projects + WHERE id = _project_id; + ELSE + _current_mode := 'default'; + END IF; + + -- Update the task with progress value and set the progress mode + UPDATE tasks + SET progress_value = _progress_value, + manual_progress = TRUE, + progress_mode = _current_mode, + updated_at = CURRENT_TIMESTAMP + WHERE id = _task_id; + + -- Return the updated task info + RETURN JSON_BUILD_OBJECT( + 'task_id', _task_id, + 'progress_value', _progress_value, + 'progress_mode', _current_mode + ); + END; + $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION on_update_task_weight(_body json) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _task_id UUID; + _weight INTEGER; + _parent_task_id UUID; + _project_id UUID; + BEGIN + _task_id = (_body ->> 'task_id')::UUID; + _weight = (_body ->> 'weight')::INTEGER; + _parent_task_id = (_body ->> 'parent_task_id')::UUID; + + -- Get the project ID + SELECT project_id INTO _project_id FROM tasks WHERE id = _task_id; + + -- Update the task with weight value and set progress_mode to 'weighted' + UPDATE tasks + SET weight = _weight, + progress_mode = 'weighted', + updated_at = CURRENT_TIMESTAMP + WHERE id = _task_id; + + -- Return the updated task info + RETURN JSON_BUILD_OBJECT( + 'task_id', _task_id, + 'weight', _weight + ); + END; + $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION reset_project_progress_values() RETURNS TRIGGER + LANGUAGE plpgsql + AS + $$ + DECLARE + _old_mode progress_mode_type; + _new_mode progress_mode_type; + _project_id UUID; + BEGIN + _project_id := NEW.id; + + -- Determine old and new modes + _old_mode := + CASE + WHEN OLD.use_manual_progress IS TRUE THEN 'manual' + WHEN OLD.use_weighted_progress IS TRUE THEN 'weighted' + WHEN OLD.use_time_progress IS TRUE THEN 'time' + ELSE 'default' + END; + + _new_mode := + CASE + WHEN NEW.use_manual_progress IS TRUE THEN 'manual' + WHEN NEW.use_weighted_progress IS TRUE THEN 'weighted' + WHEN NEW.use_time_progress IS TRUE THEN 'time' + ELSE 'default' + END; + + -- If mode has changed, reset progress values for tasks with the old mode + IF _old_mode <> _new_mode THEN + -- Reset progress values for tasks that were set in the old mode + UPDATE tasks + SET progress_value = NULL, + progress_mode = NULL + WHERE project_id = _project_id + AND progress_mode = _old_mode; + END IF; + + RETURN NEW; + END; + $$; + `); + + pgm.sql(` + DROP TRIGGER IF EXISTS reset_progress_on_mode_change ON projects; + CREATE TRIGGER reset_progress_on_mode_change + AFTER UPDATE OF use_manual_progress, use_weighted_progress, use_time_progress + ON projects + FOR EACH ROW + EXECUTE FUNCTION reset_project_progress_values(); + `); +}; + +exports.down = pgm => { + pgm.sql('DROP TRIGGER IF EXISTS reset_progress_on_mode_change ON projects;'); + pgm.sql('DROP FUNCTION IF EXISTS reset_project_progress_values();'); + pgm.sql('DROP FUNCTION IF EXISTS on_update_task_weight(json);'); + pgm.sql('DROP FUNCTION IF EXISTS on_update_task_progress(json);'); + pgm.sql('ALTER TABLE tasks DROP COLUMN IF EXISTS progress_mode;'); + pgm.sql('DROP TYPE IF EXISTS progress_mode_type;'); +}; \ No newline at end of file diff --git a/worklenz-backend/database/pg-migrations/20250426000000_improve-parent-task-progress-calculation.js b/worklenz-backend/database/pg-migrations/20250426000000_improve-parent-task-progress-calculation.js new file mode 100644 index 000000000..654fe0bb7 --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250426000000_improve-parent-task-progress-calculation.js @@ -0,0 +1,301 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = false; + +exports.up = pgm => { + pgm.sql(` + CREATE OR REPLACE FUNCTION get_task_complete_ratio(_task_id uuid) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _parent_task_done FLOAT = 0; + _sub_tasks_done FLOAT = 0; + _sub_tasks_count FLOAT = 0; + _total_completed FLOAT = 0; + _total_tasks FLOAT = 0; + _ratio FLOAT = 0; + _is_manual BOOLEAN = FALSE; + _manual_value INTEGER = NULL; + _project_id UUID; + _use_manual_progress BOOLEAN = FALSE; + _use_weighted_progress BOOLEAN = FALSE; + _use_time_progress BOOLEAN = FALSE; + BEGIN + -- Check if manual progress is set for this task + SELECT manual_progress, progress_value, project_id + FROM tasks + WHERE id = _task_id + INTO _is_manual, _manual_value, _project_id; + + -- Check if the project uses manual progress + IF _project_id IS NOT NULL THEN + SELECT COALESCE(use_manual_progress, FALSE), + COALESCE(use_weighted_progress, FALSE), + COALESCE(use_time_progress, FALSE) + FROM projects + WHERE id = _project_id + INTO _use_manual_progress, _use_weighted_progress, _use_time_progress; + END IF; + + -- Get all subtasks + SELECT COUNT(*) + FROM tasks + WHERE parent_task_id = _task_id AND archived IS FALSE + INTO _sub_tasks_count; + + -- Only respect manual progress for tasks without subtasks + IF _is_manual IS TRUE AND _manual_value IS NOT NULL AND _sub_tasks_count = 0 THEN + RETURN JSON_BUILD_OBJECT( + 'ratio', _manual_value, + 'total_completed', 0, + 'total_tasks', 0, + 'is_manual', TRUE + ); + END IF; + + -- If there are no subtasks, just use the parent task's status + IF _sub_tasks_count = 0 THEN + -- For tasks without subtasks in time-based mode + IF _use_time_progress IS TRUE THEN + SELECT + CASE + WHEN EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = _task_id + AND is_done IS TRUE + ) THEN 100 + ELSE COALESCE(_manual_value, 0) + END + INTO _ratio; + ELSE + -- Traditional calculation for non-time-based tasks + SELECT (CASE + WHEN EXISTS(SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = _task_id + AND is_done IS TRUE) THEN 1 + ELSE 0 END) + INTO _parent_task_done; + + _ratio = _parent_task_done * 100; + END IF; + ELSE + -- For parent tasks with subtasks, always use the appropriate calculation based on project mode + -- If project uses manual progress, calculate based on subtask manual progress values + IF _use_manual_progress IS TRUE THEN + WITH subtask_progress AS ( + SELECT + CASE + -- If subtask has manual progress, use that value + WHEN manual_progress IS TRUE AND progress_value IS NOT NULL THEN + progress_value + -- Otherwise use completion status (0 or 100) + ELSE + CASE + WHEN EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = t.id + AND is_done IS TRUE + ) THEN 100 + ELSE 0 + END + END AS progress_value + FROM tasks t + WHERE t.parent_task_id = _task_id + AND t.archived IS FALSE + ) + SELECT COALESCE(AVG(progress_value), 0) + FROM subtask_progress + INTO _ratio; + -- If project uses weighted progress, calculate based on subtask weights + ELSIF _use_weighted_progress IS TRUE THEN + WITH subtask_progress AS ( + SELECT + CASE + -- If subtask has manual progress, use that value + WHEN manual_progress IS TRUE AND progress_value IS NOT NULL THEN + progress_value + -- Otherwise use completion status (0 or 100) + ELSE + CASE + WHEN EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = t.id + AND is_done IS TRUE + ) THEN 100 + ELSE 0 + END + END AS progress_value, + COALESCE(weight, 100) AS weight -- Default weight is 100 if not specified + FROM tasks t + WHERE t.parent_task_id = _task_id + AND t.archived IS FALSE + ) + SELECT COALESCE( + SUM(progress_value * weight) / NULLIF(SUM(weight), 0), + 0 + ) + FROM subtask_progress + INTO _ratio; + -- If project uses time-based progress, calculate based on estimated time (total_minutes) + ELSIF _use_time_progress IS TRUE THEN + WITH subtask_progress AS ( + SELECT + CASE + -- If subtask has manual progress, use that value + WHEN manual_progress IS TRUE AND progress_value IS NOT NULL THEN + progress_value + -- Otherwise use completion status (0 or 100) + ELSE + CASE + WHEN EXISTS( + SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = t.id + AND is_done IS TRUE + ) THEN 100 + ELSE 0 + END + END AS progress_value, + COALESCE(total_minutes, 0) AS estimated_minutes -- Use time estimation for weighting + FROM tasks t + WHERE t.parent_task_id = _task_id + AND t.archived IS FALSE + ) + SELECT COALESCE( + SUM(progress_value * estimated_minutes) / NULLIF(SUM(estimated_minutes), 0), + 0 + ) + FROM subtask_progress + INTO _ratio; + ELSE + -- Traditional calculation based on completion status when no special mode is enabled + SELECT (CASE + WHEN EXISTS(SELECT 1 + FROM tasks_with_status_view + WHERE tasks_with_status_view.task_id = _task_id + AND is_done IS TRUE) THEN 1 + ELSE 0 END) + INTO _parent_task_done; + + SELECT COUNT(*) + FROM tasks_with_status_view + WHERE parent_task_id = _task_id + AND is_done IS TRUE + INTO _sub_tasks_done; + + _total_completed = _parent_task_done + _sub_tasks_done; + _total_tasks = _sub_tasks_count + 1; -- +1 for the parent task + + IF _total_tasks = 0 THEN + _ratio = 0; + ELSE + _ratio = (_total_completed / _total_tasks) * 100; + END IF; + END IF; + END IF; + + -- Ensure ratio is between 0 and 100 + IF _ratio < 0 THEN + _ratio = 0; + ELSIF _ratio > 100 THEN + _ratio = 100; + END IF; + + RETURN JSON_BUILD_OBJECT( + 'ratio', _ratio, + 'total_completed', _total_completed, + 'total_tasks', _total_tasks, + 'is_manual', _is_manual + ); + END + $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION update_parent_task_progress() RETURNS TRIGGER AS + $$ + DECLARE + _parent_task_id UUID; + _project_id UUID; + _ratio FLOAT; + BEGIN + -- Check if this is a subtask + IF NEW.parent_task_id IS NOT NULL THEN + _parent_task_id := NEW.parent_task_id; + + -- Force any parent task with subtasks to NOT use manual progress + UPDATE tasks + SET manual_progress = FALSE + WHERE id = _parent_task_id; + END IF; + + -- If this task has progress value of 100 and doesn't have subtasks, we might want to prompt the user + -- to mark it as done. We'll annotate this in a way that the socket handler can detect. + IF NEW.progress_value = 100 OR NEW.weight = 100 OR NEW.total_minutes > 0 THEN + -- Check if task has status in "done" category + SELECT project_id FROM tasks WHERE id = NEW.id INTO _project_id; + + -- Get the progress ratio for this task + SELECT get_task_complete_ratio(NEW.id)->>'ratio' INTO _ratio; + + IF _ratio::FLOAT >= 100 THEN + -- Log that this task is at 100% progress + RAISE NOTICE 'Task % progress is at 100%%, may need status update', NEW.id; + END IF; + END IF; + + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + `); + + pgm.sql(` + DROP TRIGGER IF EXISTS update_parent_task_progress_trigger ON tasks; + CREATE TRIGGER update_parent_task_progress_trigger + AFTER UPDATE OF progress_value, weight, total_minutes ON tasks + FOR EACH ROW + EXECUTE FUNCTION update_parent_task_progress(); + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION ensure_parent_task_without_manual_progress() RETURNS TRIGGER AS + $$ + BEGIN + -- If this is a new subtask being created or a task is being converted to a subtask + IF NEW.parent_task_id IS NOT NULL THEN + -- Force the parent task to NOT use manual progress + UPDATE tasks + SET manual_progress = FALSE + WHERE id = NEW.parent_task_id; + + -- Log that we've reset manual progress for a parent task + RAISE NOTICE 'Reset manual progress for parent task % because it has subtasks', NEW.parent_task_id; + END IF; + + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + `); + + pgm.sql(` + DROP TRIGGER IF EXISTS ensure_parent_task_without_manual_progress_trigger ON tasks; + CREATE TRIGGER ensure_parent_task_without_manual_progress_trigger + AFTER INSERT OR UPDATE OF parent_task_id ON tasks + FOR EACH ROW + EXECUTE FUNCTION ensure_parent_task_without_manual_progress(); + `); +}; + +exports.down = pgm => { + pgm.sql('DROP TRIGGER IF EXISTS ensure_parent_task_without_manual_progress_trigger ON tasks;'); + pgm.sql('DROP FUNCTION IF EXISTS ensure_parent_task_without_manual_progress();'); + pgm.sql('DROP TRIGGER IF EXISTS update_parent_task_progress_trigger ON tasks;'); + pgm.sql('DROP FUNCTION IF EXISTS update_parent_task_progress();'); + pgm.dropFunction('get_task_complete_ratio', [{ name: '_task_id', type: 'uuid' }], { ifExists: true }); +}; diff --git a/worklenz-backend/database/pg-migrations/20250427000000-fix-progress-mode-type.js b/worklenz-backend/database/pg-migrations/20250427000000-fix-progress-mode-type.js new file mode 100644 index 000000000..1c7f07dee --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250427000000-fix-progress-mode-type.js @@ -0,0 +1,169 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = false; + +exports.up = pgm => { + pgm.sql(` + DO $$ + BEGIN + -- Check if the type exists + IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'progress_mode_type') THEN + CREATE TYPE progress_mode_type AS ENUM ('manual', 'weighted', 'time', 'default'); + ELSE + -- Add any missing values to the existing ENUM + BEGIN + ALTER TYPE progress_mode_type ADD VALUE IF NOT EXISTS 'manual'; + ALTER TYPE progress_mode_type ADD VALUE IF NOT EXISTS 'weighted'; + ALTER TYPE progress_mode_type ADD VALUE IF NOT EXISTS 'time'; + ALTER TYPE progress_mode_type ADD VALUE IF NOT EXISTS 'default'; + EXCEPTION + WHEN duplicate_object THEN + -- Ignore if values already exist + NULL; + END; + END IF; + END $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION on_update_task_progress(_body json) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _task_id UUID; + _progress_value INTEGER; + _parent_task_id UUID; + _project_id UUID; + _current_mode progress_mode_type; + BEGIN + _task_id = (_body ->> 'task_id')::UUID; + _progress_value = (_body ->> 'progress_value')::INTEGER; + _parent_task_id = (_body ->> 'parent_task_id')::UUID; + + -- Get the project ID and determine the current progress mode + SELECT project_id INTO _project_id FROM tasks WHERE id = _task_id; + + IF _project_id IS NOT NULL THEN + SELECT + CASE + WHEN use_manual_progress IS TRUE THEN 'manual'::progress_mode_type + WHEN use_weighted_progress IS TRUE THEN 'weighted'::progress_mode_type + WHEN use_time_progress IS TRUE THEN 'time'::progress_mode_type + ELSE 'default'::progress_mode_type + END + INTO _current_mode + FROM projects + WHERE id = _project_id; + ELSE + _current_mode := 'default'::progress_mode_type; + END IF; + + -- Update the task with progress value and set the progress mode + UPDATE tasks + SET progress_value = _progress_value, + manual_progress = TRUE, + progress_mode = _current_mode, + updated_at = CURRENT_TIMESTAMP + WHERE id = _task_id; + + -- Return the updated task info + RETURN JSON_BUILD_OBJECT( + 'task_id', _task_id, + 'progress_value', _progress_value, + 'progress_mode', _current_mode + ); + END; + $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION on_update_task_weight(_body json) RETURNS json + LANGUAGE plpgsql + AS + $$ + DECLARE + _task_id UUID; + _weight INTEGER; + _parent_task_id UUID; + _project_id UUID; + BEGIN + _task_id = (_body ->> 'task_id')::UUID; + _weight = (_body ->> 'weight')::INTEGER; + _parent_task_id = (_body ->> 'parent_task_id')::UUID; + + -- Get the project ID + SELECT project_id INTO _project_id FROM tasks WHERE id = _task_id; + + -- Update the task with weight value and set progress_mode to 'weighted' + UPDATE tasks + SET weight = _weight, + progress_mode = 'weighted'::progress_mode_type, + updated_at = CURRENT_TIMESTAMP + WHERE id = _task_id; + + -- Return the updated task info + RETURN JSON_BUILD_OBJECT( + 'task_id', _task_id, + 'weight', _weight + ); + END; + $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION reset_project_progress_values() RETURNS TRIGGER + LANGUAGE plpgsql + AS + $$ + DECLARE + _old_mode progress_mode_type; + _new_mode progress_mode_type; + _project_id UUID; + BEGIN + _project_id := NEW.id; + + -- Determine old and new modes with proper type casting + _old_mode := + CASE + WHEN OLD.use_manual_progress IS TRUE THEN 'manual'::progress_mode_type + WHEN OLD.use_weighted_progress IS TRUE THEN 'weighted'::progress_mode_type + WHEN OLD.use_time_progress IS TRUE THEN 'time'::progress_mode_type + ELSE 'default'::progress_mode_type + END; + + _new_mode := + CASE + WHEN NEW.use_manual_progress IS TRUE THEN 'manual'::progress_mode_type + WHEN NEW.use_weighted_progress IS TRUE THEN 'weighted'::progress_mode_type + WHEN NEW.use_time_progress IS TRUE THEN 'time'::progress_mode_type + ELSE 'default'::progress_mode_type + END; + + -- If mode has changed, reset progress values for tasks with the old mode + IF _old_mode <> _new_mode THEN + -- Reset progress values for tasks that were set in the old mode + UPDATE tasks + SET progress_value = NULL, + progress_mode = NULL + WHERE project_id = _project_id + AND progress_mode::text::progress_mode_type = _old_mode; + END IF; + + RETURN NEW; + END; + $$; + `); + + pgm.sql(` + UPDATE tasks + SET progress_mode = progress_mode::text::progress_mode_type + WHERE progress_mode IS NOT NULL; + `); +}; + +exports.down = pgm => { + // Don't drop the ENUM type in the down migration, as other migrations might depend on it. + // Reverting the functions to their previous state is sufficient. +}; diff --git a/worklenz-backend/database/pg-migrations/20250506000000-fix-multilevel-subtask-progress-calculation.js b/worklenz-backend/database/pg-migrations/20250506000000-fix-multilevel-subtask-progress-calculation.js new file mode 100644 index 000000000..9223548fd --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250506000000-fix-multilevel-subtask-progress-calculation.js @@ -0,0 +1,220 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = false; + +exports.up = pgm => { + // First, check if progress_value column exists, if not create it + pgm.sql(` + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_name = 'tasks' AND column_name = 'progress_value' + ) THEN + ALTER TABLE tasks ADD COLUMN progress_value FLOAT DEFAULT 0; + RAISE NOTICE 'Added progress_value column to tasks table'; + END IF; + END $$; + `); + + // Also check for other columns that might be missing + pgm.sql(` + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_name = 'tasks' AND column_name = 'weight' + ) THEN + ALTER TABLE tasks ADD COLUMN weight INTEGER DEFAULT 0; + RAISE NOTICE 'Added weight column to tasks table'; + END IF; + + IF NOT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_name = 'tasks' AND column_name = 'total_minutes' + ) THEN + ALTER TABLE tasks ADD COLUMN total_minutes INTEGER DEFAULT 0; + RAISE NOTICE 'Added total_minutes column to tasks table'; + END IF; + + IF NOT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_name = 'tasks' AND column_name = 'manual_progress' + ) THEN + ALTER TABLE tasks ADD COLUMN manual_progress BOOLEAN DEFAULT FALSE; + RAISE NOTICE 'Added manual_progress column to tasks table'; + END IF; + END $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION update_parent_task_progress() RETURNS TRIGGER AS + $$ + DECLARE + _parent_task_id UUID; + _project_id UUID; + _ratio FLOAT; + BEGIN + -- Check if this is a subtask + IF NEW.parent_task_id IS NOT NULL THEN + _parent_task_id := NEW.parent_task_id; + + -- Force any parent task with subtasks to NOT use manual progress + UPDATE tasks + SET manual_progress = FALSE + WHERE id = _parent_task_id; + + -- Calculate and update the parent's progress value + SELECT (get_task_complete_ratio(_parent_task_id)->>'ratio')::FLOAT INTO _ratio; + + -- Update the parent's progress value + UPDATE tasks + SET progress_value = _ratio + WHERE id = _parent_task_id; + + -- Recursively propagate changes up the hierarchy by using a recursive CTE + WITH RECURSIVE task_hierarchy AS ( + -- Base case: Start with the parent task + SELECT + id, + parent_task_id + FROM tasks + WHERE id = _parent_task_id + + UNION ALL + + -- Recursive case: Go up to each ancestor + SELECT + t.id, + t.parent_task_id + FROM tasks t + JOIN task_hierarchy th ON t.id = th.parent_task_id + WHERE t.id IS NOT NULL + ) + -- For each ancestor, recalculate its progress + UPDATE tasks + SET + manual_progress = FALSE, + progress_value = (SELECT (get_task_complete_ratio(task_hierarchy.id)->>'ratio')::FLOAT) + FROM task_hierarchy + WHERE tasks.id = task_hierarchy.id + AND task_hierarchy.parent_task_id IS NOT NULL; + + -- Log the recalculation for debugging + RAISE NOTICE 'Updated progress for task % to %', _parent_task_id, _ratio; + END IF; + + -- If this task has progress value of 100 and doesn't have subtasks, we might want to prompt the user + -- to mark it as done. We'll annotate this in a way that the socket handler can detect. + IF NEW.progress_value = 100 OR NEW.weight = 100 OR NEW.total_minutes > 0 THEN + -- Check if task has status in "done" category + SELECT project_id FROM tasks WHERE id = NEW.id INTO _project_id; + + -- Get the progress ratio for this task + SELECT (get_task_complete_ratio(NEW.id)->>'ratio')::FLOAT INTO _ratio; + + IF _ratio >= 100 THEN + -- Log that this task is at 100% progress + RAISE NOTICE 'Task % progress is at 100%%, may need status update', NEW.id; + END IF; + END IF; + + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + `); + + pgm.sql(` + DROP TRIGGER IF EXISTS update_parent_task_progress_trigger ON tasks; + CREATE TRIGGER update_parent_task_progress_trigger + AFTER UPDATE OF progress_value, weight, total_minutes, parent_task_id, manual_progress ON tasks + FOR EACH ROW + EXECUTE FUNCTION update_parent_task_progress(); + `); + + pgm.sql(` + DROP TRIGGER IF EXISTS update_parent_task_progress_on_insert_trigger ON tasks; + CREATE TRIGGER update_parent_task_progress_on_insert_trigger + AFTER INSERT ON tasks + FOR EACH ROW + WHEN (NEW.parent_task_id IS NOT NULL) + EXECUTE FUNCTION update_parent_task_progress(); + `); + + pgm.sql(` + COMMENT ON FUNCTION update_parent_task_progress() IS + 'This function recursively updates progress values for all ancestors when a task''s progress changes. + The previous version only updated the immediate parent, which led to incorrect progress values for + higher-level parent tasks when using weighted or manual progress calculations with multi-level subtasks.'; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION recalculate_all_task_progress() RETURNS void AS + $$ + BEGIN + -- First, reset manual_progress flag for all tasks that have subtasks + UPDATE tasks AS t + SET manual_progress = FALSE + WHERE EXISTS ( + SELECT 1 + FROM tasks + WHERE parent_task_id = t.id + AND archived IS FALSE + ); + + -- Start recalculation from leaf tasks (no subtasks) and propagate upward + -- This ensures calculations are done in the right order + WITH RECURSIVE task_hierarchy AS ( + -- Base case: Start with all leaf tasks (no subtasks) + SELECT + id, + parent_task_id, + 0 AS level + FROM tasks + WHERE NOT EXISTS ( + SELECT 1 FROM tasks AS sub + WHERE sub.parent_task_id = tasks.id + AND sub.archived IS FALSE + ) + AND archived IS FALSE + + UNION ALL + + -- Recursive case: Move up to parent tasks, but only after processing all their children + SELECT + t.id, + t.parent_task_id, + th.level + 1 + FROM tasks t + JOIN task_hierarchy th ON t.id = th.parent_task_id + WHERE t.archived IS FALSE + ), + ordered_tasks AS ( + SELECT DISTINCT id, level + FROM task_hierarchy + ORDER BY level ASC + ) + -- Sort by level to ensure we calculate in the right order (leaves first, then parents) + -- This ensures we're using already updated progress values + UPDATE tasks + SET progress_value = (SELECT (get_task_complete_ratio(tasks.id)->>'ratio')::FLOAT) + FROM ordered_tasks + WHERE tasks.id = ordered_tasks.id + AND (manual_progress IS FALSE OR manual_progress IS NULL); + + -- Log the completion of the recalculation + RAISE NOTICE 'Finished recalculating all task progress values'; + END; + $$ LANGUAGE plpgsql; + `); + + pgm.sql('SELECT recalculate_all_task_progress();'); +}; + +exports.down = pgm => { + pgm.sql('DROP FUNCTION IF EXISTS recalculate_all_task_progress();'); + pgm.sql('DROP TRIGGER IF EXISTS update_parent_task_progress_on_insert_trigger ON tasks;'); + pgm.sql('DROP TRIGGER IF EXISTS update_parent_task_progress_trigger ON tasks;'); + pgm.sql('DROP FUNCTION IF EXISTS update_parent_task_progress();'); +}; \ No newline at end of file diff --git a/worklenz-backend/database/pg-migrations/20250715000000-add-grouping-sort-orders.js b/worklenz-backend/database/pg-migrations/20250715000000-add-grouping-sort-orders.js new file mode 100644 index 000000000..22cb51f4a --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250715000000-add-grouping-sort-orders.js @@ -0,0 +1,118 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = true; + +exports.up = pgm => { + // Add columns if they don't exist + pgm.sql(` + ALTER TABLE tasks + ADD COLUMN IF NOT EXISTS status_sort_order integer DEFAULT 0, + ADD COLUMN IF NOT EXISTS priority_sort_order integer DEFAULT 0, + ADD COLUMN IF NOT EXISTS phase_sort_order integer DEFAULT 0, + ADD COLUMN IF NOT EXISTS member_sort_order integer DEFAULT 0 + `); + + // Update existing records + pgm.sql(` + UPDATE tasks SET + status_sort_order = sort_order, + priority_sort_order = sort_order, + phase_sort_order = sort_order, + member_sort_order = sort_order + WHERE status_sort_order = 0 + OR priority_sort_order = 0 + OR phase_sort_order = 0 + OR member_sort_order = 0; + `); + + // Add constraints only if they don't exist + pgm.sql(` + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'tasks_status_sort_order_check' + AND conrelid = 'tasks'::regclass + ) THEN + ALTER TABLE tasks ADD CONSTRAINT tasks_status_sort_order_check CHECK (status_sort_order >= 0); + END IF; + END $$; + `); + + pgm.sql(` + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'tasks_priority_sort_order_check' + AND conrelid = 'tasks'::regclass + ) THEN + ALTER TABLE tasks ADD CONSTRAINT tasks_priority_sort_order_check CHECK (priority_sort_order >= 0); + END IF; + END $$; + `); + + pgm.sql(` + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'tasks_phase_sort_order_check' + AND conrelid = 'tasks'::regclass + ) THEN + ALTER TABLE tasks ADD CONSTRAINT tasks_phase_sort_order_check CHECK (phase_sort_order >= 0); + END IF; + END $$; + `); + + pgm.sql(` + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'tasks_member_sort_order_check' + AND conrelid = 'tasks'::regclass + ) THEN + ALTER TABLE tasks ADD CONSTRAINT tasks_member_sort_order_check CHECK (member_sort_order >= 0); + END IF; + END $$; + `); + + // Create indexes + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_status_sort_order ON tasks(project_id, status_sort_order); + CREATE INDEX IF NOT EXISTS idx_tasks_priority_sort_order ON tasks(project_id, priority_sort_order); + CREATE INDEX IF NOT EXISTS idx_tasks_phase_sort_order ON tasks(project_id, phase_sort_order); + CREATE INDEX IF NOT EXISTS idx_tasks_member_sort_order ON tasks(project_id, member_sort_order); + `); + + // Add comments + pgm.sql(`COMMENT ON COLUMN tasks.status_sort_order IS 'Sort order when grouped by status';`); + pgm.sql(`COMMENT ON COLUMN tasks.priority_sort_order IS 'Sort order when grouped by priority';`); + pgm.sql(`COMMENT ON COLUMN tasks.phase_sort_order IS 'Sort order when grouped by phase';`); + pgm.sql(`COMMENT ON COLUMN tasks.member_sort_order IS 'Sort order when grouped by members/assignees';`); +}; + +exports.down = pgm => { + // Remove constraints + pgm.sql('ALTER TABLE tasks DROP CONSTRAINT IF EXISTS tasks_member_sort_order_check;'); + pgm.sql('ALTER TABLE tasks DROP CONSTRAINT IF EXISTS tasks_phase_sort_order_check;'); + pgm.sql('ALTER TABLE tasks DROP CONSTRAINT IF EXISTS tasks_priority_sort_order_check;'); + pgm.sql('ALTER TABLE tasks DROP CONSTRAINT IF EXISTS tasks_status_sort_order_check;'); + + // Remove indexes + pgm.sql('DROP INDEX IF EXISTS idx_tasks_member_sort_order;'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_phase_sort_order;'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_priority_sort_order;'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_status_sort_order;'); + + // Remove columns + pgm.sql(` + ALTER TABLE tasks + DROP COLUMN IF EXISTS member_sort_order, + DROP COLUMN IF EXISTS phase_sort_order, + DROP COLUMN IF EXISTS priority_sort_order, + DROP COLUMN IF EXISTS status_sort_order; + `); +}; diff --git a/worklenz-backend/database/pg-migrations/20250715000000_add-grouping-sort-orders.js b/worklenz-backend/database/pg-migrations/20250715000000_add-grouping-sort-orders.js new file mode 100644 index 000000000..447ab707f --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250715000000_add-grouping-sort-orders.js @@ -0,0 +1,135 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = true; + +exports.up = pgm => { + // Add columns if they don't exist + pgm.sql(` + ALTER TABLE tasks + ADD COLUMN IF NOT EXISTS status_sort_order integer DEFAULT 0, + ADD COLUMN IF NOT EXISTS priority_sort_order integer DEFAULT 0, + ADD COLUMN IF NOT EXISTS phase_sort_order integer DEFAULT 0, + ADD COLUMN IF NOT EXISTS member_sort_order integer DEFAULT 0 + `); + + // Update existing records + pgm.sql(` + UPDATE tasks SET + status_sort_order = sort_order, + priority_sort_order = sort_order, + phase_sort_order = sort_order, + member_sort_order = sort_order + WHERE status_sort_order = 0 + OR priority_sort_order = 0 + OR phase_sort_order = 0 + OR member_sort_order = 0 + `); + + // Add constraints only if they don't exist + pgm.sql(` + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'tasks_status_sort_order_check' + AND conrelid = 'tasks'::regclass + ) THEN + ALTER TABLE tasks ADD CONSTRAINT tasks_status_sort_order_check CHECK (status_sort_order >= 0); + END IF; + END $$ + `); + + pgm.sql(` + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'tasks_priority_sort_order_check' + AND conrelid = 'tasks'::regclass + ) THEN + ALTER TABLE tasks ADD CONSTRAINT tasks_priority_sort_order_check CHECK (priority_sort_order >= 0); + END IF; + END $$ + `); + + pgm.sql(` + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'tasks_phase_sort_order_check' + AND conrelid = 'tasks'::regclass + ) THEN + ALTER TABLE tasks ADD CONSTRAINT tasks_phase_sort_order_check CHECK (phase_sort_order >= 0); + END IF; + END $$ + `); + + pgm.sql(` + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'tasks_member_sort_order_check' + AND conrelid = 'tasks'::regclass + ) THEN + ALTER TABLE tasks ADD CONSTRAINT tasks_member_sort_order_check CHECK (member_sort_order >= 0); + END IF; + END $$ + `); + + // Create indexes + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_status_sort_order + ON tasks (project_id, status_sort_order) + `); + + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_priority_sort_order + ON tasks (project_id, priority_sort_order) + `); + + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_phase_sort_order + ON tasks (project_id, phase_sort_order) + `); + + pgm.sql(` + CREATE INDEX IF NOT EXISTS idx_tasks_member_sort_order + ON tasks (project_id, member_sort_order) + `); + + // Add comments + pgm.sql(`COMMENT ON COLUMN tasks.status_sort_order IS 'Sort order when grouped by status'`); + pgm.sql(`COMMENT ON COLUMN tasks.priority_sort_order IS 'Sort order when grouped by priority'`); + pgm.sql(`COMMENT ON COLUMN tasks.phase_sort_order IS 'Sort order when grouped by phase'`); + pgm.sql(`COMMENT ON COLUMN tasks.member_sort_order IS 'Sort order when grouped by members/assignees'`); +}; + +exports.down = pgm => { + // Remove constraints + pgm.sql('ALTER TABLE tasks DROP CONSTRAINT IF EXISTS tasks_member_sort_order_check'); + pgm.sql('ALTER TABLE tasks DROP CONSTRAINT IF EXISTS tasks_phase_sort_order_check'); + pgm.sql('ALTER TABLE tasks DROP CONSTRAINT IF EXISTS tasks_priority_sort_order_check'); + pgm.sql('ALTER TABLE tasks DROP CONSTRAINT IF EXISTS tasks_status_sort_order_check'); + + // Remove indexes + pgm.sql('DROP INDEX IF EXISTS idx_tasks_member_sort_order'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_phase_sort_order'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_priority_sort_order'); + pgm.sql('DROP INDEX IF EXISTS idx_tasks_status_sort_order'); + + // Remove columns + pgm.sql(` + ALTER TABLE tasks + DROP COLUMN IF EXISTS member_sort_order, + DROP COLUMN IF EXISTS phase_sort_order, + DROP COLUMN IF EXISTS priority_sort_order, + DROP COLUMN IF EXISTS status_sort_order + `); +}; + +exports.__migration = { + transaction: false, +}; \ No newline at end of file diff --git a/worklenz-backend/database/pg-migrations/20250715000001-update-sort-functions.js b/worklenz-backend/database/pg-migrations/20250715000001-update-sort-functions.js new file mode 100644 index 000000000..c31d57c95 --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250715000001-update-sort-functions.js @@ -0,0 +1,186 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = false; + +exports.up = pgm => { + pgm.sql(` + CREATE OR REPLACE FUNCTION get_sort_column_name(_group_by TEXT) RETURNS TEXT + LANGUAGE plpgsql + AS + $$ + BEGIN + CASE _group_by + WHEN 'status' THEN RETURN 'status_sort_order'; + WHEN 'priority' THEN RETURN 'priority_sort_order'; + WHEN 'phase' THEN RETURN 'phase_sort_order'; + WHEN 'members' THEN RETURN 'member_sort_order'; + ELSE RETURN 'sort_order'; -- fallback to general sort_order + END CASE; + END; + $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION update_task_sort_orders_bulk(_updates json, _group_by text DEFAULT 'status') RETURNS void + LANGUAGE plpgsql + AS + $$ + DECLARE + _update_record RECORD; + _sort_column TEXT; + _sql TEXT; + BEGIN + -- Get the appropriate sort column based on grouping + _sort_column := get_sort_column_name(_group_by); + + -- Simple approach: update each task's sort_order from the provided array + FOR _update_record IN + SELECT + (item->>'task_id')::uuid as task_id, + (item->>'sort_order')::int as sort_order, + (item->>'status_id')::uuid as status_id, + (item->>'priority_id')::uuid as priority_id, + (item->>'phase_id')::uuid as phase_id + FROM json_array_elements(_updates) as item + LOOP + -- Update the appropriate sort column and other fields using dynamic SQL + -- Only update sort_order if we're using the default sorting + IF _sort_column = 'sort_order' THEN + UPDATE tasks SET + sort_order = _update_record.sort_order, + status_id = COALESCE(_update_record.status_id, status_id), + priority_id = COALESCE(_update_record.priority_id, priority_id) + WHERE id = _update_record.task_id; + ELSE + -- Update only the grouping-specific sort column, not the main sort_order + _sql := 'UPDATE tasks SET ' || _sort_column || ' = $1, ' || + 'status_id = COALESCE($2, status_id), ' || + 'priority_id = COALESCE($3, priority_id) ' || + 'WHERE id = $4'; + + EXECUTE _sql USING + _update_record.sort_order, + _update_record.status_id, + _update_record.priority_id, + _update_record.task_id; + END IF; + + -- Handle phase updates separately since it's in a different table + IF _update_record.phase_id IS NOT NULL THEN + INSERT INTO task_phase (task_id, phase_id) + VALUES (_update_record.task_id, _update_record.phase_id) + ON CONFLICT (task_id) DO UPDATE SET phase_id = _update_record.phase_id; + END IF; + END LOOP; + END; + $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION handle_task_list_sort_order_change(_body json) RETURNS void + LANGUAGE plpgsql + AS + $$ + DECLARE + _from_index INT; + _to_index INT; + _task_id UUID; + _project_id UUID; + _from_group UUID; + _to_group UUID; + _group_by TEXT; + _batch_size INT := 100; + _sort_column TEXT; + _sql TEXT; + BEGIN + _project_id = (_body ->> 'project_id')::UUID; + _task_id = (_body ->> 'task_id')::UUID; + _from_index = (_body ->> 'from_index')::INT; + _to_index = (_body ->> 'to_index')::INT; + _from_group = (_body ->> 'from_group')::UUID; + _to_group = (_body ->> 'to_group')::UUID; + _group_by = (_body ->> 'group_by')::TEXT; + + -- Get the appropriate sort column + _sort_column := get_sort_column_name(_group_by); + + -- Handle group changes + IF (_from_group <> _to_group OR (_from_group <> _to_group) IS NULL) THEN + IF (_group_by = 'status') THEN + UPDATE tasks + SET status_id = _to_group + WHERE id = _task_id + AND status_id = _from_group + AND project_id = _project_id; + END IF; + + IF (_group_by = 'priority') THEN + UPDATE tasks + SET priority_id = _to_group + WHERE id = _task_id + AND priority_id = _from_group + AND project_id = _project_id; + END IF; + + IF (_group_by = 'phase') THEN + IF (is_null_or_empty(_to_group) IS FALSE) THEN + INSERT INTO task_phase (task_id, phase_id) + VALUES (_task_id, _to_group) + ON CONFLICT (task_id) DO UPDATE SET phase_id = _to_group; + ELSE + DELETE FROM task_phase WHERE task_id = _task_id; + END IF; + END IF; + END IF; + + -- Handle sort order changes using dynamic SQL + IF (_from_index <> _to_index) THEN + -- For the main sort_order column, we need to be careful about unique constraints + IF _sort_column = 'sort_order' THEN + -- Use a transaction-safe approach for the main sort_order column + IF (_to_index > _from_index) THEN + -- Moving down: decrease sort_order for items between old and new position + UPDATE tasks SET sort_order = sort_order - 1 + WHERE project_id = _project_id + AND sort_order > _from_index + AND sort_order <= _to_index; + ELSE + -- Moving up: increase sort_order for items between new and old position + UPDATE tasks SET sort_order = sort_order + 1 + WHERE project_id = _project_id + AND sort_order >= _to_index + AND sort_order < _from_index; + END IF; + + -- Set the new sort_order for the moved task + UPDATE tasks SET sort_order = _to_index WHERE id = _task_id; + ELSE + -- For grouping-specific columns, use dynamic SQL since there's no unique constraint + IF (_to_index > _from_index) THEN + -- Moving down: decrease sort_order for items between old and new position + _sql := 'UPDATE tasks SET ' || _sort_column || ' = ' || _sort_column || ' - 1 ' || + 'WHERE project_id = $1 AND ' || _sort_column || ' > $2 AND ' || _sort_column || ' <= $3'; + EXECUTE _sql USING _project_id, _from_index, _to_index; + ELSE + -- Moving up: increase sort_order for items between new and old position + _sql := 'UPDATE tasks SET ' || _sort_column || ' = ' || _sort_column || ' + 1 ' || + 'WHERE project_id = $1 AND ' || _sort_column || ' >= $2 AND ' || _sort_column || ' < $3'; + EXECUTE _sql USING _project_id, _to_index, _from_index; + END IF; + + -- Set the new sort_order for the moved task + _sql := 'UPDATE tasks SET ' || _sort_column || ' = $1 WHERE id = $2'; + EXECUTE _sql USING _to_index, _task_id; + END IF; + END IF; + END; + $$; + `); +}; + +exports.down = pgm => { + pgm.sql('DROP FUNCTION IF EXISTS handle_task_list_sort_order_change(json);'); + pgm.sql('DROP FUNCTION IF EXISTS update_task_sort_orders_bulk(json, text);'); + pgm.sql('DROP FUNCTION IF EXISTS get_sort_column_name(TEXT);'); +}; diff --git a/worklenz-backend/database/pg-migrations/20250715000002-fix-sort-constraint.js b/worklenz-backend/database/pg-migrations/20250715000002-fix-sort-constraint.js new file mode 100644 index 000000000..a5eec9e00 --- /dev/null +++ b/worklenz-backend/database/pg-migrations/20250715000002-fix-sort-constraint.js @@ -0,0 +1,188 @@ +/* eslint-disable camelcase */ + +exports.shorthands = undefined; +exports.noTransaction = false; + +exports.up = pgm => { + pgm.sql(` + DO $$ + DECLARE + _project RECORD; + _task RECORD; + _counter INTEGER; + BEGIN + -- For each project, reassign sort_order values to ensure uniqueness + FOR _project IN + SELECT DISTINCT project_id + FROM tasks + WHERE project_id IS NOT NULL + LOOP + _counter := 0; + + -- Reassign sort_order values sequentially for this project + FOR _task IN + SELECT id + FROM tasks + WHERE project_id = _project.project_id + ORDER BY sort_order, created_at + LOOP + UPDATE tasks + SET sort_order = _counter + WHERE id = _task.id; + + _counter := _counter + 1; + END LOOP; + END LOOP; + END + $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION update_task_sort_orders_bulk(_updates json, _group_by text DEFAULT 'status') RETURNS void + LANGUAGE plpgsql + AS + $$ + DECLARE + _update_record RECORD; + _sort_column TEXT; + _sql TEXT; + BEGIN + -- Get the appropriate sort column based on grouping + _sort_column := get_sort_column_name(_group_by); + + -- Process each update record + FOR _update_record IN + SELECT + (item->>'task_id')::uuid as task_id, + (item->>'sort_order')::int as sort_order, + (item->>'status_id')::uuid as status_id, + (item->>'priority_id')::uuid as priority_id, + (item->>'phase_id')::uuid as phase_id + FROM json_array_elements(_updates) as item + LOOP + -- Update the grouping-specific sort column and other fields + _sql := 'UPDATE tasks SET ' || _sort_column || ' = $1, ' || + 'status_id = COALESCE($2, status_id), ' || + 'priority_id = COALESCE($3, priority_id), ' || + 'updated_at = CURRENT_TIMESTAMP ' || + 'WHERE id = $4'; + + EXECUTE _sql USING + _update_record.sort_order, + _update_record.status_id, + _update_record.priority_id, + _update_record.task_id; + + -- Handle phase updates separately since it's in a different table + IF _update_record.phase_id IS NOT NULL THEN + INSERT INTO task_phase (task_id, phase_id) + VALUES (_update_record.task_id, _update_record.phase_id) + ON CONFLICT (task_id) DO UPDATE SET phase_id = _update_record.phase_id; + END IF; + END LOOP; + END; + $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION get_sort_column_name(_group_by TEXT) RETURNS TEXT + LANGUAGE plpgsql + AS + $$ + BEGIN + CASE _group_by + WHEN 'status' THEN RETURN 'status_sort_order'; + WHEN 'priority' THEN RETURN 'priority_sort_order'; + WHEN 'phase' THEN RETURN 'phase_sort_order'; + WHEN 'members' THEN RETURN 'member_sort_order'; + -- For backward compatibility, still support general sort_order but be explicit + WHEN 'general' THEN RETURN 'sort_order'; + ELSE RETURN 'status_sort_order'; -- Default to status sorting + END CASE; + END; + $$; + `); + + pgm.sql(` + CREATE OR REPLACE FUNCTION handle_task_list_sort_order_change(_body json) RETURNS void + LANGUAGE plpgsql + AS + $$ + DECLARE + _from_index INT; + _to_index INT; + _task_id UUID; + _project_id UUID; + _from_group UUID; + _to_group UUID; + _group_by TEXT; + _sort_column TEXT; + _sql TEXT; + BEGIN + _project_id = (_body ->> 'project_id')::UUID; + _task_id = (_body ->> 'task_id')::UUID; + _from_index = (_body ->> 'from_index')::INT; + _to_index = (_body ->> 'to_index')::INT; + _from_group = (_body ->> 'from_group')::UUID; + _to_group = (_body ->> 'to_group')::UUID; + _group_by = (_body ->> 'group_by')::TEXT; + + -- Get the appropriate sort column + _sort_column := get_sort_column_name(_group_by); + + -- Handle group changes first + IF (_from_group <> _to_group OR (_from_group <> _to_group) IS NULL) THEN + IF (_group_by = 'status') THEN + UPDATE tasks + SET status_id = _to_group, updated_at = CURRENT_TIMESTAMP + WHERE id = _task_id + AND project_id = _project_id; + END IF; + + IF (_group_by = 'priority') THEN + UPDATE tasks + SET priority_id = _to_group, updated_at = CURRENT_TIMESTAMP + WHERE id = _task_id + AND project_id = _project_id; + END IF; + + IF (_group_by = 'phase') THEN + IF (is_null_or_empty(_to_group) IS FALSE) THEN + INSERT INTO task_phase (task_id, phase_id) + VALUES (_task_id, _to_group) + ON CONFLICT (task_id) DO UPDATE SET phase_id = _to_group; + ELSE + DELETE FROM task_phase WHERE task_id = _task_id; + END IF; + END IF; + END IF; + + -- Handle sort order changes for the grouping-specific column only + IF (_from_index <> _to_index) THEN + -- Update the grouping-specific sort order (no unique constraint issues) + IF (_to_index > _from_index) THEN + -- Moving down: decrease sort order for items between old and new position + _sql := 'UPDATE tasks SET ' || _sort_column || ' = ' || _sort_column || ' - 1, ' || + 'updated_at = CURRENT_TIMESTAMP ' || + 'WHERE project_id = $1 AND ' || _sort_column || ' > $2 AND ' || _sort_column || ' <= $3'; + EXECUTE _sql USING _project_id, _from_index, _to_index; + ELSE + -- Moving up: increase sort order for items between new and old position + _sql := 'UPDATE tasks SET ' || _sort_column || ' = ' || _sort_column || ' + 1, ' || + 'updated_at = CURRENT_TIMESTAMP ' || + 'WHERE project_id = $1 AND ' || _sort_column || ' >= $2 AND ' || _sort_column || ' < $3'; + EXECUTE _sql USING _project_id, _to_index, _from_index; + END IF; + + -- Set the new sort order for the moved task + _sql := 'UPDATE tasks SET ' || _sort_column || ' = $1, updated_at = CURRENT_TIMESTAMP WHERE id = $2'; + EXECUTE _sql USING _to_index, _task_id; + END IF; + END; + $$; + `); +}; + +exports.down = pgm => { + // Functions will be reverted by the down migration of the previous file +}; diff --git a/worklenz-backend/database/pg-migrations/README.md b/worklenz-backend/database/pg-migrations/README.md deleted file mode 100644 index ee063447f..000000000 --- a/worklenz-backend/database/pg-migrations/README.md +++ /dev/null @@ -1,72 +0,0 @@ -# Node-pg-migrate Migrations - -This directory contains database migrations managed by node-pg-migrate. - -## Migration Commands - -- `npm run migrate:create -- migration-name` - Create a new migration file -- `npm run migrate:up` - Run all pending migrations -- `npm run migrate:down` - Rollback the last migration -- `npm run migrate:redo` - Rollback and re-run the last migration - -## Migration File Format - -Migrations are JavaScript files with timestamp prefixes (e.g., `20250115000000_performance-indexes.js`). - -Each migration file exports two functions: -- `exports.up` - Contains the forward migration logic -- `exports.down` - Contains the rollback logic - -## Best Practices - -1. **Always use IF EXISTS/IF NOT EXISTS checks** to make migrations idempotent -2. **Test migrations locally** before deploying to production -3. **Include rollback logic** in the `down` function for all changes -4. **Use descriptive names** for migration files -5. **Keep migrations focused** - one logical change per migration - -## Example Migration - -```javascript -exports.up = pgm => { - // Create table with IF NOT EXISTS - pgm.createTable('users', { - id: 'id', - name: { type: 'varchar(100)', notNull: true }, - created_at: { - type: 'timestamp', - notNull: true, - default: pgm.func('current_timestamp') - } - }, { ifNotExists: true }); - - // Add index with IF NOT EXISTS - pgm.createIndex('users', 'name', { - name: 'idx_users_name', - ifNotExists: true - }); -}; - -exports.down = pgm => { - // Drop in reverse order - pgm.dropIndex('users', 'name', { - name: 'idx_users_name', - ifExists: true - }); - - pgm.dropTable('users', { ifExists: true }); -}; -``` - -## Migration History - -The `pgmigrations` table tracks which migrations have been run. Do not modify this table manually. - -## Converting from SQL Migrations - -When converting SQL migrations to node-pg-migrate format: - -1. Wrap SQL statements in `pgm.sql()` calls -2. Use node-pg-migrate helper methods where possible (createTable, addColumns, etc.) -3. Always include `IF EXISTS/IF NOT EXISTS` checks -4. Ensure proper rollback logic in the `down` function \ No newline at end of file diff --git a/worklenz-backend/database/sql/1_tables.sql b/worklenz-backend/database/sql/1_tables.sql index 2ab000773..fb066d342 100644 --- a/worklenz-backend/database/sql/1_tables.sql +++ b/worklenz-backend/database/sql/1_tables.sql @@ -25,6 +25,13 @@ CREATE TABLE IF NOT EXISTS pg_sessions ( expire TIMESTAMP(6) NOT NULL ); +-- Create pgmigrations table for node-pg-migrate +CREATE TABLE IF NOT EXISTS pgmigrations ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + run_on TIMESTAMP WITH TIME ZONE NOT NULL +); + CREATE TABLE IF NOT EXISTS project_access_levels ( id UUID DEFAULT uuid_generate_v4() NOT NULL, name TEXT NOT NULL, diff --git a/worklenz-backend/migrate.json b/worklenz-backend/migrate.json new file mode 100644 index 000000000..9cd8152d2 --- /dev/null +++ b/worklenz-backend/migrate.json @@ -0,0 +1,22 @@ +{ + "migrations-dir": "database/pg-migrations", + "migrations-schema": "public", + "migrations-table": "pgmigrations", + "db": { + "user": { + "ENV": "DB_USER" + }, + "password": { + "ENV": "DB_PASSWORD" + }, + "host": { + "ENV": "DB_HOST" + }, + "port": { + "ENV": "DB_PORT" + }, + "database": { + "ENV": "DB_NAME" + } + } +} \ No newline at end of file diff --git a/worklenz-backend/package-lock.json b/worklenz-backend/package-lock.json index 1a0f78d3f..04162c0aa 100644 --- a/worklenz-backend/package-lock.json +++ b/worklenz-backend/package-lock.json @@ -33,7 +33,6 @@ "express-rate-limit": "^6.8.0", "express-session": "^1.17.3", "express-validator": "^6.15.0", - "grunt-cli": "^1.5.0", "helmet": "^6.2.0", "hpp": "^0.2.3", "http-errors": "^2.0.0", @@ -116,6 +115,7 @@ "jest": "^28.1.3", "jest-sonar-reporter": "^2.0.0", "ncp": "^2.0.0", + "node-pg-migrate": "^8.0.3", "nodeman": "^1.1.2", "rimraf": "^6.0.1", "swagger-jsdoc": "^6.2.8", @@ -126,7 +126,7 @@ "typescript": "^4.9.5" }, "engines": { - "node": ">=16.13.0", + "node": ">=20.0.0", "npm": ">=8.11.0", "yarn": "WARNING: Please use npm package manager instead of yarn" } @@ -6455,30 +6455,12 @@ "dev": true, "license": "Python-2.0" }, - "node_modules/array-each": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/array-each/-/array-each-1.0.1.tgz", - "integrity": "sha512-zHjL5SZa68hkKHBFBK6DJCTtr9sfTCPCaph/L7tMSLcTFgy+zX7E+6q5UArbtOtMBCtxdICpfTCspRse+ywyXA==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==", "license": "MIT" }, - "node_modules/array-slice": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/array-slice/-/array-slice-1.1.0.tgz", - "integrity": "sha512-B1qMD3RBP7O8o0H2KbrXDyB0IccejMF15+87Lvlor12ONPRHP6gTjXMNkt/d3ZuOGbAe66hFmaCfECI24Ufp6w==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/array-union": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz", @@ -6951,6 +6933,7 @@ "version": "3.0.3", "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", + "dev": true, "license": "MIT", "dependencies": { "fill-range": "^7.1.1" @@ -8056,15 +8039,6 @@ "npm": "1.2.8000 || >= 1.4.16" } }, - "node_modules/detect-file": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/detect-file/-/detect-file-1.0.0.tgz", - "integrity": "sha512-DtCOLG98P007x7wiiOmfI0fi3eIKyWiLTGJ2MDnVi/E04lWGbf+JzrRHMm0rgIIZJGtHpKpbVgLWHrv8xXpc3Q==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/detect-libc": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.4.tgz", @@ -8924,18 +8898,6 @@ "node": ">=6" } }, - "node_modules/expand-tilde": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/expand-tilde/-/expand-tilde-2.0.2.tgz", - "integrity": "sha512-A5EmesHW6rfnZ9ysHQjPdJRni0SRar0tjtG5MNtm9n5TUvsYU8oozprtRD4AqHxcZWWlVuAmQo2nWKfN9oyjTw==", - "license": "MIT", - "dependencies": { - "homedir-polyfill": "^1.0.1" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/expect": { "version": "28.1.3", "resolved": "https://registry.npmjs.org/expect/-/expect-28.1.3.tgz", @@ -9088,12 +9050,6 @@ "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", "license": "MIT" }, - "node_modules/extend": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", - "integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==", - "license": "MIT" - }, "node_modules/fast-csv": { "version": "4.3.6", "resolved": "https://registry.npmjs.org/fast-csv/-/fast-csv-4.3.6.tgz", @@ -9222,6 +9178,7 @@ "version": "7.1.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", + "dev": true, "license": "MIT", "dependencies": { "to-regex-range": "^5.0.1" @@ -9287,46 +9244,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/findup-sync": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/findup-sync/-/findup-sync-4.0.0.tgz", - "integrity": "sha512-6jvvn/12IC4quLBL1KNokxC7wWTvYncaVUYSoxWw7YykPLuRrnv4qdHcSOywOI5RpkOVGeQRtWM8/q+G6W6qfQ==", - "license": "MIT", - "dependencies": { - "detect-file": "^1.0.0", - "is-glob": "^4.0.0", - "micromatch": "^4.0.2", - "resolve-dir": "^1.0.1" - }, - "engines": { - "node": ">= 8" - } - }, - "node_modules/fined": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/fined/-/fined-1.2.0.tgz", - "integrity": "sha512-ZYDqPLGxDkDhDZBjZBb+oD1+j0rA4E0pXY50eplAAOPg2N/gUBSSk5IM1/QhPfyVo19lJ+CvXpqfvk+b2p/8Ng==", - "license": "MIT", - "dependencies": { - "expand-tilde": "^2.0.2", - "is-plain-object": "^2.0.3", - "object.defaults": "^1.1.0", - "object.pick": "^1.2.0", - "parse-filepath": "^1.0.1" - }, - "engines": { - "node": ">= 0.10" - } - }, - "node_modules/flagged-respawn": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/flagged-respawn/-/flagged-respawn-1.0.1.tgz", - "integrity": "sha512-lNaHNVymajmk0OJMBn8fVUAU1BtDeKIqKoVhk4xAALB57aALg6b4W0MfJ/cUE0g9YBXy5XhSlPIpYIJ7HaY/3Q==", - "license": "MIT", - "engines": { - "node": ">= 0.10" - } - }, "node_modules/flat-cache": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-3.2.0.tgz", @@ -9427,27 +9344,6 @@ } } }, - "node_modules/for-in": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", - "integrity": "sha512-7EwmXrOjyL+ChxMhmG5lnW9MPt1aIeZEwKhQzoBUdTV0N3zuwWDZYVJatDvZ2OyzPUvdIAZDsCetk3coyMfcnQ==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/for-own": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/for-own/-/for-own-1.0.0.tgz", - "integrity": "sha512-0OABksIGrxKK8K4kynWkQ7y1zounQxP+CWnyclVwj81KW3vlLlGUx57DKGcP/LH216GzqnstnPocF16Nxs0Ycg==", - "license": "MIT", - "dependencies": { - "for-in": "^1.0.1" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/foreground-child": { "version": "3.3.1", "resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-3.3.1.tgz", @@ -9845,48 +9741,6 @@ "node": ">= 0.10" } }, - "node_modules/global-modules": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/global-modules/-/global-modules-1.0.0.tgz", - "integrity": "sha512-sKzpEkf11GpOFuw0Zzjzmt4B4UZwjOcG757PPvrfhxcLFbq0wpsgpOqxpxtxFiCG4DtG93M6XRVbF2oGdev7bg==", - "license": "MIT", - "dependencies": { - "global-prefix": "^1.0.1", - "is-windows": "^1.0.1", - "resolve-dir": "^1.0.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/global-prefix": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/global-prefix/-/global-prefix-1.0.2.tgz", - "integrity": "sha512-5lsx1NUDHtSjfg0eHlmYvZKv8/nVqX4ckFbM+FrGcQ+04KWcWFo9P5MxPZYSzUvyzmdTbI7Eix8Q4IbELDqzKg==", - "license": "MIT", - "dependencies": { - "expand-tilde": "^2.0.2", - "homedir-polyfill": "^1.0.1", - "ini": "^1.3.4", - "is-windows": "^1.0.1", - "which": "^1.2.14" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/global-prefix/node_modules/which": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", - "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", - "license": "ISC", - "dependencies": { - "isexe": "^2.0.0" - }, - "bin": { - "which": "bin/which" - } - }, "node_modules/globals": { "version": "11.12.0", "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", @@ -9943,34 +9797,6 @@ "dev": true, "license": "MIT" }, - "node_modules/grunt-cli": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/grunt-cli/-/grunt-cli-1.5.0.tgz", - "integrity": "sha512-rILKAFoU0dzlf22SUfDtq2R1fosChXXlJM5j7wI6uoW8gwmXDXzbUvirlKZSYCdXl3LXFbR+8xyS+WFo+b6vlA==", - "license": "MIT", - "dependencies": { - "grunt-known-options": "~2.0.0", - "interpret": "~1.1.0", - "liftup": "~3.0.1", - "nopt": "~5.0.0", - "v8flags": "^4.0.1" - }, - "bin": { - "grunt": "bin/grunt" - }, - "engines": { - "node": ">=10" - } - }, - "node_modules/grunt-known-options": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/grunt-known-options/-/grunt-known-options-2.0.0.tgz", - "integrity": "sha512-GD7cTz0I4SAede1/+pAbmJRG44zFLPipVtdL9o3vqx9IEyb7b4/Y3s7r6ofI3CchR5GvYJ+8buCSioDv5dQLiA==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", @@ -10042,18 +9868,6 @@ "dev": true, "license": "https://www.highcharts.com/license" }, - "node_modules/homedir-polyfill": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/homedir-polyfill/-/homedir-polyfill-1.0.3.tgz", - "integrity": "sha512-eSmmWE5bZTK2Nou4g0AI3zZ9rswp7GRKoKXS1BLUkvPviOqs4YTN1djQIqrXy9k5gEtdLPy86JjRwsNM9tnDcA==", - "license": "MIT", - "dependencies": { - "parse-passwd": "^1.0.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/hpp": { "version": "0.2.3", "resolved": "https://registry.npmjs.org/hpp/-/hpp-0.2.3.tgz", @@ -10263,12 +10077,6 @@ "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", "license": "ISC" }, - "node_modules/interpret": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.1.0.tgz", - "integrity": "sha512-CLM8SNMDu7C5psFCn6Wg/tgpj/bKAg7hc2gWqcuR9OD5Ft9PhBpIu8PLicPeis+xDd6YX2ncI8MCA64I9tftIA==", - "license": "MIT" - }, "node_modules/ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", @@ -10278,19 +10086,6 @@ "node": ">= 0.10" } }, - "node_modules/is-absolute": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-absolute/-/is-absolute-1.0.0.tgz", - "integrity": "sha512-dOWoqflvcydARa360Gvv18DZ/gRuHKi2NU/wU5X1ZFzdYfH29nkiNZsF3mp4OJ3H4yo9Mx8A/uAGNzpzPN3yBA==", - "license": "MIT", - "dependencies": { - "is-relative": "^1.0.0", - "is-windows": "^1.0.1" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/is-arrayish": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", @@ -10352,6 +10147,7 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "dev": true, "license": "MIT", "engines": { "node": ">=0.10.0" @@ -10380,6 +10176,7 @@ "version": "4.0.3", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dev": true, "license": "MIT", "dependencies": { "is-extglob": "^2.1.1" @@ -10392,6 +10189,7 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true, "license": "MIT", "engines": { "node": ">=0.12.0" @@ -10407,18 +10205,6 @@ "node": ">=8" } }, - "node_modules/is-plain-object": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.4.tgz", - "integrity": "sha512-h5PpgXkWitc38BBMYawTYMWJHFZJVnBquFE57xFpjB8pJFiF6gZ+bU+WyI/yqXiFR5mdLsgYNaPe8uao6Uv9Og==", - "license": "MIT", - "dependencies": { - "isobject": "^3.0.1" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/is-promise": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.2.2.tgz", @@ -10443,18 +10229,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/is-relative": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-relative/-/is-relative-1.0.0.tgz", - "integrity": "sha512-Kw/ReK0iqwKeu0MITLFuj0jbPAmEiOsIwyIXvvbfa6QfmN9pkD1M+8pdk7Rl/dTKbH34/XBFMbgD4iMJhLQbGA==", - "license": "MIT", - "dependencies": { - "is-unc-path": "^1.0.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/is-stream": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz", @@ -10467,27 +10241,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/is-unc-path": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-unc-path/-/is-unc-path-1.0.0.tgz", - "integrity": "sha512-mrGpVd0fs7WWLfVsStvgF6iEJnbjDFZh9/emhRDcGWTduTfNHd9CHeUwH3gYIjdbwo4On6hunkztwOaAw0yllQ==", - "license": "MIT", - "dependencies": { - "unc-path-regex": "^0.1.2" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/is-windows": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz", - "integrity": "sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -10498,17 +10251,9 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", + "dev": true, "license": "ISC" }, - "node_modules/isobject": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz", - "integrity": "sha512-WhB9zCku7EGTj/HQQRz5aUQEUeoQZH2bWcltRErOpymJ4boYE6wL9Tbr23krRPSZ+C5zqNSrSw+Cc7sZZ4b7vg==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/istanbul-lib-coverage": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz", @@ -11526,15 +11271,6 @@ "json-buffer": "3.0.1" } }, - "node_modules/kind-of": { - "version": "6.0.3", - "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz", - "integrity": "sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/kleur": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", @@ -11626,25 +11362,6 @@ "immediate": "~3.0.5" } }, - "node_modules/liftup": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/liftup/-/liftup-3.0.1.tgz", - "integrity": "sha512-yRHaiQDizWSzoXk3APcA71eOI/UuhEkNN9DiW2Tt44mhYzX4joFoCZlxsSOF7RyeLlfqzFLQI1ngFq3ggMPhOw==", - "license": "MIT", - "dependencies": { - "extend": "^3.0.2", - "findup-sync": "^4.0.0", - "fined": "^1.2.0", - "flagged-respawn": "^1.0.1", - "is-plain-object": "^2.0.4", - "object.map": "^1.0.1", - "rechoir": "^0.7.0", - "resolve": "^1.19.0" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/lines-and-columns": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", @@ -11883,18 +11600,6 @@ "dev": true, "license": "ISC" }, - "node_modules/make-iterator": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/make-iterator/-/make-iterator-1.0.1.tgz", - "integrity": "sha512-pxiuXh0iVEq7VM7KMIhs5gxsfxCux2URptUQaXo4iZZJxBAzTPOLE2BumO5dbfVYq/hBJFBR/a1mFDmOx5AGmw==", - "license": "MIT", - "dependencies": { - "kind-of": "^6.0.2" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/makeerror": { "version": "1.0.12", "resolved": "https://registry.npmjs.org/makeerror/-/makeerror-1.0.12.tgz", @@ -11905,15 +11610,6 @@ "tmpl": "1.0.5" } }, - "node_modules/map-cache": { - "version": "0.2.2", - "resolved": "https://registry.npmjs.org/map-cache/-/map-cache-0.2.2.tgz", - "integrity": "sha512-8y/eV9QQZCiyn1SprXSrCmqJN0yNRATe+PO8ztwqrvrbdRLA3eYJF0yaR0YayLWkMbsQSKWS9N2gPcGEc4UsZg==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/math-intrinsics": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", @@ -11971,6 +11667,7 @@ "version": "4.0.8", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", + "dev": true, "license": "MIT", "dependencies": { "braces": "^3.0.3", @@ -12318,6 +12015,32 @@ "dev": true, "license": "MIT" }, + "node_modules/node-pg-migrate": { + "version": "8.0.3", + "resolved": "https://registry.npmjs.org/node-pg-migrate/-/node-pg-migrate-8.0.3.tgz", + "integrity": "sha512-oKzZyzTULTryO1jehX19VnyPCGf3G/3oWZg3gODphvID56T0WjPOShTVPVnxGdlcueaIW3uAVrr7M8xLZq5TcA==", + "dev": true, + "license": "MIT", + "dependencies": { + "glob": "~11.0.0", + "yargs": "~17.7.0" + }, + "bin": { + "node-pg-migrate": "bin/node-pg-migrate.js" + }, + "engines": { + "node": ">=20.11.0" + }, + "peerDependencies": { + "@types/pg": ">=6.0.0 <9.0.0", + "pg": ">=4.3.0 <9.0.0" + }, + "peerDependenciesMeta": { + "@types/pg": { + "optional": true + } + } + }, "node_modules/node-releases": { "version": "2.0.19", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.19.tgz", @@ -12418,46 +12141,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/object.defaults": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/object.defaults/-/object.defaults-1.1.0.tgz", - "integrity": "sha512-c/K0mw/F11k4dEUBMW8naXUuBuhxRCfG7W+yFy8EcijU/rSmazOUd1XAEEe6bC0OuXY4HUKjTJv7xbxIMqdxrA==", - "license": "MIT", - "dependencies": { - "array-each": "^1.0.1", - "array-slice": "^1.0.0", - "for-own": "^1.0.0", - "isobject": "^3.0.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/object.map": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/object.map/-/object.map-1.0.1.tgz", - "integrity": "sha512-3+mAJu2PLfnSVGHwIWubpOFLscJANBKuB/6A4CxBstc4aqwQY0FWcsppuy4jU5GSB95yES5JHSI+33AWuS4k6w==", - "license": "MIT", - "dependencies": { - "for-own": "^1.0.0", - "make-iterator": "^1.0.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/object.pick": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/object.pick/-/object.pick-1.3.0.tgz", - "integrity": "sha512-tqa/UMy/CCoYmj+H5qc07qvSL9dqcs/WZENZ1JbtWBlATP+iVOe778gE6MSijnyCnORzDuX6hU+LA4SZ09YjFQ==", - "license": "MIT", - "dependencies": { - "isobject": "^3.0.1" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/on-finished": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz", @@ -12620,20 +12303,6 @@ "node": ">=6" } }, - "node_modules/parse-filepath": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/parse-filepath/-/parse-filepath-1.0.2.tgz", - "integrity": "sha512-FwdRXKCohSVeXqwtYonZTXtbGJKrn+HNyWDYVcp5yuJlesTwNH4rsmRZ+GrKAPJ5bLpRxESMeS+Rl0VCHRvB2Q==", - "license": "MIT", - "dependencies": { - "is-absolute": "^1.0.0", - "map-cache": "^0.2.0", - "path-root": "^0.1.1" - }, - "engines": { - "node": ">=0.8" - } - }, "node_modules/parse-json": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-5.2.0.tgz", @@ -12653,15 +12322,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/parse-passwd": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/parse-passwd/-/parse-passwd-1.0.0.tgz", - "integrity": "sha512-1Y1A//QUXEZK7YKz+rD9WydcE1+EuPr6ZBgKecAB8tmoW6UFv0NREVJe1p+jRxtThkcbbKkfwIbWJe/IeE6m2Q==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/parse-srcset": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/parse-srcset/-/parse-srcset-1.0.2.tgz", @@ -12800,27 +12460,6 @@ "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", "license": "MIT" }, - "node_modules/path-root": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/path-root/-/path-root-0.1.1.tgz", - "integrity": "sha512-QLcPegTHF11axjfojBIoDygmS2E3Lf+8+jI6wOVmNVenrKSo3mFdSGiIgdSHenczw3wPtlVMQaFVwGmM7BJdtg==", - "license": "MIT", - "dependencies": { - "path-root-regex": "^0.1.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/path-root-regex": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/path-root-regex/-/path-root-regex-0.1.2.tgz", - "integrity": "sha512-4GlJ6rZDhQZFE0DPVKh0e9jmZ5egZfxTkp7bcRDuPlJXbAwhxcl2dINPUAsjLdejqaLsCeg8axcLjIbvBjN4pQ==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/path-scurry": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-2.0.0.tgz", @@ -12968,6 +12607,7 @@ "version": "2.3.1", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, "license": "MIT", "engines": { "node": ">=8.6" @@ -13563,18 +13203,6 @@ "node": ">=8.10.0" } }, - "node_modules/rechoir": { - "version": "0.7.1", - "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.7.1.tgz", - "integrity": "sha512-/njmZ8s1wVeR6pjTZ+0nCnv8SpZNRMT2D1RLOJQESlYFDBvwpTA4KWJpZ+sBJ4+vhjILRcK7JIFdGCdxEAAitg==", - "license": "MIT", - "dependencies": { - "resolve": "^1.9.0" - }, - "engines": { - "node": ">= 0.10" - } - }, "node_modules/redis": { "version": "4.7.1", "resolved": "https://registry.npmjs.org/redis/-/redis-4.7.1.tgz", @@ -13726,19 +13354,6 @@ "node": ">=8" } }, - "node_modules/resolve-dir": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/resolve-dir/-/resolve-dir-1.0.1.tgz", - "integrity": "sha512-R7uiTjECzvOsWSfdM0QKFNBVFcK27aHOUwdvK53BcW8zqnGdYp0Fbj82cy54+2A4P2tFM22J5kRfe1R+lM/1yg==", - "license": "MIT", - "dependencies": { - "expand-tilde": "^2.0.0", - "global-modules": "^1.0.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/resolve-from": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz", @@ -14974,6 +14589,7 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, "license": "MIT", "dependencies": { "is-number": "^7.0.0" @@ -15494,15 +15110,6 @@ "integrity": "sha512-IevTus0SbGwQzYh3+fRsAMTVVPOoIVufzacXcHPmdlle1jUpq7BRL+mw3dgeLanvGZdwwbWhRV6XrcFNdBmjWA==", "license": "MIT" }, - "node_modules/unc-path-regex": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/unc-path-regex/-/unc-path-regex-0.1.2.tgz", - "integrity": "sha512-eXL4nmJT7oCpkZsHZUOJo8hcX3GbsiDOa0Qu9F646fi8dT3XuSVopVqAcEiVzSKKH7UoDti23wNX3qGFxcW5Qg==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", @@ -15732,15 +15339,6 @@ "node": ">=10.12.0" } }, - "node_modules/v8flags": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/v8flags/-/v8flags-4.0.1.tgz", - "integrity": "sha512-fcRLaS4H/hrZk9hYwbdRM35D0U8IYMfEClhXxCivOojl+yTRAZH3Zy2sSy6qVCiGbV9YAtPssP6jaChqC9vPCg==", - "license": "MIT", - "engines": { - "node": ">= 10.13.0" - } - }, "node_modules/validator": { "version": "13.15.15", "resolved": "https://registry.npmjs.org/validator/-/validator-13.15.15.tgz", diff --git a/worklenz-backend/package.json b/worklenz-backend/package.json index d4e07de2e..d3d7e664f 100644 --- a/worklenz-backend/package.json +++ b/worklenz-backend/package.json @@ -35,7 +35,12 @@ "inline-queries": "node ./cli/inline-queries", "sonar": "sonar-scanner -Dproject.settings=sonar-project-dev.properties", "tsc": "tsc", - "test:watch": "jest --watch --setupFiles dotenv/config" + "test:watch": "jest --watch --setupFiles dotenv/config", + "migrate": "node-pg-migrate", + "migrate:up": "node-pg-migrate up --no-transaction --no-lock ", + "migrate:down": "npm run migrate down", + "migrate:create": "npm run migrate create", + "migrate:redo": "npm run migrate redo" }, "jestSonar": { "reportPath": "coverage", @@ -150,6 +155,7 @@ "jest": "^28.1.3", "jest-sonar-reporter": "^2.0.0", "ncp": "^2.0.0", + "node-pg-migrate": "^8.0.3", "nodeman": "^1.1.2", "rimraf": "^6.0.1", "swagger-jsdoc": "^6.2.8",