@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 48 lines 1.5 KB
Raw
1 import * as Effect from "effect/Effect";
2 import type { Connection } from "mysql2/promise";
3 import { MigrationError, type SqlExecutor } from "./Format.ts";
4
5 /**
6 * Adapt an open `mysql2` connection into the registry's
7 * {@link SqlExecutor}. Batches run in a transaction (MySQL DDL
8 * auto-commits regardless, matching the previous statement-by-statement
9 * behavior).
10 *
11 * The `mysql2` import is type-only — how the connection is opened stays
12 * with the database provider.
13 */
14 export const makeMySQLMigrationExecutor = (connection: Connection): SqlExecutor => ({
15 dialect: "mysql",
16 query: (sql, params) =>
17 Effect.tryPromise({
18 try: () =>
19 connection
20 .query(sql, params ? [...params] : undefined)
21 .then(([rows]) => rows as Array<Record<string, unknown>>),
22 catch: (cause) =>
23 new MigrationError({
24 message: `mysql query failed: ${String(cause)}`,
25 cause,
26 }),
27 }),
28 batch: (statements) =>
29 Effect.tryPromise({
30 try: async () => {
31 await connection.query("START TRANSACTION");
32 try {
33 for (const statement of statements) {
34 await connection.query(statement);
35 }
36 await connection.query("COMMIT");
37 } catch (error) {
38 await connection.query("ROLLBACK").catch(() => {});
39 throw error;
40 }
41 },
42 catch: (cause) =>
43 new MigrationError({
44 message: `mysql migration batch failed: ${String(cause)}`,
45 cause,
46 }),
47 }),
48 });