@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 71 lines 2.63 KB
Raw
1 /**
2 * Runs database migrations for Durable Object SQLite storage that uses Effect
3 * SQL.
4 *
5 * This module re-exports the shared `Migrator` loaders and error types, then
6 * provides `run` and `layer` helpers that execute ordered migrations through the
7 * current Durable Object SQLite `SqlClient`. Use it when a Durable
8 * Object needs to create or upgrade its local schema during construction, before
9 * repositories or request handlers use the object storage, or in tests that
10 * exercise Durable Object persistence.
11 *
12 * Migrations are recorded in `effect_sql_migrations` by default and are loaded
13 * using the shared `<id>_<name>` file or record-key convention. The underlying
14 * storage is scoped to a Durable Object id, so running migrations for one object
15 * does not update any other object instance; run the migrator against the same
16 * `DurableObjectStorage`-backed client that the object uses for normal queries
17 * so migrations can run in Cloudflare-managed transactions. These SQL
18 * migrations are separate from Cloudflare's Durable Object class migrations, and
19 * the Durable Object must already be configured with SQLite storage before this
20 * module can apply schema changes. Repeated startup runs are expected and are
21 * guarded by the migrations table, but request handling should wait until the
22 * migration layer has finished. This adapter does not currently write SQLite
23 * schema dumps for `schemaDirectory`.
24 *
25 * @stability unstable
26 * @since 4.0.0
27 */
28 import type * as Effect from "effect/Effect"
29 import * as Layer from "effect/Layer"
30 import * as Migrator from "effect/sql/Migrator"
31 import type * as Client from "effect/sql/SqlClient"
32 import type { SqlError } from "effect/sql/SqlError"
33
34 /**
35 * @since 4.0.0
36 */
37 export * from "effect/sql/Migrator"
38 export {
39 /**
40 * @stability unstable
41 * @category loaders
42 * @since 4.0.0
43 */
44 fromFileSystem
45 } from "effect/sql/MigratorFileSystem"
46
47 /**
48 * Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied.
49 *
50 * @stability unstable
51 * @category running
52 * @since 4.0.0
53 */
54 export const run: <R2 = never>(
55 { loader, schemaDirectory, table }: Migrator.MigratorOptions<R2>
56 ) => Effect.Effect<
57 ReadonlyArray<readonly [id: number, name: string]>,
58 Migrator.MigrationError | SqlError,
59 Client.SqlClient | R2
60 > = Migrator.make({})
61
62 /**
63 * Creates a layer that runs the configured SQL migrations during layer construction.
64 *
65 * @stability unstable
66 * @category layers
67 * @since 4.0.0
68 */
69 export const layer = <R>(
70 options: Migrator.MigratorOptions<R>
71 ): Layer.Layer<never, Migrator.MigrationError | SqlError, Client.SqlClient | R> => Layer.effectDiscard(run(options))