MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / purgeOrganizationData

Function purgeOrganizationData

apps/cloud/src/db/org-deletion.ts:42–72  ·  view source on GitHub ↗
(db: DrizzleDb, organizationId: string)

Source from the content-addressed store, hash-verified

40 * cascades to local `memberships`). Idempotent — a second run deletes nothing.
41 */
42export const purgeOrganizationData = (db: DrizzleDb, organizationId: string): Promise<void> =>
43 db.transaction(async (tx) => {
44 // Executor tenant tables — every row is scoped by `tenant = organizationId`.
45 await tx.delete(tool).where(eq(tool.tenant, organizationId));
46 await tx.delete(definition).where(eq(definition.tenant, organizationId));
47 await tx.delete(connection).where(eq(connection.tenant, organizationId));
48 await tx.delete(integration).where(eq(integration.tenant, organizationId));
49 await tx.delete(oauth_client).where(eq(oauth_client.tenant, organizationId));
50 await tx.delete(oauth_session).where(eq(oauth_session.tenant, organizationId));
51 await tx.delete(tool_policy).where(eq(tool_policy.tenant, organizationId));
52 await tx.delete(plugin_storage).where(eq(plugin_storage.tenant, organizationId));
53 await tx.delete(subject).where(eq(subject.tenant, organizationId));
54 await tx.delete(artifact).where(eq(artifact.tenant, organizationId));
55
56 // Secrets, OAuth tokens, and cached specs live in `blob`, namespaced by
57 // owner: `o:<org>/<plugin>` (org scope) and `u:<org>:<subject>/<plugin>`
58 // (per-user scope). Match both prefixes for this org.
59 const esc = escapeLike(organizationId);
60 await tx
61 .delete(blob)
62 .where(
63 or(
64 sql`${blob.namespace} LIKE ${`o:${esc}/%`} ESCAPE '\\'`,
65 sql`${blob.namespace} LIKE ${`u:${esc}:%`} ESCAPE '\\'`,
66 ),
67 );
68
69 // Identity mirror — FK `ON DELETE CASCADE` removes local memberships too.
70 // `accounts` are intentionally left: a user may belong to other orgs.
71 await tx.delete(organizations).where(eq(organizations.id, organizationId));
72 });

Callers 1

makeUserStoreFunction · 0.90

Calls 3

escapeLikeFunction · 0.85
transactionMethod · 0.65
deleteMethod · 0.65

Tested by

no test coverage detected