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 * as crypto from "node:crypto"; |
| 2 | import * as Artifacts from "alchemy/Artifacts"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { Interaction } from "alchemy/Interaction"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import * as Match from "effect/Match"; |
| 11 | import * as Option from "effect/Option"; |
| 12 | import * as Path from "effect/Path"; |
| 13 | import * as Record from "effect/Record"; |
| 14 | import * as S from "effect/Schema"; |
| 15 | import * as Semaphore from "effect/Semaphore"; |
| 16 | import type { Providers } from "./Providers.ts"; |
| 17 | |
| 18 | export type Dialect = "postgres" | "mysql" | "sqlite"; |
| 19 | |
| 20 | // Unlisted fields are kept (the rest records), so a decoded snapshot can be |
| 21 | // written back whole. |
| 22 | const DdlEntry = S.StructWithRest( |
| 23 | S.Struct({ |
| 24 | entityType: S.String, |
| 25 | schema: S.optional(S.String), |
| 26 | table: S.optional(S.String), |
| 27 | name: S.optional(S.String), |
| 28 | }), |
| 29 | [S.Record(S.String, S.Unknown)], |
| 30 | ); |
| 31 | type DdlEntry = typeof DdlEntry.Type; |
| 32 | |
| 33 | // A `snapshot.json`. Postgres folders hold one `base` snapshot followed by |
| 34 | // `diff` snapshots carrying `deltas`; mysql and sqlite snapshots are full. |
| 35 | const Snapshot = S.StructWithRest( |
| 36 | S.Struct({ |
| 37 | type: S.optional(S.Literals(["base", "diff"])), |
| 38 | version: S.optional(S.String), |
| 39 | dialect: S.optional(S.String), |
| 40 | id: S.optional(S.String), |
| 41 | prevIds: S.String.pipe(S.Array, S.optional), |
| 42 | ddl: DdlEntry.pipe(S.Array, S.optional), |
| 43 | }), |
| 44 | [S.Record(S.String, S.Unknown)], |
| 45 | ); |
| 46 | type Snapshot = typeof Snapshot.Type; |
| 47 | |
| 48 | const decodeSnapshot = S.decodeEffect(S.fromJsonString(Snapshot)); |
| 49 | |
| 50 | // drizzle-kit's `MissingHint` / `Hint`, narrowed to what `generate` can ask: |
| 51 | // rename-or-create. Its `confirm_data_loss` questions need a live database |
| 52 | // (`push` checks whether tables are empty), so generate never raises them. |
| 53 | // Entity ids are tuples: `[name]`, `[schema, name]` or `[schema, table, name]`. |
| 54 | type MissingHint = { |
| 55 | readonly type: "rename_or_create"; |
| 56 | readonly kind: string; |
| 57 | readonly entity: ReadonlyArray<string>; |
| 58 | }; |
| 59 | |
| 60 | type Hint = |
| 61 | | { type: "rename"; kind: string; from: ReadonlyArray<string>; to: ReadonlyArray<string> } |
| 62 | | { type: "create"; kind: string; entity: ReadonlyArray<string> }; |
| 63 | |
| 64 | type GenerateOptions = { |
| 65 | dialect: string; |
| 66 | schema: string; |
| 67 | out: string; |
| 68 | explain?: boolean; |
| 69 | hints?: Array<Hint>; |
| 70 | }; |
| 71 | |
| 72 | type UpOptions = { dialect: string; out: string }; |
| 73 | |
| 74 | type GenerateResult = |
| 75 | | { status: "ok" } |
| 76 | | { status: "no_changes" } |
| 77 | | { status: "missing_hints"; unresolved: ReadonlyArray<MissingHint> }; |
| 78 | |
| 79 | type UpResult = { status: "ok"; upgraded: ReadonlyArray<string> }; |
| 80 | |
| 81 | type CommandFailed = { status: "error"; error: unknown }; |
| 82 | |
| 83 | // The `drizzle-kit` commands Drizzle.Schema runs: `[options, result]`. |
| 84 | type Commands = { |
| 85 | generate: [GenerateOptions, GenerateResult]; |
| 86 | up: [UpOptions, UpResult]; |
| 87 | }; |
| 88 | |
| 89 | // `drizzle-kit/cli`: the commands as functions. They never prompt and report |
| 90 | // results (including unanswered questions) as values. |
| 91 | type DrizzleKitCli = { |
| 92 | [K in keyof Commands]: (options: Commands[K][0]) => Promise<Commands[K][1] | CommandFailed>; |
| 93 | }; |
| 94 | |
| 95 | // `drizzle-kit/payload/*`: snapshot builders. Only postgres has `reconstruct`. |
| 96 | type DrizzleKitPayload = { |
| 97 | generateDrizzleJson: (imports: Record.ReadonlyRecord<string, unknown>) => Promise<Snapshot>; |
| 98 | reconstruct?: (files: Record.ReadonlyRecord<string, Snapshot>, id?: string) => Promise<Snapshot>; |
| 99 | }; |
| 100 | |
| 101 | class DrizzleKitLoadFailed extends Data.TaggedError("DrizzleKitLoadFailed")<{ |
| 102 | readonly module: string; |
| 103 | readonly cause: unknown; |
| 104 | }> { |
| 105 | override get message() { |
| 106 | return `Failed to load ${this.module} (is drizzle-kit installed?): ${this.cause instanceof Error ? this.cause.message : String(this.cause)}`; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | class InvalidSnapshot extends Data.TaggedError("InvalidSnapshot")<{ |
| 111 | readonly file: string; |
| 112 | readonly cause: S.SchemaError; |
| 113 | }> { |
| 114 | override get message() { |
| 115 | return `Invalid drizzle snapshot at ${this.file}: ${this.cause.message}`; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | class SchemaImportFailed extends Data.TaggedError("SchemaImportFailed")<{ |
| 120 | readonly schema: string; |
| 121 | readonly cause: unknown; |
| 122 | }> { |
| 123 | override get message() { |
| 124 | return `Failed to import schema at ${this.schema}: ${this.cause instanceof Error ? this.cause.message : String(this.cause)}`; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | class DrizzleKitFailed extends Data.TaggedError("DrizzleKitFailed")<{ |
| 129 | readonly command: string; |
| 130 | readonly cause: unknown; |
| 131 | }> { |
| 132 | override get message() { |
| 133 | // Thrown errors carry their own message; reported ones (`{ status: |
| 134 | // "error", error }`) are plain objects like `{ code, kind, snapshot }`. |
| 135 | const { cause } = this; |
| 136 | const detail = cause instanceof Error ? cause.message : JSON.stringify(cause); |
| 137 | return `drizzle-kit ${this.command} failed: ${detail}`; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | class DrizzleKitExited extends Data.TaggedError("DrizzleKitExited")<{ |
| 142 | readonly command: string; |
| 143 | }> { |
| 144 | override get message() { |
| 145 | return `drizzle-kit ${this.command} found errors in the schema (such as two columns with one name) and tried to exit the process`; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** An ambiguous schema change with no terminal to ask. */ |
| 150 | class UndecidedSchemaChange extends Data.TaggedError("UndecidedSchemaChange")<{ |
| 151 | readonly props: SchemaProps; |
| 152 | readonly hint: MissingHint; |
| 153 | }> { |
| 154 | override get message() { |
| 155 | const { props, hint } = this; |
| 156 | return [ |
| 157 | `${props.schema} has an ambiguous change that needs a decision:`, |
| 158 | ` ${formatEntity(hint.entity)}: created or renamed?`, |
| 159 | "Re-run in an interactive terminal to answer it.", |
| 160 | ].join("\n"); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | export type SchemaProps = { |
| 165 | /** |
| 166 | * Path to the schema module, relative to the current working directory. |
| 167 | * The module is loaded via dynamic `import()` so drizzle-kit can introspect |
| 168 | * the table definitions, then diffed against the latest snapshot under |
| 169 | * `out` to detect changes. |
| 170 | * |
| 171 | * @example "./src/schema.ts" |
| 172 | */ |
| 173 | schema: string; |
| 174 | /** |
| 175 | * Output directory for generated migrations, in drizzle-kit's own layout: |
| 176 | * `{out}/{timestamp}_{name}/{migration.sql, snapshot.json}`. Pass this |
| 177 | * through as a database resource's `migrations` prop (`Neon.Branch`, |
| 178 | * `Fly.Postgres`, `Cloudflare.D1.Database`, …) to apply pending |
| 179 | * migrations on deploy. |
| 180 | * |
| 181 | * @default "./migrations" |
| 182 | */ |
| 183 | out?: string; |
| 184 | /** |
| 185 | * SQL dialect to generate migrations for. |
| 186 | * |
| 187 | * @default "postgres" |
| 188 | */ |
| 189 | dialect?: Dialect; |
| 190 | }; |
| 191 | |
| 192 | export type Schema = Resource< |
| 193 | "Drizzle.Schema", |
| 194 | SchemaProps, |
| 195 | { |
| 196 | /** Path to the migrations directory, relative to the current working directory. */ |
| 197 | out: string; |
| 198 | /** |
| 199 | * sha256 of the latest snapshot.json. Stable across deploys when the |
| 200 | * schema is unchanged; bumps trigger an update which regenerates pending |
| 201 | * migration SQL. Downstream `migrationsDir` consumers read this to |
| 202 | * detect drift and reapply. |
| 203 | */ |
| 204 | snapshotHash: string; |
| 205 | /** Names of all migration directories under `out`, in order. */ |
| 206 | migrations: string[]; |
| 207 | }, |
| 208 | never, |
| 209 | Providers |
| 210 | >; |
| 211 | |
| 212 | /** |
| 213 | * A Drizzle schema managed as an Alchemy resource. |
| 214 | * |
| 215 | * Runs `drizzle-kit generate` through drizzle-kit's programmatic API so |
| 216 | * migration SQL is regenerated as part of `alchemy deploy` whenever the |
| 217 | * source schema changes. Ambiguous changes (a rename or a new column/table?) |
| 218 | * are asked with Alchemy's prompts during plan. The output directory is |
| 219 | * intended to be passed straight to a database resource's `migrations` |
| 220 | * prop, giving you a single deploy-driven flow: |
| 221 | * |
| 222 | * ```typescript |
| 223 | * const schema = yield* Drizzle.Schema("app-schema", { |
| 224 | * schema: "./src/schema.ts", |
| 225 | * }); |
| 226 | * |
| 227 | * const branch = yield* Neon.Branch("app-branch", { |
| 228 | * project, |
| 229 | * migrations: schema, |
| 230 | * }); |
| 231 | * |
| 232 | * const db = yield* Fly.Postgres("Db", { |
| 233 | * region: "iad", |
| 234 | * migrations: schema, |
| 235 | * }); |
| 236 | * ``` |
| 237 | * |
| 238 | * `Drizzle.Schema` runs first (because the database resource depends |
| 239 | * on its `out` output), regenerates pending migration files, and the |
| 240 | * database resource then applies them. |
| 241 | * |
| 242 | * The resource is delete-safe: removing it from the stack does **not** wipe |
| 243 | * the migrations directory, since migration files are typically checked in |
| 244 | * and shared with other environments. |
| 245 | * |
| 246 | * @resource |
| 247 | */ |
| 248 | export const Schema = Resource<Schema>("Drizzle.Schema"); |
| 249 | |
| 250 | const cliDialect = (props: SchemaProps) => |
| 251 | props.dialect === undefined || props.dialect === "postgres" ? "postgresql" : props.dialect; |
| 252 | |
| 253 | const sha = (s: string) => crypto.createHash("sha256").update(s).digest("hex"); |
| 254 | |
| 255 | const importModule = <A>(module: string) => |
| 256 | Effect.tryPromise({ |
| 257 | try: () => import(/* @vite-ignore */ module) as Promise<A>, |
| 258 | catch: (cause) => new DrizzleKitLoadFailed({ module, cause }), |
| 259 | }); |
| 260 | |
| 261 | // drizzle-kit runs in-process but calls `process.exit` on schema errors. |
| 262 | // Calls trap the exit and fail instead, one at a time since the trap swaps |
| 263 | // the global. |
| 264 | const exitTrap = Semaphore.makeUnsafe(1); |
| 265 | |
| 266 | const callDrizzleKit = <A>(command: string, run: () => Promise<A>) => |
| 267 | Effect.acquireUseRelease( |
| 268 | Effect.sync(() => { |
| 269 | // oxlint-disable-next-line typescript/unbound-method -- saved only to restore `process.exit` verbatim, never called unbound |
| 270 | const trap = { exit: process.exit, exited: false }; |
| 271 | process.exit = () => { |
| 272 | trap.exited = true; |
| 273 | throw new Error("drizzle-kit exited"); |
| 274 | }; |
| 275 | return trap; |
| 276 | }), |
| 277 | (trap) => |
| 278 | // `cli` commands catch the trap's throw and report it as an error result. |
| 279 | // Uninterruptible: the promise can't be cancelled, so the trap stays |
| 280 | // until it settles. |
| 281 | Effect.tryPromise({ |
| 282 | try: run, |
| 283 | catch: (cause) => |
| 284 | trap.exited |
| 285 | ? new DrizzleKitExited({ command }) |
| 286 | : new DrizzleKitFailed({ command, cause }), |
| 287 | }).pipe( |
| 288 | Effect.flatMap((result) => |
| 289 | trap.exited ? Effect.fail(new DrizzleKitExited({ command })) : Effect.succeed(result), |
| 290 | ), |
| 291 | Effect.uninterruptible, |
| 292 | ), |
| 293 | (trap) => |
| 294 | Effect.sync(() => { |
| 295 | process.exit = trap.exit; |
| 296 | }), |
| 297 | ).pipe((effect) => exitTrap.withPermit(effect)); |
| 298 | |
| 299 | // A `drizzle-kit/cli` command as an Effect, failing on a thrown or reported error. |
| 300 | const command = <K extends keyof Commands>(name: K) => |
| 301 | Effect.fn(`drizzleKit.${name}`)(function* (options: Commands[K][0]) { |
| 302 | const cli = yield* importModule<DrizzleKitCli>("drizzle-kit/cli"); |
| 303 | const result = yield* callDrizzleKit(name, () => cli[name](options)); |
| 304 | if (result.status === "error") { |
| 305 | return yield* new DrizzleKitFailed({ command: name, cause: result.error }); |
| 306 | } |
| 307 | return result as Commands[K][1]; |
| 308 | }); |
| 309 | |
| 310 | const drizzleKit = { |
| 311 | /** `drizzle-kit generate`; with `explain` a dry run that writes nothing. */ |
| 312 | generate: command("generate"), |
| 313 | /** `drizzle-kit up`: upgrade snapshots to the current format. */ |
| 314 | up: command("up"), |
| 315 | }; |
| 316 | |
| 317 | export const SchemaProvider = () => |
| 318 | Provider.effect( |
| 319 | Schema, |
| 320 | Effect.gen(function* () { |
| 321 | const fs = yield* FileSystem.FileSystem; |
| 322 | const path = yield* Path.Path; |
| 323 | |
| 324 | const resolveOut = (p: SchemaProps) => path.resolve(process.cwd(), p.out ?? "./migrations"); |
| 325 | |
| 326 | // The `out` attribute is exposed as a path relative to the current |
| 327 | // working directory so that persisted state stays portable across |
| 328 | // machines/checkouts. Internal filesystem ops always use the absolute |
| 329 | // `resolveOut` form. |
| 330 | const relativeOut = (abs: string) => path.relative(process.cwd(), abs); |
| 331 | |
| 332 | const resolveSchema = (p: SchemaProps) => path.resolve(process.cwd(), p.schema); |
| 333 | // drizzle-kit reads `schema` as a glob, where `\` is an escape. |
| 334 | const schemaGlob = (p: SchemaProps) => resolveSchema(p).replaceAll("\\", "/"); |
| 335 | |
| 336 | // List `<ts>_*` migration directories under `out`, sorted by numeric |
| 337 | // prefix. Returns an empty array if `out` doesn't exist. |
| 338 | const listMigrationDirs = Effect.fn("Drizzle.listMigrationDirs")(function* (out: string) { |
| 339 | if (!(yield* fs.exists(out))) return []; |
| 340 | const entries = yield* fs.readDirectory(out); |
| 341 | return entries.filter((name) => /^\d+_/.test(name)).sort(); |
| 342 | }); |
| 343 | |
| 344 | const readSnapshots = Effect.fn("Drizzle.readSnapshots")(function* (out: string) { |
| 345 | const entries = yield* Effect.forEach(yield* listMigrationDirs(out), (dir) => |
| 346 | Effect.gen(function* () { |
| 347 | const file = path.join(out, dir, "snapshot.json"); |
| 348 | if (!(yield* fs.exists(file))) return []; |
| 349 | const text = yield* fs.readFileString(file); |
| 350 | const snapshot = yield* decodeSnapshot(text).pipe( |
| 351 | Effect.mapError((cause) => new InvalidSnapshot({ file, cause })), |
| 352 | ); |
| 353 | return [[dir, { snapshot, text }] as const]; |
| 354 | }), |
| 355 | ); |
| 356 | return Record.fromEntries(entries.flat()); |
| 357 | }); |
| 358 | |
| 359 | // The latest snapshot is the head of the `prevIds` chain — the one no |
| 360 | // other snapshot points back to. Directory-name order alone is not |
| 361 | // enough: two migrations generated within the same second share the |
| 362 | // timestamp prefix, and the random name suffix then decides the sort. |
| 363 | const readLatestSnapshot = Effect.fn("Drizzle.readLatestSnapshot")(function* (out: string) { |
| 364 | const entries = Record.values(yield* readSnapshots(out)); |
| 365 | if (entries.length === 0) return undefined; |
| 366 | const referenced = new Set(entries.flatMap((entry) => entry.snapshot.prevIds ?? [])); |
| 367 | const head = entries.find( |
| 368 | (entry) => entry.snapshot.id !== undefined && !referenced.has(entry.snapshot.id), |
| 369 | ); |
| 370 | // Fall back to name order for snapshots without id/prevIds chains. |
| 371 | const latest = head ?? entries[entries.length - 1]!; |
| 372 | return { snapshot: latest.snapshot, hash: sha(latest.text) }; |
| 373 | }); |
| 374 | |
| 375 | /** |
| 376 | * Bring a migrations folder up to drizzle-kit's current snapshot format |
| 377 | * with `drizzle-kit up`. Returns whether anything changed. |
| 378 | * |
| 379 | * Earlier Alchemy versions wrote every postgres snapshot as a full |
| 380 | * version-9 snapshot without a `type`. That is the version-8 shape, which |
| 381 | * `up` turns into drizzle-kit's base/diff chain, so relabel them first. |
| 382 | */ |
| 383 | const upgradeFolder = Effect.fn("Drizzle.upgradeFolder")(function* ( |
| 384 | props: SchemaProps, |
| 385 | out: string, |
| 386 | ) { |
| 387 | if (!(yield* fs.exists(out))) return false; |
| 388 | if (cliDialect(props) === "postgresql") { |
| 389 | for (const [dir, { snapshot }] of Record.toEntries(yield* readSnapshots(out))) { |
| 390 | if (snapshot.type !== undefined || snapshot.version !== "9") continue; |
| 391 | yield* fs.writeFileString( |
| 392 | path.join(out, dir, "snapshot.json"), |
| 393 | JSON.stringify({ ...snapshot, version: "8", renames: [] }, null, 2), |
| 394 | ); |
| 395 | } |
| 396 | } |
| 397 | const { upgraded } = yield* drizzleKit.up({ dialect: cliDialect(props), out }); |
| 398 | return upgraded.length > 0; |
| 399 | }); |
| 400 | |
| 401 | // The schema the migrations folder describes. Postgres folders are diff |
| 402 | // chains drizzle-kit rebuilds; other dialects store full snapshots. |
| 403 | const readPreviousSnapshot = Effect.fn("Drizzle.readPreviousSnapshot")(function* ( |
| 404 | kit: DrizzleKitPayload, |
| 405 | out: string, |
| 406 | ) { |
| 407 | const reconstruct = kit.reconstruct; |
| 408 | if (reconstruct === undefined) return (yield* readLatestSnapshot(out))?.snapshot; |
| 409 | const files = Record.map(yield* readSnapshots(out), ({ snapshot }) => snapshot); |
| 410 | if (Record.isEmptyRecord(files)) return undefined; |
| 411 | const parents = new Set(Record.values(files).flatMap((file) => file.prevIds ?? [])); |
| 412 | const leaves = Record.values(files).filter( |
| 413 | ({ id }) => id !== undefined && !parents.has(id), |
| 414 | ); |
| 415 | if (leaves.length < 2) { |
| 416 | return yield* callDrizzleKit("reconstruct", () => reconstruct(files)); |
| 417 | } |
| 418 | // Unmerged branches: reconstruct an empty merge of their leaves, the |
| 419 | // combined state drizzle-kit's generate diffs against. |
| 420 | const merge = { |
| 421 | ...leaves[0]!, |
| 422 | id: MERGE_ID, |
| 423 | prevIds: leaves.map(({ id }) => id!), |
| 424 | deltas: [], |
| 425 | }; |
| 426 | return yield* callDrizzleKit("reconstruct", () => |
| 427 | reconstruct({ ...files, [`99999999999999_${MERGE_ID}`]: merge }, MERGE_ID), |
| 428 | ); |
| 429 | }); |
| 430 | |
| 431 | /** |
| 432 | * The previous and current schema, for working out which entities a |
| 433 | * rename-or-create question could have been renamed from. |
| 434 | */ |
| 435 | const loadRenameContext = Effect.fn("Drizzle.loadRenameContext")(function* ( |
| 436 | props: SchemaProps, |
| 437 | out: string, |
| 438 | ) { |
| 439 | const dialect = props.dialect ?? "postgres"; |
| 440 | const kit = yield* importModule<DrizzleKitPayload>(`drizzle-kit/payload/${dialect}`); |
| 441 | const schemaModule = yield* Effect.tryPromise({ |
| 442 | try: () => |
| 443 | import(/* @vite-ignore */ resolveSchema(props)) as Promise< |
| 444 | Record.ReadonlyRecord<string, unknown> |
| 445 | >, |
| 446 | catch: (cause) => new SchemaImportFailed({ schema: props.schema, cause }), |
| 447 | }); |
| 448 | const cur = yield* callDrizzleKit("generateDrizzleJson", () => |
| 449 | kit.generateDrizzleJson(schemaModule), |
| 450 | ); |
| 451 | const prev = yield* readPreviousSnapshot(kit, out); |
| 452 | return { prev: prev?.ddl ?? [], cur: cur.ddl ?? [] }; |
| 453 | }); |
| 454 | |
| 455 | /** |
| 456 | * Dry-run `drizzle-kit generate` until it has no unanswered questions, |
| 457 | * asking the user each rename-or-create question it reports. The SQL is |
| 458 | * applied to the real database later in the same deploy, so ambiguous |
| 459 | * changes are never answered automatically. |
| 460 | */ |
| 461 | const resolveHints = Effect.fn("Drizzle.resolveHints")(function* ( |
| 462 | props: SchemaProps, |
| 463 | out: string, |
| 464 | ) { |
| 465 | const hints: Array<Hint> = []; |
| 466 | const context = yield* Effect.cached(loadRenameContext(props, out)); |
| 467 | // `explain` dry-runs generate: it reports the pending migration |
| 468 | // (`ok`), `no_changes`, or the questions it still needs answered |
| 469 | // (`missing_hints`) without writing anything. Answers can raise new |
| 470 | // questions (columns of a renamed table), so ask until none are left. |
| 471 | const result = yield* Effect.gen(function* () { |
| 472 | const result = yield* drizzleKit.generate({ |
| 473 | dialect: cliDialect(props), |
| 474 | schema: schemaGlob(props), |
| 475 | out, |
| 476 | explain: true, |
| 477 | hints, |
| 478 | }); |
| 479 | if (result.status === "missing_hints") { |
| 480 | const { prev, cur } = yield* context; |
| 481 | for (const hint of result.unresolved) { |
| 482 | hints.push(yield* askHint(props, hint, prev, cur, hints)); |
| 483 | } |
| 484 | } |
| 485 | return result; |
| 486 | }).pipe(Effect.repeat({ until: (result) => result.status !== "missing_hints" })); |
| 487 | return { hints, changed: result.status === "ok" }; |
| 488 | }); |
| 489 | |
| 490 | /** |
| 491 | * Decide what a deploy would change, without touching `out`: upgrade a |
| 492 | * scratch copy of the folder, then resolve every question drizzle-kit |
| 493 | * has about the schema change. Cached so `diff` and `reconcile` share |
| 494 | * one set of answers. |
| 495 | */ |
| 496 | const detectDrift = Effect.fn("Drizzle.detectDrift")( |
| 497 | function* (props: SchemaProps) { |
| 498 | const out = resolveOut(props); |
| 499 | const scratch = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-drizzle-" }); |
| 500 | const migrations = path.join(scratch, "migrations"); |
| 501 | if (yield* fs.exists(out)) yield* fs.copy(out, migrations); |
| 502 | const upgraded = yield* upgradeFolder(props, migrations); |
| 503 | const { hints, changed } = yield* resolveHints(props, migrations); |
| 504 | return { out, upgraded, hints, changed }; |
| 505 | }, |
| 506 | // Keyed by props: an interrupted create's recovery `read` sees the |
| 507 | // persisted props, which may differ from the ones being deployed. |
| 508 | (effect, props) => |
| 509 | effect.pipe( |
| 510 | Effect.scoped, |
| 511 | Artifacts.cached( |
| 512 | `detectDrift:${cliDialect(props)}:${resolveSchema(props)}:${resolveOut(props)}`, |
| 513 | ), |
| 514 | ), |
| 515 | ); |
| 516 | |
| 517 | /** |
| 518 | * Upgrade `out` if needed and generate the pending migration with the |
| 519 | * answers gathered by {@link detectDrift}. |
| 520 | */ |
| 521 | const regenerate = Effect.fn("Drizzle.regenerate")(function* (props: SchemaProps) { |
| 522 | const { out, upgraded, hints, changed } = yield* detectDrift(props); |
| 523 | if (upgraded) yield* upgradeFolder(props, out); |
| 524 | if (changed) { |
| 525 | yield* fs.makeDirectory(out, { recursive: true }); |
| 526 | const result = yield* drizzleKit.generate({ |
| 527 | dialect: cliDialect(props), |
| 528 | schema: schemaGlob(props), |
| 529 | out, |
| 530 | hints, |
| 531 | }); |
| 532 | if (result.status === "missing_hints") { |
| 533 | return yield* new UndecidedSchemaChange({ props, hint: result.unresolved[0]! }); |
| 534 | } |
| 535 | } |
| 536 | const latest = yield* readLatestSnapshot(out); |
| 537 | return { |
| 538 | out: relativeOut(out), |
| 539 | snapshotHash: latest?.hash ?? sha(""), |
| 540 | migrations: yield* listMigrationDirs(out), |
| 541 | }; |
| 542 | }); |
| 543 | |
| 544 | return { |
| 545 | // Non-listable: a Drizzle.Schema is a local build artifact (generated |
| 546 | // migration SQL under `out`, a path supplied entirely by props with no |
| 547 | // account/store to enumerate). There is no remote enumeration API, so |
| 548 | // there is nothing to discover out-of-band. |
| 549 | list: () => Effect.succeed([]), |
| 550 | diff: Effect.fn(function* ({ news, output }) { |
| 551 | if (!isResolved(news)) return undefined; |
| 552 | if (!output) { |
| 553 | // Re-driving an interrupted create: answer questions now, not |
| 554 | // during apply. |
| 555 | yield* detectDrift(news); |
| 556 | return undefined; |
| 557 | } |
| 558 | // Only flag an update when drizzle-kit would emit new SQL (or the |
| 559 | // folder needs upgrading) — otherwise downstream resources (e.g. |
| 560 | // Neon.Branch) would see `schema.out` as an unresolved Output during |
| 561 | // plan and cascade into spurious updates of their own. |
| 562 | const { changed, upgraded } = yield* detectDrift(news); |
| 563 | // Originally `output.out` was an absolute path, which is not portable. |
| 564 | // So, we trigger an update to migrate existing resources to the |
| 565 | // canonical (cwd-relative) form. This is safe because `regenerate` |
| 566 | // is idempotent. Compare against the canonical form rather than |
| 567 | // testing `isAbsolute`: on Windows a cross-drive `out` can never be |
| 568 | // relativized, so its canonical form IS absolute and an isAbsolute |
| 569 | // check would flag an update on every deploy. |
| 570 | return changed || upgraded || output.out !== relativeOut(resolveOut(news)) |
| 571 | ? { action: "update" } |
| 572 | : undefined; |
| 573 | }), |
| 574 | read: Effect.fn(function* ({ olds, output }) { |
| 575 | if (!output) { |
| 576 | // A create never reaches `diff`; plan only calls `read` to look |
| 577 | // for something to adopt. Ask any rename questions now so they |
| 578 | // are answered before apply starts changing other resources. |
| 579 | if (olds && isResolved(olds)) yield* detectDrift(olds); |
| 580 | return undefined; |
| 581 | } |
| 582 | const out = resolveOut(olds ?? ({} as SchemaProps)); |
| 583 | const exists = yield* fs.exists(out); |
| 584 | if (!exists) return undefined; |
| 585 | const latest = yield* readLatestSnapshot(out); |
| 586 | const migrations = yield* listMigrationDirs(out); |
| 587 | return { |
| 588 | out: relativeOut(out), |
| 589 | snapshotHash: latest?.hash ?? output.snapshotHash, |
| 590 | migrations, |
| 591 | }; |
| 592 | }), |
| 593 | reconcile: Effect.fn(function* ({ news, output, session }) { |
| 594 | yield* session.note( |
| 595 | `${output ? "Regenerating" : "Generating"} drizzle migrations for ${news.schema}`, |
| 596 | ); |
| 597 | return yield* regenerate(news); |
| 598 | }), |
| 599 | delete: Effect.fn(function* () { |
| 600 | // Migrations are typically checked in; do not delete on resource |
| 601 | // teardown. |
| 602 | }), |
| 603 | }; |
| 604 | }), |
| 605 | ); |
| 606 | |
| 607 | // drizzle-kit hint kinds → snapshot `ddl` entity types. |
| 608 | const ddlEntityTypes: Record.ReadonlyRecord<string, string> = { |
| 609 | table: "tables", |
| 610 | column: "columns", |
| 611 | index: "indexes", |
| 612 | enum: "enums", |
| 613 | sequence: "sequences", |
| 614 | view: "views", |
| 615 | schema: "schemas", |
| 616 | policy: "policies", |
| 617 | role: "roles", |
| 618 | check: "checks", |
| 619 | unique: "uniques", |
| 620 | primary_key: "pks", |
| 621 | "foreign key": "fks", |
| 622 | }; |
| 623 | |
| 624 | const MERGE_ID = "alchemy-unmerged-branches"; |
| 625 | |
| 626 | // mysql and sqlite have no schemas; drizzle-kit fills ids with `public`. |
| 627 | const DEFAULT_SCHEMA = "public"; |
| 628 | |
| 629 | const formatEntity = (entity: ReadonlyArray<string>) => |
| 630 | (entity.length > 1 && entity[0] === DEFAULT_SCHEMA ? entity.slice(1) : entity).join("."); |
| 631 | |
| 632 | type Located = { entityType: string; schema: string; table: string; name: string }; |
| 633 | |
| 634 | const locate = (row: DdlEntry) => ({ |
| 635 | entityType: row.entityType, |
| 636 | schema: row.schema ?? DEFAULT_SCHEMA, |
| 637 | table: row.table ?? "", |
| 638 | name: row.name ?? "", |
| 639 | }); |
| 640 | |
| 641 | const idOf = (row: Located, arity: number) => |
| 642 | Match.value(arity).pipe( |
| 643 | Match.when(1, () => [row.name]), |
| 644 | Match.when(2, () => [row.schema, row.name]), |
| 645 | Match.orElse(() => [row.schema, row.table, row.name]), |
| 646 | ); |
| 647 | |
| 648 | type Rename = Extract<Hint, { type: "rename" }>; |
| 649 | |
| 650 | // Give `row` the id `to`, shaped like the `from` id it was matched by. |
| 651 | const withId = (row: Located, to: ReadonlyArray<string>) => |
| 652 | Match.value(to.length).pipe( |
| 653 | Match.when(1, () => ({ ...row, name: to[0]! })), |
| 654 | Match.when(2, () => ({ ...row, schema: to[0]!, name: to[1]! })), |
| 655 | Match.orElse(() => ({ ...row, schema: to[0]!, table: to[1]!, name: to[2]! })), |
| 656 | ); |
| 657 | |
| 658 | const renameRow = (row: Located, rename: Rename) => { |
| 659 | const { from, to } = rename; |
| 660 | return Match.value(rename).pipe( |
| 661 | // Schema rows name themselves; everything else names its schema. |
| 662 | Match.when({ kind: "schema" }, () => |
| 663 | Match.value(row).pipe( |
| 664 | Match.when({ entityType: "schemas", name: from[0] }, () => ({ ...row, name: to[0]! })), |
| 665 | Match.when({ entityType: (type) => type !== "schemas", schema: from[0] }, () => ({ |
| 666 | ...row, |
| 667 | schema: to[0]!, |
| 668 | })), |
| 669 | Match.orElse(() => row), |
| 670 | ), |
| 671 | ), |
| 672 | // The table and everything inside it follow the rename (or move). |
| 673 | Match.when({ kind: "table" }, () => |
| 674 | Match.value(row).pipe( |
| 675 | Match.when({ entityType: "tables", schema: from[0], name: from[1] }, () => withId(row, to)), |
| 676 | Match.when({ schema: from[0], table: from[1] }, () => ({ |
| 677 | ...row, |
| 678 | schema: to[0]!, |
| 679 | table: to[1]!, |
| 680 | })), |
| 681 | Match.orElse(() => row), |
| 682 | ), |
| 683 | ), |
| 684 | Match.orElse(({ kind }) => |
| 685 | row.entityType === ddlEntityTypes[kind] && |
| 686 | JSON.stringify(idOf(row, from.length)) === JSON.stringify(from) |
| 687 | ? withId(row, to) |
| 688 | : row, |
| 689 | ), |
| 690 | ); |
| 691 | }; |
| 692 | |
| 693 | /** |
| 694 | * Carry previous entities through the renames answered so far, so a later |
| 695 | * question about the renamed table (or a table moved to another schema) |
| 696 | * matches the entities that moved with it. |
| 697 | */ |
| 698 | const applyRenames = (rows: ReadonlyArray<Located>, hints: ReadonlyArray<Hint>) => { |
| 699 | let current = rows; |
| 700 | for (const hint of hints) { |
| 701 | if (hint.type === "rename") current = current.map((row) => renameRow(row, hint)); |
| 702 | } |
| 703 | return current; |
| 704 | }; |
| 705 | |
| 706 | /** |
| 707 | * Entities a rename-or-create question could have been renamed from: same |
| 708 | * kind, present before (after the renames answered so far), gone now. |
| 709 | * Columns, indexes, … are offered from the same table first. |
| 710 | */ |
| 711 | const renameCandidates = ( |
| 712 | hint: MissingHint, |
| 713 | prev: ReadonlyArray<DdlEntry>, |
| 714 | cur: ReadonlyArray<DdlEntry>, |
| 715 | hints: ReadonlyArray<Hint>, |
| 716 | ) => { |
| 717 | const entityType = ddlEntityTypes[hint.kind]; |
| 718 | const arity = hint.entity.length; |
| 719 | const ofKind = (rows: ReadonlyArray<Located>) => |
| 720 | rows.filter((row) => row.entityType === entityType); |
| 721 | const current = new Set(ofKind(cur.map(locate)).map((row) => JSON.stringify(idOf(row, arity)))); |
| 722 | const gone = ofKind(applyRenames(prev.map(locate), hints)) |
| 723 | .map((row) => idOf(row, arity)) |
| 724 | .filter((id) => !current.has(JSON.stringify(id))); |
| 725 | const sameParent = gone.filter( |
| 726 | (id) => arity < 3 || (id[0] === hint.entity[0] && id[1] === hint.entity[1]), |
| 727 | ); |
| 728 | return sameParent.length > 0 ? sameParent : gone; |
| 729 | }; |
| 730 | |
| 731 | /** |
| 732 | * Answer one drizzle-kit `MissingHint` with an Alchemy prompt. drizzle-kit |
| 733 | * only asks when a rename is possible, so the question is always shown — |
| 734 | * never answered with "create" on the user's behalf. A dismissed prompt stays |
| 735 | * a `TerminalCancelled` so the CLI exits quietly, the same as cancelling the |
| 736 | * deploy confirmation. |
| 737 | */ |
| 738 | const askHint = Effect.fn("Drizzle.askHint")( |
| 739 | function* ( |
| 740 | props: SchemaProps, |
| 741 | hint: MissingHint, |
| 742 | prev: ReadonlyArray<DdlEntry>, |
| 743 | cur: ReadonlyArray<DdlEntry>, |
| 744 | hints: ReadonlyArray<Hint>, |
| 745 | ) { |
| 746 | const interaction = yield* Effect.serviceOption(Interaction); |
| 747 | if (Option.isNone(interaction)) return yield* new UndecidedSchemaChange({ props, hint }); |
| 748 | |
| 749 | const name = formatEntity(hint.entity); |
| 750 | return yield* interaction.value.prompt.select<Hint>({ |
| 751 | message: `Is ${hint.kind} ${name} created or renamed from another ${hint.kind}?`, |
| 752 | options: [ |
| 753 | { |
| 754 | value: { type: "create", kind: hint.kind, entity: hint.entity }, |
| 755 | label: `+ ${name}`, |
| 756 | description: `create ${hint.kind}`, |
| 757 | }, |
| 758 | ...renameCandidates(hint, prev, cur, hints).map((from) => ({ |
| 759 | value: { type: "rename", kind: hint.kind, from, to: hint.entity } satisfies Hint, |
| 760 | label: `~ ${formatEntity(from)} › ${name}`, |
| 761 | description: `rename ${hint.kind}`, |
| 762 | })), |
| 763 | ], |
| 764 | descriptionPlacement: "inline", |
| 765 | }); |
| 766 | }, |
| 767 | (effect, props, hint) => |
| 768 | effect.pipe( |
| 769 | Effect.catchTag("NonInteractiveTerminal", () => |
| 770 | Effect.fail(new UndecidedSchemaChange({ props, hint })), |
| 771 | ), |
| 772 | ), |
| 773 | ); |