sailfork / t3code-github / .repos/ alchemy-effect/ packages/ gcp/ src/ Spanner/ InstancesBackup.ts

import * as spanner from "@distilled.cloud/gcp/spanner_v1";
import { Unowned } from "alchemy/AdoptPolicy";
import { isResolved } from "alchemy/Diff";
import * as Provider from "alchemy/Provider";
import { Resource } from "alchemy/Resource";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";
import { GcpEnvironment } from "../Environment.ts";
import type { Providers } from "../Providers.ts";
import {
  backupName,
  databaseIdOf,
  databaseNameOf,
  instanceIdOf,
  instanceName,
  listAlchemyInstances,
  MAX_BACKUP_ID_LENGTH,
  parentOwned,
  parseResourceName,
  retryConcurrentChanges,
  toPhysicalId,
  waitForOperation,
} from "./operations.ts";

const DEFAULT_EXPIRE_MS = 7 * 24 * 60 * 60 * 1000;

export type BackupEncryptionConfig = {
  /**
   * Encryption type (`USE_DATABASE_ENCRYPTION`,
   * `GOOGLE_DEFAULT_ENCRYPTION`, `CUSTOMER_MANAGED_ENCRYPTION`).
   */
  encryptionType?:
    | spanner.CreateProjectsInstancesBackupsEncryptionConfig_encryptionTypeEnum
    | (string & {});
  /**
   * Cloud KMS key. Use only when the key location matches the instance
   * config exactly. Prefer `kmsKeyNames` for new backups.
   */
  kmsKeyName?: string;
  /**
   * Cloud KMS keys covering every region of the instance config.
   */
  kmsKeyNames?: string[];
};

