1import * as GCP from "@/GCP";
2import type { StackServices } from "@/Stack";
3import * as Test from "@/Test/Alchemy";
4import * as sqladmin from "@distilled.cloud/gcp/sqladmin_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8import { MinimumLogLevel } from "effect/References";
9import * as Schedule from "effect/Schedule";
10import { GcpEnvironment } from "@/GCP/Environment";
11
12const { test } = Test.make({
13 providers: GCP.providers() as Layer.Layer<
14 GCP.ProviderRequirements,
15 never,
16 StackServices
17 >,
18});
19
20const logLevel = Effect.provideService(
21 MinimumLogLevel,
22 process.env.DEBUG ? "Debug" : "Info",
23);
24
25// Runs against an existing Cloud SQL instance (creating one takes well over
26// 5 minutes); set GCP_SQL_INSTANCE to its name.
27const sqlInstance =
28 process.env.GCP_SQL_INSTANCE || process.env.GCP_TEST_SQL_INSTANCE;
29const runLifecycle = !!sqlInstance && !process.env.FAST;
30
31const waitUntilGone = (instance: string, databaseName: string) =>
32 GcpEnvironment.current.pipe(
33 Effect.flatMap(({ project }) =>
34 sqladmin
35 .getDatabases({
36 project,
37 instance,
38 database: databaseName,
39 })
40 .pipe(
41 Effect.as("found" as const),
42 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
43 Effect.repeat({
44 schedule: Schedule.spaced("1 second"),
45 until: (status) => status === "gone",
46 times: 10,
47 }),
48 ),
49 ),
50 );
51
52test.provider(
53 "getDatabases on a missing instance fails with SqlInstanceNotAuthorized",
54 (stack) =>
55 Effect.gen(function* () {
56 const { project } = yield* GcpEnvironment.current;
57 yield* stack.destroy();
58
59 const error = yield* Effect.flip(
60 sqladmin.getDatabases({
61 project,
62 instance: "alchemy-sql-instance-does-not-exist",
63 database: "alchemy_db_does_not_exist",
64 }),
65 );
66 // Cloud SQL hides unknown instances behind 403 rather than 404.
67 expect(error._tag).toBe("SqlInstanceNotAuthorized");
68
69 yield* stack.destroy();
70 }).pipe(logLevel),
71 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
72);
73
74test.provider(
75 "listDatabases on a missing instance fails with SqlInstanceNotAuthorized",
76 (stack) =>
77 Effect.gen(function* () {
78 const { project } = yield* GcpEnvironment.current;
79 yield* stack.destroy();
80
81 const error = yield* Effect.flip(
82 sqladmin.listDatabases({
83 project,
84 instance: "alchemy-sql-instance-does-not-exist",
85 }),
86 );
87 expect(error._tag).toBe("SqlInstanceNotAuthorized");
88
89 yield* stack.destroy();
90 }).pipe(logLevel),
91 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
92);
93
94test.provider.skipIf(!runLifecycle)(
95 "create, update, and delete a sql database",
96 (stack) =>
97 Effect.gen(function* () {
98 const { project } = yield* GcpEnvironment.current;
99 yield* stack.destroy();
100
101 const instance = sqlInstance!;
102
103 const created = yield* stack.deploy(
104 Effect.gen(function* () {
105 return yield* GCP.SQL.Database("App", {
106 instance,
107 });
108 }),
109 );
110
111 expect(created.databaseName).toEqual(expect.any(String));
112 expect(created.databaseName).toMatch(/^[a-z][a-z0-9_]{0,62}$/);
113 expect(created.instance).toEqual(instance);
114 expect(created.project).toEqual(project);
115
116 const fetched = yield* sqladmin.getDatabases({
117 project: created.project,
118 instance: created.instance,
119 database: created.databaseName,
120 });
121 expect(fetched.name).toEqual(created.databaseName);
122 expect(fetched.instance).toEqual(created.instance);
123
124 const live = yield* sqladmin.getInstances({
125 project: created.project,
126 instance: created.instance,
127 });
128 const isMysql = (live.databaseVersion ?? "")
129 .toUpperCase()
130 .startsWith("MYSQL");
131
132 const updated = yield* stack.deploy(
133 Effect.gen(function* () {
134 return yield* GCP.SQL.Database("App", {
135 instance,
136 databaseName: created.databaseName,
137 charset: isMysql ? "utf8mb4" : created.charset,
138 collation: isMysql ? "utf8mb4_unicode_ci" : created.collation,
139 });
140 }),
141 );
142
143 expect(updated.databaseName).toEqual(created.databaseName);
144 expect(updated.instance).toEqual(created.instance);
145 if (isMysql) {
146 expect(updated.charset?.toLowerCase()).toEqual("utf8mb4");
147 }
148
149 const refetched = yield* sqladmin.getDatabases({
150 project: updated.project,
151 instance: updated.instance,
152 database: updated.databaseName,
153 });
154 expect(refetched.name).toEqual(updated.databaseName);
155 if (isMysql) {
156 expect(refetched.charset?.toLowerCase()).toEqual("utf8mb4");
157 }
158
159 yield* stack.destroy();
160
161 const gone = yield* waitUntilGone(created.instance, created.databaseName);
162 expect(gone).toEqual("gone");
163 }).pipe(logLevel),
164 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 120_000 },
165);