| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as spanner from "@distilled.cloud/gcp/spanner_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | spanner.getProjectsInstancesDatabases({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("2 seconds"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsInstancesDatabases on a missing database fails with NotFound", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | const error = yield* Effect.flip( |
| 36 | spanner.getProjectsInstancesDatabases({ |
| 37 | name: `projects/${project}/instances/alchemy-spanner-missing/databases/alchemy-missing`, |
| 38 | }), |
| 39 | ); |
| 40 | expect(error._tag).toBe("NotFound"); |
| 41 | |
| 42 | const page = yield* spanner.listProjectsInstances({ |
| 43 | parent: `projects/${project}`, |
| 44 | pageSize: 10, |
| 45 | }); |
| 46 | expect( |
| 47 | (page.instances ?? []).map((item) => item.name?.split("/").pop()), |
| 48 | ).not.toContain("alchemy-spanner-missing"); |
| 49 | |
| 50 | yield* stack.destroy(); |
| 51 | }).pipe(logLevel), |
| 52 | { tags: ["provider:gcp", "provider:gcp:spanner", "live"], timeout: 90_000 }, |
| 53 | ); |
| 54 | |
| 55 | test.provider( |
| 56 | "create, update, query, and delete a spanner database", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | const instance = yield* GCP.Spanner.Instance("App", { |
| 64 | config: "regional-us-central1", |
| 65 | processingUnits: 100, |
| 66 | displayName: "alchemy-db-spnr", |
| 67 | defaultBackupScheduleType: "NONE", |
| 68 | }); |
| 69 | const database = yield* GCP.Spanner.Database("AppDb", { |
| 70 | instance: instance.instanceId, |
| 71 | extraStatements: [ |
| 72 | "CREATE TABLE Users (UserId INT64 NOT NULL) PRIMARY KEY (UserId)", |
| 73 | ], |
| 74 | }); |
| 75 | return { instance, database }; |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | expect(created.database.name).toContain("/databases/"); |
| 80 | expect(created.database.databaseId).toEqual(expect.any(String)); |
| 81 | expect(created.database.instanceId).toEqual(created.instance.instanceId); |
| 82 | expect(created.database.enableDropProtection).toEqual(false); |
| 83 | expect(created.database.state).toEqual("READY"); |
| 84 | |
| 85 | const fetched = yield* spanner.getProjectsInstancesDatabases({ |
| 86 | name: created.database.name, |
| 87 | }); |
| 88 | expect(fetched.name).toEqual(created.database.name); |
| 89 | expect(fetched.enableDropProtection === true).toEqual(false); |
| 90 | |
| 91 | const ddl = yield* spanner.getDdlProjectsInstancesDatabases({ |
| 92 | database: created.database.name, |
| 93 | }); |
| 94 | expect( |
| 95 | ddl.statements?.some((statement) => statement.includes("Users")), |
| 96 | ).toEqual(true); |
| 97 | |
| 98 | const session = yield* spanner.createProjectsInstancesDatabasesSessions({ |
| 99 | database: created.database.name, |
| 100 | body: { session: {} }, |
| 101 | }); |
| 102 | const result = yield* spanner |
| 103 | .executeSqlProjectsInstancesDatabasesSessions({ |
| 104 | session: session.name ?? "", |
| 105 | body: { sql: "SELECT 1 AS n" }, |
| 106 | }) |
| 107 | .pipe( |
| 108 | Effect.ensuring( |
| 109 | session.name |
| 110 | ? spanner |
| 111 | .deleteProjectsInstancesDatabasesSessions({ |
| 112 | name: session.name, |
| 113 | }) |
| 114 | .pipe( |
| 115 | Effect.catchTag("NotFound", () => Effect.void), |
| 116 | Effect.ignore, |
| 117 | ) |
| 118 | : Effect.void, |
| 119 | ), |
| 120 | ); |
| 121 | expect((result.rows ?? []).length).toBeGreaterThan(0); |
| 122 | |
| 123 | const updated = yield* stack.deploy( |
| 124 | Effect.gen(function* () { |
| 125 | const instance = yield* GCP.Spanner.Instance("App", { |
| 126 | instanceId: created.instance.instanceId, |
| 127 | config: "regional-us-central1", |
| 128 | processingUnits: 100, |
| 129 | displayName: "alchemy-db-spnr", |
| 130 | defaultBackupScheduleType: "NONE", |
| 131 | }); |
| 132 | const database = yield* GCP.Spanner.Database("AppDb", { |
| 133 | instance: instance.instanceId, |
| 134 | databaseId: created.database.databaseId, |
| 135 | extraStatements: [ |
| 136 | "CREATE TABLE Users (UserId INT64 NOT NULL) PRIMARY KEY (UserId)", |
| 137 | ], |
| 138 | enableDropProtection: true, |
| 139 | }); |
| 140 | return { instance, database }; |
| 141 | }), |
| 142 | ); |
| 143 | |
| 144 | expect(updated.database.name).toEqual(created.database.name); |
| 145 | expect(updated.database.enableDropProtection).toEqual(true); |
| 146 | |
| 147 | const refetched = yield* spanner.getProjectsInstancesDatabases({ |
| 148 | name: created.database.name, |
| 149 | }); |
| 150 | expect(refetched.enableDropProtection).toEqual(true); |
| 151 | |
| 152 | yield* stack.destroy(); |
| 153 | |
| 154 | const gone = yield* waitUntilGone(created.database.name); |
| 155 | expect(gone).toEqual("gone"); |
| 156 | }).pipe(logLevel), |
| 157 | { tags: ["provider:gcp", "provider:gcp:spanner", "live"], timeout: 180_000 }, |
| 158 | ); |