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, 06:00 UTC
| 1 | import { applyPendingAlchemyFormat } from "@alchemy.run/sql/Migrations/AlchemyFormat"; |
| 2 | import { |
| 3 | MigrationError, |
| 4 | type MigrationHistoryConflictError, |
| 5 | type SqlExecutor, |
| 6 | } from "@alchemy.run/sql/Migrations/Format"; |
| 7 | import { inlineSqlParams } from "@alchemy.run/sql/Migrations/Utils"; |
| 8 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import { DurableObjectState } from "./DurableObjectState.ts"; |
| 11 | import type { SqlMigrationSnapshot } from "./SqlMigrationsRuntime.ts"; |
| 12 | |
| 13 | /** @internal */ |
| 14 | export const applySqlMigrations: ( |
| 15 | migrations: SqlMigrationSnapshot, |
| 16 | ) => Effect.Effect< |
| 17 | void, |
| 18 | MigrationError | MigrationHistoryConflictError, |
| 19 | DurableObjectState | RuntimeContext |
| 20 | > = Effect.fn("Cloudflare.SqlMigrations.apply")(function* (migrations) { |
| 21 | const { raw } = yield* DurableObjectState; |
| 22 | const storage = raw.storage; |
| 23 | const executor: SqlExecutor = { |
| 24 | dialect: "sqlite", |
| 25 | query: (sql, params = []) => |
| 26 | Effect.try({ |
| 27 | try: () => storage.sql.exec(inlineSqlParams(sql, params, "sqlite")).toArray(), |
| 28 | catch: (cause) => |
| 29 | new MigrationError({ |
| 30 | message: "Failed to query migration history", |
| 31 | cause, |
| 32 | }), |
| 33 | }), |
| 34 | batch: (statements) => |
| 35 | Effect.try({ |
| 36 | try: () => |
| 37 | storage.transactionSync(() => { |
| 38 | for (const statement of statements) storage.sql.exec(statement); |
| 39 | }), |
| 40 | catch: (cause) => |
| 41 | new MigrationError({ |
| 42 | message: "Failed to apply SQL migration", |
| 43 | cause, |
| 44 | }), |
| 45 | }), |
| 46 | }; |
| 47 | yield* applyPendingAlchemyFormat({ |
| 48 | executor, |
| 49 | table: migrations.table, |
| 50 | records: migrations.records, |
| 51 | }); |
| 52 | }); |