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

import {
  diffMigrations,
  migrationAttributesOf,
  migrationsAttrs,
  migrationsInputOf,
  stampedOf,
  type MigrationAttributes,
  type MigrationsInput,
} from "@alchemy.run/sql/Migrations";
import * as spanner from "@distilled.cloud/gcp/spanner_v1";
import { isResolved } from "alchemy/Diff";
import { createPhysicalName } from "alchemy/PhysicalName";
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 { ALCHEMY_LABEL_PREFIX } from "../Labels.ts";
import type { Providers } from "../Providers.ts";
import { runDatabaseMigrations } from "./Migrations.ts";
import { waitForOperation } from "./operations.ts";

const DEFAULT_DIALECT = "GOOGLE_STANDARD_SQL";
const MAX_DATABASE_ID_LENGTH = 30;

export type DatabaseDialect = spanner.DatabaseDatabaseDialectEnum | (string & {});

export type EncryptionConfig = {
  /**
   * Cloud KMS key
   * (`projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`).
   * Immutable — changing it replaces the database.
   */
  kmsKeyName?: string;
  /**
   * KMS keys covering every region of the instance config. Immutable —
   * changing them replaces the database.
   */
  kmsKeyNames?: string[];
};

export type DatabaseProps = {
  /**
   * Parent instance id or full name
   * (`projects/{project}/instances/{instance}`). Immutable — changing it
   * replaces the database.
   */
  instance: string;
  /**
   * Database id (the `{database}` segment of
   * `projects/{project}/instances/{instance}/databases/{database}`). If
   * omitted, a unique name is generated from the stack, stage, and
   * logical id. Must match `^[a-z][-a-z0-9]*[a-z0-9]

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/gcp/src/Spanner/Database.ts at main · sailfork/t3code-github · Sailfork
	
		
(2–30 * characters). Immutable — changing it replaces the database. */ databaseId?: string; /** * SQL dialect. Immutable — changing it replaces the database. * @default "GOOGLE_STANDARD_SQL" */ databaseDialect?: DatabaseDialect; /** * Extra DDL statements run atomically with create. Immutable — * changing them replaces the database, so evolve a schema with * `migrations` instead. */ extraStatements?: string[]; /** * SQL migrations to apply on deploy, once the database is ready. Accepts * a directory path, a `{ dir, table }` object, or a `Drizzle.Schema` * output. Only pending migrations run; editing or removing an applied * file fails the deploy. Works on both dialects. * * Spanner can't commit DDL and DML together. A migration's DDL runs one * statement per schema operation, each named after the migration and * the statement: a deploy after a crash waits for the operations it * started instead of rerunning them, and after a failed statement is * fixed only that statement and the ones after it run. Its trailing DML * commits with the bookkeeping row. DML between DDL statements fails the * deploy; split it into its own migration. Bookkeeping lives in the `alchemy_migrations` table * (Spanner names can't start with `_`). */ migrations?: MigrationsInput; /** * Customer-managed encryption. Immutable — changing it replaces the * database. */ encryptionConfig?: EncryptionConfig; /** * Drop protection. Alchemy defaults to disabled so `destroy` can drop * the database. Delete disables protection first when it is on. * @default false */ enableDropProtection?: boolean; }; export type Database = Resource< "GCP.Spanner.Database", DatabaseProps, { /** Full resource name `projects/{project}/instances/{instance}/databases/{database}`. */ name: string; /** Database id (last path segment). */ databaseId: string; /** Parent instance id. */ instanceId: string; /** Project id. */ project: string; /** SQL dialect. */ databaseDialect: string | undefined; /** Whether drop protection is enabled. */ enableDropProtection: boolean; /** Current database state (`CREATING`, `READY`, `READY_OPTIMIZING`). */ state: string | undefined; /** Version retention period (e.g. `"1h"`). */ versionRetentionPeriod: string | undefined; /** Earliest PITR timestamp, if available. */ earliestVersionTime: string | undefined; /** Default leader region, if set. */ defaultLeader: string | undefined; /** CMEK config, if any. */ encryptionConfig: EncryptionConfig | undefined; /** Whether an update is in flight. */ reconciling: boolean | undefined; /** RFC3339 creation timestamp. */ createTime: string | undefined; /** Directory of the applied SQL migrations. */ migrationsDir: string | undefined; /** Migration bookkeeping table. */ migrationsTable: string | undefined; /** Applied migration content hashes. */ migrationsHashes: Record<string, string>; }, never, Providers >; /** * A Cloud Spanner database inside an instance. * * Spanner databases have no labels field. `list` enumerates databases on * alchemy-labeled instances so `pnpm nuke:gcp` can find leaked rows. * Changing `instance`, `databaseId`, dialect, CMEK, or extra DDL * replaces the database. Drop protection updates in place. * * ### Creating a Database * **Example:** Generated name on a Spanner instance * ```typescript * const instance = yield* GCP.Spanner.Instance("App", {}); * const database = yield* GCP.Spanner.Database("AppDb", { * instance: instance.instanceId, * }); * ``` * * **Example:** Explicit id, dialect, and extra DDL * ```typescript * const database = yield* GCP.Spanner.Database("AppDb", { * instance: instance.instanceId, * databaseId: "appdb", * extraStatements: [ * "CREATE TABLE Users (UserId INT64 NOT NULL) PRIMARY KEY (UserId)", * ], * }); * ``` * * ### Querying * **Example:** Run SQL * ```typescript * const executeSql = yield* GCP.Spanner.ExecuteSql(database); * const result = yield* executeSql({ sql: "SELECT 1 AS n" }); * ``` * * ### Reading Schema * **Example:** Fetch live DDL * ```typescript * const getDdl = yield* GCP.Spanner.GetDdl(database); * const { statements } = yield* getDdl(); * ``` * * ### Migrations * **Example:** Apply migrations on deploy * ```typescript * const database = yield* GCP.Spanner.Database("AppDb", { * instance: instance.instanceId, * migrations: "./migrations", * }); * ``` * * Each file in `./migrations` holds GoogleSQL (or PostgreSQL, with * `databaseDialect: "POSTGRESQL"`) statements separated by `;`. Migrations * run as the deploying identity, which needs `spanner.databases.updateDdl` * and `spanner.databases.write`. * * @resource * @category Spanner */ export const Database = Resource<Database>("GCP.Spanner.Database"); export class DatabaseNotResolved extends Data.TaggedError("GCP.Spanner.DatabaseNotResolved")<{ resourceName: string; }> {} export class DatabaseNotReady extends Data.TaggedError("GCP.Spanner.DatabaseNotReady")<{ resourceName: string; state: string; }> {} export class DatabaseStillExists extends Data.TaggedError("GCP.Spanner.DatabaseStillExists")<{ resourceName: string; }> {} const lastSegment = (value: string) => { const trimmed = value.replace(/\/+$/, ""); const parts = trimmed.split("/"); return parts[parts.length - 1] || trimmed; }; const instanceIdOf = (value: string) => lastSegment(value); const normalizeDialect = (value: string | undefined) => { const next = (value ?? DEFAULT_DIALECT).toUpperCase(); return next.endsWith("_UNSPECIFIED") ? DEFAULT_DIALECT : next; }; const resourceName = (project: string, instanceId: string, databaseId: string) => `projects/${project}/instances/${instanceId}/databases/${databaseId}`; const instanceName = (project: string, instanceId: string) => `projects/${project}/instances/${instanceId}`; const parseName = (name: string) => { const parts = name.split("/").filter((part) => part.length > 0); const databasesAt = parts.lastIndexOf("databases"); const instancesAt = parts.lastIndexOf("instances"); const projectsAt = parts.lastIndexOf("projects"); return { project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "", instanceId: instancesAt >= 0 && parts[instancesAt + 1] ? parts[instancesAt + 1]! : "", databaseId: databasesAt >= 0 && parts[databasesAt + 1] ? parts[databasesAt + 1]! : lastSegment(name), }; }; const toSpannerId = (name: string) => { let next = name .toLowerCase() .replace(/[^a-z0-9-]/g, "-") .replace(/-+/g, "-"); next = next.replace(/^-+/, "").replace(/-+$/g, ""); if (!/^[a-z]/.test(next)) next = `d${next}`; next = next.slice(0, MAX_DATABASE_ID_LENGTH).replace(/-+$/g, ""); if (next.length < 2) next = `${next}xx`.slice(0, MAX_DATABASE_ID_LENGTH); return next; }; const toDatabaseId = (id: string, databaseId: string | undefined, existing?: string) => Effect.gen(function* () { if (databaseId !== undefined) return databaseId; if (existing !== undefined) return existing; return toSpannerId( yield* createPhysicalName({ id, maxLength: MAX_DATABASE_ID_LENGTH, lowercase: true, }), ); }); const createStatementOf = (databaseId: string, dialect: string) => dialect === "POSTGRESQL" ? `CREATE DATABASE "${databaseId}"` : `CREATE DATABASE \`${databaseId}\``; const encryptionOf = ( config: spanner.EncryptionConfig | undefined, ): EncryptionConfig | undefined => { if (config === undefined) return undefined; if ( (config.kmsKeyName === undefined || config.kmsKeyName.length === 0) && (config.kmsKeyNames === undefined || config.kmsKeyNames.length === 0) ) { return undefined; } return { kmsKeyName: config.kmsKeyName, kmsKeyNames: config.kmsKeyNames, }; }; const encryptionKey = (config: EncryptionConfig | undefined) => JSON.stringify({ kmsKeyName: config?.kmsKeyName ?? "", kmsKeyNames: [...(config?.kmsKeyNames ?? [])].sort(), }); const extraKey = (statements: string[] | undefined) => JSON.stringify(statements ?? []); const toAttrs = ( database: spanner.Database, project: string, migrations?: Partial<MigrationAttributes>, ): Database["Attributes"] => { const name = database.name ?? ""; const parsed = parseName(name); return { name, databaseId: parsed.databaseId, instanceId: parsed.instanceId, project: parsed.project || project, databaseDialect: database.databaseDialect, enableDropProtection: database.enableDropProtection === true, state: database.state, versionRetentionPeriod: database.versionRetentionPeriod, earliestVersionTime: database.earliestVersionTime, defaultLeader: database.defaultLeader, encryptionConfig: encryptionOf(database.encryptionConfig), reconciling: database.reconciling, createTime: database.createTime, ...migrationAttributesOf(migrations), }; }; const getByName = (name: string) => spanner .getProjectsInstancesDatabases({ name }) .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); const waitUntilExists = (name: string) => getByName(name).pipe( Effect.flatMap((database) => database ? Effect.succeed(database) : Effect.fail(new DatabaseNotResolved({ resourceName: name })), ), Effect.retry({ while: (error) => error._tag === "GCP.Spanner.DatabaseNotResolved", times: 8, schedule: Schedule.spaced("2 seconds"), }), ); const isReadyState = (state: string | undefined) => state === "READY" || state === "READY_OPTIMIZING"; const waitUntilReady = (name: string) => getByName(name).pipe( Effect.filterOrFail( (database): database is spanner.Database => database !== undefined, () => new DatabaseNotResolved({ resourceName: name }), ), Effect.filterOrFail( (database) => isReadyState(database.state), (database) => new DatabaseNotReady({ resourceName: name, state: database.state ?? "STATE_UNSPECIFIED", }), ), Effect.retry({ while: (error) => error._tag === "GCP.Spanner.DatabaseNotReady" || error._tag === "GCP.Spanner.DatabaseNotResolved", times: 10, schedule: Schedule.spaced("8 seconds"), }), ); const waitUntilGone = (name: string) => getByName(name).pipe( Effect.flatMap((database) => database === undefined ? Effect.void : Effect.fail(new DatabaseStillExists({ resourceName: name })), ), Effect.retry({ while: (error) => error._tag === "GCP.Spanner.DatabaseStillExists", times: 10, schedule: Schedule.spaced("8 seconds"), }), ); const retryConcurrentChanges = <A, E extends { readonly _tag: string }, R>( effect: Effect.Effect<A, E, R>, ) => effect.pipe( Effect.retry({ while: (error) => error._tag === "Conflict", times: 8, schedule: Schedule.spaced("5 seconds"), }), ); const listAlchemyInstances = (project: string) => spanner.listProjectsInstances .pages({ parent: `projects/${project}`, pageSize: 1000, }) .pipe( Stream.flatMap((page) => Stream.fromIterable(page.instances ?? [])), Stream.filter((instance) => Object.keys(instance.labels ?? {}).some((key) => key.startsWith(ALCHEMY_LABEL_PREFIX)), ), Stream.runCollect, Effect.map((chunk) => Array.from(chunk)), Effect.catchTag("NotFound", () => Effect.succeed([] as spanner.Instance[])), ); export const DatabaseProvider = () => Provider.succeed(Database, { stables: ["name", "databaseId", "instanceId", "project", "createTime"], diff: Effect.fn(function* ({ news, olds, output }) { if (!isResolved(news)) return undefined; const previousInstance = olds?.instance ?? output?.instanceId; const nextInstance = news.instance; const previousId = olds?.databaseId ?? output?.databaseId; const nextId = news.databaseId ?? previousId; const previousDialect = normalizeDialect(olds?.databaseDialect ?? output?.databaseDialect); const nextDialect = normalizeDialect(news.databaseDialect ?? output?.databaseDialect); const previousKms = encryptionKey(olds?.encryptionConfig ?? output?.encryptionConfig); const nextKms = encryptionKey(news.encryptionConfig ?? output?.encryptionConfig); const extraChanged = olds?.extraStatements !== undefined && news.extraStatements !== undefined && extraKey(olds.extraStatements) !== extraKey(news.extraStatements); const instanceChanged = previousInstance !== undefined && instanceIdOf(previousInstance) !== instanceIdOf(nextInstance); const idChanged = previousId !== undefined && nextId !== undefined && previousId !== nextId; const replace = instanceChanged || idChanged || previousDialect !== nextDialect || previousKms !== nextKms || extraChanged; if (!replace) { if (yield* diffMigrations({ news, output })) { return { action: "update" as const }; } return undefined; } return { action: "replace" as const, deleteFirst: !instanceChanged && previousId !== undefined && nextId === previousId, }; }), read: Effect.fn(function* ({ id, olds, output }) { const env = yield* GcpEnvironment.current; const instanceId = instanceIdOf(olds?.instance ?? output?.instanceId ?? ""); if (instanceId.length === 0) return undefined; const databaseId = yield* toDatabaseId(id, olds?.databaseId, output?.databaseId); const name = output?.name ?? resourceName(env.project, instanceId, databaseId); const existing = yield* getByName(name); if (existing === undefined) return undefined; return toAttrs(existing, env.project, output); }), 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 Database["Attributes"][]); } return spanner.listProjectsInstancesDatabases .pages({ parent, pageSize: 1000, }) .pipe( Stream.flatMap((page) => Stream.fromIterable(page.databases ?? [])), Stream.map((database) => toAttrs(database, env.project)), Stream.runCollect, Effect.map((chunk) => Array.from(chunk)), Effect.catchTag("NotFound", () => Effect.succeed([] as Database["Attributes"][])), ); }, { concurrency: 4 }, ); return pages.flat(); }), reconcile: Effect.fn(function* ({ id, news, output }) { const env = yield* GcpEnvironment.current; const instanceId = instanceIdOf(news.instance); const databaseId = yield* toDatabaseId(id, news.databaseId, output?.databaseId); const name = resourceName(env.project, instanceId, databaseId); const dialect = normalizeDialect(news.databaseDialect); const desiredProtection = news.enableDropProtection === true; let current = yield* getByName(output?.name ?? name); if (current === undefined) { const created = yield* spanner .createProjectsInstancesDatabases({ parent: instanceName(env.project, instanceId), body: { createStatement: createStatementOf(databaseId, dialect), extraStatements: news.extraStatements, encryptionConfig: news.encryptionConfig, databaseDialect: dialect === DEFAULT_DIALECT ? undefined : dialect, }, }) .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); if (created !== undefined) { yield* waitForOperation(created, { alreadyExistsOk: true }); } current = yield* waitUntilExists(name); } if (current === undefined) { return yield* new DatabaseNotResolved({ resourceName: name }); } if (!isReadyState(current.state)) { current = yield* waitUntilReady(name); } const observedProtection = current.enableDropProtection === true; if (observedProtection !== desiredProtection) { const patched = yield* retryConcurrentChanges( spanner.patchProjectsInstancesDatabases({ name, updateMask: "enableDropProtection", body: { name, enableDropProtection: desiredProtection, }, }), ); yield* waitForOperation(patched); current = yield* waitUntilReady(name); } // Sync migrations — the shared pipeline skips already-applied ones. const migrationsInput = migrationsInputOf(news); const migrations = migrationsInput ? yield* runDatabaseMigrations({ database: current.name ?? name, dialect: normalizeDialect(current.databaseDialect) === "POSTGRESQL" ? "postgres" : "googlesql", input: migrationsInput, stamped: stampedOf(output), }) : undefined; return toAttrs( current, env.project, migrationsAttrs({ input: migrationsInput, run: migrations, output }), ); }), delete: Effect.fn(function* ({ output }) { const current = yield* getByName(output.name); if (current === undefined) return; if (current.enableDropProtection === true) { const patched = yield* retryConcurrentChanges( spanner.patchProjectsInstancesDatabases({ name: output.name, updateMask: "enableDropProtection", body: { name: output.name, enableDropProtection: false, }, }), ); yield* waitForOperation(patched); } yield* retryConcurrentChanges( spanner .dropDatabaseProjectsInstancesDatabases({ database: output.name }) .pipe(Effect.catchTag("NotFound", () => Effect.void)), ); yield* waitUntilGone(output.name); }), });
.repos/alchemy-effect/packages/gcp/src/Spanner/Database.ts at main · sailfork/t3code-github · Sailfork
(2–30\n * characters). Immutable — changing it replaces the database.\n */\n databaseId?: string;\n /**\n * SQL dialect. Immutable — changing it replaces the database.\n * @default \"GOOGLE_STANDARD_SQL\"\n */\n databaseDialect?: DatabaseDialect;\n /**\n * Extra DDL statements run atomically with create. Immutable —\n * changing them replaces the database, so evolve a schema with\n * `migrations` instead.\n */\n extraStatements?: string[];\n /**\n * SQL migrations to apply on deploy, once the database is ready. Accepts\n * a directory path, a `{ dir, table }` object, or a `Drizzle.Schema`\n * output. Only pending migrations run; editing or removing an applied\n * file fails the deploy. Works on both dialects.\n *\n * Spanner can't commit DDL and DML together. A migration's DDL runs one\n * statement per schema operation, each named after the migration and\n * the statement: a deploy after a crash waits for the operations it\n * started instead of rerunning them, and after a failed statement is\n * fixed only that statement and the ones after it run. Its trailing DML\n * commits with the bookkeeping row. DML between DDL statements fails the\n * deploy; split it into its own migration. Bookkeeping lives in the `alchemy_migrations` table\n * (Spanner names can't start with `_`).\n */\n migrations?: MigrationsInput;\n /**\n * Customer-managed encryption. Immutable — changing it replaces the\n * database.\n */\n encryptionConfig?: EncryptionConfig;\n /**\n * Drop protection. Alchemy defaults to disabled so `destroy` can drop\n * the database. Delete disables protection first when it is on.\n * @default false\n */\n enableDropProtection?: boolean;\n};\n\nexport type Database = Resource\u003c\n \"GCP.Spanner.Database\",\n DatabaseProps,\n {\n /** Full resource name `projects/{project}/instances/{instance}/databases/{database}`. */\n name: string;\n /** Database id (last path segment). */\n databaseId: string;\n /** Parent instance id. */\n instanceId: string;\n /** Project id. */\n project: string;\n /** SQL dialect. */\n databaseDialect: string | undefined;\n /** Whether drop protection is enabled. */\n enableDropProtection: boolean;\n /** Current database state (`CREATING`, `READY`, `READY_OPTIMIZING`). */\n state: string | undefined;\n /** Version retention period (e.g. `\"1h\"`). */\n versionRetentionPeriod: string | undefined;\n /** Earliest PITR timestamp, if available. */\n earliestVersionTime: string | undefined;\n /** Default leader region, if set. */\n defaultLeader: string | undefined;\n /** CMEK config, if any. */\n encryptionConfig: EncryptionConfig | undefined;\n /** Whether an update is in flight. */\n reconciling: boolean | undefined;\n /** RFC3339 creation timestamp. */\n createTime: string | undefined;\n /** Directory of the applied SQL migrations. */\n migrationsDir: string | undefined;\n /** Migration bookkeeping table. */\n migrationsTable: string | undefined;\n /** Applied migration content hashes. */\n migrationsHashes: Record\u003cstring, string>;\n },\n never,\n Providers\n>;\n\n/**\n * A Cloud Spanner database inside an instance.\n *\n * Spanner databases have no labels field. `list` enumerates databases on\n * alchemy-labeled instances so `pnpm nuke:gcp` can find leaked rows.\n * Changing `instance`, `databaseId`, dialect, CMEK, or extra DDL\n * replaces the database. Drop protection updates in place.\n *\n * ### Creating a Database\n * **Example:** Generated name on a Spanner instance\n * ```typescript\n * const instance = yield* GCP.Spanner.Instance(\"App\", {});\n * const database = yield* GCP.Spanner.Database(\"AppDb\", {\n * instance: instance.instanceId,\n * });\n * ```\n *\n * **Example:** Explicit id, dialect, and extra DDL\n * ```typescript\n * const database = yield* GCP.Spanner.Database(\"AppDb\", {\n * instance: instance.instanceId,\n * databaseId: \"appdb\",\n * extraStatements: [\n * \"CREATE TABLE Users (UserId INT64 NOT NULL) PRIMARY KEY (UserId)\",\n * ],\n * });\n * ```\n *\n * ### Querying\n * **Example:** Run SQL\n * ```typescript\n * const executeSql = yield* GCP.Spanner.ExecuteSql(database);\n * const result = yield* executeSql({ sql: \"SELECT 1 AS n\" });\n * ```\n *\n * ### Reading Schema\n * **Example:** Fetch live DDL\n * ```typescript\n * const getDdl = yield* GCP.Spanner.GetDdl(database);\n * const { statements } = yield* getDdl();\n * ```\n *\n * ### Migrations\n * **Example:** Apply migrations on deploy\n * ```typescript\n * const database = yield* GCP.Spanner.Database(\"AppDb\", {\n * instance: instance.instanceId,\n * migrations: \"./migrations\",\n * });\n * ```\n *\n * Each file in `./migrations` holds GoogleSQL (or PostgreSQL, with\n * `databaseDialect: \"POSTGRESQL\"`) statements separated by `;`. Migrations\n * run as the deploying identity, which needs `spanner.databases.updateDdl`\n * and `spanner.databases.write`.\n *\n * @resource\n * @category Spanner\n */\nexport const Database = Resource\u003cDatabase>(\"GCP.Spanner.Database\");\n\nexport class DatabaseNotResolved extends Data.TaggedError(\"GCP.Spanner.DatabaseNotResolved\")\u003c{\n resourceName: string;\n}> {}\n\nexport class DatabaseNotReady extends Data.TaggedError(\"GCP.Spanner.DatabaseNotReady\")\u003c{\n resourceName: string;\n state: string;\n}> {}\n\nexport class DatabaseStillExists extends Data.TaggedError(\"GCP.Spanner.DatabaseStillExists\")\u003c{\n resourceName: string;\n}> {}\n\nconst lastSegment = (value: string) => {\n const trimmed = value.replace(/\\/+$/, \"\");\n const parts = trimmed.split(\"/\");\n return parts[parts.length - 1] || trimmed;\n};\n\nconst instanceIdOf = (value: string) => lastSegment(value);\n\nconst normalizeDialect = (value: string | undefined) => {\n const next = (value ?? DEFAULT_DIALECT).toUpperCase();\n return next.endsWith(\"_UNSPECIFIED\") ? DEFAULT_DIALECT : next;\n};\n\nconst resourceName = (project: string, instanceId: string, databaseId: string) =>\n `projects/${project}/instances/${instanceId}/databases/${databaseId}`;\n\nconst instanceName = (project: string, instanceId: string) =>\n `projects/${project}/instances/${instanceId}`;\n\nconst parseName = (name: string) => {\n const parts = name.split(\"/\").filter((part) => part.length > 0);\n const databasesAt = parts.lastIndexOf(\"databases\");\n const instancesAt = parts.lastIndexOf(\"instances\");\n const projectsAt = parts.lastIndexOf(\"projects\");\n return {\n project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : \"\",\n instanceId: instancesAt >= 0 && parts[instancesAt + 1] ? parts[instancesAt + 1]! : \"\",\n databaseId:\n databasesAt >= 0 && parts[databasesAt + 1] ? parts[databasesAt + 1]! : lastSegment(name),\n };\n};\n\nconst toSpannerId = (name: string) => {\n let next = name\n .toLowerCase()\n .replace(/[^a-z0-9-]/g, \"-\")\n .replace(/-+/g, \"-\");\n next = next.replace(/^-+/, \"\").replace(/-+$/g, \"\");\n if (!/^[a-z]/.test(next)) next = `d${next}`;\n next = next.slice(0, MAX_DATABASE_ID_LENGTH).replace(/-+$/g, \"\");\n if (next.length \u003c 2) next = `${next}xx`.slice(0, MAX_DATABASE_ID_LENGTH);\n return next;\n};\n\nconst toDatabaseId = (id: string, databaseId: string | undefined, existing?: string) =>\n Effect.gen(function* () {\n if (databaseId !== undefined) return databaseId;\n if (existing !== undefined) return existing;\n return toSpannerId(\n yield* createPhysicalName({\n id,\n maxLength: MAX_DATABASE_ID_LENGTH,\n lowercase: true,\n }),\n );\n });\n\nconst createStatementOf = (databaseId: string, dialect: string) =>\n dialect === \"POSTGRESQL\"\n ? `CREATE DATABASE \"${databaseId}\"`\n : `CREATE DATABASE \\`${databaseId}\\``;\n\nconst encryptionOf = (\n config: spanner.EncryptionConfig | undefined,\n): EncryptionConfig | undefined => {\n if (config === undefined) return undefined;\n if (\n (config.kmsKeyName === undefined || config.kmsKeyName.length === 0) &&\n (config.kmsKeyNames === undefined || config.kmsKeyNames.length === 0)\n ) {\n return undefined;\n }\n return {\n kmsKeyName: config.kmsKeyName,\n kmsKeyNames: config.kmsKeyNames,\n };\n};\n\nconst encryptionKey = (config: EncryptionConfig | undefined) =>\n JSON.stringify({\n kmsKeyName: config?.kmsKeyName ?? \"\",\n kmsKeyNames: [...(config?.kmsKeyNames ?? [])].sort(),\n });\n\nconst extraKey = (statements: string[] | undefined) => JSON.stringify(statements ?? []);\n\nconst toAttrs = (\n database: spanner.Database,\n project: string,\n migrations?: Partial\u003cMigrationAttributes>,\n): Database[\"Attributes\"] => {\n const name = database.name ?? \"\";\n const parsed = parseName(name);\n return {\n name,\n databaseId: parsed.databaseId,\n instanceId: parsed.instanceId,\n project: parsed.project || project,\n databaseDialect: database.databaseDialect,\n enableDropProtection: database.enableDropProtection === true,\n state: database.state,\n versionRetentionPeriod: database.versionRetentionPeriod,\n earliestVersionTime: database.earliestVersionTime,\n defaultLeader: database.defaultLeader,\n encryptionConfig: encryptionOf(database.encryptionConfig),\n reconciling: database.reconciling,\n createTime: database.createTime,\n ...migrationAttributesOf(migrations),\n };\n};\n\nconst getByName = (name: string) =>\n spanner\n .getProjectsInstancesDatabases({ name })\n .pipe(Effect.catchTag(\"NotFound\", () => Effect.undefined));\n\nconst waitUntilExists = (name: string) =>\n getByName(name).pipe(\n Effect.flatMap((database) =>\n database\n ? Effect.succeed(database)\n : Effect.fail(new DatabaseNotResolved({ resourceName: name })),\n ),\n Effect.retry({\n while: (error) => error._tag === \"GCP.Spanner.DatabaseNotResolved\",\n times: 8,\n schedule: Schedule.spaced(\"2 seconds\"),\n }),\n );\n\nconst isReadyState = (state: string | undefined) =>\n state === \"READY\" || state === \"READY_OPTIMIZING\";\n\nconst waitUntilReady = (name: string) =>\n getByName(name).pipe(\n Effect.filterOrFail(\n (database): database is spanner.Database => database !== undefined,\n () => new DatabaseNotResolved({ resourceName: name }),\n ),\n Effect.filterOrFail(\n (database) => isReadyState(database.state),\n (database) =>\n new DatabaseNotReady({\n resourceName: name,\n state: database.state ?? \"STATE_UNSPECIFIED\",\n }),\n ),\n Effect.retry({\n while: (error) =>\n error._tag === \"GCP.Spanner.DatabaseNotReady\" ||\n error._tag === \"GCP.Spanner.DatabaseNotResolved\",\n times: 10,\n schedule: Schedule.spaced(\"8 seconds\"),\n }),\n );\n\nconst waitUntilGone = (name: string) =>\n getByName(name).pipe(\n Effect.flatMap((database) =>\n database === undefined\n ? Effect.void\n : Effect.fail(new DatabaseStillExists({ resourceName: name })),\n ),\n Effect.retry({\n while: (error) => error._tag === \"GCP.Spanner.DatabaseStillExists\",\n times: 10,\n schedule: Schedule.spaced(\"8 seconds\"),\n }),\n );\n\nconst retryConcurrentChanges = \u003cA, E extends { readonly _tag: string }, R>(\n effect: Effect.Effect\u003cA, E, R>,\n) =>\n effect.pipe(\n Effect.retry({\n while: (error) => error._tag === \"Conflict\",\n times: 8,\n schedule: Schedule.spaced(\"5 seconds\"),\n }),\n );\n\nconst listAlchemyInstances = (project: string) =>\n spanner.listProjectsInstances\n .pages({\n parent: `projects/${project}`,\n pageSize: 1000,\n })\n .pipe(\n Stream.flatMap((page) => Stream.fromIterable(page.instances ?? [])),\n Stream.filter((instance) =>\n Object.keys(instance.labels ?? {}).some((key) => key.startsWith(ALCHEMY_LABEL_PREFIX)),\n ),\n Stream.runCollect,\n Effect.map((chunk) => Array.from(chunk)),\n Effect.catchTag(\"NotFound\", () => Effect.succeed([] as spanner.Instance[])),\n );\n\nexport const DatabaseProvider = () =>\n Provider.succeed(Database, {\n stables: [\"name\", \"databaseId\", \"instanceId\", \"project\", \"createTime\"],\n\n diff: Effect.fn(function* ({ news, olds, output }) {\n if (!isResolved(news)) return undefined;\n\n const previousInstance = olds?.instance ?? output?.instanceId;\n const nextInstance = news.instance;\n const previousId = olds?.databaseId ?? output?.databaseId;\n const nextId = news.databaseId ?? previousId;\n const previousDialect = normalizeDialect(olds?.databaseDialect ?? output?.databaseDialect);\n const nextDialect = normalizeDialect(news.databaseDialect ?? output?.databaseDialect);\n const previousKms = encryptionKey(olds?.encryptionConfig ?? output?.encryptionConfig);\n const nextKms = encryptionKey(news.encryptionConfig ?? output?.encryptionConfig);\n const extraChanged =\n olds?.extraStatements !== undefined &&\n news.extraStatements !== undefined &&\n extraKey(olds.extraStatements) !== extraKey(news.extraStatements);\n\n const instanceChanged =\n previousInstance !== undefined &&\n instanceIdOf(previousInstance) !== instanceIdOf(nextInstance);\n const idChanged = previousId !== undefined && nextId !== undefined && previousId !== nextId;\n\n const replace =\n instanceChanged ||\n idChanged ||\n previousDialect !== nextDialect ||\n previousKms !== nextKms ||\n extraChanged;\n\n if (!replace) {\n if (yield* diffMigrations({ news, output })) {\n return { action: \"update\" as const };\n }\n return undefined;\n }\n return {\n action: \"replace\" as const,\n deleteFirst: !instanceChanged && previousId !== undefined && nextId === previousId,\n };\n }),\n\n read: Effect.fn(function* ({ id, olds, output }) {\n const env = yield* GcpEnvironment.current;\n const instanceId = instanceIdOf(olds?.instance ?? output?.instanceId ?? \"\");\n if (instanceId.length === 0) return undefined;\n const databaseId = yield* toDatabaseId(id, olds?.databaseId, output?.databaseId);\n const name = output?.name ?? resourceName(env.project, instanceId, databaseId);\n const existing = yield* getByName(name);\n if (existing === undefined) return undefined;\n return toAttrs(existing, env.project, output);\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 Database[\"Attributes\"][]);\n }\n return spanner.listProjectsInstancesDatabases\n .pages({\n parent,\n pageSize: 1000,\n })\n .pipe(\n Stream.flatMap((page) => Stream.fromIterable(page.databases ?? [])),\n Stream.map((database) => toAttrs(database, env.project)),\n Stream.runCollect,\n Effect.map((chunk) => Array.from(chunk)),\n Effect.catchTag(\"NotFound\", () => Effect.succeed([] as Database[\"Attributes\"][])),\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 databaseId = yield* toDatabaseId(id, news.databaseId, output?.databaseId);\n const name = resourceName(env.project, instanceId, databaseId);\n const dialect = normalizeDialect(news.databaseDialect);\n const desiredProtection = news.enableDropProtection === true;\n\n let current = yield* getByName(output?.name ?? name);\n\n if (current === undefined) {\n const created = yield* spanner\n .createProjectsInstancesDatabases({\n parent: instanceName(env.project, instanceId),\n body: {\n createStatement: createStatementOf(databaseId, dialect),\n extraStatements: news.extraStatements,\n encryptionConfig: news.encryptionConfig,\n databaseDialect: dialect === DEFAULT_DIALECT ? undefined : dialect,\n },\n })\n .pipe(Effect.catchTag(\"Conflict\", () => Effect.undefined));\n if (created !== undefined) {\n yield* waitForOperation(created, { alreadyExistsOk: true });\n }\n current = yield* waitUntilExists(name);\n }\n\n if (current === undefined) {\n return yield* new DatabaseNotResolved({ resourceName: name });\n }\n\n if (!isReadyState(current.state)) {\n current = yield* waitUntilReady(name);\n }\n\n const observedProtection = current.enableDropProtection === true;\n if (observedProtection !== desiredProtection) {\n const patched = yield* retryConcurrentChanges(\n spanner.patchProjectsInstancesDatabases({\n name,\n updateMask: \"enableDropProtection\",\n body: {\n name,\n enableDropProtection: desiredProtection,\n },\n }),\n );\n yield* waitForOperation(patched);\n current = yield* waitUntilReady(name);\n }\n\n // Sync migrations — the shared pipeline skips already-applied ones.\n const migrationsInput = migrationsInputOf(news);\n const migrations = migrationsInput\n ? yield* runDatabaseMigrations({\n database: current.name ?? name,\n dialect:\n normalizeDialect(current.databaseDialect) === \"POSTGRESQL\" ? \"postgres\" : \"googlesql\",\n input: migrationsInput,\n stamped: stampedOf(output),\n })\n : undefined;\n\n return toAttrs(\n current,\n env.project,\n migrationsAttrs({ input: migrationsInput, run: migrations, output }),\n );\n }),\n\n delete: Effect.fn(function* ({ output }) {\n const current = yield* getByName(output.name);\n if (current === undefined) return;\n\n if (current.enableDropProtection === true) {\n const patched = yield* retryConcurrentChanges(\n spanner.patchProjectsInstancesDatabases({\n name: output.name,\n updateMask: \"enableDropProtection\",\n body: {\n name: output.name,\n enableDropProtection: false,\n },\n }),\n );\n yield* waitForOperation(patched);\n }\n\n yield* retryConcurrentChanges(\n spanner\n .dropDatabaseProjectsInstancesDatabases({ database: output.name })\n .pipe(Effect.catchTag(\"NotFound\", () => Effect.void)),\n );\n yield* waitUntilGone(output.name);\n }),\n });\n","preview":null,"history":null,"version":"9430441101cdfe5d1564ddfa56e70c11949ad944"},"/_session?shell=1":null}