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, 06:00 UTC
| 1 | import { prepareDsqlStatements } from "@alchemy.run/aws/DSQL/MigrationSql"; |
| 2 | import { expect, test } from "@alchemy.run/test"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Result from "effect/Result"; |
| 5 | |
| 6 | test.effect( |
| 7 | "DSQL splits scripts without splitting SQL literals or comments", |
| 8 | () => |
| 9 | Effect.gen(function* () { |
| 10 | const statements = yield* prepareDsqlStatements( |
| 11 | ` |
| 12 | -- CREATE SEQUENCE nope; |
| 13 | CREATE TABLE "serial" (id uuid PRIMARY KEY, value text DEFAULT 'a;''b'); |
| 14 | /* outer ; /* inner ; */ */ INSERT INTO "serial" VALUES (gen_random_uuid(), $$a;b$$); |
| 15 | INSERT INTO "serial" VALUES (gen_random_uuid(), E'a\\';b'); |
| 16 | `, |
| 17 | "0001.sql", |
| 18 | ); |
| 19 | expect(statements).toHaveLength(3); |
| 20 | expect(statements[0]).toContain("'a;''b'"); |
| 21 | expect(statements[1]).toContain("$$a;b$$"); |
| 22 | }), |
| 23 | { tags: ["unit", "provider:aws", "provider:aws:dsql", "local"] }, |
| 24 | ); |
| 25 | |
| 26 | test.effect( |
| 27 | "DSQL adapts Drizzle indexes and accepts current foreign key and sequence syntax", |
| 28 | () => |
| 29 | Effect.gen(function* () { |
| 30 | const statements = yield* prepareDsqlStatements( |
| 31 | ` |
| 32 | CREATE TABLE users (id uuid PRIMARY KEY); |
| 33 | --> statement-breakpoint |
| 34 | CREATE TABLE posts (id uuid PRIMARY KEY, user_id uuid REFERENCES users(id)); |
| 35 | CREATE INDEX "posts_user" ON "posts" USING btree ("user_id"); |
| 36 | CREATE UNIQUE INDEX ASYNC "users_id" ON users (id); |
| 37 | CREATE SEQUENCE ids CACHE 65536; |
| 38 | `, |
| 39 | "schema", |
| 40 | ); |
| 41 | expect(statements).toHaveLength(5); |
| 42 | expect(statements[2]).toBe('CREATE INDEX ASYNC "posts_user" ON "posts" ("user_id")'); |
| 43 | expect(statements[3]).toBe('CREATE UNIQUE INDEX ASYNC "users_id" ON users (id)'); |
| 44 | }), |
| 45 | { tags: ["unit", "provider:aws", "provider:aws:dsql", "local"] }, |
| 46 | ); |
| 47 | |
| 48 | for (const sql of [ |
| 49 | "BEGIN; CREATE TABLE users(id uuid); COMMIT;", |
| 50 | "CREATE TABLE users(id serial PRIMARY KEY);", |
| 51 | "CREATE SEQUENCE ids;", |
| 52 | "CREATE INDEX CONCURRENTLY users_id ON users(id);", |
| 53 | "CREATE TABLE users(id bigint GENERATED ALWAYS AS IDENTITY);", |
| 54 | "CREATE TABLE users(id text DEFAULT 'unterminated);", |
| 55 | // Each rejected by DSQL with "ALTER TABLE ADD COLUMN with constraint not supported". |
| 56 | "ALTER TABLE users ADD COLUMN active boolean NOT NULL DEFAULT true;", |
| 57 | "ALTER TABLE users ADD COLUMN nickname text DEFAULT 'a';", |
| 58 | "ALTER TABLE users ADD COLUMN email text UNIQUE;", |
| 59 | "ALTER TABLE users ADD age int CHECK (age > 0);", |
| 60 | ]) { |
| 61 | test.effect( |
| 62 | `DSQL preflight rejects ${sql}`, |
| 63 | () => |
| 64 | Effect.gen(function* () { |
| 65 | const result = yield* Effect.result(prepareDsqlStatements(sql, "0004_users.sql")); |
| 66 | expect(Result.isFailure(result)).toBe(true); |
| 67 | if (Result.isFailure(result)) expect(result.failure.message).toContain("0004_users.sql"); |
| 68 | }), |
| 69 | { tags: ["unit", "provider:aws", "provider:aws:dsql", "local"] }, |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | for (const sql of [ |
| 74 | "CREATE SEQUENCE ids CACHE 65535;", |
| 75 | "CREATE TABLE users(a bigint GENERATED ALWAYS AS IDENTITY (CACHE 65536), b bigint GENERATED ALWAYS AS IDENTITY);", |
| 76 | "SET search_path TO another_schema;", |
| 77 | ]) { |
| 78 | test.effect( |
| 79 | `DSQL validates all sequence definitions and session settings: ${sql}`, |
| 80 | () => |
| 81 | Effect.gen(function* () { |
| 82 | expect(Result.isFailure(yield* Effect.result(prepareDsqlStatements(sql, "0001.sql")))).toBe( |
| 83 | true, |
| 84 | ); |
| 85 | }), |
| 86 | { tags: ["unit", "provider:aws", "provider:aws:dsql", "local"] }, |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | test.effect( |
| 91 | "DSQL retains literal-only statements for database validation", |
| 92 | () => |
| 93 | Effect.gen(function* () { |
| 94 | expect( |
| 95 | yield* prepareDsqlStatements("'not a command'; $$nor is this$$; -- comment", "0001.sql"), |
| 96 | ).toEqual(["'not a command'", "$$nor is this$$"]); |
| 97 | }), |
| 98 | { tags: ["unit", "provider:aws", "provider:aws:dsql", "local"] }, |
| 99 | ); |
| 100 | |
| 101 | test.effect( |
| 102 | "DSQL accepts the ADD COLUMN forms it supports", |
| 103 | () => |
| 104 | Effect.gen(function* () { |
| 105 | const statements = yield* prepareDsqlStatements( |
| 106 | `ALTER TABLE users ADD COLUMN nickname text; |
| 107 | ALTER TABLE users ADD bio text NULL; |
| 108 | ALTER TABLE users RENAME COLUMN email TO address;`, |
| 109 | "0005_users.sql", |
| 110 | ); |
| 111 | expect(statements).toHaveLength(3); |
| 112 | }), |
| 113 | { tags: ["unit", "provider:aws", "provider:aws:dsql", "local"] }, |
| 114 | ); |