The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { MigrationDialect } from "./Format.ts"; |
| 2 | |
| 3 | /** Matches drizzle-kit v1 migration directories: `YYYYMMDDHHMMSS_name`. */ |
| 4 | export const DRIZZLE_DIR_PATTERN = /^\d{14}_.+/; |
| 5 | |
| 6 | /** |
| 7 | * Parse a 14-digit `YYYYMMDDHHMMSS` prefix into UTC millis (drizzle's |
| 8 | * `formatToMillis`). Returns undefined when the name has no such prefix. |
| 9 | */ |
| 10 | export const timestampPrefixMillis = (name: string): number | undefined => { |
| 11 | const prefix = name.slice(0, 14); |
| 12 | if (!/^\d{14}$/.test(prefix)) return undefined; |
| 13 | const year = Number.parseInt(prefix.slice(0, 4), 10); |
| 14 | const month = Number.parseInt(prefix.slice(4, 6), 10) - 1; |
| 15 | const day = Number.parseInt(prefix.slice(6, 8), 10); |
| 16 | const hour = Number.parseInt(prefix.slice(8, 10), 10); |
| 17 | const minute = Number.parseInt(prefix.slice(10, 12), 10); |
| 18 | const second = Number.parseInt(prefix.slice(12, 14), 10); |
| 19 | return Date.UTC(year, month, day, hour, minute, second); |
| 20 | }; |
| 21 | |
| 22 | /** |
| 23 | * Stringify a scalar value a driver returned (or a bookkeeping parameter). |
| 24 | * Binary values (e.g. mysql `Buffer`s) decode as UTF-8. |
| 25 | */ |
| 26 | export const scalarText = (value: unknown): string => |
| 27 | typeof value === "string" |
| 28 | ? value |
| 29 | : value instanceof Uint8Array |
| 30 | ? new TextDecoder().decode(value) |
| 31 | : String(value); |
| 32 | |
| 33 | /** |
| 34 | * Render a parameter as a SQL literal. Only used for Alchemy's own |
| 35 | * bookkeeping queries (names, hashes, millis, ISO dates) against executors |
| 36 | * without native parameter support (the D1 HTTP API and the local workerd |
| 37 | * tunnel). |
| 38 | */ |
| 39 | export const sqlLiteral = (value: unknown, dialect?: MigrationDialect): string => { |
| 40 | if (value === null || value === undefined) return "NULL"; |
| 41 | if (typeof value === "number" || typeof value === "bigint") { |
| 42 | return String(value); |
| 43 | } |
| 44 | if (typeof value === "boolean") return value ? "1" : "0"; |
| 45 | const text = value instanceof Date ? value.toISOString() : scalarText(value); |
| 46 | // GoogleSQL escapes with backslashes; `''` is two adjacent literals. |
| 47 | return dialect === "googlesql" |
| 48 | ? `'${text.replaceAll("\\", "\\\\").replaceAll("'", "\\'")}'` |
| 49 | : `'${text.replaceAll("'", "''")}'`; |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Inline `?` (sqlite/mysql) or `$n` (postgres) placeholders as SQL |
| 54 | * literals. String scanning respects quoted spans so literal `?`s inside |
| 55 | * strings survive. |
| 56 | */ |
| 57 | export const inlineSqlParams = ( |
| 58 | sql: string, |
| 59 | params: ReadonlyArray<unknown>, |
| 60 | dialect: MigrationDialect, |
| 61 | ): string => { |
| 62 | if (params.length === 0) return sql; |
| 63 | if (dialect === "postgres") { |
| 64 | return sql.replace(/\$(\d+)/g, (match, n: string) => { |
| 65 | const index = Number.parseInt(n, 10) - 1; |
| 66 | return index >= 0 && index < params.length ? sqlLiteral(params[index]) : match; |
| 67 | }); |
| 68 | } |
| 69 | let out = ""; |
| 70 | let paramIndex = 0; |
| 71 | let quote: string | undefined; |
| 72 | for (let i = 0; i < sql.length; i++) { |
| 73 | const ch = sql[i]; |
| 74 | if (quote) { |
| 75 | out += ch; |
| 76 | if (ch === quote) quote = undefined; |
| 77 | continue; |
| 78 | } |
| 79 | if (ch === "'" || ch === '"' || ch === "`") { |
| 80 | quote = ch; |
| 81 | out += ch; |
| 82 | continue; |
| 83 | } |
| 84 | if (ch === "?" && paramIndex < params.length) { |
| 85 | out += sqlLiteral(params[paramIndex++]); |
| 86 | continue; |
| 87 | } |
| 88 | out += ch; |
| 89 | } |
| 90 | return out; |
| 91 | }; |
| 92 | |
| 93 | /** Quote an identifier for the given dialect. */ |
| 94 | export const quoteIdentifier = (identifier: string, dialect: MigrationDialect): string => |
| 95 | dialect === "mysql" |
| 96 | ? `\`${identifier.replaceAll("`", "``")}\`` |
| 97 | : dialect === "googlesql" |
| 98 | ? `\`${identifier.replaceAll("\\", "\\\\").replaceAll("`", "\\`")}\`` |
| 99 | : `"${identifier.replaceAll('"', '""')}"`; |
| 100 | |
| 101 | /** |
| 102 | * A bookkeeping column reference. GoogleSQL reserves `hash`, so its |
| 103 | * columns are always quoted; other dialects keep the bare names. |
| 104 | */ |
| 105 | export const bookkeepingColumns = ( |
| 106 | dialect: MigrationDialect, |
| 107 | ...columns: ReadonlyArray<string> |
| 108 | ): string => |
| 109 | columns |
| 110 | .map((column) => (dialect === "googlesql" ? quoteIdentifier(column, dialect) : column)) |
| 111 | .join(", "); |