export type InstancesBackupProps = {
  /**
   * Parent instance id or full name
   * (`projects/{project}/instances/{instance}`). Must be the instance
   * that owns `database`. Immutable — changing it replaces the backup.
   */
  instance: string;
  /**
   * Source database id or full name
   * (`projects/{project}/instances/{instance}/databases/{database}`).
   * Immutable — changing it replaces the backup.
   */
  database: string;
  /**
   * Backup id (the `{backup}` segment of
   * `projects/{project}/instances/{instance}/backups/{backup}`). If
   * omitted, a unique name is generated. Must match
   * `^[a-z][-a-z0-9]*[a-z0-9]

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/gcp/src/Spanner/InstancesBackup.ts at main · sailfork/t3code-github · Sailfork
	
		
(2–60 characters). Immutable — * changing it replaces the backup. */ backupId?: string; /** * RFC3339 expiration. Must be at least 6 hours and at most 366 days * from create time. Defaults to 7 days from reconcile time on create. */ expireTime?: string; /** * Externally consistent version timestamp. Defaults to create time. * Immutable — changing it replaces the backup. */ versionTime?: string; /** * Backup encryption. Immutable — changing it replaces the backup. */ encryptionConfig?: BackupEncryptionConfig; }; export type InstancesBackup = Resource< "GCP.Spanner.InstancesBackup", InstancesBackupProps, { /** Full resource name `projects/{project}/instances/{instance}/backups/{backup}`. */ name: string; /** Backup id (last path segment). */ backupId: string; /** Parent instance id. */ instanceId: string; /** Project id. */ project: string; /** Source database resource name. */ database: string | undefined; /** RFC3339 expiration. */ expireTime: string | undefined; /** Version timestamp captured in the backup. */ versionTime: string | undefined; /** RFC3339 create time. */ createTime: string | undefined; /** Size in bytes (decimal string). */ sizeBytes: string | undefined; /** Bytes freed by deleting this backup. */ freeableSizeBytes: string | undefined; /** Incremental exclusive size in bytes. */ exclusiveSizeBytes: string | undefined; /** Server-reported state (`CREATING`, `READY`). */ state: string | undefined; /** Databases restored from this backup. */ referencingDatabases: string[]; /** Encryption info. */ encryptionInfo: spanner.EncryptionInfo | undefined; /** SQL dialect of the source database. */ databaseDialect: string | undefined; /** Destination backups copying this backup. */ referencingBackups: string[]; /** Max allowed expiration. */ maxExpireTime: string | undefined; /** Backup schedule names that produced this backup. */ backupSchedules: string[]; /** Incremental backup chain id, if any. */ incrementalBackupChainId: string | undefined; /** Oldest retained version timestamp. */ oldestVersionTime: string | undefined; /** Instance partitions stored in the backup. */ instancePartitions: string[]; /** Minimum edition required to restore. */ minimumRestorableEdition: string | undefined; }, never, Providers >; /** * A Cloud Spanner backup of a database, stored on the parent instance. * * Backups have no labels field. Alchemy treats a backup as owned when * its parent instance carries Alchemy labels, so `list` / * `pnpm nuke:gcp` can find it. Changing `backupId`, `instance`, * `database`, `versionTime`, or encryption replaces the backup. * `expireTime` updates in place. * * ### Creating a Backup * **Example:** 7-day backup of an existing database * ```typescript * const backup = yield* GCP.Spanner.InstancesBackup("Nightly", { * instance: instance.instanceId, * database: database.databaseId, * }); * ``` * * **Example:** Explicit id and expiration * ```typescript * const backup = yield* GCP.Spanner.InstancesBackup("Nightly", { * instance: instance.name, * database: database.name, * backupId: "nightly", * expireTime: "2026-12-31T00:00:00Z", * }); * ``` * * @resource * @category Spanner */ export const InstancesBackup = Resource<InstancesBackup>("GCP.Spanner.InstancesBackup"); export class BackupNotResolved extends Data.TaggedError("GCP.Spanner.BackupNotResolved")<{ resourceName: string; }> {} export class BackupNotReady extends Data.TaggedError("GCP.Spanner.BackupNotReady")<{ resourceName: string; state: string; }> {} export class BackupStillExists extends Data.TaggedError("GCP.Spanner.BackupStillExists")<{ resourceName: string; }> {} const toId = (id: string, backupId: string | undefined, existing?: string) => toPhysicalId(id, backupId, existing, MAX_BACKUP_ID_LENGTH); const defaultExpireTime = () => Effect.sync(() => new Date(Date.now() + DEFAULT_EXPIRE_MS).toISOString()); const encryptionKey = (config: BackupEncryptionConfig | undefined) => JSON.stringify({ encryptionType: (config?.encryptionType ?? "").toUpperCase(), kmsKeyName: config?.kmsKeyName ?? "", kmsKeyNames: [...(config?.kmsKeyNames ?? [])].sort(), }); const toAttrs = (backup: spanner.Backup, project: string): InstancesBackup["Attributes"] => { const name = backup.name ?? ""; const parsed = parseResourceName(name); return { name, backupId: parsed.backupId, instanceId: parsed.instanceId, project: parsed.project || project, database: backup.database, expireTime: backup.expireTime, versionTime: backup.versionTime, createTime: backup.createTime, sizeBytes: backup.sizeBytes, freeableSizeBytes: backup.freeableSizeBytes, exclusiveSizeBytes: backup.exclusiveSizeBytes, state: backup.state, referencingDatabases: backup.referencingDatabases ?? [], encryptionInfo: backup.encryptionInfo, databaseDialect: backup.databaseDialect, referencingBackups: backup.referencingBackups ?? [], maxExpireTime: backup.maxExpireTime, backupSchedules: backup.backupSchedules ?? [], incrementalBackupChainId: backup.incrementalBackupChainId, oldestVersionTime: backup.oldestVersionTime, instancePartitions: (backup.instancePartitions ?? []) .map((item) => item.instancePartition) .filter((item): item is string => typeof item === "string"), minimumRestorableEdition: backup.minimumRestorableEdition, }; }; const getByName = (name: string) => spanner .getProjectsInstancesBackups({ name }) .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); const isBusy = (state: string | undefined) => state === "CREATING" || state === "STATE_UNSPECIFIED" || state === undefined; const waitUntilExists = (name: string) => getByName(name).pipe( Effect.flatMap((backup) => backup ? Effect.succeed(backup) : Effect.fail(new BackupNotResolved({ resourceName: name })), ), Effect.retry({ while: (error) => error._tag === "GCP.Spanner.BackupNotResolved", times: 8, schedule: Schedule.spaced("2 seconds"), }), ); const waitUntilReady = (name: string) => getByName(name).pipe( Effect.filterOrFail( (backup): backup is spanner.Backup => backup !== undefined, () => new BackupNotResolved({ resourceName: name }), ), Effect.filterOrFail( (backup) => !isBusy(backup.state), (backup) => new BackupNotReady({ resourceName: name, state: backup.state ?? "STATE_UNSPECIFIED", }), ), Effect.retry({ while: (error) => error._tag === "GCP.Spanner.BackupNotReady" || error._tag === "GCP.Spanner.BackupNotResolved", times: 8, schedule: Schedule.spaced("5 seconds"), }), ); const refreshOrFail = (name: string) => getByName(name).pipe( Effect.flatMap((backup) => backup ? Effect.succeed(backup) : Effect.fail(new BackupNotResolved({ resourceName: name })), ), ); const waitUntilGone = (name: string) => getByName(name).pipe( Effect.flatMap((backup) => backup === undefined ? Effect.void : Effect.fail(new BackupStillExists({ resourceName: name })), ), Effect.retry({ while: (error) => error._tag === "GCP.Spanner.BackupStillExists", times: 10, schedule: Schedule.spaced("5 seconds"), }), ); const expireEquals = (left: string | undefined, right: string | undefined) => { if (left === right) return true; if (left === undefined || right === undefined) return false; const leftMs = Date.parse(left); const rightMs = Date.parse(right); if (Number.isNaN(leftMs) || Number.isNaN(rightMs)) return left === right; return Math.abs(leftMs - rightMs) < 1000; }; export const InstancesBackupProvider = () => Provider.succeed(InstancesBackup, { stables: ["name", "backupId", "instanceId", "project", "database", "createTime", "versionTime"], diff: Effect.fn(function* ({ news, olds, output }) { if (!isResolved(news)) return undefined; const previousId = olds?.backupId ?? output?.backupId; const nextId = news.backupId ?? previousId; const previousInstance = instanceIdOf(olds?.instance ?? output?.instanceId ?? ""); const nextInstance = instanceIdOf(news.instance); const previousDatabase = databaseIdOf(olds?.database ?? output?.database ?? ""); const nextDatabase = databaseIdOf(news.database); const previousVersion = olds?.versionTime ?? output?.versionTime ?? ""; const nextVersion = news.versionTime ?? previousVersion; const previousEncryption = encryptionKey(olds?.encryptionConfig); const nextEncryption = encryptionKey(news.encryptionConfig ?? olds?.encryptionConfig); const replace = (previousId !== undefined && nextId !== undefined && previousId !== nextId) || (previousInstance.length > 0 && previousInstance !== nextInstance) || (previousDatabase.length > 0 && previousDatabase !== nextDatabase) || (news.versionTime !== undefined && previousVersion.length > 0 && previousVersion !== nextVersion) || (news.encryptionConfig !== undefined && previousEncryption !== nextEncryption); if (!replace) return undefined; return { action: "replace" as const }; }), read: Effect.fn(function* ({ id, olds, output }) { const env = yield* GcpEnvironment.current; const backupId = yield* toId(id, olds?.backupId, output?.backupId); const instanceId = instanceIdOf(olds?.instance ?? output?.instanceId ?? ""); if (instanceId.length === 0) return undefined; const name = output?.name ?? backupName(env.project, instanceId, backupId); const existing = yield* getByName(name); if (existing === undefined) return undefined; const attrs = toAttrs(existing, env.project); return (yield* parentOwned(instanceName(attrs.project, attrs.instanceId))) ? attrs : Unowned(attrs); }), list: () => Effect.gen(function* () { const env = yield* GcpEnvironment.current; const instances = yield* listAlchemyInstances(env.project); const pages = yield* Effect.forEach( instances, (instance) => { const parent = instance.name; if (parent === undefined || parent.length === 0) { return Effect.succeed([] as InstancesBackup["Attributes"][]); } return spanner.listProjectsInstancesBackups .pages({ parent, pageSize: 1000, }) .pipe( Stream.flatMap((page) => Stream.fromIterable(page.backups ?? [])), Stream.map((backup) => toAttrs(backup, env.project)), Stream.runCollect, Effect.map((chunk) => Array.from(chunk)), Effect.catchTag("NotFound", () => Effect.succeed([] as InstancesBackup["Attributes"][]), ), ); }, { concurrency: 4 }, ); return pages.flat(); }), reconcile: Effect.fn(function* ({ id, news, output }) { const env = yield* GcpEnvironment.current; const instanceId = instanceIdOf(news.instance); const backupId = yield* toId(id, news.backupId, output?.backupId); const name = backupName(env.project, instanceId, backupId); const parent = instanceName(env.project, instanceId); const database = databaseNameOf(env.project, news.instance, news.database); const expireTime = news.expireTime ?? (yield* defaultExpireTime()); let current = yield* getByName(output?.name ?? name); if (current === undefined) { const created = yield* spanner .createProjectsInstancesBackups({ parent, backupId, "encryptionConfig.encryptionType": news.encryptionConfig?.encryptionType, "encryptionConfig.kmsKeyName": news.encryptionConfig?.kmsKeyName, "encryptionConfig.kmsKeyNames": news.encryptionConfig?.kmsKeyNames, body: { database, expireTime, versionTime: news.versionTime, }, }) .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); if (created !== undefined && created.done === true) { yield* waitForOperation(created, { alreadyExistsOk: true }); } current = yield* waitUntilExists(name); } if (isBusy(current.state)) { current = yield* waitUntilReady(name).pipe( Effect.catchTag(["GCP.Spanner.BackupNotReady", "GCP.Spanner.BackupNotResolved"], () => refreshOrFail(name), ), ); } if (news.expireTime !== undefined && !expireEquals(current.expireTime, news.expireTime)) { current = yield* retryConcurrentChanges( spanner.patchProjectsInstancesBackups({ name, updateMask: "expire_time", body: { expireTime: news.expireTime, }, }), ); } return toAttrs(current, env.project); }), delete: Effect.fn(function* ({ output }) { yield* spanner.deleteProjectsInstancesBackups({ name: output.name }).pipe( Effect.catchTag("NotFound", () => Effect.void), Effect.retry({ while: (error) => error._tag === "Conflict", times: 8, schedule: Schedule.spaced("5 seconds"), }), ); yield* waitUntilGone(output.name); }), });
.repos/alchemy-effect/packages/gcp/src/Spanner/InstancesBackup.ts at main · sailfork/t3code-github · Sailfork
(2–60 characters). Immutable —\n * changing it replaces the backup.\n */\n backupId?: string;\n /**\n * RFC3339 expiration. Must be at least 6 hours and at most 366 days\n * from create time. Defaults to 7 days from reconcile time on create.\n */\n expireTime?: string;\n /**\n * Externally consistent version timestamp. Defaults to create time.\n * Immutable — changing it replaces the backup.\n */\n versionTime?: string;\n /**\n * Backup encryption. Immutable — changing it replaces the backup.\n */\n encryptionConfig?: BackupEncryptionConfig;\n};\n\nexport type InstancesBackup = Resource\u003c\n \"GCP.Spanner.InstancesBackup\",\n InstancesBackupProps,\n {\n /** Full resource name `projects/{project}/instances/{instance}/backups/{backup}`. */\n name: string;\n /** Backup id (last path segment). */\n backupId: string;\n /** Parent instance id. */\n instanceId: string;\n /** Project id. */\n project: string;\n /** Source database resource name. */\n database: string | undefined;\n /** RFC3339 expiration. */\n expireTime: string | undefined;\n /** Version timestamp captured in the backup. */\n versionTime: string | undefined;\n /** RFC3339 create time. */\n createTime: string | undefined;\n /** Size in bytes (decimal string). */\n sizeBytes: string | undefined;\n /** Bytes freed by deleting this backup. */\n freeableSizeBytes: string | undefined;\n /** Incremental exclusive size in bytes. */\n exclusiveSizeBytes: string | undefined;\n /** Server-reported state (`CREATING`, `READY`). */\n state: string | undefined;\n /** Databases restored from this backup. */\n referencingDatabases: string[];\n /** Encryption info. */\n encryptionInfo: spanner.EncryptionInfo | undefined;\n /** SQL dialect of the source database. */\n databaseDialect: string | undefined;\n /** Destination backups copying this backup. */\n referencingBackups: string[];\n /** Max allowed expiration. */\n maxExpireTime: string | undefined;\n /** Backup schedule names that produced this backup. */\n backupSchedules: string[];\n /** Incremental backup chain id, if any. */\n incrementalBackupChainId: string | undefined;\n /** Oldest retained version timestamp. */\n oldestVersionTime: string | undefined;\n /** Instance partitions stored in the backup. */\n instancePartitions: string[];\n /** Minimum edition required to restore. */\n minimumRestorableEdition: string | undefined;\n },\n never,\n Providers\n>;\n\n/**\n * A Cloud Spanner backup of a database, stored on the parent instance.\n *\n * Backups have no labels field. Alchemy treats a backup as owned when\n * its parent instance carries Alchemy labels, so `list` /\n * `pnpm nuke:gcp` can find it. Changing `backupId`, `instance`,\n * `database`, `versionTime`, or encryption replaces the backup.\n * `expireTime` updates in place.\n *\n * ### Creating a Backup\n * **Example:** 7-day backup of an existing database\n * ```typescript\n * const backup = yield* GCP.Spanner.InstancesBackup(\"Nightly\", {\n * instance: instance.instanceId,\n * database: database.databaseId,\n * });\n * ```\n *\n * **Example:** Explicit id and expiration\n * ```typescript\n * const backup = yield* GCP.Spanner.InstancesBackup(\"Nightly\", {\n * instance: instance.name,\n * database: database.name,\n * backupId: \"nightly\",\n * expireTime: \"2026-12-31T00:00:00Z\",\n * });\n * ```\n *\n * @resource\n * @category Spanner\n */\nexport const InstancesBackup = Resource\u003cInstancesBackup>(\"GCP.Spanner.InstancesBackup\");\n\nexport class BackupNotResolved extends Data.TaggedError(\"GCP.Spanner.BackupNotResolved\")\u003c{\n resourceName: string;\n}> {}\n\nexport class BackupNotReady extends Data.TaggedError(\"GCP.Spanner.BackupNotReady\")\u003c{\n resourceName: string;\n state: string;\n}> {}\n\nexport class BackupStillExists extends Data.TaggedError(\"GCP.Spanner.BackupStillExists\")\u003c{\n resourceName: string;\n}> {}\n\nconst toId = (id: string, backupId: string | undefined, existing?: string) =>\n toPhysicalId(id, backupId, existing, MAX_BACKUP_ID_LENGTH);\n\nconst defaultExpireTime = () =>\n Effect.sync(() => new Date(Date.now() + DEFAULT_EXPIRE_MS).toISOString());\n\nconst encryptionKey = (config: BackupEncryptionConfig | undefined) =>\n JSON.stringify({\n encryptionType: (config?.encryptionType ?? \"\").toUpperCase(),\n kmsKeyName: config?.kmsKeyName ?? \"\",\n kmsKeyNames: [...(config?.kmsKeyNames ?? [])].sort(),\n });\n\nconst toAttrs = (backup: spanner.Backup, project: string): InstancesBackup[\"Attributes\"] => {\n const name = backup.name ?? \"\";\n const parsed = parseResourceName(name);\n return {\n name,\n backupId: parsed.backupId,\n instanceId: parsed.instanceId,\n project: parsed.project || project,\n database: backup.database,\n expireTime: backup.expireTime,\n versionTime: backup.versionTime,\n createTime: backup.createTime,\n sizeBytes: backup.sizeBytes,\n freeableSizeBytes: backup.freeableSizeBytes,\n exclusiveSizeBytes: backup.exclusiveSizeBytes,\n state: backup.state,\n referencingDatabases: backup.referencingDatabases ?? [],\n encryptionInfo: backup.encryptionInfo,\n databaseDialect: backup.databaseDialect,\n referencingBackups: backup.referencingBackups ?? [],\n maxExpireTime: backup.maxExpireTime,\n backupSchedules: backup.backupSchedules ?? [],\n incrementalBackupChainId: backup.incrementalBackupChainId,\n oldestVersionTime: backup.oldestVersionTime,\n instancePartitions: (backup.instancePartitions ?? [])\n .map((item) => item.instancePartition)\n .filter((item): item is string => typeof item === \"string\"),\n minimumRestorableEdition: backup.minimumRestorableEdition,\n };\n};\n\nconst getByName = (name: string) =>\n spanner\n .getProjectsInstancesBackups({ name })\n .pipe(Effect.catchTag(\"NotFound\", () => Effect.undefined));\n\nconst isBusy = (state: string | undefined) =>\n state === \"CREATING\" || state === \"STATE_UNSPECIFIED\" || state === undefined;\n\nconst waitUntilExists = (name: string) =>\n getByName(name).pipe(\n Effect.flatMap((backup) =>\n backup ? Effect.succeed(backup) : Effect.fail(new BackupNotResolved({ resourceName: name })),\n ),\n Effect.retry({\n while: (error) => error._tag === \"GCP.Spanner.BackupNotResolved\",\n times: 8,\n schedule: Schedule.spaced(\"2 seconds\"),\n }),\n );\n\nconst waitUntilReady = (name: string) =>\n getByName(name).pipe(\n Effect.filterOrFail(\n (backup): backup is spanner.Backup => backup !== undefined,\n () => new BackupNotResolved({ resourceName: name }),\n ),\n Effect.filterOrFail(\n (backup) => !isBusy(backup.state),\n (backup) =>\n new BackupNotReady({\n resourceName: name,\n state: backup.state ?? \"STATE_UNSPECIFIED\",\n }),\n ),\n Effect.retry({\n while: (error) =>\n error._tag === \"GCP.Spanner.BackupNotReady\" ||\n error._tag === \"GCP.Spanner.BackupNotResolved\",\n times: 8,\n schedule: Schedule.spaced(\"5 seconds\"),\n }),\n );\n\nconst refreshOrFail = (name: string) =>\n getByName(name).pipe(\n Effect.flatMap((backup) =>\n backup ? Effect.succeed(backup) : Effect.fail(new BackupNotResolved({ resourceName: name })),\n ),\n );\n\nconst waitUntilGone = (name: string) =>\n getByName(name).pipe(\n Effect.flatMap((backup) =>\n backup === undefined\n ? Effect.void\n : Effect.fail(new BackupStillExists({ resourceName: name })),\n ),\n Effect.retry({\n while: (error) => error._tag === \"GCP.Spanner.BackupStillExists\",\n times: 10,\n schedule: Schedule.spaced(\"5 seconds\"),\n }),\n );\n\nconst expireEquals = (left: string | undefined, right: string | undefined) => {\n if (left === right) return true;\n if (left === undefined || right === undefined) return false;\n const leftMs = Date.parse(left);\n const rightMs = Date.parse(right);\n if (Number.isNaN(leftMs) || Number.isNaN(rightMs)) return left === right;\n return Math.abs(leftMs - rightMs) \u003c 1000;\n};\n\nexport const InstancesBackupProvider = () =>\n Provider.succeed(InstancesBackup, {\n stables: [\"name\", \"backupId\", \"instanceId\", \"project\", \"database\", \"createTime\", \"versionTime\"],\n\n diff: Effect.fn(function* ({ news, olds, output }) {\n if (!isResolved(news)) return undefined;\n\n const previousId = olds?.backupId ?? output?.backupId;\n const nextId = news.backupId ?? previousId;\n const previousInstance = instanceIdOf(olds?.instance ?? output?.instanceId ?? \"\");\n const nextInstance = instanceIdOf(news.instance);\n const previousDatabase = databaseIdOf(olds?.database ?? output?.database ?? \"\");\n const nextDatabase = databaseIdOf(news.database);\n const previousVersion = olds?.versionTime ?? output?.versionTime ?? \"\";\n const nextVersion = news.versionTime ?? previousVersion;\n const previousEncryption = encryptionKey(olds?.encryptionConfig);\n const nextEncryption = encryptionKey(news.encryptionConfig ?? olds?.encryptionConfig);\n\n const replace =\n (previousId !== undefined && nextId !== undefined && previousId !== nextId) ||\n (previousInstance.length > 0 && previousInstance !== nextInstance) ||\n (previousDatabase.length > 0 && previousDatabase !== nextDatabase) ||\n (news.versionTime !== undefined &&\n previousVersion.length > 0 &&\n previousVersion !== nextVersion) ||\n (news.encryptionConfig !== undefined && previousEncryption !== nextEncryption);\n\n if (!replace) return undefined;\n return { action: \"replace\" as const };\n }),\n\n read: Effect.fn(function* ({ id, olds, output }) {\n const env = yield* GcpEnvironment.current;\n const backupId = yield* toId(id, olds?.backupId, output?.backupId);\n const instanceId = instanceIdOf(olds?.instance ?? output?.instanceId ?? \"\");\n if (instanceId.length === 0) return undefined;\n const name = output?.name ?? backupName(env.project, instanceId, backupId);\n const existing = yield* getByName(name);\n if (existing === undefined) return undefined;\n const attrs = toAttrs(existing, env.project);\n return (yield* parentOwned(instanceName(attrs.project, attrs.instanceId)))\n ? attrs\n : Unowned(attrs);\n }),\n\n list: () =>\n Effect.gen(function* () {\n const env = yield* GcpEnvironment.current;\n const instances = yield* listAlchemyInstances(env.project);\n const pages = yield* Effect.forEach(\n instances,\n (instance) => {\n const parent = instance.name;\n if (parent === undefined || parent.length === 0) {\n return Effect.succeed([] as InstancesBackup[\"Attributes\"][]);\n }\n return spanner.listProjectsInstancesBackups\n .pages({\n parent,\n pageSize: 1000,\n })\n .pipe(\n Stream.flatMap((page) => Stream.fromIterable(page.backups ?? [])),\n Stream.map((backup) => toAttrs(backup, env.project)),\n Stream.runCollect,\n Effect.map((chunk) => Array.from(chunk)),\n Effect.catchTag(\"NotFound\", () =>\n Effect.succeed([] as InstancesBackup[\"Attributes\"][]),\n ),\n );\n },\n { concurrency: 4 },\n );\n return pages.flat();\n }),\n\n reconcile: Effect.fn(function* ({ id, news, output }) {\n const env = yield* GcpEnvironment.current;\n const instanceId = instanceIdOf(news.instance);\n const backupId = yield* toId(id, news.backupId, output?.backupId);\n const name = backupName(env.project, instanceId, backupId);\n const parent = instanceName(env.project, instanceId);\n const database = databaseNameOf(env.project, news.instance, news.database);\n const expireTime = news.expireTime ?? (yield* defaultExpireTime());\n\n let current = yield* getByName(output?.name ?? name);\n\n if (current === undefined) {\n const created = yield* spanner\n .createProjectsInstancesBackups({\n parent,\n backupId,\n \"encryptionConfig.encryptionType\": news.encryptionConfig?.encryptionType,\n \"encryptionConfig.kmsKeyName\": news.encryptionConfig?.kmsKeyName,\n \"encryptionConfig.kmsKeyNames\": news.encryptionConfig?.kmsKeyNames,\n body: {\n database,\n expireTime,\n versionTime: news.versionTime,\n },\n })\n .pipe(Effect.catchTag(\"Conflict\", () => Effect.undefined));\n if (created !== undefined && created.done === true) {\n yield* waitForOperation(created, { alreadyExistsOk: true });\n }\n current = yield* waitUntilExists(name);\n }\n\n if (isBusy(current.state)) {\n current = yield* waitUntilReady(name).pipe(\n Effect.catchTag([\"GCP.Spanner.BackupNotReady\", \"GCP.Spanner.BackupNotResolved\"], () =>\n refreshOrFail(name),\n ),\n );\n }\n\n if (news.expireTime !== undefined && !expireEquals(current.expireTime, news.expireTime)) {\n current = yield* retryConcurrentChanges(\n spanner.patchProjectsInstancesBackups({\n name,\n updateMask: \"expire_time\",\n body: {\n expireTime: news.expireTime,\n },\n }),\n );\n }\n\n return toAttrs(current, env.project);\n }),\n\n delete: Effect.fn(function* ({ output }) {\n yield* spanner.deleteProjectsInstancesBackups({ name: output.name }).pipe(\n Effect.catchTag(\"NotFound\", () => Effect.void),\n Effect.retry({\n while: (error) => error._tag === \"Conflict\",\n times: 8,\n schedule: Schedule.spaced(\"5 seconds\"),\n }),\n );\n yield* waitUntilGone(output.name);\n }),\n });\n","preview":null,"history":null,"version":"7a7cf2d510b2442ef327a215e0466b4e35850161"},"/_session?shell=1":null}