The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import crypto from "node:crypto"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Path from "effect/Path"; |
| 5 | |
| 6 | export interface SqlFile { |
| 7 | id: string; |
| 8 | sql: string; |
| 9 | hash: string; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Recursively list `.sql` files under `directory`, sorted by their numeric |
| 14 | * prefix (e.g. `0001_init.sql`) and then by name. |
| 15 | */ |
| 16 | export const listSqlFiles = (directory: string) => |
| 17 | Effect.gen(function* () { |
| 18 | const fs = yield* FileSystem.FileSystem; |
| 19 | const entries = yield* fs.readDirectory(directory, { recursive: true }); |
| 20 | |
| 21 | const sqlFiles = entries |
| 22 | .filter((name) => name.endsWith(".sql")) |
| 23 | .sort((a, b) => { |
| 24 | const aNum = getPrefix(a); |
| 25 | const bNum = getPrefix(b); |
| 26 | if (aNum !== null && bNum !== null) return aNum - bNum; |
| 27 | if (aNum !== null) return -1; |
| 28 | if (bNum !== null) return 1; |
| 29 | return a.localeCompare(b); |
| 30 | }); |
| 31 | |
| 32 | return yield* Effect.forEach(sqlFiles, (id) => readSqlFile(directory, id)); |
| 33 | }); |
| 34 | |
| 35 | /** |
| 36 | * Read a single `.sql` file relative to `directory` and compute its content |
| 37 | * hash. The `sql` field is marked non-enumerable so it isn't serialized into |
| 38 | * resource state. |
| 39 | */ |
| 40 | export const readSqlFile = (directory: string, name: string) => |
| 41 | Effect.gen(function* () { |
| 42 | const fs = yield* FileSystem.FileSystem; |
| 43 | const path = yield* Path.Path; |
| 44 | const sql = yield* fs.readFileString(path.resolve(directory, name)); |
| 45 | const hash = crypto.createHash("sha256").update(sql).digest("hex"); |
| 46 | const file: SqlFile = { id: name, sql, hash }; |
| 47 | Object.defineProperty(file, "sql", { enumerable: false }); |
| 48 | return file; |
| 49 | }); |
| 50 | |
| 51 | export const hashMigrations = (migrationsDir: string) => |
| 52 | listSqlFiles(migrationsDir).pipe( |
| 53 | Effect.map((files) => { |
| 54 | const hashes: Record<string, string> = {}; |
| 55 | for (const file of files) hashes[file.id] = file.hash; |
| 56 | return hashes; |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | /** |
| 61 | * Apply `importFiles` (paths relative to `rootDir`): each file runs through |
| 62 | * `apply` once, and again only when its contents change. `previous` is the |
| 63 | * `importHashes` a prior deploy persisted; the result is the new |
| 64 | * `importHashes`, covering exactly the files still listed. |
| 65 | */ |
| 66 | export const applySqlImports = <E, R>(options: { |
| 67 | importFiles: ReadonlyArray<string> | undefined; |
| 68 | rootDir: string; |
| 69 | previous: Record<string, string> | undefined; |
| 70 | apply: (file: SqlFile) => Effect.Effect<void, E, R>; |
| 71 | }) => |
| 72 | Effect.gen(function* () { |
| 73 | const hashes: Record<string, string> = {}; |
| 74 | for (const filePath of options.importFiles ?? []) { |
| 75 | const file = yield* readSqlFile(options.rootDir, filePath); |
| 76 | if (options.previous?.[filePath] !== file.hash) { |
| 77 | yield* options.apply(file); |
| 78 | } |
| 79 | hashes[filePath] = file.hash; |
| 80 | } |
| 81 | return hashes; |
| 82 | }); |
| 83 | |
| 84 | export const hashImports = (importFiles: ReadonlyArray<string>, rootDir: string) => |
| 85 | Effect.gen(function* () { |
| 86 | const hashes: Record<string, string> = {}; |
| 87 | for (const filePath of importFiles) { |
| 88 | const file = yield* readSqlFile(rootDir, filePath); |
| 89 | hashes[filePath] = file.hash; |
| 90 | } |
| 91 | return hashes; |
| 92 | }); |
| 93 | |
| 94 | const getPrefix = (name: string): number | null => { |
| 95 | const prefix = name.split("_")[0]; |
| 96 | const num = Number.parseInt(prefix, 10); |
| 97 | return Number.isNaN(num) ? null : num; |
| 98 | }; |
| 99 | |
| 100 | // Drizzle (and other migration tools) emit `--> statement-breakpoint` between |
| 101 | // statements. drizzle-kit puts the marker directly after single-line |
| 102 | // statements (`...;--> statement-breakpoint`), so the split can't anchor on a |
| 103 | // preceding newline. |
| 104 | const STATEMENT_BREAKPOINT = /\s*-->\s*statement-breakpoint\s*/g; |
| 105 | |
| 106 | /** |
| 107 | * Split a migration file into individual statements on its |
| 108 | * `--> statement-breakpoint` markers. MySQL engines need this: they only |
| 109 | * treat `--` as a comment when followed by whitespace, so the `-->` token is |
| 110 | * a syntax error ("syntax error at position 2" on Vitess). Postgres and |
| 111 | * sqlite comment on `--` regardless of the next character, so their runners |
| 112 | * send files whole. |
| 113 | */ |
| 114 | export const splitSqlStatements = (sql: string): string[] => |
| 115 | sql |
| 116 | .split(STATEMENT_BREAKPOINT) |
| 117 | .map((s) => s.trim()) |
| 118 | .filter((s) => s.length > 0); |
| 119 | |
| 120 | type SqliteToken = "semi" | "ws" | "other" | "explain" | "create" | "temp" | "trigger" | "end"; |
| 121 | type SqliteState = |
| 122 | | "invalid" |
| 123 | | "start" |
| 124 | | "normal" |
| 125 | | "explain" |
| 126 | | "create" |
| 127 | | "trigger" |
| 128 | | "semi" |
| 129 | | "end"; |
| 130 | |
| 131 | // SQLite's `sqlite3_complete` state machine: a `;` ends a statement unless |
| 132 | // it sits inside a `CREATE [TEMP] TRIGGER ... BEGIN ... END` body. |
| 133 | const SQLITE_TRANSITIONS: Record<SqliteState, Record<SqliteToken, SqliteState>> = { |
| 134 | invalid: { |
| 135 | semi: "start", |
| 136 | ws: "invalid", |
| 137 | other: "normal", |
| 138 | explain: "explain", |
| 139 | create: "create", |
| 140 | temp: "normal", |
| 141 | trigger: "normal", |
| 142 | end: "normal", |
| 143 | }, |
| 144 | start: { |
| 145 | semi: "start", |
| 146 | ws: "start", |
| 147 | other: "normal", |
| 148 | explain: "explain", |
| 149 | create: "create", |
| 150 | temp: "normal", |
| 151 | trigger: "normal", |
| 152 | end: "normal", |
| 153 | }, |
| 154 | normal: { |
| 155 | semi: "start", |
| 156 | ws: "normal", |
| 157 | other: "normal", |
| 158 | explain: "normal", |
| 159 | create: "normal", |
| 160 | temp: "normal", |
| 161 | trigger: "normal", |
| 162 | end: "normal", |
| 163 | }, |
| 164 | explain: { |
| 165 | semi: "start", |
| 166 | ws: "explain", |
| 167 | other: "explain", |
| 168 | explain: "normal", |
| 169 | create: "create", |
| 170 | temp: "normal", |
| 171 | trigger: "normal", |
| 172 | end: "normal", |
| 173 | }, |
| 174 | create: { |
| 175 | semi: "start", |
| 176 | ws: "create", |
| 177 | other: "normal", |
| 178 | explain: "normal", |
| 179 | create: "normal", |
| 180 | temp: "create", |
| 181 | trigger: "trigger", |
| 182 | end: "normal", |
| 183 | }, |
| 184 | trigger: { |
| 185 | semi: "semi", |
| 186 | ws: "trigger", |
| 187 | other: "trigger", |
| 188 | explain: "trigger", |
| 189 | create: "trigger", |
| 190 | temp: "trigger", |
| 191 | trigger: "trigger", |
| 192 | end: "trigger", |
| 193 | }, |
| 194 | semi: { |
| 195 | semi: "semi", |
| 196 | ws: "semi", |
| 197 | other: "trigger", |
| 198 | explain: "trigger", |
| 199 | create: "trigger", |
| 200 | temp: "trigger", |
| 201 | trigger: "trigger", |
| 202 | end: "end", |
| 203 | }, |
| 204 | end: { |
| 205 | semi: "start", |
| 206 | ws: "end", |
| 207 | other: "trigger", |
| 208 | explain: "trigger", |
| 209 | create: "trigger", |
| 210 | temp: "trigger", |
| 211 | trigger: "trigger", |
| 212 | end: "trigger", |
| 213 | }, |
| 214 | }; |
| 215 | |
| 216 | const SQLITE_IDENTIFIER_CHAR = /[A-Za-z0-9_$\u0080-]/; |
| 217 | |
| 218 | const classifySqliteIdentifier = (identifier: string): SqliteToken => { |
| 219 | switch (identifier.toLowerCase()) { |
| 220 | case "explain": |
| 221 | return "explain"; |
| 222 | case "create": |
| 223 | return "create"; |
| 224 | case "temp": |
| 225 | case "temporary": |
| 226 | return "temp"; |
| 227 | case "trigger": |
| 228 | return "trigger"; |
| 229 | case "end": |
| 230 | return "end"; |
| 231 | default: |
| 232 | return "other"; |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | /** |
| 237 | * Split a SQLite script into its statements, without their terminating `;`. |
| 238 | * Semicolons inside quoted strings, identifiers (`"…"`, `` `…` ``, `[…]`), |
| 239 | * comments, and trigger bodies don't split. Comments before a statement stay |
| 240 | * with it, and comment-only segments are dropped. Matches how |
| 241 | * `wrangler d1 execute --file` splits a file. |
| 242 | */ |
| 243 | export const splitSqliteStatements = (sql: string): string[] => { |
| 244 | const statements: string[] = []; |
| 245 | let start = 0; |
| 246 | let state: SqliteState = "invalid"; |
| 247 | let hasContent = false; |
| 248 | const flush = (end: number) => { |
| 249 | const statement = sql.slice(start, end).trim(); |
| 250 | if (hasContent && statement.length > 0) statements.push(statement); |
| 251 | }; |
| 252 | for (let index = 0; index < sql.length; index++) { |
| 253 | const char = sql[index]; |
| 254 | let token: SqliteToken; |
| 255 | if (char === ";") { |
| 256 | token = "semi"; |
| 257 | } else if (/\s/.test(char)) { |
| 258 | token = "ws"; |
| 259 | } else if (char === "/" && sql[index + 1] === "*") { |
| 260 | const end = sql.indexOf("*/", index + 2); |
| 261 | index = end === -1 ? sql.length : end + 1; |
| 262 | token = "ws"; |
| 263 | } else if (char === "-" && sql[index + 1] === "-") { |
| 264 | const end = sql.indexOf("\n", index + 2); |
| 265 | index = end === -1 ? sql.length : end; |
| 266 | token = "ws"; |
| 267 | } else if (char === "'" || char === '"' || char === "`" || char === "[") { |
| 268 | const end = sql.indexOf(char === "[" ? "]" : char, index + 1); |
| 269 | index = end === -1 ? sql.length : end; |
| 270 | token = "other"; |
| 271 | } else if (SQLITE_IDENTIFIER_CHAR.test(char)) { |
| 272 | const identifierStart = index; |
| 273 | while (index + 1 < sql.length && SQLITE_IDENTIFIER_CHAR.test(sql[index + 1])) index++; |
| 274 | token = classifySqliteIdentifier(sql.slice(identifierStart, index + 1)); |
| 275 | } else { |
| 276 | token = "other"; |
| 277 | } |
| 278 | hasContent ||= token !== "semi" && token !== "ws"; |
| 279 | state = SQLITE_TRANSITIONS[state][token]; |
| 280 | if (token === "semi" && state === "start") { |
| 281 | flush(index); |
| 282 | start = index + 1; |
| 283 | hasContent = false; |
| 284 | } |
| 285 | } |
| 286 | flush(sql.length); |
| 287 | return statements; |
| 288 | }; |