fix: use integer instead of serial for taskNumber, add backfill on startup
serial caused db:push failure on existing tables. Now uses nullable integer with app-level sequencing and startup backfill.
This commit is contained in:
@@ -2,9 +2,38 @@ import { Elysia } from "elysia";
|
||||
import { cors } from "@elysiajs/cors";
|
||||
import { taskRoutes } from "./routes/tasks";
|
||||
import { auth } from "./lib/auth";
|
||||
import { db } from "./db";
|
||||
import { tasks } from "./db/schema";
|
||||
import { isNull, asc, sql } from "drizzle-orm";
|
||||
|
||||
const PORT = process.env.PORT || 3100;
|
||||
|
||||
// Backfill task numbers for existing tasks that don't have one
|
||||
async function backfillTaskNumbers() {
|
||||
const unnumbered = await db
|
||||
.select()
|
||||
.from(tasks)
|
||||
.where(isNull(tasks.taskNumber))
|
||||
.orderBy(asc(tasks.createdAt));
|
||||
|
||||
if (unnumbered.length === 0) return;
|
||||
|
||||
const maxNum = await db
|
||||
.select({ max: sql<number>`COALESCE(MAX(${tasks.taskNumber}), 0)` })
|
||||
.from(tasks);
|
||||
let next = (maxNum[0]?.max ?? 0) + 1;
|
||||
|
||||
for (const task of unnumbered) {
|
||||
await db
|
||||
.update(tasks)
|
||||
.set({ taskNumber: next++ })
|
||||
.where(sql`${tasks.id} = ${task.id}`);
|
||||
}
|
||||
console.log(`Backfilled ${unnumbered.length} task numbers (${next - unnumbered.length} to ${next - 1})`);
|
||||
}
|
||||
|
||||
backfillTaskNumbers().catch(console.error);
|
||||
|
||||
const app = new Elysia()
|
||||
.use(
|
||||
cors({
|
||||
|
||||
Reference in New Issue
Block a user