feat: Add migration functionality to update template-based tasks and set custom_max_wait_days to NULL

This commit is contained in:
Chop
2025-06-03 21:28:33 +02:00
parent 330744daf9
commit e136c9f0e8
2 changed files with 66 additions and 3 deletions

View File

@@ -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 }
);
}
}