feat: Add migration functionality to update template-based tasks and set custom_max_wait_days to NULL
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
||||
createProjectTask,
|
||||
} from "@/lib/queries/tasks";
|
||||
import { NextResponse } from "next/server";
|
||||
import db from "@/lib/db";
|
||||
|
||||
// GET: Get all project tasks or task templates based on query params
|
||||
export async function GET(req) {
|
||||
@@ -50,3 +51,65 @@ export async function POST(req) {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PATCH: Handle special operations like migration
|
||||
export async function PATCH(req) {
|
||||
try {
|
||||
const data = await req.json();
|
||||
|
||||
if (data.action === "fix_max_wait_days") {
|
||||
console.log(
|
||||
"Running migration to fix template-based tasks max_wait_days..."
|
||||
);
|
||||
|
||||
// Update existing template-based tasks to set custom_max_wait_days to NULL
|
||||
const result = db
|
||||
.prepare(
|
||||
`
|
||||
UPDATE project_tasks
|
||||
SET custom_max_wait_days = NULL
|
||||
WHERE task_template_id IS NOT NULL
|
||||
AND custom_max_wait_days = 0
|
||||
`
|
||||
)
|
||||
.run();
|
||||
|
||||
console.log(
|
||||
`Migration completed. Updated ${result.changes} template-based tasks.`
|
||||
);
|
||||
|
||||
// Return updated tasks to verify
|
||||
const updatedTasks = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT
|
||||
pt.id,
|
||||
pt.task_template_id,
|
||||
pt.custom_max_wait_days,
|
||||
t.max_wait_days as template_max,
|
||||
t.name as template_name,
|
||||
COALESCE(pt.custom_max_wait_days, t.max_wait_days) as calculated_max
|
||||
FROM project_tasks pt
|
||||
LEFT JOIN tasks t ON pt.task_template_id = t.task_id
|
||||
WHERE pt.task_template_id IS NOT NULL
|
||||
`
|
||||
)
|
||||
.all();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: `Updated ${result.changes} template-based tasks`,
|
||||
updatedTasksCount: result.changes,
|
||||
updatedTasks,
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: "Unknown action" }, { status: 400 });
|
||||
} catch (error) {
|
||||
console.error("Migration failed:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Migration failed", details: error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,10 +60,10 @@ export function getProjectTasks(projectId) {
|
||||
// Create a new project task
|
||||
export function createProjectTask(data) {
|
||||
if (data.task_template_id) {
|
||||
// Creating from template
|
||||
// Creating from template - explicitly set custom_max_wait_days to NULL so COALESCE uses template value
|
||||
const stmt = db.prepare(`
|
||||
INSERT INTO project_tasks (project_id, task_template_id, status, priority)
|
||||
VALUES (?, ?, ?, ?)
|
||||
INSERT INTO project_tasks (project_id, task_template_id, custom_max_wait_days, status, priority)
|
||||
VALUES (?, ?, NULL, ?, ?)
|
||||
`);
|
||||
return stmt.run(
|
||||
data.project_id,
|
||||
|
||||
Reference in New Issue
Block a user