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 { stripSslQueryParams } from "@alchemy.run/sql/PostgresDriver"; |
| 2 | import { |
| 3 | applySqlImports, |
| 4 | splitSqliteStatements, |
| 5 | splitSqlStatements, |
| 6 | } from "@alchemy.run/sql/SqlFile"; |
| 7 | import { describe, expect, layer, test } from "@alchemy.run/test"; |
| 8 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as FileSystem from "effect/FileSystem"; |
| 11 | import * as Path from "effect/Path"; |
| 12 | |
| 13 | describe("SqlFile", { tags: ["unit", "local"] }, () => { |
| 14 | describe("splitSqlStatements", () => { |
| 15 | test("splits statements separated by breakpoints on their own line", () => { |
| 16 | const sql = [ |
| 17 | "CREATE TABLE `users` (\n\t`id` int NOT NULL\n);", |
| 18 | "--> statement-breakpoint", |
| 19 | "CREATE TABLE `sessions` (\n\t`id` int NOT NULL\n);", |
| 20 | ].join("\n"); |
| 21 | expect(splitSqlStatements(sql)).toEqual([ |
| 22 | "CREATE TABLE `users` (\n\t`id` int NOT NULL\n);", |
| 23 | "CREATE TABLE `sessions` (\n\t`id` int NOT NULL\n);", |
| 24 | ]); |
| 25 | }); |
| 26 | |
| 27 | // drizzle-kit puts the marker on the same line after single-line |
| 28 | // statements (`...;--> statement-breakpoint`). A newline-anchored split |
| 29 | // misses those, and the leftover `-->` makes Vitess bail with |
| 30 | // "syntax error at position 2". |
| 31 | test("splits breakpoints appended on the same line as the statement", () => { |
| 32 | const sql = [ |
| 33 | "CREATE TABLE `users` (\n\t`id` int NOT NULL\n);", |
| 34 | "--> statement-breakpoint", |
| 35 | "CREATE INDEX `users_email` ON `users` (`email`);--> statement-breakpoint", |
| 36 | "CREATE INDEX `users_name` ON `users` (`name`);", |
| 37 | ].join("\n"); |
| 38 | expect(splitSqlStatements(sql)).toEqual([ |
| 39 | "CREATE TABLE `users` (\n\t`id` int NOT NULL\n);", |
| 40 | "CREATE INDEX `users_email` ON `users` (`email`);", |
| 41 | "CREATE INDEX `users_name` ON `users` (`name`);", |
| 42 | ]); |
| 43 | }); |
| 44 | |
| 45 | test("splits CRLF-separated breakpoints", () => { |
| 46 | const sql = |
| 47 | "CREATE TABLE `users` (`id` int);\r\n--> statement-breakpoint\r\nCREATE TABLE `sessions` (`id` int);"; |
| 48 | expect(splitSqlStatements(sql)).toEqual([ |
| 49 | "CREATE TABLE `users` (`id` int);", |
| 50 | "CREATE TABLE `sessions` (`id` int);", |
| 51 | ]); |
| 52 | }); |
| 53 | |
| 54 | test("drops empty segments and keeps sql without markers intact", () => { |
| 55 | expect(splitSqlStatements("--> statement-breakpoint\n")).toEqual([]); |
| 56 | expect(splitSqlStatements("SELECT 1;")).toEqual(["SELECT 1;"]); |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | describe("splitSqliteStatements", () => { |
| 61 | test("splits on semicolons and drops the terminators", () => { |
| 62 | expect( |
| 63 | splitSqliteStatements("CREATE TABLE t (id INTEGER);\nINSERT INTO t VALUES (1);\n"), |
| 64 | ).toEqual(["CREATE TABLE t (id INTEGER)", "INSERT INTO t VALUES (1)"]); |
| 65 | }); |
| 66 | |
| 67 | test("keeps a final statement without a semicolon", () => { |
| 68 | expect(splitSqliteStatements("SELECT 1; SELECT 2")).toEqual(["SELECT 1", "SELECT 2"]); |
| 69 | }); |
| 70 | |
| 71 | test("ignores semicolons inside strings, quoted identifiers, and comments", () => { |
| 72 | const sql = [ |
| 73 | "INSERT INTO t VALUES ('a;b', 'it''s; fine');", |
| 74 | 'CREATE TABLE "x;y" (`a;` TEXT, [b;] TEXT);', |
| 75 | "-- a comment; not a statement", |
| 76 | "/* block; comment */ SELECT 1;", |
| 77 | ].join("\n"); |
| 78 | expect(splitSqliteStatements(sql)).toEqual([ |
| 79 | "INSERT INTO t VALUES ('a;b', 'it''s; fine')", |
| 80 | 'CREATE TABLE "x;y" (`a;` TEXT, [b;] TEXT)', |
| 81 | "-- a comment; not a statement\n/* block; comment */ SELECT 1", |
| 82 | ]); |
| 83 | }); |
| 84 | |
| 85 | test("keeps a trigger body as one statement", () => { |
| 86 | const trigger = [ |
| 87 | "CREATE TEMP TRIGGER log_insert AFTER INSERT ON t", |
| 88 | "BEGIN", |
| 89 | " INSERT INTO log VALUES (new.id);", |
| 90 | " UPDATE counts SET n = n + 1;", |
| 91 | "END", |
| 92 | ].join("\n"); |
| 93 | expect(splitSqliteStatements(`${trigger};\nSELECT 1;`)).toEqual([trigger, "SELECT 1"]); |
| 94 | }); |
| 95 | |
| 96 | test("drops comment-only and empty segments", () => { |
| 97 | expect(splitSqliteStatements(";;\n-- only a comment\n")).toEqual([]); |
| 98 | expect(splitSqliteStatements("")).toEqual([]); |
| 99 | }); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | const withFs = layer(NodeServices.layer); |
| 104 | |
| 105 | withFs("applySqlImports", (it) => { |
| 106 | it.effect( |
| 107 | "applies new and changed files once, and keeps hashes only for listed files", |
| 108 | () => |
| 109 | Effect.gen(function* () { |
| 110 | const fs = yield* FileSystem.FileSystem; |
| 111 | const path = yield* Path.Path; |
| 112 | const rootDir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-imports-" }); |
| 113 | yield* fs.writeFileString(path.join(rootDir, "a.sql"), "SELECT 1;"); |
| 114 | yield* fs.writeFileString(path.join(rootDir, "b.sql"), "SELECT 2;"); |
| 115 | const applied: string[] = []; |
| 116 | const apply = (file: { id: string }) => Effect.sync(() => void applied.push(file.id)); |
| 117 | |
| 118 | const first = yield* applySqlImports({ |
| 119 | importFiles: ["a.sql", "b.sql"], |
| 120 | rootDir, |
| 121 | previous: { "gone.sql": "stale" }, |
| 122 | apply, |
| 123 | }); |
| 124 | expect(Object.keys(first).sort()).toEqual(["a.sql", "b.sql"]); |
| 125 | |
| 126 | yield* fs.writeFileString(path.join(rootDir, "b.sql"), "SELECT 3;"); |
| 127 | const second = yield* applySqlImports({ |
| 128 | importFiles: ["a.sql", "b.sql"], |
| 129 | rootDir, |
| 130 | previous: first, |
| 131 | apply, |
| 132 | }); |
| 133 | expect(second["a.sql"]).toEqual(first["a.sql"]); |
| 134 | expect(second["b.sql"]).not.toEqual(first["b.sql"]); |
| 135 | expect(applied).toEqual(["a.sql", "b.sql", "b.sql"]); |
| 136 | }), |
| 137 | { tags: ["unit", "local"] }, |
| 138 | ); |
| 139 | }); |
| 140 | |
| 141 | describe("stripSslQueryParams", { tags: ["unit", "local"] }, () => { |
| 142 | test("drops sslmode and channel_binding and keeps everything else", () => { |
| 143 | expect( |
| 144 | stripSslQueryParams( |
| 145 | "postgresql://u:p@host/db?sslmode=require&channel_binding=require&application_name=x", |
| 146 | ), |
| 147 | ).toEqual("postgresql://u:p@host/db?application_name=x"); |
| 148 | expect(stripSslQueryParams("not a url")).toEqual("not a url"); |
| 149 | }); |
| 150 | }); |