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 type { MigrationError, SqlExecutor } from "./Format.ts"; |
| 3 | import { quoteIdentifier, scalarText, sqlLiteral } from "./Utils.ts"; |
| 4 | |
| 5 | export interface TableColumn { |
| 6 | name: string; |
| 7 | type: string; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * List a table's columns (empty when the table doesn't exist), normalized |
| 12 | * across dialects. `schema` defaults to the executor's `defaultSchema`, then |
| 13 | * the connection's current/default schema. |
| 14 | */ |
| 15 | export const tableColumns = ( |
| 16 | executor: SqlExecutor, |
| 17 | table: string, |
| 18 | schema?: string, |
| 19 | ): Effect.Effect<TableColumn[], MigrationError, never> => { |
| 20 | switch (executor.dialect) { |
| 21 | case "sqlite": |
| 22 | return executor.query(`PRAGMA table_info(${quoteIdentifier(table, "sqlite")});`).pipe( |
| 23 | Effect.map((rows) => |
| 24 | rows.map((row) => ({ |
| 25 | name: String(row.name), |
| 26 | type: scalarText(row.type ?? "").toUpperCase(), |
| 27 | })), |
| 28 | ), |
| 29 | // A missing table yields an empty PRAGMA result, not an error, but |
| 30 | // some tunnels surface it as one — treat both as "absent". |
| 31 | Effect.orElseSucceed(() => []), |
| 32 | ); |
| 33 | case "postgres": |
| 34 | return executor |
| 35 | .query( |
| 36 | `SELECT column_name AS name, data_type AS type |
| 37 | FROM information_schema.columns |
| 38 | WHERE table_name = ${sqlLiteral(table)} |
| 39 | AND table_schema = ${ |
| 40 | (schema ?? executor.defaultSchema) |
| 41 | ? sqlLiteral(schema ?? executor.defaultSchema) |
| 42 | : "current_schema()" |
| 43 | } |
| 44 | ORDER BY ordinal_position;`, |
| 45 | ) |
| 46 | .pipe( |
| 47 | Effect.map((rows) => |
| 48 | rows.map((row) => ({ |
| 49 | name: String(row.name), |
| 50 | type: scalarText(row.type ?? "").toUpperCase(), |
| 51 | })), |
| 52 | ), |
| 53 | ); |
| 54 | case "mysql": |
| 55 | return executor |
| 56 | .query( |
| 57 | `SELECT COLUMN_NAME AS name, DATA_TYPE AS type |
| 58 | FROM information_schema.columns |
| 59 | WHERE table_name = ${sqlLiteral(table)} |
| 60 | AND table_schema = ${schema ? sqlLiteral(schema) : "DATABASE()"} |
| 61 | ORDER BY ORDINAL_POSITION;`, |
| 62 | ) |
| 63 | .pipe( |
| 64 | Effect.map((rows) => |
| 65 | rows.map((row) => ({ |
| 66 | name: String(row.name), |
| 67 | type: scalarText(row.type ?? "").toUpperCase(), |
| 68 | })), |
| 69 | ), |
| 70 | Effect.orElseSucceed(() => []), |
| 71 | ); |
| 72 | case "googlesql": |
| 73 | // The default schema is the empty string; named schemas qualify tables. |
| 74 | return executor |
| 75 | .query( |
| 76 | `SELECT COLUMN_NAME AS name, SPANNER_TYPE AS type |
| 77 | FROM INFORMATION_SCHEMA.COLUMNS |
| 78 | WHERE TABLE_NAME = ${sqlLiteral(table, "googlesql")} |
| 79 | AND TABLE_SCHEMA = ${sqlLiteral(schema ?? "", "googlesql")} |
| 80 | ORDER BY ORDINAL_POSITION`, |
| 81 | ) |
| 82 | .pipe( |
| 83 | Effect.map((rows) => |
| 84 | rows.map((row) => ({ |
| 85 | name: scalarText(row.name), |
| 86 | type: scalarText(row.type ?? "").toUpperCase(), |
| 87 | })), |
| 88 | ), |
| 89 | ); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * Classify an existing applied-migrations table by column set — the same |
| 95 | * versioning trick drizzle uses (there is deliberately no version column): |
| 96 | * |
| 97 | * - `absent` — no table |
| 98 | * - `drizzle-shaped` — has `hash` (drizzle v1 / alchemy current) |
| 99 | * - `legacy-alchemy` — `name` + `applied_at`, no `hash` (pre-registry Alchemy) |
| 100 | * - `legacy-2col` — two columns, no `name`/`hash` (oldest Alchemy shape, |
| 101 | * where the primary column carried the migration name) |
| 102 | * - `wrangler` — `id`/`name`/`applied_at` with an INTEGER id (already |
| 103 | * wrangler's real shape) |
| 104 | * - `unknown` — anything else |
| 105 | */ |
| 106 | export type TableShape = |
| 107 | | "absent" |
| 108 | | "drizzle-shaped" |
| 109 | | "legacy-alchemy" |
| 110 | | "legacy-2col" |
| 111 | | "wrangler" |
| 112 | | "unknown"; |
| 113 | |
| 114 | export const classifyTable = (columns: TableColumn[]): TableShape => { |
| 115 | if (columns.length === 0) return "absent"; |
| 116 | const names = new Set(columns.map((c) => c.name)); |
| 117 | if (names.has("hash")) return "drizzle-shaped"; |
| 118 | if (names.has("name") && names.has("applied_at")) { |
| 119 | const id = columns.find((c) => c.name === "id"); |
| 120 | return id && /INT/.test(id.type) ? "wrangler" : "legacy-alchemy"; |
| 121 | } |
| 122 | if (columns.length === 2 && !names.has("name")) return "legacy-2col"; |
| 123 | return "unknown"; |
| 124 | }; |