The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as sqladmin from "@distilled.cloud/gcp/sqladmin_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { GcpEnvironment } from "../Environment.ts"; |
| 12 | import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 13 | import type { Providers } from "../Providers.ts"; |
| 14 | import { recoverIfInstanceMissing, waitForSqlOperation } from "./operations.ts"; |
| 15 | import { |
| 16 | encodeDescription, |
| 17 | hasOwnershipMarker, |
| 18 | instanceIdOf, |
| 19 | lastSegment, |
| 20 | matchesOwnership, |
| 21 | parseDescription, |
| 22 | } from "./ownership.ts"; |
| 23 | |
| 24 | export type BackupRunProps = { |
| 25 | /** |
| 26 | * Cloud SQL instance id (the `{instance}` segment of |
| 27 | * `projects/{project}/instances/{instance}`). Full resource names are |
| 28 | * accepted and reduced to the last path segment. Immutable — changing |
| 29 | * it replaces the backup run. |
| 30 | */ |
| 31 | instance: string; |
| 32 | /** |
| 33 | * Storage location of the backup (`us`, `us-central1`, …). If omitted, |
| 34 | * Cloud SQL picks the closest multi-region. Immutable — changing it |
| 35 | * replaces the backup run. |
| 36 | */ |
| 37 | location?: string; |
| 38 | /** |
| 39 | * Human-readable description. Cloud SQL backup runs have no labels |
| 40 | * field and no update API, so Alchemy stamps ownership into the |
| 41 | * description at create and cannot change it in place. |
| 42 | */ |
| 43 | description?: string; |
| 44 | /** |
| 45 | * Existing backup-run id used to observe a run when state is missing. |
| 46 | * The insert API assigns ids — this is not sent on insert. |
| 47 | */ |
| 48 | backupRunId?: string; |
| 49 | }; |
| 50 | |
| 51 | export type BackupRun = Resource< |
| 52 | "GCP.SQL.BackupRun", |
| 53 | BackupRunProps, |
| 54 | { |
| 55 | /** Backup-run id unique to the instance. */ |
| 56 | backupRunId: string; |
| 57 | /** Cloud SQL instance id. */ |
| 58 | instance: string; |
| 59 | /** Project id. */ |
| 60 | project: string; |
| 61 | /** User description with the Alchemy ownership prefix stripped. */ |
| 62 | description: string | undefined; |
| 63 | /** Run type (`ON_DEMAND`, `AUTOMATED`). */ |
| 64 | type: string | undefined; |
| 65 | /** Server-reported status (`ENQUEUED`, `RUNNING`, `SUCCESSFUL`, …). */ |
| 66 | status: string | undefined; |
| 67 | /** Storage location. */ |
| 68 | location: string | undefined; |
| 69 | /** SQL Admin self-link. */ |
| 70 | selfLink: string | undefined; |
| 71 | /** RFC3339 enqueue time. */ |
| 72 | enqueuedTime: string | undefined; |
| 73 | /** RFC3339 start time. */ |
| 74 | startTime: string | undefined; |
| 75 | /** RFC3339 end time. */ |
| 76 | endTime: string | undefined; |
| 77 | /** RFC3339 backup-window start. */ |
| 78 | windowStartTime: string | undefined; |
| 79 | /** Database version at backup time. */ |
| 80 | databaseVersion: string | undefined; |
| 81 | /** Kind of backup (`SNAPSHOT`, `PHYSICAL`). */ |
| 82 | backupKind: string | undefined; |
| 83 | /** Backup time zone (SQL Server). */ |
| 84 | timeZone: string | undefined; |
| 85 | /** Maximum chargeable bytes. */ |
| 86 | maxChargeableBytes: string | undefined; |
| 87 | }, |
| 88 | never, |
| 89 | Providers |
| 90 | >; |
| 91 | |
| 92 | /** |
| 93 | * An on-demand Cloud SQL backup run |
| 94 | * (`projects/{project}/instances/{instance}/backupRuns/{id}`). |
| 95 | * |
| 96 | * Insert assigns the run id. Alchemy stamps ownership into `description` |
| 97 | * so `list` / `pnpm nuke:gcp` can find leaked rows. Changing `instance` |
| 98 | * or `location` replaces the run. Description is create-only — backup |
| 99 | * runs have no update API. |
| 100 | * |
| 101 | * ### Creating a Backup Run |
| 102 | * **Example:** On-demand backup of a Cloud SQL instance |
| 103 | * ```typescript |
| 104 | * const instance = yield* GCP.SQL.Instance("AppDb", { |
| 105 | * tier: "db-f1-micro", |
| 106 | * backup: { enabled: true }, |
| 107 | * }); |
| 108 | * const backup = yield* GCP.SQL.BackupRun("Nightly", { |
| 109 | * instance: instance.instanceName, |
| 110 | * }); |
| 111 | * ``` |
| 112 | * |
| 113 | * **Example:** Description and location |
| 114 | * ```typescript |
| 115 | * const backup = yield* GCP.SQL.BackupRun("Nightly", { |
| 116 | * instance: instance.instanceName, |
| 117 | * location: "us", |
| 118 | * description: "pre-release", |
| 119 | * }); |
| 120 | * ``` |
| 121 | * |
| 122 | * @resource |
| 123 | * @category SQL |
| 124 | */ |
| 125 | export const BackupRun = Resource<BackupRun>("GCP.SQL.BackupRun"); |
| 126 | |
| 127 | export class BackupRunNotResolved extends Data.TaggedError("GCP.SQL.BackupRunNotResolved")<{ |
| 128 | instance: string; |
| 129 | backupRunId: string; |
| 130 | }> {} |
| 131 | |
| 132 | export class BackupRunNotReady extends Data.TaggedError("GCP.SQL.BackupRunNotReady")<{ |
| 133 | instance: string; |
| 134 | backupRunId: string; |
| 135 | status: string | undefined; |
| 136 | }> {} |
| 137 | |
| 138 | export class BackupRunStillExists extends Data.TaggedError("GCP.SQL.BackupRunStillExists")<{ |
| 139 | instance: string; |
| 140 | backupRunId: string; |
| 141 | }> {} |
| 142 | |
| 143 | const normalizeLocation = (value: string | undefined) => |
| 144 | value === undefined || value.length === 0 ? undefined : lastSegment(value).toLowerCase(); |
| 145 | |
| 146 | const idOf = (value: string | number | undefined) => (value === undefined ? "" : String(value)); |
| 147 | |
| 148 | const toUserDescription = (id: string, description: string | undefined, existing?: string) => |
| 149 | Effect.gen(function* () { |
| 150 | if (description !== undefined) return description; |
| 151 | if (existing !== undefined) return existing; |
| 152 | return yield* createPhysicalName({ |
| 153 | id, |
| 154 | maxLength: 63, |
| 155 | lowercase: true, |
| 156 | }); |
| 157 | }); |
| 158 | |
| 159 | const toAttrs = (run: sqladmin.BackupRun, project: string, instance: string) => ({ |
| 160 | backupRunId: idOf(run.id), |
| 161 | instance: instanceIdOf(run.instance ?? instance), |
| 162 | project, |
| 163 | description: parseDescription(run.description).description, |
| 164 | type: run.type, |
| 165 | status: run.status, |
| 166 | location: run.location, |
| 167 | selfLink: run.selfLink, |
| 168 | enqueuedTime: run.enqueuedTime, |
| 169 | startTime: run.startTime, |
| 170 | endTime: run.endTime, |
| 171 | windowStartTime: run.windowStartTime, |
| 172 | databaseVersion: run.databaseVersion, |
| 173 | backupKind: run.backupKind, |
| 174 | timeZone: run.timeZone, |
| 175 | maxChargeableBytes: run.maxChargeableBytes, |
| 176 | }); |
| 177 | |
| 178 | const getById = (project: string, instance: string, backupRunId: string) => |
| 179 | backupRunId.length === 0 |
| 180 | ? Effect.undefined |
| 181 | : sqladmin |
| 182 | .getBackupRuns({ |
| 183 | project, |
| 184 | instance, |
| 185 | id: backupRunId, |
| 186 | }) |
| 187 | .pipe( |
| 188 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 189 | recoverIfInstanceMissing(project, instance, () => undefined), |
| 190 | ); |
| 191 | |
| 192 | const listRuns = (project: string, instance: string) => |
| 193 | sqladmin.listBackupRuns |
| 194 | .items({ |
| 195 | project, |
| 196 | instance, |
| 197 | maxResults: 1000, |
| 198 | }) |
| 199 | .pipe( |
| 200 | Stream.runCollect, |
| 201 | Effect.map((chunk) => Array.from(chunk)), |
| 202 | Effect.catchTag("NotFound", () => Effect.succeed([] as sqladmin.BackupRun[])), |
| 203 | recoverIfInstanceMissing(project, instance, () => [] as sqladmin.BackupRun[]), |
| 204 | ); |
| 205 | |
| 206 | const findByOwnership = (project: string, instance: string, labels: Record<string, string>) => |
| 207 | listRuns(project, instance).pipe( |
| 208 | Effect.map((runs) => runs.find((run) => matchesOwnership(run.description, labels))), |
| 209 | ); |
| 210 | |
| 211 | const observe = ( |
| 212 | project: string, |
| 213 | instance: string, |
| 214 | backupRunId: string | undefined, |
| 215 | labels: Record<string, string>, |
| 216 | ) => |
| 217 | Effect.gen(function* () { |
| 218 | if (backupRunId !== undefined && backupRunId.length > 0) { |
| 219 | const existing = yield* getById(project, instance, backupRunId); |
| 220 | if (existing !== undefined) return existing; |
| 221 | } |
| 222 | return yield* findByOwnership(project, instance, labels); |
| 223 | }); |
| 224 | |
| 225 | const waitForOperation = ( |
| 226 | project: string, |
| 227 | operation: sqladmin.Operation, |
| 228 | options?: { notFoundOk?: boolean }, |
| 229 | ) => |
| 230 | waitForSqlOperation(project, operation, { |
| 231 | budget: "20 minutes", |
| 232 | notFoundOk: options?.notFoundOk, |
| 233 | }); |
| 234 | |
| 235 | const waitUntilExists = (project: string, instance: string, backupRunId: string) => |
| 236 | getById(project, instance, backupRunId).pipe( |
| 237 | Effect.flatMap((run) => |
| 238 | run ? Effect.succeed(run) : Effect.fail(new BackupRunNotResolved({ instance, backupRunId })), |
| 239 | ), |
| 240 | Effect.retry({ |
| 241 | while: (error) => error._tag === "GCP.SQL.BackupRunNotResolved", |
| 242 | times: 8, |
| 243 | schedule: Schedule.spaced("2 seconds"), |
| 244 | }), |
| 245 | ); |
| 246 | |
| 247 | const isBusy = (status: string | undefined) => |
| 248 | status === "ENQUEUED" || |
| 249 | status === "OVERDUE" || |
| 250 | status === "RUNNING" || |
| 251 | status === "SQL_BACKUP_RUN_STATUS_UNSPECIFIED" || |
| 252 | status === undefined; |
| 253 | |
| 254 | const waitUntilReady = (project: string, instance: string, backupRunId: string) => |
| 255 | getById(project, instance, backupRunId).pipe( |
| 256 | Effect.filterOrFail( |
| 257 | (run): run is sqladmin.BackupRun => run !== undefined, |
| 258 | () => new BackupRunNotResolved({ instance, backupRunId }), |
| 259 | ), |
| 260 | Effect.filterOrFail( |
| 261 | (run) => !isBusy(run.status), |
| 262 | (run) => |
| 263 | new BackupRunNotReady({ |
| 264 | instance, |
| 265 | backupRunId, |
| 266 | status: run.status, |
| 267 | }), |
| 268 | ), |
| 269 | Effect.retry({ |
| 270 | while: (error) => |
| 271 | error._tag === "GCP.SQL.BackupRunNotReady" || error._tag === "GCP.SQL.BackupRunNotResolved", |
| 272 | times: 10, |
| 273 | schedule: Schedule.spaced("8 seconds"), |
| 274 | }), |
| 275 | ); |
| 276 | |
| 277 | const waitUntilGone = (project: string, instance: string, backupRunId: string) => |
| 278 | getById(project, instance, backupRunId).pipe( |
| 279 | Effect.flatMap((run) => |
| 280 | run === undefined || run.status === "DELETED" |
| 281 | ? Effect.void |
| 282 | : Effect.fail(new BackupRunStillExists({ instance, backupRunId })), |
| 283 | ), |
| 284 | Effect.retry({ |
| 285 | while: (error) => error._tag === "GCP.SQL.BackupRunStillExists", |
| 286 | times: 10, |
| 287 | schedule: Schedule.spaced("5 seconds"), |
| 288 | }), |
| 289 | ); |
| 290 | |
| 291 | const idFromOperation = (operation: sqladmin.Operation) => idOf(operation.backupContext?.backupId); |
| 292 | |
| 293 | export const BackupRunProvider = () => |
| 294 | Provider.succeed(BackupRun, { |
| 295 | stables: ["backupRunId", "instance", "project", "selfLink"], |
| 296 | |
| 297 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 298 | if (!isResolved(news)) return undefined; |
| 299 | const previousInstance = olds?.instance ?? output?.instance; |
| 300 | const nextInstance = news.instance; |
| 301 | const previousLocation = normalizeLocation(olds?.location ?? output?.location); |
| 302 | const nextLocation = normalizeLocation(news.location ?? output?.location); |
| 303 | const instanceChanged = |
| 304 | previousInstance !== undefined && |
| 305 | instanceIdOf(previousInstance) !== instanceIdOf(nextInstance); |
| 306 | const locationChanged = |
| 307 | news.location !== undefined && |
| 308 | previousLocation !== undefined && |
| 309 | nextLocation !== undefined && |
| 310 | previousLocation !== nextLocation; |
| 311 | if (!instanceChanged && !locationChanged) return undefined; |
| 312 | return { |
| 313 | action: "replace" as const, |
| 314 | deleteFirst: false, |
| 315 | }; |
| 316 | }), |
| 317 | |
| 318 | read: Effect.fn(function* ({ id, olds, output }) { |
| 319 | const env = yield* GcpEnvironment.current; |
| 320 | const instance = instanceIdOf(olds?.instance ?? output?.instance ?? ""); |
| 321 | if (instance.length === 0) return undefined; |
| 322 | const ownership = yield* createInternalLabels(id); |
| 323 | const existing = yield* observe( |
| 324 | env.project, |
| 325 | instance, |
| 326 | olds?.backupRunId ?? output?.backupRunId, |
| 327 | ownership, |
| 328 | ); |
| 329 | if (existing === undefined) return undefined; |
| 330 | const attrs = toAttrs(existing, env.project, instance); |
| 331 | const { labels } = parseDescription(existing.description); |
| 332 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 333 | }), |
| 334 | |
| 335 | list: () => |
| 336 | Effect.gen(function* () { |
| 337 | const env = yield* GcpEnvironment.current; |
| 338 | const runs = yield* listRuns(env.project, "-"); |
| 339 | return runs |
| 340 | .filter((run) => hasOwnershipMarker(run.description)) |
| 341 | .map((run) => toAttrs(run, env.project, instanceIdOf(run.instance ?? ""))); |
| 342 | }), |
| 343 | |
| 344 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 345 | const env = yield* GcpEnvironment.current; |
| 346 | const instance = instanceIdOf(news.instance); |
| 347 | const ownership = yield* createInternalLabels(id); |
| 348 | const userDescription = yield* toUserDescription(id, news.description, output?.description); |
| 349 | const desiredDescription = encodeDescription(ownership, userDescription); |
| 350 | |
| 351 | let current = yield* observe( |
| 352 | env.project, |
| 353 | instance, |
| 354 | news.backupRunId ?? output?.backupRunId, |
| 355 | ownership, |
| 356 | ); |
| 357 | |
| 358 | if (current === undefined) { |
| 359 | const created = yield* sqladmin |
| 360 | .insertBackupRuns({ |
| 361 | project: env.project, |
| 362 | instance, |
| 363 | body: { |
| 364 | instance, |
| 365 | description: desiredDescription, |
| 366 | location: news.location, |
| 367 | }, |
| 368 | }) |
| 369 | .pipe( |
| 370 | Effect.catchTag("Conflict", (error) => |
| 371 | findByOwnership(env.project, instance, ownership).pipe( |
| 372 | Effect.flatMap((existing) => (existing ? Effect.undefined : Effect.fail(error))), |
| 373 | ), |
| 374 | ), |
| 375 | Effect.retry({ |
| 376 | while: (error) => error._tag === "Conflict", |
| 377 | times: 8, |
| 378 | schedule: Schedule.spaced("5 seconds"), |
| 379 | }), |
| 380 | ); |
| 381 | |
| 382 | if (created !== undefined) { |
| 383 | const done = yield* waitForOperation(env.project, created); |
| 384 | const backupRunId = idFromOperation(done) || idFromOperation(created); |
| 385 | if (backupRunId.length > 0) { |
| 386 | current = yield* waitUntilExists(env.project, instance, backupRunId); |
| 387 | } |
| 388 | } |
| 389 | if (current === undefined) { |
| 390 | current = yield* findByOwnership(env.project, instance, ownership).pipe( |
| 391 | Effect.flatMap((existing) => |
| 392 | existing |
| 393 | ? Effect.succeed(existing) |
| 394 | : Effect.fail( |
| 395 | new BackupRunNotResolved({ |
| 396 | instance, |
| 397 | backupRunId: output?.backupRunId ?? ownership[alchemyLabelKeys.id] ?? "", |
| 398 | }), |
| 399 | ), |
| 400 | ), |
| 401 | Effect.retry({ |
| 402 | while: (error) => error._tag === "GCP.SQL.BackupRunNotResolved", |
| 403 | times: 8, |
| 404 | schedule: Schedule.spaced("2 seconds"), |
| 405 | }), |
| 406 | ); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | const backupRunId = idOf(current.id); |
| 411 | if (backupRunId.length === 0) { |
| 412 | return yield* new BackupRunNotResolved({ |
| 413 | instance, |
| 414 | backupRunId, |
| 415 | }); |
| 416 | } |
| 417 | |
| 418 | if (isBusy(current.status)) { |
| 419 | current = yield* waitUntilReady(env.project, instance, backupRunId); |
| 420 | } |
| 421 | |
| 422 | return toAttrs(current, env.project, instance); |
| 423 | }), |
| 424 | |
| 425 | delete: Effect.fn(function* ({ output }) { |
| 426 | const project = output.project; |
| 427 | const instance = instanceIdOf(output.instance); |
| 428 | const backupRunId = output.backupRunId; |
| 429 | if (backupRunId.length === 0) return; |
| 430 | yield* sqladmin |
| 431 | .deleteBackupRuns({ |
| 432 | project, |
| 433 | instance, |
| 434 | id: backupRunId, |
| 435 | }) |
| 436 | .pipe( |
| 437 | Effect.flatMap((operation) => waitForOperation(project, operation, { notFoundOk: true })), |
| 438 | Effect.catchTag("NotFound", () => Effect.void), |
| 439 | recoverIfInstanceMissing(project, instance, () => undefined), |
| 440 | Effect.retry({ |
| 441 | while: (error) => error._tag === "Conflict", |
| 442 | times: 8, |
| 443 | schedule: Schedule.spaced("5 seconds"), |
| 444 | }), |
| 445 | ); |
| 446 | yield* waitUntilGone(project, instance, backupRunId); |
| 447 | }), |
| 448 | }); |