Block A of the EchoDo plan. Workspaces used to live as `objects(type='workspace')`,
which made it impossible to put a real RLS-friendly tenant boundary on the schema
or to give each workspace a stable URL slug. This commit:
- Adds a top-level `workspaces` table (slug unique, owner FK, plan_tier hook).
- Migrates the 8 anchor tables (objects, workspace_members, object_type_defs,
property_definitions, templates, forms, markdown_backlog_items,
cursor_sync_mappings) to FK into `workspaces.id` instead of `objects.id`,
with a hand-augmented data-copy migration that preserves IDs and slug-collision-
proofs on backfill.
- Introduces a `workspaceProcedure` tRPC middleware + `resolveWorkspace` helper
that take a UUID-or-slug `workspace` handle and expose `ctx.workspace`. All
tenant-scoped routers (objects, types, properties, templates, forms, search,
ai, relations, favorites) now flow through it.
- Updates the web app to pass `workspace` slugs from the URL (or store) instead
of the old `workspaceId`, including a workspace-sync layer that rewrites
/<UUID>/... links to /<slug>/...
- Updates the MCP tools (list_objects, create_object, search_objects) and the
workspace://{handle}/tree resource to accept either a slug or UUID so existing
agents keep working.
- Adds a Create Workspace dialog and a Workspace Settings page (rename + slug
rename with redirect, owner-only archive).
Verified locally against a fresh Postgres: migration applies cleanly, slug
uniqueness holds, tenant data is isolated by workspace_id, slug↔UUID resolution
works in both directions, and ON DELETE CASCADE cleans up child rows in the
correct workspace only.
Co-authored-by: Cursor <cursoragent@cursor.com>
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { TRPCError } from "@trpc/server";
|
|
import { and, eq } from "drizzle-orm";
|
|
import {
|
|
objects,
|
|
forms,
|
|
propertyDefinitions,
|
|
templates,
|
|
objectTypeDefs,
|
|
} from "@tasks/database/schema";
|
|
import type { db as defaultDb } from "@tasks/database";
|
|
|
|
type Db = typeof defaultDb;
|
|
|
|
/**
|
|
* Generic "this row belongs to this workspace" guard used by tenant-scoped
|
|
* routers when a mutation targets a specific row by id. Throws NOT_FOUND if the
|
|
* row either doesn't exist or lives in a different workspace, so callers can't
|
|
* use the error code to probe IDs across tenants.
|
|
*/
|
|
export async function assertRowInWorkspace<
|
|
T extends { id: typeof objects.id; workspaceId: typeof objects.workspaceId },
|
|
>(args: {
|
|
db: Db;
|
|
table: T;
|
|
rowId: string;
|
|
workspaceId: string;
|
|
notFoundMessage?: string;
|
|
}): Promise<void> {
|
|
const [row] = await args.db
|
|
.select({ id: args.table.id })
|
|
.from(args.table as any)
|
|
.where(and(eq(args.table.id, args.rowId), eq(args.table.workspaceId, args.workspaceId)))
|
|
.limit(1);
|
|
if (!row) {
|
|
throw new TRPCError({
|
|
code: "NOT_FOUND",
|
|
message: args.notFoundMessage ?? "Resource not found",
|
|
});
|
|
}
|
|
}
|
|
|
|
export const tableForms = forms;
|
|
export const tablePropertyDefs = propertyDefinitions;
|
|
export const tableTemplates = templates;
|
|
export const tableObjectTypeDefs = objectTypeDefs;
|
|
export const tableObjects = objects;
|