@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 99 lines 3.23 KB
Raw
1 import * as 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 import { listSqlFiles, splitSqlStatements } from "../SqlFile.ts";
6 import { detectLayout } from "./Detect.ts";
7 import { MigrationError, type MigrationRecord } from "./Format.ts";
8 import { DRIZZLE_DIR_PATTERN, timestampPrefixMillis } from "./Utils.ts";
9
10 export {
11 DRIZZLE_DIR_PATTERN,
12 inlineSqlParams,
13 quoteIdentifier,
14 sqlLiteral,
15 timestampPrefixMillis,
16 } from "./Utils.ts";
17
18 /** Map filesystem failures into the migration error channel. */
19 export const mapPlatformError = <A, E, R>(
20 effect: Effect.Effect<A, E, R>,
21 context: string,
22 ): Effect.Effect<A, MigrationError, R> =>
23 effect.pipe(
24 Effect.mapError(
25 (cause) =>
26 new MigrationError({
27 message: `${context}: ${String(cause)}`,
28 cause,
29 }),
30 ),
31 );
32
33 const sha256 = (content: string) =>
34 Effect.sync(() => crypto.createHash("sha256").update(content).digest("hex"));
35
36 /**
37 * Read a drizzle-v1-layout directory (`{ts}_{name}/migration.sql`) into
38 * records keyed the way drizzle keys them: `name` = the directory name,
39 * sorted by name (drizzle's own sort), hash = sha256 of `migration.sql`.
40 */
41 export const readDrizzleDirRecords = (dir: string) =>
42 mapPlatformError(
43 Effect.gen(function* () {
44 const fs = yield* FileSystem.FileSystem;
45 const path = yield* Path.Path;
46 const entries = yield* fs.readDirectory(dir);
47 const names: string[] = [];
48 for (const entry of entries) {
49 if (!DRIZZLE_DIR_PATTERN.test(entry)) continue;
50 const sqlPath = path.join(dir, entry, "migration.sql");
51 if (yield* fs.exists(sqlPath)) names.push(entry);
52 }
53 names.sort((a, b) => a.localeCompare(b));
54 const records: MigrationRecord[] = [];
55 for (const name of names) {
56 const sql = yield* fs.readFileString(path.join(dir, name, "migration.sql"));
57 records.push({
58 name,
59 hash: yield* sha256(sql),
60 createdAtMillis: timestampPrefixMillis(name),
61 sql,
62 statements: splitSqlStatements(sql),
63 });
64 }
65 return records;
66 }),
67 `Failed to read drizzle migrations from ${dir}`,
68 );
69
70 /**
71 * Read a flat directory of `.sql` files into records keyed by relative file
72 * path — the convention wrangler and legacy Alchemy state share. Nested
73 * `dir/migration.sql` paths are included (via `listSqlFiles`'s recursive
74 * listing) so legacy state written against drizzle-layout dirs keeps
75 * resolving.
76 */
77 export const readFlatRecords = (dir: string) =>
78 mapPlatformError(
79 listSqlFiles(dir).pipe(
80 Effect.map((files) =>
81 files.map((file): MigrationRecord => ({
82 name: file.id,
83 hash: file.hash,
84 createdAtMillis: timestampPrefixMillis(file.id),
85 sql: file.sql,
86 statements: splitSqlStatements(file.sql),
87 })),
88 ),
89 ),
90 `Failed to read migrations from ${dir}`,
91 );
92
93 /** Read migration records using the directory's detected layout. */
94 export const readMigrationRecords = (dir: string) =>
95 detectLayout(dir).pipe(
96 Effect.flatMap((layout) =>
97 layout === "flat" ? readFlatRecords(dir) : readDrizzleDirRecords(dir),
98 ),
99 );