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 * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as spanner from "@distilled.cloud/gcp/spanner_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as Path from "effect/Path"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: GCP.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | const waitUntilGone = (name: string) => |
| 17 | spanner.getProjectsInstancesDatabases({ name }).pipe( |
| 18 | Effect.as("found" as const), |
| 19 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 20 | Effect.repeat({ |
| 21 | schedule: Schedule.spaced("2 seconds"), |
| 22 | until: (status) => status === "gone", |
| 23 | times: 10, |
| 24 | }), |
| 25 | ); |
| 26 | |
| 27 | test.provider( |
| 28 | "getProjectsInstancesDatabases on a missing database fails with NotFound", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | const { project } = yield* GcpEnvironment.current; |
| 32 | yield* stack.destroy(); |
| 33 | |
| 34 | const error = yield* Effect.flip( |
| 35 | spanner.getProjectsInstancesDatabases({ |
| 36 | name: `projects/${project}/instances/alchemy-spanner-missing/databases/alchemy-missing`, |
| 37 | }), |
| 38 | ); |
| 39 | expect(error._tag).toBe("NotFound"); |
| 40 | |
| 41 | const page = yield* spanner.listProjectsInstances({ |
| 42 | parent: `projects/${project}`, |
| 43 | pageSize: 10, |
| 44 | }); |
| 45 | expect((page.instances ?? []).map((item) => item.name?.split("/").pop())).not.toContain( |
| 46 | "alchemy-spanner-missing", |
| 47 | ); |
| 48 | |
| 49 | yield* stack.destroy(); |
| 50 | }).pipe(logLevel), |
| 51 | { tags: ["provider:gcp", "provider:gcp:spanner", "live"], timeout: 90_000 }, |
| 52 | ); |
| 53 | |
| 54 | test.provider( |
| 55 | "create, update, query, and delete a spanner database", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | yield* stack.destroy(); |
| 59 | |
| 60 | const created = yield* stack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | const instance = yield* GCP.Spanner.Instance("App", { |
| 63 | config: "regional-us-central1", |
| 64 | processingUnits: 100, |
| 65 | displayName: "alchemy-db-spnr", |
| 66 | defaultBackupScheduleType: "NONE", |
| 67 | }); |
| 68 | const database = yield* GCP.Spanner.Database("AppDb", { |
| 69 | instance: instance.instanceId, |
| 70 | extraStatements: ["CREATE TABLE Users (UserId INT64 NOT NULL) PRIMARY KEY (UserId)"], |
| 71 | }); |
| 72 | return { instance, database }; |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | expect(created.database.name).toContain("/databases/"); |
| 77 | expect(created.database.databaseId).toEqual(expect.any(String)); |
| 78 | expect(created.database.instanceId).toEqual(created.instance.instanceId); |
| 79 | expect(created.database.enableDropProtection).toEqual(false); |
| 80 | expect(created.database.state).toEqual("READY"); |
| 81 | |
| 82 | const fetched = yield* spanner.getProjectsInstancesDatabases({ |
| 83 | name: created.database.name, |
| 84 | }); |
| 85 | expect(fetched.name).toEqual(created.database.name); |
| 86 | expect(fetched.enableDropProtection === true).toEqual(false); |
| 87 | |
| 88 | const ddl = yield* spanner.getDdlProjectsInstancesDatabases({ |
| 89 | database: created.database.name, |
| 90 | }); |
| 91 | expect(ddl.statements?.some((statement) => statement.includes("Users"))).toEqual(true); |
| 92 | |
| 93 | const session = yield* spanner.createProjectsInstancesDatabasesSessions({ |
| 94 | database: created.database.name, |
| 95 | body: { session: {} }, |
| 96 | }); |
| 97 | const result = yield* spanner |
| 98 | .executeSqlProjectsInstancesDatabasesSessions({ |
| 99 | session: session.name ?? "", |
| 100 | body: { sql: "SELECT 1 AS n" }, |
| 101 | }) |
| 102 | .pipe( |
| 103 | Effect.ensuring( |
| 104 | session.name |
| 105 | ? spanner |
| 106 | .deleteProjectsInstancesDatabasesSessions({ |
| 107 | name: session.name, |
| 108 | }) |
| 109 | .pipe( |
| 110 | Effect.catchTag("NotFound", () => Effect.void), |
| 111 | Effect.ignore, |
| 112 | ) |
| 113 | : Effect.void, |
| 114 | ), |
| 115 | ); |
| 116 | expect((result.rows ?? []).length).toBeGreaterThan(0); |
| 117 | |
| 118 | const updated = yield* stack.deploy( |
| 119 | Effect.gen(function* () { |
| 120 | const instance = yield* GCP.Spanner.Instance("App", { |
| 121 | instanceId: created.instance.instanceId, |
| 122 | config: "regional-us-central1", |
| 123 | processingUnits: 100, |
| 124 | displayName: "alchemy-db-spnr", |
| 125 | defaultBackupScheduleType: "NONE", |
| 126 | }); |
| 127 | const database = yield* GCP.Spanner.Database("AppDb", { |
| 128 | instance: instance.instanceId, |
| 129 | databaseId: created.database.databaseId, |
| 130 | extraStatements: ["CREATE TABLE Users (UserId INT64 NOT NULL) PRIMARY KEY (UserId)"], |
| 131 | enableDropProtection: true, |
| 132 | }); |
| 133 | return { instance, database }; |
| 134 | }), |
| 135 | ); |
| 136 | |
| 137 | expect(updated.database.name).toEqual(created.database.name); |
| 138 | expect(updated.database.enableDropProtection).toEqual(true); |
| 139 | |
| 140 | const refetched = yield* spanner.getProjectsInstancesDatabases({ |
| 141 | name: created.database.name, |
| 142 | }); |
| 143 | expect(refetched.enableDropProtection).toEqual(true); |
| 144 | |
| 145 | yield* stack.destroy(); |
| 146 | |
| 147 | const gone = yield* waitUntilGone(created.database.name); |
| 148 | expect(gone).toEqual("gone"); |
| 149 | }).pipe(logLevel), |
| 150 | { tags: ["provider:gcp", "provider:gcp:spanner", "live"], timeout: 180_000 }, |
| 151 | ); |
| 152 | |
| 153 | /** Rows of one query, read out-of-band through a fresh session. */ |
| 154 | const querySpanner = (database: string, sql: string) => |
| 155 | Effect.acquireUseRelease( |
| 156 | spanner.createProjectsInstancesDatabasesSessions({ database, body: { session: {} } }), |
| 157 | (session) => |
| 158 | spanner |
| 159 | .executeSqlProjectsInstancesDatabasesSessions({ |
| 160 | session: session.name ?? "", |
| 161 | body: { sql }, |
| 162 | }) |
| 163 | .pipe(Effect.map((result) => (result.rows ?? []) as unknown[][])), |
| 164 | (session) => |
| 165 | spanner |
| 166 | .deleteProjectsInstancesDatabasesSessions({ name: session.name ?? "" }) |
| 167 | .pipe(Effect.ignore), |
| 168 | ); |
| 169 | |
| 170 | const migrationsCases = [ |
| 171 | { |
| 172 | dialect: "GOOGLE_STANDARD_SQL", |
| 173 | label: "GoogleSQL", |
| 174 | first: `-- Users and their posts |
| 175 | CREATE TABLE Users ( |
| 176 | UserId INT64 NOT NULL, |
| 177 | Name STRING(MAX) -- a ';' in a comment |
| 178 | ) PRIMARY KEY (UserId); |
| 179 | CREATE TABLE Posts (PostId INT64 NOT NULL, Title STRING(MAX)) PRIMARY KEY (PostId);`, |
| 180 | // DDL, then trailing DML that commits with the bookkeeping row. |
| 181 | second: `ALTER TABLE Users ADD COLUMN Email STRING(MAX); |
| 182 | INSERT INTO Users (UserId, Name, Email) VALUES (1, 'it\\'s; me', 'me@example.com');`, |
| 183 | interleaved: `CREATE TABLE A (Id INT64 NOT NULL) PRIMARY KEY (Id); |
| 184 | INSERT INTO A (Id) VALUES (1); |
| 185 | CREATE TABLE B (Id INT64 NOT NULL) PRIMARY KEY (Id);`, |
| 186 | // The second statement names a primary key column that doesn't exist. |
| 187 | broken: `CREATE TABLE C (Id INT64 NOT NULL) PRIMARY KEY (Id); |
| 188 | CREATE TABLE D (Id INT64 NOT NULL) PRIMARY KEY (Missing);`, |
| 189 | fixed: `CREATE TABLE C (Id INT64 NOT NULL) PRIMARY KEY (Id); |
| 190 | CREATE TABLE D (Id INT64 NOT NULL) PRIMARY KEY (Id);`, |
| 191 | // UserId 1 already exists. |
| 192 | duplicate: "INSERT INTO Users (UserId, Name) VALUES (1, 'again')", |
| 193 | tables: |
| 194 | "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '' AND TABLE_NAME IN ('C', 'D') ORDER BY TABLE_NAME", |
| 195 | applied: "SELECT name FROM alchemy_migrations ORDER BY name", |
| 196 | seeded: "SELECT Name, Email FROM Users", |
| 197 | }, |
| 198 | { |
| 199 | dialect: "POSTGRESQL", |
| 200 | label: "PostgreSQL", |
| 201 | first: `CREATE TABLE users ( |
| 202 | user_id bigint NOT NULL PRIMARY KEY, |
| 203 | name text |
| 204 | ); |
| 205 | CREATE TABLE posts (post_id bigint NOT NULL PRIMARY KEY, title text);`, |
| 206 | second: `ALTER TABLE users ADD COLUMN email text; |
| 207 | INSERT INTO users (user_id, name, email) VALUES (1, 'it''s; me', 'me@example.com');`, |
| 208 | interleaved: `CREATE TABLE a (id bigint NOT NULL PRIMARY KEY); |
| 209 | INSERT INTO a (id) VALUES (1); |
| 210 | CREATE TABLE b (id bigint NOT NULL PRIMARY KEY);`, |
| 211 | broken: `CREATE TABLE c (id bigint NOT NULL PRIMARY KEY); |
| 212 | CREATE TABLE d (id bigint NOT NULL, PRIMARY KEY (missing));`, |
| 213 | fixed: `CREATE TABLE c (id bigint NOT NULL PRIMARY KEY); |
| 214 | CREATE TABLE d (id bigint NOT NULL PRIMARY KEY);`, |
| 215 | duplicate: "INSERT INTO users (user_id, name) VALUES (1, 'again')", |
| 216 | tables: |
| 217 | "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name IN ('c', 'd') ORDER BY table_name", |
| 218 | applied: "SELECT name FROM alchemy_migrations ORDER BY name", |
| 219 | seeded: "SELECT name, email FROM users", |
| 220 | }, |
| 221 | ] as const; |
| 222 | |
| 223 | for (const testCase of migrationsCases) { |
| 224 | test.provider.skipIf(!!process.env.FAST)( |
| 225 | `${testCase.label} database applies migrations, then only pending ones`, |
| 226 | (stack) => |
| 227 | Effect.gen(function* () { |
| 228 | const fs = yield* FileSystem.FileSystem; |
| 229 | const path = yield* Path.Path; |
| 230 | const dir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-spanner-migrations-" }); |
| 231 | const add = (name: string, sql: string) => fs.writeFileString(path.join(dir, name), sql); |
| 232 | yield* add("0001_users.sql", testCase.first); |
| 233 | |
| 234 | const program = Effect.gen(function* () { |
| 235 | const instance = yield* GCP.Spanner.Instance("Migrations", { |
| 236 | config: "regional-us-central1", |
| 237 | processingUnits: 100, |
| 238 | displayName: "alchemy-migrations", |
| 239 | defaultBackupScheduleType: "NONE", |
| 240 | }); |
| 241 | return yield* GCP.Spanner.Database("AppDb", { |
| 242 | instance: instance.instanceId, |
| 243 | databaseDialect: testCase.dialect, |
| 244 | migrations: dir, |
| 245 | }); |
| 246 | }); |
| 247 | |
| 248 | yield* stack.destroy(); |
| 249 | |
| 250 | const created = yield* stack.deploy(program); |
| 251 | expect(created.migrationsTable).toEqual("alchemy_migrations"); |
| 252 | expect(Object.keys(created.migrationsHashes)).toEqual(["0001_users.sql"]); |
| 253 | expect(yield* querySpanner(created.name, testCase.applied)).toEqual([["0001_users.sql"]]); |
| 254 | |
| 255 | // Nothing changed: the next deploy has nothing to apply. |
| 256 | const unchanged = yield* stack.plan(program); |
| 257 | expect(unchanged.resources.AppDb?.action).toEqual("noop"); |
| 258 | |
| 259 | yield* add("0002_email.sql", testCase.second); |
| 260 | const pending = yield* stack.plan(program); |
| 261 | expect(pending.resources.AppDb?.action).toEqual("update"); |
| 262 | |
| 263 | const updated = yield* stack.deploy(program); |
| 264 | expect(updated.name).toEqual(created.name); |
| 265 | expect(Object.keys(updated.migrationsHashes).sort()).toEqual([ |
| 266 | "0001_users.sql", |
| 267 | "0002_email.sql", |
| 268 | ]); |
| 269 | expect(yield* querySpanner(updated.name, testCase.applied)).toEqual([ |
| 270 | ["0001_users.sql"], |
| 271 | ["0002_email.sql"], |
| 272 | ]); |
| 273 | expect(yield* querySpanner(updated.name, testCase.seeded)).toEqual([ |
| 274 | ["it's; me", "me@example.com"], |
| 275 | ]); |
| 276 | |
| 277 | // DML between DDL statements can't be applied exactly once. |
| 278 | yield* add("0003_interleaved.sql", testCase.interleaved); |
| 279 | const error = yield* stack.deploy(program).pipe(Effect.flip); |
| 280 | expect(String(error)).toContain("runs DML between DDL statements"); |
| 281 | expect(yield* querySpanner(updated.name, testCase.applied)).toEqual([ |
| 282 | ["0001_users.sql"], |
| 283 | ["0002_email.sql"], |
| 284 | ]); |
| 285 | yield* fs.remove(path.join(dir, "0003_interleaved.sql")); |
| 286 | |
| 287 | // A statement fails after an earlier one applied: the deploy fails, |
| 288 | // the migration stays unrecorded, and redeploying the same file |
| 289 | // fails on the statement again rather than on a stale operation. |
| 290 | yield* add("0003_tables.sql", testCase.broken); |
| 291 | for (let attempt = 0; attempt < 2; attempt++) { |
| 292 | const failed = yield* stack.deploy(program).pipe(Effect.flip); |
| 293 | expect(String(failed)).toContain("Spanner DDL"); |
| 294 | expect(String(failed)).not.toContain("already exists"); |
| 295 | } |
| 296 | expect(yield* querySpanner(updated.name, testCase.applied)).toEqual([ |
| 297 | ["0001_users.sql"], |
| 298 | ["0002_email.sql"], |
| 299 | ]); |
| 300 | |
| 301 | // Fixing the statement resumes after the one already applied. |
| 302 | yield* add("0003_tables.sql", testCase.fixed); |
| 303 | yield* stack.deploy(program); |
| 304 | expect(yield* querySpanner(updated.name, testCase.tables)).toEqual([ |
| 305 | [testCase.dialect === "POSTGRESQL" ? "c" : "C"], |
| 306 | [testCase.dialect === "POSTGRESQL" ? "d" : "D"], |
| 307 | ]); |
| 308 | expect(yield* querySpanner(updated.name, testCase.applied)).toEqual([ |
| 309 | ["0001_users.sql"], |
| 310 | ["0002_email.sql"], |
| 311 | ["0003_tables.sql"], |
| 312 | ]); |
| 313 | |
| 314 | // Failing trailing DML records nothing. |
| 315 | yield* add("0004_duplicate.sql", testCase.duplicate); |
| 316 | const duplicate = yield* stack.deploy(program).pipe(Effect.flip); |
| 317 | expect(String(duplicate)).toContain("Spanner DML"); |
| 318 | expect(yield* querySpanner(updated.name, testCase.applied)).toEqual([ |
| 319 | ["0001_users.sql"], |
| 320 | ["0002_email.sql"], |
| 321 | ["0003_tables.sql"], |
| 322 | ]); |
| 323 | |
| 324 | yield* stack.destroy(); |
| 325 | expect(yield* waitUntilGone(created.name)).toEqual("gone"); |
| 326 | }).pipe(logLevel), |
| 327 | { tags: ["provider:gcp", "provider:gcp:spanner", "live"], timeout: 600_000 }, |
| 328 | ); |
| 329 | } |