@storyshelf/db-mysql 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -1
- package/dist/baseline-preparation.mjs +154 -0
- package/dist/baseline-preparation.mjs.map +1 -0
- package/dist/build-deletion-steps.mjs +98 -0
- package/dist/build-deletion-steps.mjs.map +1 -0
- package/dist/build-reference-lifecycle.mjs +157 -0
- package/dist/build-reference-lifecycle.mjs.map +1 -0
- package/dist/content-uses.mjs +192 -0
- package/dist/content-uses.mjs.map +1 -0
- package/dist/ddl.d.mts +1 -1
- package/dist/ddl.mjs +80 -0
- package/dist/ddl.mjs.map +1 -1
- package/dist/drizzle-factory-mysql.mjs +6 -2
- package/dist/drizzle-factory-mysql.mjs.map +1 -1
- package/dist/foreign-keys.mjs +57 -0
- package/dist/foreign-keys.mjs.map +1 -1
- package/dist/index.d.mts +9 -1
- package/dist/index.mjs +36 -10
- package/dist/index.mjs.map +1 -1
- package/dist/lifecycle-transaction.mjs +105 -0
- package/dist/lifecycle-transaction.mjs.map +1 -0
- package/dist/migrate.mjs +41 -1
- package/dist/migrate.mjs.map +1 -1
- package/dist/planetscale.mjs +2 -2
- package/dist/planetscale.mjs.map +1 -1
- package/dist/retention-activation.mjs +127 -0
- package/dist/retention-activation.mjs.map +1 -0
- package/dist/schema/build.d.mts +73 -2
- package/dist/schema/build.mjs +8 -1
- package/dist/schema/build.mjs.map +1 -1
- package/dist/schema/git-connection.d.mts +505 -0
- package/dist/schema/git-connection.mjs +66 -0
- package/dist/schema/git-connection.mjs.map +1 -0
- package/dist/schema/project.d.mts +18 -0
- package/dist/schema/project.mjs +1 -0
- package/dist/schema/project.mjs.map +1 -1
- package/dist/schema.d.mts +4 -1
- package/dist/schema.mjs +5 -2
- package/dist/schema.mjs.map +1 -1
- package/dist/tidb.mjs +2 -2
- package/dist/tidb.mjs.map +1 -1
- package/package.json +3 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"drizzle-factory-mysql.mjs","names":[],"sources":["../src/drizzle-factory-mysql.ts"],"sourcesContent":["import type {\n DatabaseAdapter,\n DrizzleAdapterOptions,\n ListOptions,\n TxStore,\n} from \"@storyshelf/core/adapter/database\";\nimport { listSelection } from \"@storyshelf/core/adapter/database\";\nimport type { AdapterLifecycle } from \"@storyshelf/core/adapter/metadata\";\nimport type { SQL } from \"drizzle-orm\";\nimport type { SQLWrapper } from \"drizzle-orm\";\nimport type { Table } from \"drizzle-orm\";\nimport { and, eq, getTableColumns } from \"drizzle-orm\";\nimport { applyListOptions, buildLifecycle } from \"./drizzle-factory.ts\";\n\n/** Chainable select surface of the MySQL Drizzle dialect (directly awaitable). */\ninterface DrizzleMySqlSelectChain extends Promise<unknown[]> {\n where(where: SQL): DrizzleMySqlSelectChain;\n orderBy(order: SQL): DrizzleMySqlSelectChain;\n limit(n: number): DrizzleMySqlSelectChain;\n offset(n: number): DrizzleMySqlSelectChain;\n}\n\n/**\n * Minimal MySQL Drizzle surface consumed by {@link createDrizzleMySqlAdapter}.\n * Drivers pass their dialect instance as `unknown`; the cast lives here, once.\n */\ninterface DrizzleMySqlLike {\n insert(table: Table): {\n values(values: unknown): { returning(): Promise<unknown[]> };\n };\n update(table: Table): {\n set(values: unknown): { where(where: SQL): { returning(): Promise<unknown[]> } };\n };\n select(fields?: Record<string, unknown>): { from(table: Table): DrizzleMySqlSelectChain };\n delete(table: Table): { where(where: SQL): Promise<unknown> };\n $count(table: Table, where?: SQL): Promise<number>;\n execute<T>(query: SQL): Promise<T>;\n transaction<R>(fn: (tx: unknown) => Promise<R>): Promise<R>;\n}\n\nfunction idColumn(table: Table): SQLWrapper {\n const id = getTableColumns(table)[\"id\"];\n if (!id) {\n throw new Error(\"Table has no id column\");\n }\n return id;\n}\n\nfunction firstRow(rows: unknown[], what: string): unknown {\n const [row] = rows;\n if (row === undefined) {\n throw new Error(`${what} returned no rows`);\n }\n return row;\n}\n\nasync function updateWhereOne<T extends Table>(\n drizzle: DrizzleMySqlLike,\n table: T,\n id: string,\n values: Partial<T[\"$inferInsert\"]>,\n where: SQL,\n): Promise<T[\"$inferSelect\"] | null> {\n // MySQL has no UPDATE RETURNING. The condition is evaluated by the UPDATE;\n // reading afterwards may observe a newer state, but cannot undo cancellation.\n // oxlint-disable-next-line typescript/await-thenable -- MySQL updates are thenable via drizzle\n await drizzle\n .update(table)\n .set(values)\n .where(and(eq(idColumn(table), id), where) ?? where);\n const current = (await drizzle\n .select()\n .from(table)\n .where(eq(idColumn(table), id))\n .limit(1)) as T[\"$inferSelect\"][];\n return current[0] ?? null;\n}\n\nasync function insertOne<T extends Table>(\n drizzle: DrizzleMySqlLike,\n table: T,\n values: T[\"$inferInsert\"],\n): Promise<T[\"$inferSelect\"]> {\n try {\n const rows = await drizzle.insert(table).values(values).returning();\n return firstRow(rows, \"Insert\") as T[\"$inferSelect\"];\n } catch {\n // oxlint-disable-next-line typescript/await-thenable -- MySQL insert is thenable via drizzle\n await drizzle.insert(table).values(values);\n const id = (values as Record<string, unknown>)[\"id\"] as string;\n const where = eq(idColumn(table), id);\n const rows = (await drizzle.select().from(table).where(where).limit(1)) as T[\"$inferSelect\"][];\n return firstRow(rows, \"Insert\") as T[\"$inferSelect\"];\n }\n}\n\nasync function updateOne<T extends Table>(\n drizzle: DrizzleMySqlLike,\n table: T,\n id: string,\n values: Partial<T[\"$inferInsert\"]>,\n): Promise<T[\"$inferSelect\"]> {\n const where = eq(idColumn(table), id);\n try {\n const rows = await drizzle.update(table).set(values).where(where).returning();\n return firstRow(rows, \"Update\") as T[\"$inferSelect\"];\n } catch {\n // oxlint-disable-next-line typescript/await-thenable -- MySQL update is thenable via drizzle\n await drizzle.update(table).set(values).where(where);\n const rows = (await drizzle.select().from(table).where(where).limit(1)) as T[\"$inferSelect\"][];\n return firstRow(rows, \"Update\") as T[\"$inferSelect\"];\n }\n}\n\n/** mysql2 resolves `execute` to `[rows, fields]`; adapters expose just the rows. */\nasync function allRows<T>(drizzle: DrizzleMySqlLike, query: SQL): Promise<T[]> {\n const result: unknown = await drizzle.execute(query);\n if (Array.isArray(result) && Array.isArray(result[0]) && result.length === 2) {\n return result[0] as T[];\n }\n return result as T[];\n}\n\n/**\n * Build a {@link DatabaseAdapter} over any Postgres-compatible Drizzle\n * dialect. Mirrors `createDrizzleAdapter` with Postgres terminal shapes\n * (chains await directly; `returning()` yields arrays).\n */\nexport function createDrizzleMySqlAdapter(\n db: unknown,\n options: DrizzleAdapterOptions,\n): DatabaseAdapter {\n const drizzle = db as DrizzleMySqlLike;\n const lifecycle: AdapterLifecycle = buildLifecycle(options);\n return {\n metadata: options.metadata,\n tables: options.tables,\n lifecycle,\n insert: async <T extends Table>(\n table: T,\n values: T[\"$inferInsert\"],\n ): Promise<T[\"$inferSelect\"]> => await insertOne(drizzle, table, values),\n update: async <T extends Table>(\n table: T,\n id: string,\n values: Partial<T[\"$inferInsert\"]>,\n ): Promise<T[\"$inferSelect\"]> => await updateOne(drizzle, table, id, values),\n updateWhere: async <T extends Table>(\n table: T,\n id: string,\n values: Partial<T[\"$inferInsert\"]>,\n where: SQL,\n ): Promise<T[\"$inferSelect\"] | null> => await updateWhereOne(drizzle, table, id, values, where),\n get: async <T extends Table>(table: T, id: string): Promise<T[\"$inferSelect\"] | null> =>\n await getOne(drizzle, table, id),\n remove: async (table: Table, id: string): Promise<void> => {\n const where = eq(idColumn(table), id);\n await drizzle.delete(table).where(where);\n },\n list: async <T extends Table>(\n table: T,\n opts: ListOptions = {},\n ): Promise<T[\"$inferSelect\"][]> => {\n const rows: unknown = await applyListOptions(\n drizzle.select(listSelection(table, opts.columns)).from(table),\n opts,\n idColumn(table),\n );\n return rows as T[\"$inferSelect\"][];\n },\n count: async (table: Table, where?: SQL): Promise<number> => await drizzle.$count(table, where),\n all: async <T>(query: SQL): Promise<T[]> => await allRows<T>(drizzle, query),\n transact: async <R>(fn: (tx: TxStore) => Promise<R>): Promise<R> =>\n await drizzle.transaction(async (tx) => await fn(buildTxStore(tx as DrizzleMySqlLike))),\n };\n}\n\n/** CRUD subset bound to a Postgres transaction handle. */\nfunction buildTxStore(drizzle: DrizzleMySqlLike): TxStore {\n return {\n insert: async (table, values) => await insertOne(drizzle, table, values as never),\n update: async (table, id, values) => await updateOne(drizzle, table, id, values as never),\n get: async (table, id) => await getOne(drizzle, table, id),\n remove: async (table, id) => {\n const where = eq(idColumn(table), id);\n await drizzle.delete(table).where(where);\n },\n list: async (table, opts: ListOptions = {}) => {\n const rows: unknown = await applyListOptions(\n drizzle.select(listSelection(table, opts.columns)).from(table),\n opts,\n idColumn(table),\n );\n return rows as never[];\n },\n count: async (table, where) => await drizzle.$count(table, where),\n };\n}\n\nasync function getOne<T extends Table>(\n drizzle: DrizzleMySqlLike,\n table: T,\n id: string,\n): Promise<T[\"$inferSelect\"] | null> {\n const where = eq(idColumn(table), id);\n const rows = (await drizzle.select().from(table).where(where).limit(1)) as T[\"$inferSelect\"][];\n return rows[0] ?? null;\n}\n"],"mappings":";;;;;;;AAwCA,SAAS,SAAS,OAA0B;CAC1C,MAAM,KAAK,gBAAgB,KAAK,CAAC,CAAC;CAClC,IAAI,CAAC,IACH,MAAM,IAAI,MAAM,wBAAwB;CAE1C,OAAO;AACT;AAEA,SAAS,SAAS,MAAiB,MAAuB;CACxD,MAAM,CAAC,OAAO;CACd,IAAI,QAAQ,KAAA,GACV,MAAM,IAAI,MAAM,GAAG,KAAK,kBAAkB;CAE5C,OAAO;AACT;AAEA,eAAe,eACb,SACA,OACA,IACA,QACA,OACmC;CAInC,MAAM,QACH,OAAO,KAAK,CAAC,CACb,IAAI,MAAM,CAAC,CACX,MAAM,IAAI,GAAG,SAAS,KAAK,GAAG,EAAE,GAAG,KAAK,KAAK,KAAK;CAMrD,QAAO,MALgB,QACpB,OAAO,CAAC,CACR,KAAK,KAAK,CAAC,CACX,MAAM,GAAG,SAAS,KAAK,GAAG,EAAE,CAAC,CAAC,CAC9B,MAAM,CAAC,EAAA,CACK,MAAM;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iBAC1B,QAAQ,OAAO,cAAc,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,KAAK,GAC7D,MACA,SAAS,KAAK,CAChB;EAEF;EACA,OAAO,OAAO,OAAO,UAAU,MAAM,QAAQ,OAAO,OAAO,KAAK;CAClE;AACF;AAEA,eAAe,OACb,SACA,OACA,IACmC;CACnC,MAAM,QAAQ,GAAG,SAAS,KAAK,GAAG,EAAE;CAEpC,QAAO,MADa,QAAQ,OAAO,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,CAAC,EAAA,CACzD,MAAM;AACpB"}
|
|
1
|
+
{"version":3,"file":"drizzle-factory-mysql.mjs","names":[],"sources":["../src/drizzle-factory-mysql.ts"],"sourcesContent":["import type {\n DatabaseAdapter,\n DrizzleAdapterOptions,\n ListOptions,\n TxStore,\n} from \"@storyshelf/core/adapter/database\";\nimport { listSelection } from \"@storyshelf/core/adapter/database\";\nimport type { AdapterLifecycle } from \"@storyshelf/core/adapter/metadata\";\nimport type { SQL } from \"drizzle-orm\";\nimport type { SQLWrapper } from \"drizzle-orm\";\nimport type { Table } from \"drizzle-orm\";\nimport { and, eq, getTableColumns } from \"drizzle-orm\";\nimport { applyListOptions, buildLifecycle } from \"./drizzle-factory.ts\";\n\n/** Chainable select surface of the MySQL Drizzle dialect (directly awaitable). */\ninterface DrizzleMySqlSelectChain extends Promise<unknown[]> {\n where(where: SQL): DrizzleMySqlSelectChain;\n orderBy(order: SQL): DrizzleMySqlSelectChain;\n limit(n: number): DrizzleMySqlSelectChain;\n offset(n: number): DrizzleMySqlSelectChain;\n}\n\n/**\n * Minimal MySQL Drizzle surface consumed by {@link createDrizzleMySqlAdapter}.\n * Drivers pass their dialect instance as `unknown`; the cast lives here, once.\n */\ninterface DrizzleMySqlLike {\n insert(table: Table): {\n values(values: unknown): { returning(): Promise<unknown[]> };\n };\n update(table: Table): {\n set(values: unknown): { where(where: SQL): { returning(): Promise<unknown[]> } };\n };\n select(fields?: Record<string, unknown>): { from(table: Table): DrizzleMySqlSelectChain };\n delete(table: Table): { where(where: SQL): Promise<unknown> };\n $count(table: Table, where?: SQL): Promise<number>;\n execute<T>(query: SQL): Promise<T>;\n transaction<R>(fn: (tx: unknown) => Promise<R>): Promise<R>;\n}\n\nfunction idColumn(table: Table): SQLWrapper {\n const id = getTableColumns(table)[\"id\"];\n if (!id) {\n throw new Error(\"Table has no id column\");\n }\n return id;\n}\n\nfunction firstRow(rows: unknown[], what: string): unknown {\n const [row] = rows;\n if (row === undefined) {\n throw new Error(`${what} returned no rows`);\n }\n return row;\n}\n\nasync function updateWhereOne<T extends Table>(\n drizzle: DrizzleMySqlLike,\n table: T,\n id: string,\n values: Partial<T[\"$inferInsert\"]>,\n where: SQL,\n): Promise<T[\"$inferSelect\"] | null> {\n // MySQL has no UPDATE RETURNING. The condition is evaluated by the UPDATE;\n // reading afterwards may observe a newer state, but cannot undo cancellation.\n // oxlint-disable-next-line typescript/await-thenable -- MySQL updates are thenable via drizzle\n await drizzle\n .update(table)\n .set(values)\n .where(and(eq(idColumn(table), id), where) ?? where);\n const current = (await drizzle\n .select()\n .from(table)\n .where(eq(idColumn(table), id))\n .limit(1)) as T[\"$inferSelect\"][];\n return current[0] ?? null;\n}\n\nasync function insertOne<T extends Table>(\n drizzle: DrizzleMySqlLike,\n table: T,\n values: T[\"$inferInsert\"],\n): Promise<T[\"$inferSelect\"]> {\n try {\n const rows = await drizzle.insert(table).values(values).returning();\n return firstRow(rows, \"Insert\") as T[\"$inferSelect\"];\n } catch {\n // oxlint-disable-next-line typescript/await-thenable -- MySQL insert is thenable via drizzle\n await drizzle.insert(table).values(values);\n const id = (values as Record<string, unknown>)[\"id\"] as string;\n const where = eq(idColumn(table), id);\n const rows = (await drizzle.select().from(table).where(where).limit(1)) as T[\"$inferSelect\"][];\n return firstRow(rows, \"Insert\") as T[\"$inferSelect\"];\n }\n}\n\nasync function updateOne<T extends Table>(\n drizzle: DrizzleMySqlLike,\n table: T,\n id: string,\n values: Partial<T[\"$inferInsert\"]>,\n): Promise<T[\"$inferSelect\"]> {\n const where = eq(idColumn(table), id);\n try {\n const rows = await drizzle.update(table).set(values).where(where).returning();\n return firstRow(rows, \"Update\") as T[\"$inferSelect\"];\n } catch {\n // oxlint-disable-next-line typescript/await-thenable -- MySQL update is thenable via drizzle\n await drizzle.update(table).set(values).where(where);\n const rows = (await drizzle.select().from(table).where(where).limit(1)) as T[\"$inferSelect\"][];\n return firstRow(rows, \"Update\") as T[\"$inferSelect\"];\n }\n}\n\n/** mysql2 resolves `execute` to `[rows, fields]`; adapters expose just the rows. */\nasync function allRows<T>(drizzle: DrizzleMySqlLike, query: SQL): Promise<T[]> {\n const result: unknown = await drizzle.execute(query);\n if (Array.isArray(result) && Array.isArray(result[0]) && result.length === 2) {\n return result[0] as T[];\n }\n return result as T[];\n}\n\n/**\n * Build a {@link DatabaseAdapter} over any Postgres-compatible Drizzle\n * dialect. Mirrors `createDrizzleAdapter` with Postgres terminal shapes\n * (chains await directly; `returning()` yields arrays).\n */\nexport function createDrizzleMySqlAdapter(\n db: unknown,\n options: DrizzleAdapterOptions,\n): DatabaseAdapter {\n const drizzle = db as DrizzleMySqlLike;\n const lifecycle: AdapterLifecycle = buildLifecycle(options);\n return {\n metadata: options.metadata,\n tables: options.tables,\n lifecycle,\n insert: async <T extends Table>(\n table: T,\n values: T[\"$inferInsert\"],\n ): Promise<T[\"$inferSelect\"]> => await insertOne(drizzle, table, values),\n update: async <T extends Table>(\n table: T,\n id: string,\n values: Partial<T[\"$inferInsert\"]>,\n ): Promise<T[\"$inferSelect\"]> => await updateOne(drizzle, table, id, values),\n updateWhere: async <T extends Table>(\n table: T,\n id: string,\n values: Partial<T[\"$inferInsert\"]>,\n where: SQL,\n ): Promise<T[\"$inferSelect\"] | null> => await updateWhereOne(drizzle, table, id, values, where),\n get: async <T extends Table>(table: T, id: string): Promise<T[\"$inferSelect\"] | null> =>\n await getOne(drizzle, table, id),\n remove: async (table: Table, id: string): Promise<void> => {\n const where = eq(idColumn(table), id);\n await drizzle.delete(table).where(where);\n },\n list: async <T extends Table>(\n table: T,\n opts: ListOptions = {},\n ): Promise<T[\"$inferSelect\"][]> => {\n const rows: unknown = await applyListOptions(\n drizzle.select(listSelection(table, opts.columns)).from(table),\n opts,\n idColumn(table),\n );\n return rows as T[\"$inferSelect\"][];\n },\n count: async (table: Table, where?: SQL): Promise<number> => await drizzle.$count(table, where),\n all: async <T>(query: SQL): Promise<T[]> => await allRows<T>(drizzle, query),\n transact: async <R>(fn: (tx: TxStore) => Promise<R>): Promise<R> =>\n await drizzle.transaction(async (tx) => await fn(buildTxStore(tx as DrizzleMySqlLike))),\n };\n}\n\n/** The {@link TxStore} of a Drizzle MySQL instance bound to one connection, for the lifecycle's own transactions. */\nexport function transactionStore(db: unknown): TxStore {\n return buildTxStore(db as DrizzleMySqlLike);\n}\n\n/** CRUD subset bound to a MySQL transaction handle. */\nfunction buildTxStore(drizzle: DrizzleMySqlLike): TxStore {\n return {\n insert: async (table, values) => await insertOne(drizzle, table, values as never),\n update: async (table, id, values) => await updateOne(drizzle, table, id, values as never),\n get: async (table, id) => await getOne(drizzle, table, id),\n remove: async (table, id) => {\n const where = eq(idColumn(table), id);\n await drizzle.delete(table).where(where);\n },\n list: async (table, opts: ListOptions = {}) => {\n const rows: unknown = await applyListOptions(\n drizzle.select(listSelection(table, opts.columns)).from(table),\n opts,\n idColumn(table),\n );\n return rows as never[];\n },\n count: async (table, where) => await drizzle.$count(table, where),\n };\n}\n\nasync function getOne<T extends Table>(\n drizzle: DrizzleMySqlLike,\n table: T,\n id: string,\n): Promise<T[\"$inferSelect\"] | null> {\n const where = eq(idColumn(table), id);\n const rows = (await drizzle.select().from(table).where(where).limit(1)) as T[\"$inferSelect\"][];\n return rows[0] ?? null;\n}\n"],"mappings":";;;;;;;AAwCA,SAAS,SAAS,OAA0B;CAC1C,MAAM,KAAK,gBAAgB,KAAK,CAAC,CAAC;CAClC,IAAI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,MAAM,QAAQ,YAAY,OAAO,OAAO,MAAM,GAAG,aAAa,EAAsB,CAAC,CAAC;CAC1F;AACF;;AAGA,SAAgB,iBAAiB,IAAsB;CACrD,OAAO,aAAa,EAAsB;AAC5C;;AAGA,SAAS,aAAa,SAAoC;CACxD,OAAO;EACL,QAAQ,OAAO,OAAO,WAAW,MAAM,UAAU,SAAS,OAAO,MAAe;EAChF,QAAQ,OAAO,OAAO,IAAI,WAAW,MAAM,UAAU,SAAS,OAAO,IAAI,MAAe;EACxF,KAAK,OAAO,OAAO,OAAO,MAAM,OAAO,SAAS,OAAO,EAAE;EACzD,QAAQ,OAAO,OAAO,OAAO;GAC3B,MAAM,QAAQ,GAAG,SAAS,KAAK,GAAG,EAAE;GACpC,MAAM,QAAQ,OAAO,KAAK,CAAC,CAAC,MAAM,KAAK;EACzC;EACA,MAAM,OAAO,OAAO,OAAoB,CAAC,MAAM;GAM7C,OAAO,MALqB,iBAC1B,QAAQ,OAAO,cAAc,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,KAAK,GAC7D,MACA,SAAS,KAAK,CAChB;EAEF;EACA,OAAO,OAAO,OAAO,UAAU,MAAM,QAAQ,OAAO,OAAO,KAAK;CAClE;AACF;AAEA,eAAe,OACb,SACA,OACA,IACmC;CACnC,MAAM,QAAQ,GAAG,SAAS,KAAK,GAAG,EAAE;CAEpC,QAAO,MADa,QAAQ,OAAO,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,CAAC,EAAA,CACzD,MAAM;AACpB"}
|
package/dist/foreign-keys.mjs
CHANGED
|
@@ -10,6 +10,49 @@ const FOREIGN_KEYS = [
|
|
|
10
10
|
parent: "projects",
|
|
11
11
|
parentColumn: "id"
|
|
12
12
|
},
|
|
13
|
+
{
|
|
14
|
+
table: "build_references",
|
|
15
|
+
name: "fk_build_references_project_id",
|
|
16
|
+
column: "project_id",
|
|
17
|
+
parent: "projects",
|
|
18
|
+
parentColumn: "id"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
table: "build_references",
|
|
22
|
+
name: "fk_build_references_build_id",
|
|
23
|
+
column: "build_id",
|
|
24
|
+
parent: "builds",
|
|
25
|
+
parentColumn: "id",
|
|
26
|
+
onDelete: "RESTRICT"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
table: "build_reference_generations",
|
|
30
|
+
name: "fk_build_reference_generations_project_id",
|
|
31
|
+
column: "project_id",
|
|
32
|
+
parent: "projects",
|
|
33
|
+
parentColumn: "id"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
table: "build_deletions",
|
|
37
|
+
name: "fk_build_deletions_build_id",
|
|
38
|
+
column: "build_id",
|
|
39
|
+
parent: "builds",
|
|
40
|
+
parentColumn: "id"
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
table: "build_deletions",
|
|
44
|
+
name: "fk_build_deletions_project_id",
|
|
45
|
+
column: "project_id",
|
|
46
|
+
parent: "projects",
|
|
47
|
+
parentColumn: "id"
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
table: "build_content_releases",
|
|
51
|
+
name: "fk_build_content_releases_build_id",
|
|
52
|
+
column: "build_id",
|
|
53
|
+
parent: "builds",
|
|
54
|
+
parentColumn: "id"
|
|
55
|
+
},
|
|
13
56
|
{
|
|
14
57
|
table: "capture_attempts",
|
|
15
58
|
name: "fk_capture_attempts_project_id",
|
|
@@ -199,6 +242,20 @@ const FOREIGN_KEYS = [
|
|
|
199
242
|
parent: "projects",
|
|
200
243
|
parentColumn: "id"
|
|
201
244
|
},
|
|
245
|
+
{
|
|
246
|
+
table: "git_repository_links",
|
|
247
|
+
name: "fk_git_repository_links_project_id",
|
|
248
|
+
column: "project_id",
|
|
249
|
+
parent: "projects",
|
|
250
|
+
parentColumn: "id"
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
table: "git_repository_links",
|
|
254
|
+
name: "fk_git_repository_links_connection_id",
|
|
255
|
+
column: "connection_id",
|
|
256
|
+
parent: "git_connections",
|
|
257
|
+
parentColumn: "id"
|
|
258
|
+
},
|
|
202
259
|
{
|
|
203
260
|
table: "build_package_usage",
|
|
204
261
|
name: "fk_build_package_usage_project_id",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"foreign-keys.mjs","names":[],"sources":["../src/foreign-keys.ts"],"sourcesContent":["/** Foreign keys added to volumes created before MySQL enforced them. */\nexport interface ForeignKeyMigration {\n table: string;\n name: string;\n column: string;\n parent: string;\n parentColumn: string;\n}\n\nexport const FOREIGN_KEYS: ForeignKeyMigration[] = [\n {\n table: \"builds\",\n name: \"fk_builds_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_attempts\",\n name: \"fk_capture_attempts_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_attempts\",\n name: \"fk_capture_attempts_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_shards\",\n name: \"fk_capture_shards_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_shards\",\n name: \"fk_capture_shards_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"build_components\",\n name: \"fk_build_components_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_components\",\n name: \"fk_build_components_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_logs\",\n name: \"fk_capture_logs_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_logs\",\n name: \"fk_capture_logs_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_logs\",\n name: \"fk_capture_logs_attempt_id\",\n column: \"attempt_id\",\n parent: \"capture_attempts\",\n parentColumn: \"id\",\n },\n {\n table: \"snapshots\",\n name: \"fk_snapshots_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"snapshots\",\n name: \"fk_snapshots_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"baselines\",\n name: \"fk_baselines_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"user_connections\",\n name: \"fk_user_connections_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"comments\",\n name: \"fk_comments_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"comments\",\n name: \"fk_comments_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"comments\",\n name: \"fk_comments_snapshot_id\",\n column: \"snapshot_id\",\n parent: \"snapshots\",\n parentColumn: \"id\",\n },\n {\n table: \"comments\",\n name: \"fk_comments_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"build_reviews\",\n name: \"fk_build_reviews_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"build_reviews\",\n name: \"fk_build_reviews_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"label_types\",\n name: \"fk_label_types_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_labels\",\n name: \"fk_build_labels_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_labels\",\n name: \"fk_build_labels_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"project_links\",\n name: \"fk_project_links_downstream_id\",\n column: \"downstream_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"project_links\",\n name: \"fk_project_links_upstream_id\",\n column: \"upstream_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"design_links\",\n name: \"fk_design_links_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"design_credentials\",\n name: \"fk_design_credentials_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"design_nodes\",\n name: \"fk_design_nodes_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_package_usage\",\n name: \"fk_build_package_usage_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_package_usage\",\n name: \"fk_build_package_usage_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"tokens\",\n name: \"fk_tokens_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"tokens\",\n name: \"fk_tokens_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"webhooks\",\n name: \"fk_webhooks_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"notification_channels\",\n name: \"fk_notification_channels_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"notification_subscriptions\",\n name: \"fk_notification_subscriptions_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"notification_subscriptions\",\n name: \"fk_notification_subscriptions_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"insights\",\n name: \"fk_insights_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"insights\",\n name: \"fk_insights_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"ai_usage\",\n name: \"fk_ai_usage_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"user_invite_tokens\",\n name: \"fk_user_invite_tokens_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"project_members\",\n name: \"fk_project_members_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"project_members\",\n name: \"fk_project_members_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"project_group_mappings\",\n name: \"fk_project_group_mappings_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"session\",\n name: \"fk_session_user_id\",\n column: \"user_id\",\n parent: \"user\",\n parentColumn: \"id\",\n },\n {\n table: \"account\",\n name: \"fk_account_user_id\",\n column: \"user_id\",\n parent: \"user\",\n parentColumn: \"id\",\n },\n {\n table: \"passkey\",\n name: \"fk_passkey_user_id\",\n column: \"user_id\",\n parent: \"user\",\n parentColumn: \"id\",\n },\n {\n table: \"ssoProvider\",\n name: \"fk_ssoProvider_user_id\",\n column: \"user_id\",\n parent: \"user\",\n parentColumn: \"id\",\n },\n];\n"],"mappings":";;;;AASA,MAAa,eAAsC;CACjD;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CAC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R,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;AACF"}
|
|
1
|
+
{"version":3,"file":"foreign-keys.mjs","names":[],"sources":["../src/foreign-keys.ts"],"sourcesContent":["/** Foreign keys added to volumes created before MySQL enforced them. */\nexport interface ForeignKeyMigration {\n table: string;\n name: string;\n column: string;\n parent: string;\n parentColumn: string;\n /** What deleting the parent does; `CASCADE` unless the child must keep the parent alive. */\n onDelete?: \"CASCADE\" | \"RESTRICT\";\n}\n\nexport const FOREIGN_KEYS: ForeignKeyMigration[] = [\n {\n table: \"builds\",\n name: \"fk_builds_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_references\",\n name: \"fk_build_references_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n // A pin keeps its build: the build row cannot go while a reference names it (ADR 0039).\n table: \"build_references\",\n name: \"fk_build_references_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n onDelete: \"RESTRICT\",\n },\n {\n table: \"build_reference_generations\",\n name: \"fk_build_reference_generations_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_deletions\",\n name: \"fk_build_deletions_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"build_deletions\",\n name: \"fk_build_deletions_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_content_releases\",\n name: \"fk_build_content_releases_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_attempts\",\n name: \"fk_capture_attempts_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_attempts\",\n name: \"fk_capture_attempts_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_shards\",\n name: \"fk_capture_shards_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_shards\",\n name: \"fk_capture_shards_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"build_components\",\n name: \"fk_build_components_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_components\",\n name: \"fk_build_components_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_logs\",\n name: \"fk_capture_logs_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_logs\",\n name: \"fk_capture_logs_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"capture_logs\",\n name: \"fk_capture_logs_attempt_id\",\n column: \"attempt_id\",\n parent: \"capture_attempts\",\n parentColumn: \"id\",\n },\n {\n table: \"snapshots\",\n name: \"fk_snapshots_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"snapshots\",\n name: \"fk_snapshots_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"baselines\",\n name: \"fk_baselines_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"user_connections\",\n name: \"fk_user_connections_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"comments\",\n name: \"fk_comments_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"comments\",\n name: \"fk_comments_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"comments\",\n name: \"fk_comments_snapshot_id\",\n column: \"snapshot_id\",\n parent: \"snapshots\",\n parentColumn: \"id\",\n },\n {\n table: \"comments\",\n name: \"fk_comments_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"build_reviews\",\n name: \"fk_build_reviews_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"build_reviews\",\n name: \"fk_build_reviews_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"label_types\",\n name: \"fk_label_types_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_labels\",\n name: \"fk_build_labels_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_labels\",\n name: \"fk_build_labels_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"project_links\",\n name: \"fk_project_links_downstream_id\",\n column: \"downstream_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"project_links\",\n name: \"fk_project_links_upstream_id\",\n column: \"upstream_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"design_links\",\n name: \"fk_design_links_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"design_credentials\",\n name: \"fk_design_credentials_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"design_nodes\",\n name: \"fk_design_nodes_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"git_repository_links\",\n name: \"fk_git_repository_links_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"git_repository_links\",\n name: \"fk_git_repository_links_connection_id\",\n column: \"connection_id\",\n parent: \"git_connections\",\n parentColumn: \"id\",\n },\n {\n table: \"build_package_usage\",\n name: \"fk_build_package_usage_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"build_package_usage\",\n name: \"fk_build_package_usage_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"tokens\",\n name: \"fk_tokens_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"tokens\",\n name: \"fk_tokens_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"webhooks\",\n name: \"fk_webhooks_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"notification_channels\",\n name: \"fk_notification_channels_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"notification_subscriptions\",\n name: \"fk_notification_subscriptions_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"notification_subscriptions\",\n name: \"fk_notification_subscriptions_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"insights\",\n name: \"fk_insights_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"insights\",\n name: \"fk_insights_build_id\",\n column: \"build_id\",\n parent: \"builds\",\n parentColumn: \"id\",\n },\n {\n table: \"ai_usage\",\n name: \"fk_ai_usage_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"user_invite_tokens\",\n name: \"fk_user_invite_tokens_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"project_members\",\n name: \"fk_project_members_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"project_members\",\n name: \"fk_project_members_user_id\",\n column: \"user_id\",\n parent: \"users\",\n parentColumn: \"id\",\n },\n {\n table: \"project_group_mappings\",\n name: \"fk_project_group_mappings_project_id\",\n column: \"project_id\",\n parent: \"projects\",\n parentColumn: \"id\",\n },\n {\n table: \"session\",\n name: \"fk_session_user_id\",\n column: \"user_id\",\n parent: \"user\",\n parentColumn: \"id\",\n },\n {\n table: \"account\",\n name: \"fk_account_user_id\",\n column: \"user_id\",\n parent: \"user\",\n parentColumn: \"id\",\n },\n {\n table: \"passkey\",\n name: \"fk_passkey_user_id\",\n column: \"user_id\",\n parent: \"user\",\n parentColumn: \"id\",\n },\n {\n table: \"ssoProvider\",\n name: \"fk_ssoProvider_user_id\",\n column: \"user_id\",\n parent: \"user\",\n parentColumn: \"id\",\n },\n];\n"],"mappings":";;;;AAWA,MAAa,eAAsC;CACjD;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EAEE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;EACd,UAAU;CACZ;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ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E,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;CACA;EACE,OAAO;EACP,MAAM;EACN,QAAQ;EACR,QAAQ;EACR,cAAc;CAChB;AACF"}
|
package/dist/index.d.mts
CHANGED
|
@@ -17,7 +17,15 @@ interface MysqlDatabaseOptions {
|
|
|
17
17
|
/**
|
|
18
18
|
* Create a MySQL/MariaDB-backed DatabaseAdapter using mysql2 + Drizzle.
|
|
19
19
|
* Works with MySQL, MariaDB, PlanetScale (via mysql2), TiDB.
|
|
20
|
+
*
|
|
21
|
+
* Build retention (ADR 0039) is available on a pool against MySQL 8 with InnoDB; on anything else
|
|
22
|
+
* the adapter works as before and retention stays fail closed, with a warning at boot.
|
|
20
23
|
*/
|
|
21
24
|
declare function createMysqlDatabase(options: MysqlDatabaseOptions): DatabaseAdapter;
|
|
22
|
-
|
|
25
|
+
/**
|
|
26
|
+
* {@link createMysqlDatabase}, for a preset: `unverifiedRetention` names a transport the lifecycle
|
|
27
|
+
* was never proven on, which keeps the capability off whatever the server reports.
|
|
28
|
+
*/
|
|
29
|
+
declare function createMysqlAdapter(options: MysqlDatabaseOptions, unverifiedRetention?: string): DatabaseAdapter;
|
|
30
|
+
export { MysqlDatabaseOptions, createMysqlAdapter, createMysqlDatabase };
|
|
23
31
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -4,6 +4,7 @@ import.meta.url;
|
|
|
4
4
|
import { createDrizzleMySqlAdapter } from "./drizzle-factory-mysql.mjs";
|
|
5
5
|
import { runMigrations } from "./migrate.mjs";
|
|
6
6
|
import { schema } from "./schema.mjs";
|
|
7
|
+
import { activateRetention, attachRetention } from "./retention-activation.mjs";
|
|
7
8
|
import { drizzle } from "drizzle-orm/mysql2";
|
|
8
9
|
import mysql from "mysql2/promise";
|
|
9
10
|
// src/index.ts
|
|
@@ -16,6 +17,7 @@ function resolvePool(options) {
|
|
|
16
17
|
if (connectionString) return {
|
|
17
18
|
pool: mysql.createPool({
|
|
18
19
|
uri: connectionString,
|
|
20
|
+
...options.ssl === void 0 ? {} : { ssl: options.ssl },
|
|
19
21
|
dateStrings: true
|
|
20
22
|
}),
|
|
21
23
|
owned: true
|
|
@@ -37,32 +39,48 @@ function resolvePool(options) {
|
|
|
37
39
|
throw new Error("createMysqlDatabase: provide `url`/`connectionString`/`host` or a preconfigured `client`");
|
|
38
40
|
}
|
|
39
41
|
/**
|
|
42
|
+
* Where retention may get its connections: only a pool lends each operation a session of its own.
|
|
43
|
+
* An injected single `Connection` would put every party on one session, where row locks prove
|
|
44
|
+
* nothing, so it stays fail closed, as a preset that names why does.
|
|
45
|
+
*/
|
|
46
|
+
function retentionSource(pool, refused) {
|
|
47
|
+
if (refused !== void 0) return { refused };
|
|
48
|
+
if (typeof pool.getConnection !== "function") return { refused: "an injected single connection, not a pool" };
|
|
49
|
+
return { pool };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
40
52
|
* Create a MySQL/MariaDB-backed DatabaseAdapter using mysql2 + Drizzle.
|
|
41
53
|
* Works with MySQL, MariaDB, PlanetScale (via mysql2), TiDB.
|
|
54
|
+
*
|
|
55
|
+
* Build retention (ADR 0039) is available on a pool against MySQL 8 with InnoDB; on anything else
|
|
56
|
+
* the adapter works as before and retention stays fail closed, with a warning at boot.
|
|
42
57
|
*/
|
|
43
58
|
function createMysqlDatabase(options) {
|
|
59
|
+
return createMysqlAdapter(options);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* {@link createMysqlDatabase}, for a preset: `unverifiedRetention` names a transport the lifecycle
|
|
63
|
+
* was never proven on, which keeps the capability off whatever the server reports.
|
|
64
|
+
*/
|
|
65
|
+
function createMysqlAdapter(options, unverifiedRetention) {
|
|
44
66
|
const { pool, owned } = resolvePool(options);
|
|
45
67
|
const db = drizzle(pool, {
|
|
46
68
|
schema,
|
|
47
69
|
mode: "default"
|
|
48
70
|
});
|
|
49
|
-
|
|
71
|
+
const retention = {};
|
|
72
|
+
const adapter = createDrizzleMySqlAdapter(db, {
|
|
50
73
|
metadata: {
|
|
51
74
|
name: "MySQL",
|
|
52
|
-
version: "0.
|
|
75
|
+
version: "0.14.0",
|
|
53
76
|
description: "MySQL/MariaDB database adapter (mysql2 + Drizzle)",
|
|
54
77
|
kind: "mysql",
|
|
55
78
|
category: "database"
|
|
56
79
|
},
|
|
57
80
|
tables: schema,
|
|
58
81
|
migrate: async ({ logger, release }) => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (!Array.isArray(result)) return [];
|
|
62
|
-
const rows = result[0];
|
|
63
|
-
return Array.isArray(rows) ? rows : [];
|
|
64
|
-
};
|
|
65
|
-
await runMigrations(run, logger, release);
|
|
82
|
+
await runMigrations(async (sql) => await runRows(pool, sql), logger, release);
|
|
83
|
+
await activateRetention(retentionSource(pool, unverifiedRetention), retention, logger);
|
|
66
84
|
},
|
|
67
85
|
close: async () => {
|
|
68
86
|
if (owned && pool.end) await pool.end();
|
|
@@ -71,7 +89,15 @@ function createMysqlDatabase(options) {
|
|
|
71
89
|
await pool.query("SELECT 1");
|
|
72
90
|
}
|
|
73
91
|
});
|
|
92
|
+
return attachRetention(adapter, retention);
|
|
93
|
+
}
|
|
94
|
+
/** The rows of one statement on the pool, for the migration runner. */
|
|
95
|
+
async function runRows(pool, sql) {
|
|
96
|
+
const result = typeof pool.query === "function" ? await pool.query(sql) : await pool.execute?.(sql);
|
|
97
|
+
if (!Array.isArray(result)) return [];
|
|
98
|
+
const rows = result[0];
|
|
99
|
+
return Array.isArray(rows) ? rows : [];
|
|
74
100
|
}
|
|
75
|
-
export { createMysqlDatabase };
|
|
101
|
+
export { createMysqlAdapter, createMysqlDatabase };
|
|
76
102
|
|
|
77
103
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { DatabaseAdapter } from \"@storyshelf/core/adapter/database\";\nimport { drizzle } from \"drizzle-orm/mysql2\";\nimport mysql from \"mysql2/promise\";\nimport { createDrizzleMySqlAdapter } from \"./drizzle-factory-mysql.ts\";\nimport { runMigrations } from \"./migrate.ts\";\nimport { schema } from \"./schema/index.ts\";\n\ndeclare const __PKG_VERSION__: string | undefined;\n\nexport interface MysqlDatabaseOptions {\n url?: string;\n connectionString?: string;\n host?: string;\n port?: number;\n user?: string;\n password?: string;\n database?: string;\n ssl?: unknown;\n connectionLimit?: number;\n client?: mysql.Pool | mysql.Connection;\n}\n\ntype PoolLike = {\n query(sql: string): Promise<unknown>;\n execute?(sql: string): Promise<unknown>;\n end?(): Promise<void>;\n};\n\nfunction resolvePool(options: MysqlDatabaseOptions): {\n pool: PoolLike;\n owned: boolean;\n} {\n if (options.client) {\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion -- Pool vs PoolLike width\n return { pool: options.client as unknown as PoolLike, owned: false };\n }\n const connectionString = options.connectionString ?? options.url;\n if (connectionString) {\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion -- Pool vs PoolLike width\n return {\n pool: mysql.createPool({
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { DatabaseAdapter } from \"@storyshelf/core/adapter/database\";\nimport { drizzle } from \"drizzle-orm/mysql2\";\nimport mysql from \"mysql2/promise\";\nimport { createDrizzleMySqlAdapter } from \"./drizzle-factory-mysql.ts\";\nimport type { LifecyclePool } from \"./lifecycle-transaction.ts\";\nimport { runMigrations } from \"./migrate.ts\";\nimport {\n activateRetention,\n attachRetention,\n type RetentionActivation,\n type RetentionSource,\n} from \"./retention-activation.ts\";\nimport { schema } from \"./schema/index.ts\";\n\ndeclare const __PKG_VERSION__: string | undefined;\n\nexport interface MysqlDatabaseOptions {\n url?: string;\n connectionString?: string;\n host?: string;\n port?: number;\n user?: string;\n password?: string;\n database?: string;\n ssl?: unknown;\n connectionLimit?: number;\n client?: mysql.Pool | mysql.Connection;\n}\n\ntype PoolLike = {\n query(sql: string): Promise<unknown>;\n execute?(sql: string): Promise<unknown>;\n end?(): Promise<void>;\n};\n\nfunction resolvePool(options: MysqlDatabaseOptions): {\n pool: PoolLike;\n owned: boolean;\n} {\n if (options.client) {\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion -- Pool vs PoolLike width\n return { pool: options.client as unknown as PoolLike, owned: false };\n }\n const connectionString = options.connectionString ?? options.url;\n if (connectionString) {\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion -- Pool vs PoolLike width\n return {\n pool: mysql.createPool({\n uri: connectionString,\n ...(options.ssl === undefined ? {} : { ssl: options.ssl as never }),\n dateStrings: true,\n }),\n owned: true,\n };\n }\n if (options.host) {\n return {\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion -- Pool vs PoolLike width\n pool: mysql.createPool({\n host: options.host,\n port: options.port,\n user: options.user,\n password: options.password,\n database: options.database,\n ssl: options.ssl as never,\n dateStrings: true,\n waitForConnections: true,\n connectionLimit: options.connectionLimit ?? 10,\n }) as unknown as PoolLike,\n owned: true,\n };\n }\n throw new Error(\n \"createMysqlDatabase: provide `url`/`connectionString`/`host` or a preconfigured `client`\",\n );\n}\n\n/**\n * Where retention may get its connections: only a pool lends each operation a session of its own.\n * An injected single `Connection` would put every party on one session, where row locks prove\n * nothing, so it stays fail closed, as a preset that names why does.\n */\nfunction retentionSource(pool: PoolLike, refused: string | undefined): RetentionSource {\n if (refused !== undefined) return { refused };\n if (typeof (pool as Partial<LifecyclePool>).getConnection !== \"function\") {\n return { refused: \"an injected single connection, not a pool\" };\n }\n return { pool: pool as unknown as LifecyclePool };\n}\n\n/**\n * Create a MySQL/MariaDB-backed DatabaseAdapter using mysql2 + Drizzle.\n * Works with MySQL, MariaDB, PlanetScale (via mysql2), TiDB.\n *\n * Build retention (ADR 0039) is available on a pool against MySQL 8 with InnoDB; on anything else\n * the adapter works as before and retention stays fail closed, with a warning at boot.\n */\nexport function createMysqlDatabase(options: MysqlDatabaseOptions): DatabaseAdapter {\n return createMysqlAdapter(options);\n}\n\n/**\n * {@link createMysqlDatabase}, for a preset: `unverifiedRetention` names a transport the lifecycle\n * was never proven on, which keeps the capability off whatever the server reports.\n */\nexport function createMysqlAdapter(\n options: MysqlDatabaseOptions,\n unverifiedRetention?: string,\n): DatabaseAdapter {\n const { pool, owned } = resolvePool(options);\n const db = drizzle(pool as never, { schema, mode: \"default\" });\n const retention: RetentionActivation = {};\n\n const adapter = createDrizzleMySqlAdapter(db, {\n metadata: {\n name: \"MySQL\",\n version: typeof __PKG_VERSION__ === \"string\" ? __PKG_VERSION__ : \"0.0.0\",\n description: \"MySQL/MariaDB database adapter (mysql2 + Drizzle)\",\n kind: \"mysql\",\n category: \"database\",\n },\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion -- schema is AnyMySqlTable, adapter expects Table\n tables: schema as unknown as DatabaseAdapter[\"tables\"],\n migrate: async ({ logger, release }): Promise<void> => {\n await runMigrations(async (sql) => await runRows(pool, sql), logger, release);\n await activateRetention(retentionSource(pool, unverifiedRetention), retention, logger);\n },\n close: async (): Promise<void> => {\n if (owned && pool.end) {\n await pool.end();\n }\n },\n ping: async (): Promise<void> => {\n await pool.query(\"SELECT 1\");\n },\n });\n return attachRetention(adapter, retention);\n}\n\n/** The rows of one statement on the pool, for the migration runner. */\nasync function runRows(pool: PoolLike, sql: string): Promise<unknown[]> {\n const result: unknown =\n typeof pool.query === \"function\" ? await pool.query(sql) : await pool.execute?.(sql);\n if (!Array.isArray(result)) {\n return [];\n }\n const rows: unknown = result[0];\n return Array.isArray(rows) ? (rows as unknown[]) : [];\n}\n"],"mappings":";;;;;;;;;;AAmCA,SAAS,YAAY,SAGnB;CACA,IAAI,QAAQ,QAEV,OAAO;EAAE,MAAM,QAAQ;EAA+B,OAAO;CAAM;CAErE,MAAM,mBAAmB,QAAQ,oBAAoB,QAAQ;CAC7D,IAAI,kBAEF,OAAO;EACL,MAAM,MAAM,WAAW;GACrB,KAAK;GACL,GAAI,QAAQ,QAAQ,KAAA,IAAY,CAAC,IAAI,EAAE,KAAK,QAAQ,IAAa;GACjE,aAAa;EACf,CAAC;EACD,OAAO;CACT;CAEF,IAAI,QAAQ,MACV,OAAO;EAEL,MAAM,MAAM,WAAW;GACrB,MAAM,QAAQ;GACd,MAAM,QAAQ;GACd,MAAM,QAAQ;GACd,UAAU,QAAQ;GAClB,UAAU,QAAQ;GAClB,KAAK,QAAQ;GACb,aAAa;GACb,oBAAoB;GACpB,iBAAiB,QAAQ,mBAAmB;EAC9C,CAAC;EACD,OAAO;CACT;CAEF,MAAM,IAAI,MACR,0FACF;AACF;;;;;;AAOA,SAAS,gBAAgB,MAAgB,SAA8C;CACrF,IAAI,YAAY,KAAA,GAAW,OAAO,EAAE,QAAQ;CAC5C,IAAI,OAAQ,KAAgC,kBAAkB,YAC5D,OAAO,EAAE,SAAS,4CAA4C;CAEhE,OAAO,EAAQ,KAAiC;AAClD;;;;;;;;AASA,SAAgB,oBAAoB,SAAgD;CAClF,OAAO,mBAAmB,OAAO;AACnC;;;;;AAMA,SAAgB,mBACd,SACA,qBACiB;CACjB,MAAM,EAAE,MAAM,UAAU,YAAY,OAAO;CAC3C,MAAM,KAAK,QAAQ,MAAe;EAAE;EAAQ,MAAM;CAAU,CAAC;CAC7D,MAAM,YAAiC,CAAC;CAExC,MAAM,UAAU,0BAA0B,IAAI;EAC5C,UAAU;GACR,MAAM;GACN,SAAA;GACA,aAAa;GACb,MAAM;GACN,UAAU;EACZ;EAEA,QAAQ;EACR,SAAS,OAAO,EAAE,QAAQ,cAA6B;GACrD,MAAM,cAAc,OAAO,QAAQ,MAAM,QAAQ,MAAM,GAAG,GAAG,QAAQ,OAAO;GAC5E,MAAM,kBAAkB,gBAAgB,MAAM,mBAAmB,GAAG,WAAW,MAAM;EACvF;EACA,OAAO,YAA2B;GAChC,IAAI,SAAS,KAAK,KAChB,MAAM,KAAK,IAAI;EAEnB;EACA,MAAM,YAA2B;GAC/B,MAAM,KAAK,MAAM,UAAU;EAC7B;CACF,CAAC;CACD,OAAO,gBAAgB,SAAS,SAAS;AAC3C;;AAGA,eAAe,QAAQ,MAAgB,KAAiC;CACtE,MAAM,SACJ,OAAO,KAAK,UAAU,aAAa,MAAM,KAAK,MAAM,GAAG,IAAI,MAAM,KAAK,UAAU,GAAG;CACrF,IAAI,CAAC,MAAM,QAAQ,MAAM,GACvB,OAAO,CAAC;CAEV,MAAM,OAAgB,OAAO;CAC7B,OAAO,MAAM,QAAQ,IAAI,IAAK,OAAqB,CAAC;AACtD"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import "node:path";
|
|
2
|
+
import "node:url";
|
|
3
|
+
import.meta.url;
|
|
4
|
+
import { transactionStore } from "./drizzle-factory-mysql.mjs";
|
|
5
|
+
import { toMySqlDateTime } from "./schema/datetime.mjs";
|
|
6
|
+
import { schema } from "./schema.mjs";
|
|
7
|
+
import { drizzle } from "drizzle-orm/mysql2";
|
|
8
|
+
// src/lifecycle-transaction.ts
|
|
9
|
+
/** InnoDB chose this transaction as a deadlock victim and rolled it back whole. */
|
|
10
|
+
const DEADLOCK = 1213;
|
|
11
|
+
/** How often an operation without a caller callback is retried after a deadlock. */
|
|
12
|
+
const DEADLOCK_ATTEMPTS = 5;
|
|
13
|
+
/** The rows of a mysql2 `query` answer (`[rows, fields]`), or none for a write. */
|
|
14
|
+
function rowsOf(result) {
|
|
15
|
+
const [rows] = Array.isArray(result) ? result : [];
|
|
16
|
+
return Array.isArray(rows) ? rows : [];
|
|
17
|
+
}
|
|
18
|
+
/** The `affectedRows` of a mysql2 write answer (`[ResultSetHeader, undefined]`). */
|
|
19
|
+
function changedOf(result) {
|
|
20
|
+
const [header] = Array.isArray(result) ? result : [];
|
|
21
|
+
return header?.affectedRows ?? 0;
|
|
22
|
+
}
|
|
23
|
+
/** Statements on one borrowed connection, outside or inside a transaction. */
|
|
24
|
+
function statementsOn(connection) {
|
|
25
|
+
return {
|
|
26
|
+
query: async (statement, values) => rowsOf(await connection.query(statement, values)),
|
|
27
|
+
run: async (statement, values) => changedOf(await connection.query(statement, values))
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Borrow a connection for `fn`. A connection whose transaction could not be rolled back is in an
|
|
32
|
+
* unknown state, so it is destroyed instead of going back to the pool.
|
|
33
|
+
*/
|
|
34
|
+
async function onConnection(pool, fn) {
|
|
35
|
+
const connection = await pool.getConnection();
|
|
36
|
+
let healthy = true;
|
|
37
|
+
try {
|
|
38
|
+
return await fn(connection, () => {
|
|
39
|
+
healthy = false;
|
|
40
|
+
});
|
|
41
|
+
} finally {
|
|
42
|
+
if (healthy) connection.release();
|
|
43
|
+
else connection.destroy();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Run `fn` in one READ COMMITTED transaction on the connection: commit when it resolves, roll back when it throws. */
|
|
47
|
+
async function transaction(connection, broken, fn) {
|
|
48
|
+
await connection.query("SET TRANSACTION ISOLATION LEVEL READ COMMITTED");
|
|
49
|
+
await connection.query("START TRANSACTION");
|
|
50
|
+
try {
|
|
51
|
+
const result = await fn({
|
|
52
|
+
...statementsOn(connection),
|
|
53
|
+
store: transactionStore(drizzle(connection, {
|
|
54
|
+
schema,
|
|
55
|
+
mode: "default"
|
|
56
|
+
}))
|
|
57
|
+
});
|
|
58
|
+
await connection.query("COMMIT");
|
|
59
|
+
return result;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
await connection.query("ROLLBACK").catch(broken);
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** Whether InnoDB rolled the transaction back to break a deadlock. */
|
|
66
|
+
function isDeadlock(error) {
|
|
67
|
+
const { errno, code } = error ?? {};
|
|
68
|
+
return errno === DEADLOCK || code === "ER_LOCK_DEADLOCK";
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Run `fn` again while InnoDB picks it as a deadlock victim. Only for operations whose whole work is
|
|
72
|
+
* the transaction's own SQL: the victim was rolled back entirely, so running it again is the same
|
|
73
|
+
* as running it once. A caller's domain callback is never repeated; its deadlock reaches the caller.
|
|
74
|
+
*/
|
|
75
|
+
async function retryingDeadlocks(fn) {
|
|
76
|
+
for (let attempt = 1;; attempt += 1) try {
|
|
77
|
+
return await fn();
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (!isDeadlock(error) || attempt >= DEADLOCK_ATTEMPTS) throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** One transaction on its own connection; `retry` repeats it after a deadlock (see {@link retryingDeadlocks}). */
|
|
83
|
+
async function inTransaction(pool, fn, retry = true) {
|
|
84
|
+
return await onConnection(pool, async (connection, broken) => {
|
|
85
|
+
const once = async () => await transaction(connection, broken, fn);
|
|
86
|
+
return retry ? await retryingDeadlocks(once) : await once();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/** An ISO instant as the `DATETIME(3)` text the columns hold (UTC, no zone). */
|
|
90
|
+
function instant(iso) {
|
|
91
|
+
return toMySqlDateTime(iso);
|
|
92
|
+
}
|
|
93
|
+
/** The current instant, as {@link instant}. */
|
|
94
|
+
function nowInstant() {
|
|
95
|
+
return instant((/* @__PURE__ */ new Date()).toISOString());
|
|
96
|
+
}
|
|
97
|
+
/** `values` in slices of `size`, for statements whose text grows with the list. */
|
|
98
|
+
function chunks(values, size = 5e3) {
|
|
99
|
+
const out = [];
|
|
100
|
+
for (let start = 0; start < values.length; start += size) out.push(values.slice(start, start + size));
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
export { chunks, inTransaction, instant, isDeadlock, nowInstant, onConnection, retryingDeadlocks, rowsOf, statementsOn, transaction };
|
|
104
|
+
|
|
105
|
+
//# sourceMappingURL=lifecycle-transaction.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifecycle-transaction.mjs","names":[],"sources":["../src/lifecycle-transaction.ts"],"sourcesContent":["/**\n * The connection and transaction every MySQL lifecycle operation runs on (ADR 0039).\n *\n * Each operation borrows one connection from the pool for its whole length, so a session-level lock\n * (`GET_LOCK`, see `content-uses.ts`) can be held across the transaction's commit, and runs its\n * transaction at READ COMMITTED: like Postgres, every statement sees what other sessions committed\n * before it, which is what a domain write that reads after taking the fence expects. InnoDB's default\n * REPEATABLE READ would answer a plain read from a snapshot taken at the first read instead.\n */\nimport type { TxStore } from \"@storyshelf/core/adapter/database\";\nimport { drizzle } from \"drizzle-orm/mysql2\";\nimport { transactionStore } from \"./drizzle-factory-mysql.ts\";\nimport { toMySqlDateTime } from \"./schema/datetime.ts\";\nimport { schema } from \"./schema/index.ts\";\n\n/** One row, keyed by column name. */\nexport type Row = Record<string, unknown>;\n\n/** A connection the pool lends out (mysql2/promise `PoolConnection`). */\nexport interface LifecycleConnection {\n query(statement: string, values?: unknown): Promise<unknown>;\n release(): void;\n destroy(): void;\n}\n\n/** What the lifecycle needs of a mysql2/promise pool: a connection of its own per operation. */\nexport interface LifecyclePool {\n getConnection(): Promise<LifecycleConnection>;\n}\n\n/** A transaction: `query` reads rows, `run` writes and counts the rows changed, `store` is for domain writes. */\nexport interface LifecycleTx {\n query: (statement: string, values?: readonly unknown[]) => Promise<Row[]>;\n run: (statement: string, values?: readonly unknown[]) => Promise<number>;\n store: TxStore;\n}\n\n/** InnoDB chose this transaction as a deadlock victim and rolled it back whole. */\nconst DEADLOCK = 1213;\n/** How often an operation without a caller callback is retried after a deadlock. */\nconst DEADLOCK_ATTEMPTS = 5;\n\n/** The rows of a mysql2 `query` answer (`[rows, fields]`), or none for a write. */\nexport function rowsOf(result: unknown): Row[] {\n const [rows] = Array.isArray(result) ? (result as unknown[]) : [];\n return Array.isArray(rows) ? (rows as Row[]) : [];\n}\n\n/** The `affectedRows` of a mysql2 write answer (`[ResultSetHeader, undefined]`). */\nfunction changedOf(result: unknown): number {\n const [header] = Array.isArray(result) ? (result as unknown[]) : [];\n return (header as { affectedRows?: number } | undefined)?.affectedRows ?? 0;\n}\n\n/** Statements on one borrowed connection, outside or inside a transaction. */\nexport function statementsOn(connection: LifecycleConnection): Omit<LifecycleTx, \"store\"> {\n return {\n query: async (statement, values) => rowsOf(await connection.query(statement, values)),\n run: async (statement, values) => changedOf(await connection.query(statement, values)),\n };\n}\n\n/**\n * Borrow a connection for `fn`. A connection whose transaction could not be rolled back is in an\n * unknown state, so it is destroyed instead of going back to the pool.\n */\nexport async function onConnection<R>(\n pool: LifecyclePool,\n fn: (connection: LifecycleConnection, broken: () => void) => Promise<R>,\n): Promise<R> {\n const connection = await pool.getConnection();\n let healthy = true;\n try {\n return await fn(connection, () => {\n healthy = false;\n });\n } finally {\n if (healthy) connection.release();\n else connection.destroy();\n }\n}\n\n/** Run `fn` in one READ COMMITTED transaction on the connection: commit when it resolves, roll back when it throws. */\nexport async function transaction<R>(\n connection: LifecycleConnection,\n broken: () => void,\n fn: (tx: LifecycleTx) => Promise<R>,\n): Promise<R> {\n await connection.query(\"SET TRANSACTION ISOLATION LEVEL READ COMMITTED\");\n await connection.query(\"START TRANSACTION\");\n try {\n const result = await fn({\n ...statementsOn(connection),\n store: transactionStore(drizzle(connection as never, { schema, mode: \"default\" })),\n });\n await connection.query(\"COMMIT\");\n return result;\n } catch (error) {\n await connection.query(\"ROLLBACK\").catch(broken);\n throw error;\n }\n}\n\n/** Whether InnoDB rolled the transaction back to break a deadlock. */\nexport function isDeadlock(error: unknown): boolean {\n const { errno, code } = (error ?? {}) as { errno?: unknown; code?: unknown };\n return errno === DEADLOCK || code === \"ER_LOCK_DEADLOCK\";\n}\n\n/**\n * Run `fn` again while InnoDB picks it as a deadlock victim. Only for operations whose whole work is\n * the transaction's own SQL: the victim was rolled back entirely, so running it again is the same\n * as running it once. A caller's domain callback is never repeated; its deadlock reaches the caller.\n */\nexport async function retryingDeadlocks<R>(fn: () => Promise<R>): Promise<R> {\n for (let attempt = 1; ; attempt += 1) {\n try {\n // oxlint-disable-next-line no-await-in-loop -- one attempt at a time\n return await fn();\n } catch (error) {\n if (!isDeadlock(error) || attempt >= DEADLOCK_ATTEMPTS) throw error;\n }\n }\n}\n\n/** One transaction on its own connection; `retry` repeats it after a deadlock (see {@link retryingDeadlocks}). */\nexport async function inTransaction<R>(\n pool: LifecyclePool,\n fn: (tx: LifecycleTx) => Promise<R>,\n retry = true,\n): Promise<R> {\n return await onConnection(pool, async (connection, broken) => {\n const once = async (): Promise<R> => await transaction(connection, broken, fn);\n return retry ? await retryingDeadlocks(once) : await once();\n });\n}\n\n/** An ISO instant as the `DATETIME(3)` text the columns hold (UTC, no zone). */\nexport function instant(iso: string): string {\n return toMySqlDateTime(iso);\n}\n\n/** The current instant, as {@link instant}. */\nexport function nowInstant(): string {\n return instant(new Date().toISOString());\n}\n\n/** `values` in slices of `size`, for statements whose text grows with the list. */\nexport function chunks<T>(values: readonly T[], size = 5000): T[][] {\n const out: T[][] = [];\n for (let start = 0; start < values.length; start += size) {\n out.push(values.slice(start, start + size));\n }\n return out;\n}\n"],"mappings":";;;;;;;;;AAsCA,MAAM,WAAW;;AAEjB,MAAM,oBAAoB;;AAG1B,SAAgB,OAAO,QAAwB;CAC7C,MAAM,CAAC,QAAQ,MAAM,QAAQ,MAAM,IAAK,SAAuB,CAAC;CAChE,OAAO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AAW,KAAK,KAAK,WAAW,mBAAmB,MAAM;CAChE;AAEJ;;AAGA,eAAsB,cACpB,MACA,IACA,QAAQ,MACI;CACZ,OAAO,MAAM,aAAa,MAAM,OAAO,YAAY,WAAW;EAC5D,MAAM,OAAO,YAAwB,MAAM,YAAY,YAAY,QAAQ,EAAE;EAC7E,OAAO,QAAQ,MAAM,kBAAkB,IAAI,IAAI,MAAM,KAAK;CAC5D,CAAC;AACH;;AAGA,SAAgB,QAAQ,KAAqB;CAC3C,OAAO,gBAAgB,GAAG;AAC5B;;AAGA,SAAgB,aAAqB;CACnC,OAAO,yBAAQ,IAAI,KAAK,EAAA,CAAE,YAAY,CAAC;AACzC;;AAGA,SAAgB,OAAU,QAAsB,OAAO,KAAa;CAClE,MAAM,MAAa,CAAC;CACpB,KAAK,IAAI,QAAQ,GAAG,QAAQ,OAAO,QAAQ,SAAS,MAClD,IAAI,KAAK,OAAO,MAAM,OAAO,QAAQ,IAAI,CAAC;CAE5C,OAAO;AACT"}
|
package/dist/migrate.mjs
CHANGED
|
@@ -260,11 +260,36 @@ const COLUMN_MIGRATIONS = [
|
|
|
260
260
|
column: "capture_generation",
|
|
261
261
|
definition: "INTEGER NOT NULL DEFAULT 0"
|
|
262
262
|
},
|
|
263
|
+
{
|
|
264
|
+
table: "builds",
|
|
265
|
+
column: "skipped_from_build_id",
|
|
266
|
+
definition: "VARCHAR(26) NULL"
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
table: "builds",
|
|
270
|
+
column: "superseded_by_build_id",
|
|
271
|
+
definition: "VARCHAR(26) NULL"
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
table: "builds",
|
|
275
|
+
column: "idempotency_key",
|
|
276
|
+
definition: "VARCHAR(191) NULL"
|
|
277
|
+
},
|
|
263
278
|
{
|
|
264
279
|
table: "builds",
|
|
265
280
|
column: "manifest_sha256",
|
|
266
281
|
definition: "VARCHAR(64) NULL"
|
|
267
282
|
},
|
|
283
|
+
{
|
|
284
|
+
table: "builds",
|
|
285
|
+
column: "retention_state",
|
|
286
|
+
definition: "VARCHAR(16) NOT NULL DEFAULT 'available'"
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
table: "projects",
|
|
290
|
+
column: "cancel_superseded",
|
|
291
|
+
definition: "TINYINT(1) NOT NULL DEFAULT 1"
|
|
292
|
+
},
|
|
268
293
|
{
|
|
269
294
|
table: "notification_subscriptions",
|
|
270
295
|
column: "review_requests",
|
|
@@ -359,11 +384,26 @@ const COLUMN_MIGRATIONS = [
|
|
|
359
384
|
table: "notification_channels",
|
|
360
385
|
column: "last_delivered_at",
|
|
361
386
|
definition: "DATETIME(3)"
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
table: "git_repository_links",
|
|
390
|
+
column: "last_delivered_at",
|
|
391
|
+
definition: "DATETIME(3)"
|
|
362
392
|
}
|
|
363
393
|
];
|
|
364
394
|
/** A run in flight when `builds.capture_job_id` was added owns the build it was started for. */
|
|
365
395
|
const BUILD_CAPTURE_JOB_BACKFILL = "UPDATE builds SET capture_job_id = (SELECT j.job_id FROM capture_jobs j WHERE j.entry = 'job' AND j.kind = 'capture' AND j.shard_id IS NULL AND j.cancel_requested_at IS NULL AND j.status IN ('queued', 'running') AND j.build_id = builds.id LIMIT 1) WHERE capture_job_id IS NULL AND status IN ('pending', 'capturing') AND (SELECT COUNT(*) FROM capture_jobs j WHERE j.entry = 'job' AND j.kind = 'capture' AND j.shard_id IS NULL AND j.cancel_requested_at IS NULL AND j.status IN ('queued', 'running') AND j.build_id = builds.id) = 1";
|
|
366
396
|
const IDENTITY_INDEXES = [
|
|
397
|
+
{
|
|
398
|
+
table: "builds",
|
|
399
|
+
name: "builds_skipped_from_idx",
|
|
400
|
+
sql: "CREATE INDEX builds_skipped_from_idx ON builds (skipped_from_build_id)"
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
table: "builds",
|
|
404
|
+
name: "builds_project_idempotency_idx",
|
|
405
|
+
sql: "CREATE UNIQUE INDEX builds_project_idempotency_idx ON builds (project_id, idempotency_key)"
|
|
406
|
+
},
|
|
367
407
|
{
|
|
368
408
|
table: "snapshots",
|
|
369
409
|
name: "snapshots_build_story_mode_browser_idx",
|
|
@@ -432,7 +472,7 @@ async function ensureForeignKey(run, foreignKey) {
|
|
|
432
472
|
const column = quote(foreignKey.column);
|
|
433
473
|
const parent = quote(foreignKey.parent);
|
|
434
474
|
const parentColumn = quote(foreignKey.parentColumn);
|
|
435
|
-
await run(`ALTER TABLE ${table} ADD CONSTRAINT ${foreignKey.name} FOREIGN KEY (${column}) REFERENCES ${parent}(${parentColumn}) ON DELETE CASCADE`);
|
|
475
|
+
await run(`ALTER TABLE ${table} ADD CONSTRAINT ${foreignKey.name} FOREIGN KEY (${column}) REFERENCES ${parent}(${parentColumn}) ON DELETE ${foreignKey.onDelete ?? "CASCADE"}`);
|
|
436
476
|
}
|
|
437
477
|
async function hasForeignKey(run, foreignKey) {
|
|
438
478
|
return (await run(`SELECT 1 FROM information_schema.key_column_usage WHERE constraint_schema = DATABASE() AND table_name = '${foreignKey.table}' AND column_name = '${foreignKey.column}' AND referenced_table_name = '${foreignKey.parent}' AND referenced_column_name = '${foreignKey.parentColumn}' LIMIT 1`)).length > 0;
|