Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(api): serialize column deletion with task imports

Tin7813635e6c2c
diff --git a/apps/api/src/column/controllers/delete-column.ts b/apps/api/src/column/controllers/delete-column.ts
index 5570a7aaf..ea0690602 100644
--- a/apps/api/src/column/controllers/delete-column.ts
+++ b/apps/api/src/column/controllers/delete-column.ts
@@ -5,28 +5,35 @@ import db from "../../database";
import { columnTable, taskTable } from "../../database/schema";
async function deleteColumn(id: string) {
- const existing = await db.query.columnTable.findFirst({
- where: eq(columnTable.id, id),
+ const existing = await db.transaction(async (tx) => {
+ // Lock before checking emptiness so queued deletes see committed inserts.
+ const [column] = await tx
+ .select()
+ .from(columnTable)
+ .where(eq(columnTable.id, id))
+ .limit(1)
+ .for("update");
+
+ if (!column) {
+ throw new HTTPException(404, { message: "Column not found" });
+ }
+
+ const [taskCount] = await tx
+ .select({ count: sql<number>`count(*)` })
+ .from(taskTable)
+ .where(eq(taskTable.columnId, id));
+
+ if (taskCount && taskCount.count > 0) {
+ throw new HTTPException(409, {
+ message:
+ "Cannot delete column that contains tasks. Move or delete tasks first.",
+ });
+ }
+
+ await tx.delete(columnTable).where(eq(columnTable.id, id));
+ return column;
});
- if (!existing) {
- throw new HTTPException(404, { message: "Column not found" });
- }
-
- const [taskCount] = await db
- .select({ count: sql<number>`count(*)` })
- .from(taskTable)
- .where(eq(taskTable.columnId, id));
-
- if (taskCount && taskCount.count > 0) {
- throw new HTTPException(409, {
- message:
- "Cannot delete column that contains tasks. Move or delete tasks first.",
- });
- }
-
- await db.delete(columnTable).where(eq(columnTable.id, id));
-
await publishEvent("project.updated", { projectId: existing.projectId });
return existing;
diff --git a/tests/api-integration/task-import-column-race.test.ts b/tests/api-integration/task-import-column-race.test.ts
index d49be0641..43de45d19 100644
--- a/tests/api-integration/task-import-column-race.test.ts
+++ b/tests/api-integration/task-import-column-race.test.ts
@@ -1,6 +1,6 @@
import { eq, sql } from "drizzle-orm";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
-import db, { getDatabasePool, schema } from "../../apps/api/src/database";
+import db, { schema } from "../../apps/api/src/database";
import deleteColumn from "../../apps/api/src/column/controllers/delete-column";
import { createApp } from "../../apps/api/src/index";
import importTasks from "../../apps/api/src/task/controllers/import-tasks";
@@ -90,26 +90,48 @@ describe("task import column deletion", () => {
},
);
- it("keeps a resolved column valid until its task transaction commits", async () => {
+ it("rejects deletion of a column that already contains a task", async () => {
const { user, project, columns } = await fixture();
- const client = await getDatabasePool().connect();
- const {
- rows: [{ pid }],
- } = await client.query<{ pid: number }>("SELECT pg_backend_pid() AS pid");
- let deletion: Promise<unknown> | undefined;
+ const result = await importTasks(
+ project.id,
+ [{ title: "Existing", status: "done" }],
+ user.id,
+ );
+ expect(result.results.successful).toBe(1);
+ hooks.publish.mockClear();
+ await expect(deleteColumn(columns.done.id)).rejects.toMatchObject({
+ status: 409,
+ });
+ expect(
+ await db.query.columnTable.findFirst({
+ where: eq(schema.columnTable.id, columns.done.id),
+ }),
+ ).toBeDefined();
+ expect(hooks.publish).not.toHaveBeenCalled();
+ });
+
+ it("rejects a queued deletion when import commits a task into the column", async () => {
+ const { user, project, columns } = await fixture();
+ let deletion: Promise<{ value: unknown } | { error: unknown }> | undefined;
try {
hooks.beforePosition.mockImplementationOnce(async () => {
- deletion = client.query('DELETE FROM "column" WHERE id = $1', [
- columns.done.id,
- ]);
- // This independent connection must wait for the import's column lock.
+ deletion = deleteColumn(columns.done.id).then(
+ (value) => ({ value }),
+ (error: unknown) => ({ error }),
+ );
+ // The controller must wait for the import's column lock before deciding.
await vi.waitFor(
async () => {
const activity = await db.execute<{
wait_event_type: string | null;
}>(sql`
- SELECT wait_event_type FROM pg_stat_activity WHERE pid = ${pid}
- `);
+ SELECT wait_event_type FROM pg_stat_activity
+ WHERE datname = current_database()
+ AND pid <> pg_backend_pid()
+ AND wait_event_type = 'Lock'
+ AND query LIKE '%"column"%'
+ AND (query ILIKE '%delete from%' OR query ILIKE '%for update%')
+ `);
expect(activity.rows[0]?.wait_event_type).toBe("Lock");
},
{ timeout: 2000, interval: 10 },
@@ -122,7 +144,12 @@ describe("task import column deletion", () => {
);
expect(result.results.successful).toBe(1);
expect(result.results.failed).toBe(0);
- await deletion;
+ expect(await deletion).toMatchObject({ error: { status: 409 } });
+ expect(
+ await db.query.columnTable.findFirst({
+ where: eq(schema.columnTable.id, columns.done.id),
+ }),
+ ).toBeDefined();
const stored = await db
.select()
.from(schema.taskTable)
@@ -131,11 +158,24 @@ describe("task import column deletion", () => {
expect(stored[0]).toMatchObject({
title: "Concurrent",
status: "done",
- columnId: null,
+ columnId: columns.done.id,
});
+
+ mockAuthenticatedSession(user);
+ const { app } = createApp();
+ const response = await app.request(
+ `/api/task/tasks/${project.id}?status=done`,
+ );
+ expect(response.status).toBe(200);
+ const board = await response.json();
+ expect(board.pagination.total).toBe(1);
+ expect(
+ board.data.columns.find(
+ (column: { slug: string }) => column.slug === "done",
+ ),
+ ).toMatchObject({ tasks: [{ title: "Concurrent", status: "done" }] });
} finally {
- await deletion?.catch(() => {});
- client.release();
+ await deletion;
}
});
});