ubiquitous-invention/packages/database/src/schema/markdown_backlog.ts
Randall Stillwell 336a5890a8 feat(audit): append-only audit_log, workspace archive cascade + restore, audit view
Soft-delete cascade was the missing half of archive: stamping
workspaces.archived_at alone left objects visible to anyone with a
direct id. The cascade runs in one transaction so the partial state
isn't reachable, and restore inverts it for any archived row in the
workspace — provenance-blind on purpose until we have a use case
that needs to distinguish per-workspace from per-object archives.

audit_log keeps the keyset index on (workspace_id, created_at) and
the actor_user_id FK with onDelete set null. recordAudit() refuses
to write a null actor without a metadata.system_actor label so the
audit view always has something to render. workspaces and invites
mutations call recordAudit on success; objects-router instrumentation
and the markdown importer's system-actor flow are filed as P2
follow-ups because each needs a thoughtful "what's audit-worthy?"
pass, not mechanical wiring.

Settings → Audit log lives at /<slug>/settings/audit, owner-gated,
keyset-paginated. ACTION_LABELS is small on purpose; new actions
fall back to their raw key so missing a label degrades gracefully.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-02 15:35:16 -05:00

64 lines
2.3 KiB
TypeScript

import {
pgTable,
uuid,
varchar,
text,
jsonb,
timestamp,
index,
uniqueIndex,
foreignKey,
} from "drizzle-orm/pg-core";
import { workspaces } from "./workspaces";
/**
* Imported plan / epic / task rows sourced from repo markdown under `plans/`.
* Scoped by workspace (tenant boundary in this app).
*/
export const markdownBacklogItems = pgTable(
"markdown_backlog_items",
{
id: uuid("id").primaryKey().defaultRandom(),
workspaceId: uuid("workspace_id")
.notNull()
.references(() => workspaces.id, { onDelete: "cascade" }),
kind: varchar("kind", { length: 20 }).notNull(),
slug: varchar("slug", { length: 200 }).notNull(),
planSlug: varchar("plan_slug", { length: 200 }).notNull(),
epicSlug: varchar("epic_slug", { length: 200 }),
parentId: uuid("parent_id"),
repoPath: text("repo_path").notNull(),
title: varchar("title", { length: 500 }).notNull().default(""),
status: varchar("status", { length: 50 }),
priority: varchar("priority", { length: 20 }),
owner: text("owner"),
frontmatter: jsonb("frontmatter").$type<Record<string, unknown> | null>(),
bodyMarkdown: text("body_markdown").notNull().default(""),
contentHash: varchar("content_hash", { length: 64 }).notNull(),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
// Soft-delete column. Active rows have `archived_at IS NULL`; the
// workspace-archive cascade stamps this to match `workspaces.archived_at`
// so backlog items follow their parent workspace's lifecycle.
archivedAt: timestamp("archived_at", { withTimezone: true }),
},
(table) => ({
parentFk: foreignKey({
columns: [table.parentId],
foreignColumns: [table.id],
}).onDelete("set null"),
workspaceRepoUnique: uniqueIndex("markdown_backlog_workspace_repo_path_unique").on(
table.workspaceId,
table.repoPath,
),
workspacePlanIdx: index("markdown_backlog_workspace_plan_idx").on(
table.workspaceId,
table.planSlug,
),
parentIdIdx: index("markdown_backlog_parent_id_idx").on(table.parentId),
workspaceKindIdx: index("markdown_backlog_workspace_kind_idx").on(
table.workspaceId,
table.kind,
),
}),
);