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
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import { |
| 3 | convertedRowInsertSql, |
| 4 | findForeignHistory, |
| 5 | matchForeignRows, |
| 6 | toTimestampString, |
| 7 | } from "./Convert.ts"; |
| 8 | import { |
| 9 | describeRewrittenHistory, |
| 10 | MigrationError, |
| 11 | RewrittenMigrationHistoryError, |
| 12 | type MigrationApplyError, |
| 13 | type MigrationDialect, |
| 14 | type MigrationHistoryConflictError, |
| 15 | type MigrationRecord, |
| 16 | type SqlExecutor, |
| 17 | } from "./Format.ts"; |
| 18 | import { classifyTable, tableColumns } from "./Introspect.ts"; |
| 19 | import { bookkeepingColumns, quoteIdentifier, scalarText, sqlLiteral } from "./Utils.ts"; |
| 20 | |
| 21 | export const ALCHEMY_DEFAULT_TABLE = "__alchemy_migrations"; |
| 22 | |
| 23 | /** |
| 24 | * THE Alchemy applied-migrations table: `id, hash, created_at, name, |
| 25 | * applied_at`, name-keyed detection. This is deliberately drizzle's column |
| 26 | * shape — a database whose history lives in `__drizzle_migrations` adopts |
| 27 | * with a verbatim row copy — but Alchemy owns the table and it is the only |
| 28 | * format Alchemy ever writes. Migrating from drizzle/prisma/wrangler |
| 29 | * bookkeeping is a one-way conversion performed once (see `Convert.ts`). |
| 30 | */ |
| 31 | const createTableSql = ( |
| 32 | table: string, |
| 33 | dialect: MigrationDialect, |
| 34 | id?: SqlExecutor["migrationTableId"], |
| 35 | ): string => { |
| 36 | const quoted = quoteIdentifier(table, dialect); |
| 37 | switch (dialect) { |
| 38 | case "sqlite": |
| 39 | return `CREATE TABLE IF NOT EXISTS ${quoted} ( |
| 40 | id INTEGER PRIMARY KEY, |
| 41 | hash text NOT NULL, |
| 42 | created_at numeric, |
| 43 | name text, |
| 44 | applied_at TEXT |
| 45 | );`; |
| 46 | case "postgres": |
| 47 | if (id === "spanner") { |
| 48 | return `CREATE TABLE IF NOT EXISTS ${quoted} ( |
| 49 | id varchar(36) NOT NULL DEFAULT spanner.generate_uuid(), |
| 50 | hash text NOT NULL, |
| 51 | created_at bigint, |
| 52 | name text, |
| 53 | applied_at timestamptz DEFAULT now(), |
| 54 | PRIMARY KEY (id) |
| 55 | )`; |
| 56 | } |
| 57 | return `CREATE TABLE IF NOT EXISTS ${quoted} ( |
| 58 | id ${id === "uuid" ? "uuid DEFAULT gen_random_uuid()" : "SERIAL"} PRIMARY KEY, |
| 59 | hash text NOT NULL, |
| 60 | created_at bigint, |
| 61 | name text, |
| 62 | applied_at timestamp with time zone DEFAULT now() |
| 63 | );`; |
| 64 | case "googlesql": |
| 65 | return `CREATE TABLE IF NOT EXISTS ${quoted} ( |
| 66 | \`id\` STRING(36) NOT NULL DEFAULT (GENERATE_UUID()), |
| 67 | \`hash\` STRING(MAX) NOT NULL, |
| 68 | \`created_at\` INT64, |
| 69 | \`name\` STRING(MAX), |
| 70 | \`applied_at\` TIMESTAMP DEFAULT (CURRENT_TIMESTAMP()) |
| 71 | ) PRIMARY KEY (\`id\`)`; |
| 72 | case "mysql": |
| 73 | return `CREATE TABLE IF NOT EXISTS ${quoted} ( |
| 74 | id SERIAL PRIMARY KEY, |
| 75 | hash TEXT NOT NULL, |
| 76 | created_at BIGINT, |
| 77 | name TEXT, |
| 78 | applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 79 | );`; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | const insertSql = ( |
| 84 | table: string, |
| 85 | dialect: MigrationDialect, |
| 86 | record: Pick<MigrationRecord, "name" | "hash" | "createdAtMillis">, |
| 87 | ): string => { |
| 88 | const quoted = quoteIdentifier(table, dialect); |
| 89 | const applied = dialect === "sqlite" ? ", datetime('now')" : ""; |
| 90 | const columns = |
| 91 | dialect === "sqlite" |
| 92 | ? bookkeepingColumns(dialect, "hash", "created_at", "name", "applied_at") |
| 93 | : bookkeepingColumns(dialect, "hash", "created_at", "name"); |
| 94 | return `INSERT INTO ${quoted} (${columns}) VALUES (${sqlLiteral(record.hash, dialect)}, ${sqlLiteral(record.createdAtMillis ?? null, dialect)}, ${sqlLiteral(record.name, dialect)}${applied});`; |
| 95 | }; |
| 96 | |
| 97 | const renameSql = (from: string, to: string, dialect: MigrationDialect): string => |
| 98 | dialect === "mysql" |
| 99 | ? `RENAME TABLE ${quoteIdentifier(from, dialect)} TO ${quoteIdentifier(to, dialect)};` |
| 100 | : `ALTER TABLE ${quoteIdentifier(from, dialect)} RENAME TO ${quoteIdentifier(to, dialect)};`; |
| 101 | |
| 102 | /** |
| 103 | * Rebuild an in-place table (legacy Alchemy 3-column / oldest 2-column / |
| 104 | * wrangler-shaped) into the Alchemy shape, backfilling `hash` and |
| 105 | * `created_at` from local records matched by name. A recorded row with no |
| 106 | * matching local file is a hard error — mirrors the same rule conversion |
| 107 | * applies (see `matchForeignRows`). |
| 108 | */ |
| 109 | const rebuildInPlace = (options: { |
| 110 | executor: SqlExecutor; |
| 111 | table: string; |
| 112 | records: ReadonlyArray<MigrationRecord>; |
| 113 | /** SQL expression yielding the migration name from the old table. */ |
| 114 | nameExpr: string; |
| 115 | tool: "legacy-alchemy" | "wrangler"; |
| 116 | }) => |
| 117 | Effect.gen(function* () { |
| 118 | const { executor, table, records, nameExpr } = options; |
| 119 | if (executor.transactionalDdl === false) { |
| 120 | return yield* new MigrationError({ |
| 121 | message: `Cannot automatically rebuild migration history in "${table}" on a target without transactional DDL. Convert the history explicitly before deploying.`, |
| 122 | }); |
| 123 | } |
| 124 | const dialect = executor.dialect; |
| 125 | const quoted = quoteIdentifier(table, dialect); |
| 126 | const rows = yield* executor.query( |
| 127 | `SELECT ${nameExpr} AS name, applied_at FROM ${quoted} ORDER BY id;`, |
| 128 | ); |
| 129 | const matched = yield* matchForeignRows({ |
| 130 | history: { |
| 131 | tool: options.tool, |
| 132 | source: table, |
| 133 | rows: rows |
| 134 | .filter((row) => row.name !== null && row.name !== undefined) |
| 135 | .map((row) => ({ |
| 136 | name: String(row.name), |
| 137 | hash: undefined, |
| 138 | createdAtMillis: undefined, |
| 139 | appliedAt: toTimestampString(row.applied_at), |
| 140 | })), |
| 141 | }, |
| 142 | records, |
| 143 | }); |
| 144 | |
| 145 | const temp = `${table}_alchemy_upgrade`; |
| 146 | yield* executor.batch([ |
| 147 | `DROP TABLE IF EXISTS ${quoteIdentifier(temp, dialect)};`, |
| 148 | createTableSql(temp, dialect, executor.migrationTableId).replace( |
| 149 | "CREATE TABLE IF NOT EXISTS", |
| 150 | "CREATE TABLE", |
| 151 | ), |
| 152 | ...matched.map((row) => convertedRowInsertSql(temp, dialect, row)), |
| 153 | `DROP TABLE ${quoted};`, |
| 154 | renameSql(temp, table, dialect), |
| 155 | ]); |
| 156 | }); |
| 157 | |
| 158 | const ensureTable = (options: { |
| 159 | executor: SqlExecutor; |
| 160 | table: string; |
| 161 | records: ReadonlyArray<MigrationRecord>; |
| 162 | }) => |
| 163 | Effect.gen(function* () { |
| 164 | const { executor, table, records } = options; |
| 165 | const shape = classifyTable(yield* tableColumns(executor, table)); |
| 166 | switch (shape) { |
| 167 | case "absent": { |
| 168 | // Greenfield for us — but possibly not for the database. Adopt any |
| 169 | // history the previous tool (drizzle-kit / prisma / wrangler) left |
| 170 | // behind: copy it into our table ONCE and freeze theirs. One-way. |
| 171 | const history = yield* findForeignHistory({ executor, table }); |
| 172 | const converted = history ? yield* matchForeignRows({ history, records }) : []; |
| 173 | if (converted.length > 0 && executor.transactionalDdl === false) { |
| 174 | return yield* new MigrationError({ |
| 175 | message: `Cannot automatically copy foreign migration history into "${table}" without transactional DDL. Convert the history explicitly before deploying.`, |
| 176 | }); |
| 177 | } |
| 178 | yield* executor.batch([ |
| 179 | createTableSql(table, executor.dialect, executor.migrationTableId), |
| 180 | ...converted.map((row) => convertedRowInsertSql(table, executor.dialect, row)), |
| 181 | ]); |
| 182 | return; |
| 183 | } |
| 184 | case "drizzle-shaped": |
| 185 | // Already our column shape (shared with drizzle v1) — including |
| 186 | // the case where the user pointed `table` straight at an existing |
| 187 | // `__drizzle_migrations`. Adopt in place. |
| 188 | return; |
| 189 | case "legacy-alchemy": |
| 190 | yield* rebuildInPlace({ |
| 191 | executor, |
| 192 | table, |
| 193 | records, |
| 194 | nameExpr: "name", |
| 195 | tool: "legacy-alchemy", |
| 196 | }); |
| 197 | return; |
| 198 | case "legacy-2col": |
| 199 | yield* rebuildInPlace({ |
| 200 | executor, |
| 201 | table, |
| 202 | records, |
| 203 | nameExpr: "id", |
| 204 | tool: "legacy-alchemy", |
| 205 | }); |
| 206 | return; |
| 207 | case "wrangler": |
| 208 | // A wrangler table at OUR resolved table name (e.g. legacy D1 |
| 209 | // state pinned to `d1_migrations`, or a wrangler user's table |
| 210 | // adopted under an explicit `table:`): convert it in place. |
| 211 | yield* rebuildInPlace({ |
| 212 | executor, |
| 213 | table, |
| 214 | records, |
| 215 | nameExpr: "name", |
| 216 | tool: "wrangler", |
| 217 | }); |
| 218 | return; |
| 219 | case "unknown": |
| 220 | return yield* new MigrationError({ |
| 221 | message: |
| 222 | `Migrations table "${table}" has an unrecognized column layout; ` + |
| 223 | `refusing to write bookkeeping into it.`, |
| 224 | }); |
| 225 | } |
| 226 | }); |
| 227 | |
| 228 | const aliasesOf = (name: string): readonly string[] => [ |
| 229 | name, |
| 230 | `${name}/migration.sql`, |
| 231 | name.replace(/\/migration\.sql$/, ""), |
| 232 | ]; |
| 233 | |
| 234 | const appliedHistory = (executor: SqlExecutor, table: string) => |
| 235 | executor |
| 236 | .query( |
| 237 | `SELECT ${bookkeepingColumns(executor.dialect, "name", "hash")} FROM ${quoteIdentifier(table, executor.dialect)};`, |
| 238 | ) |
| 239 | .pipe( |
| 240 | Effect.map((rows) => { |
| 241 | const byName = new Map<string, string | undefined>(); |
| 242 | for (const row of rows) { |
| 243 | if (row.name === null || row.name === undefined) continue; |
| 244 | byName.set( |
| 245 | scalarText(row.name), |
| 246 | row.hash === null || row.hash === undefined ? undefined : scalarText(row.hash), |
| 247 | ); |
| 248 | } |
| 249 | return byName; |
| 250 | }), |
| 251 | ); |
| 252 | |
| 253 | const lookupApplied = ( |
| 254 | byName: Map<string, string | undefined>, |
| 255 | name: string, |
| 256 | ): { name: string; hash: string | undefined } | undefined => { |
| 257 | for (const alias of aliasesOf(name)) { |
| 258 | if (byName.has(alias)) { |
| 259 | return { name: alias, hash: byName.get(alias) }; |
| 260 | } |
| 261 | } |
| 262 | return undefined; |
| 263 | }; |
| 264 | |
| 265 | const applyPending = (options: { |
| 266 | executor: SqlExecutor; |
| 267 | table: string; |
| 268 | records: ReadonlyArray<MigrationRecord>; |
| 269 | applied: Map<string, string | undefined>; |
| 270 | }) => |
| 271 | Effect.gen(function* () { |
| 272 | const { executor, table, records, applied } = options; |
| 273 | for (const record of records) { |
| 274 | if (lookupApplied(applied, record.name)) continue; |
| 275 | const bookkeeping = insertSql(table, executor.dialect, record); |
| 276 | const send = executor.applyMigration |
| 277 | ? executor.applyMigration(record, bookkeeping) |
| 278 | : executor.batch([...record.statements, bookkeeping]); |
| 279 | // A batch can commit and still lose its reply. Its bookkeeping row says |
| 280 | // whether it landed, so a lost reply is not a failure and only a |
| 281 | // transient failure that did not land is resent (#1889). |
| 282 | yield* send.pipe( |
| 283 | Effect.catchTag( |
| 284 | "MigrationError", |
| 285 | Effect.fn(function* (error) { |
| 286 | const after = yield* appliedHistory(executor, table); |
| 287 | if (lookupApplied(after, record.name)) return; |
| 288 | if (!error.transient) return yield* error; |
| 289 | yield* send; |
| 290 | }), |
| 291 | ), |
| 292 | ); |
| 293 | } |
| 294 | }); |
| 295 | |
| 296 | /** |
| 297 | * Apply pending migrations with Alchemy's bookkeeping. Idempotent: each |
| 298 | * migration's statements and its bookkeeping INSERT go through |
| 299 | * `executor.batch` as one unit (a transaction on pg/mysql, one batched |
| 300 | * query on D1, which has no transactions over HTTP). Nontransactional DDL |
| 301 | * targets can supply `applyMigration` to checkpoint individual statements. |
| 302 | * |
| 303 | * Applied-detection is name-keyed with layout aliasing: pre-registry |
| 304 | * Alchemy recorded drizzle-layout migrations under `<dir>/migration.sql` |
| 305 | * while current records key them by `<dir>`, so both keys are honored. |
| 306 | * An already-applied name whose local hash changed, or a recorded name |
| 307 | * with no local file, is a hard {@link RewrittenMigrationHistoryError} |
| 308 | * rather than a silent skip that would persist the new hashes. |
| 309 | */ |
| 310 | export const applyAlchemyFormat = (options: { |
| 311 | executor: SqlExecutor; |
| 312 | table: string; |
| 313 | records: ReadonlyArray<MigrationRecord>; |
| 314 | }): Effect.Effect<void, MigrationApplyError> => |
| 315 | Effect.gen(function* () { |
| 316 | const { executor, table, records } = options; |
| 317 | yield* ensureTable({ executor, table, records }); |
| 318 | const applied = yield* appliedHistory(executor, table); |
| 319 | const localNames = new Set(records.flatMap((record) => aliasesOf(record.name))); |
| 320 | const changed: string[] = []; |
| 321 | const removed: string[] = []; |
| 322 | for (const name of applied.keys()) { |
| 323 | if (!localNames.has(name)) removed.push(name); |
| 324 | } |
| 325 | for (const record of records) { |
| 326 | const row = lookupApplied(applied, record.name); |
| 327 | if (row?.hash && row.hash !== record.hash) { |
| 328 | changed.push(record.name); |
| 329 | } |
| 330 | } |
| 331 | if (changed.length > 0 || removed.length > 0) { |
| 332 | changed.sort(); |
| 333 | removed.sort(); |
| 334 | return yield* new RewrittenMigrationHistoryError({ |
| 335 | changed, |
| 336 | removed, |
| 337 | message: |
| 338 | `Applied migration history in "${table}" does not match the local files ` + |
| 339 | `(${describeRewrittenHistory({ changed, removed })}). ` + |
| 340 | "Add a new forward migration instead of editing or deleting already-applied files.", |
| 341 | }); |
| 342 | } |
| 343 | yield* applyPending({ executor, table, records, applied }); |
| 344 | }); |
| 345 | |
| 346 | /** |
| 347 | * {@link applyAlchemyFormat} without the rewritten-history check: applied |
| 348 | * names are skipped regardless of their hash. For runtime appliers (Durable |
| 349 | * Object SQLite) where failing would take every instance down after a |
| 350 | * deploy that already succeeded. |
| 351 | */ |
| 352 | export const applyPendingAlchemyFormat = (options: { |
| 353 | executor: SqlExecutor; |
| 354 | table: string; |
| 355 | records: ReadonlyArray<MigrationRecord>; |
| 356 | }): Effect.Effect<void, MigrationError | MigrationHistoryConflictError> => |
| 357 | Effect.gen(function* () { |
| 358 | const { executor, table, records } = options; |
| 359 | yield* ensureTable({ executor, table, records }); |
| 360 | const applied = yield* appliedHistory(executor, table); |
| 361 | yield* applyPending({ executor, table, records, applied }); |
| 362 | }); |