The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as dm from "@distilled.cloud/gcp/datamigration_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | encodeOwnershipLine, |
| 14 | fieldMask, |
| 15 | fingerprint, |
| 16 | hasOwnershipMarker, |
| 17 | locationParent, |
| 18 | normalizeLocation, |
| 19 | ownedByAlchemy, |
| 20 | parseName, |
| 21 | parseOwnership, |
| 22 | replaceOnIdentity, |
| 23 | ResourceNotResolved, |
| 24 | toPhysicalId, |
| 25 | waitForOperation, |
| 26 | waitUntilExists, |
| 27 | waitUntilGone, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | export type DatabaseEngineInfo = dm.DatabaseEngineInfo; |
| 31 | export type ConversionWorkspaceSourceProvider = |
| 32 | | dm.ConversionWorkspaceSourceProviderEnum |
| 33 | | (string & {}); |
| 34 | export type ConversionWorkspaceDestinationProvider = |
| 35 | | dm.ConversionWorkspaceDestinationProviderEnum |
| 36 | | (string & {}); |
| 37 | |
| 38 | export type ConversionWorkspaceProps = { |
| 39 | /** |
| 40 | * Conversion workspace id (the `{conversionWorkspace}` segment of |
| 41 | * `projects/{project}/locations/{location}/conversionWorkspaces/{conversionWorkspace}`). |
| 42 | * If omitted, a unique RFC1035 name is generated. Immutable — changing |
| 43 | * it replaces the workspace. |
| 44 | */ |
| 45 | conversionWorkspaceId?: string; |
| 46 | /** |
| 47 | * Region (`us-central1`, …). Immutable — changing it replaces the |
| 48 | * workspace. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 49 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 50 | */ |
| 51 | location?: string; |
| 52 | /** |
| 53 | * User-friendly display name. Maximum length is 63 characters including |
| 54 | * Alchemy's ownership marker. Conversion workspaces have no labels |
| 55 | * field, so ownership is stored in a `[alchemy …]` prefix and stripped |
| 56 | * from attributes. |
| 57 | */ |
| 58 | displayName?: string; |
| 59 | /** |
| 60 | * Source engine and version. Immutable — changing it replaces the |
| 61 | * workspace. |
| 62 | */ |
| 63 | source: DatabaseEngineInfo; |
| 64 | /** |
| 65 | * Destination engine and version. Immutable — changing it replaces the |
| 66 | * workspace. |
| 67 | */ |
| 68 | destination: DatabaseEngineInfo; |
| 69 | /** |
| 70 | * Provider for the source database (`CLOUDSQL`, `RDS`, `AURORA`, |
| 71 | * `ALLOYDB`, `AZURE_DATABASE`). Immutable — changing it replaces the |
| 72 | * workspace. |
| 73 | */ |
| 74 | sourceProvider?: ConversionWorkspaceSourceProvider; |
| 75 | /** |
| 76 | * Provider for the destination database. Immutable — changing it |
| 77 | * replaces the workspace. |
| 78 | */ |
| 79 | destinationProvider?: ConversionWorkspaceDestinationProvider; |
| 80 | /** |
| 81 | * Database-pair specific workspace settings |
| 82 | * (`convert_foreign_key_to_interleave`, `skip_triggers`, …). |
| 83 | */ |
| 84 | globalSettings?: Record<string, string>; |
| 85 | }; |
| 86 | |
| 87 | export type ConversionWorkspace = Resource< |
| 88 | "GCP.DataMigration.ConversionWorkspace", |
| 89 | ConversionWorkspaceProps, |
| 90 | { |
| 91 | /** Full resource name. */ |
| 92 | name: string; |
| 93 | /** Conversion workspace id (last path segment). */ |
| 94 | conversionWorkspaceId: string; |
| 95 | /** Project id. */ |
| 96 | project: string; |
| 97 | /** Location id. */ |
| 98 | location: string; |
| 99 | /** User display name with the Alchemy ownership prefix stripped. */ |
| 100 | displayName: string | undefined; |
| 101 | /** Source engine details. */ |
| 102 | source: DatabaseEngineInfo | undefined; |
| 103 | /** Destination engine details. */ |
| 104 | destination: DatabaseEngineInfo | undefined; |
| 105 | /** Source database provider. */ |
| 106 | sourceProvider: string | undefined; |
| 107 | /** Destination database provider. */ |
| 108 | destinationProvider: string | undefined; |
| 109 | /** Workspace-level mapping-engine settings. */ |
| 110 | globalSettings: Record<string, string>; |
| 111 | /** Whether the workspace has uncommitted changes. */ |
| 112 | hasUncommittedChanges: boolean | undefined; |
| 113 | /** Latest commit id. */ |
| 114 | latestCommitId: string | undefined; |
| 115 | /** RFC3339 latest commit timestamp. */ |
| 116 | latestCommitTime: string | undefined; |
| 117 | /** RFC3339 creation timestamp. */ |
| 118 | createTime: string | undefined; |
| 119 | /** RFC3339 last-update timestamp. */ |
| 120 | updateTime: string | undefined; |
| 121 | }, |
| 122 | never, |
| 123 | Providers |
| 124 | >; |
| 125 | |
| 126 | /** |
| 127 | * A Database Migration Service conversion workspace: source and |
| 128 | * destination engines plus optional mapping-engine settings used by |
| 129 | * heterogeneous migrations. |
| 130 | * |
| 131 | * Conversion workspaces have no labels field — Alchemy stamps ownership |
| 132 | * into the display name so `list` / nuke can find them. `source`, |
| 133 | * `destination`, and their providers are replacement triggers. Display |
| 134 | * name and `globalSettings` update in place. |
| 135 | * |
| 136 | * ### Creating a Conversion Workspace |
| 137 | * **Example:** MySQL to PostgreSQL |
| 138 | * ```typescript |
| 139 | * const workspace = yield* GCP.DataMigration.ConversionWorkspace("MysqlToPg", { |
| 140 | * displayName: "mysql-to-pg", |
| 141 | * source: { engine: "MYSQL", version: "8.0" }, |
| 142 | * destination: { engine: "POSTGRESQL", version: "14" }, |
| 143 | * }); |
| 144 | * ``` |
| 145 | * |
| 146 | * **Example:** Oracle to PostgreSQL with settings |
| 147 | * ```typescript |
| 148 | * const workspace = yield* GCP.DataMigration.ConversionWorkspace("OraToPg", { |
| 149 | * source: { engine: "ORACLE", version: "19" }, |
| 150 | * destination: { engine: "POSTGRESQL", version: "15" }, |
| 151 | * globalSettings: { skip_triggers: "true" }, |
| 152 | * }); |
| 153 | * ``` |
| 154 | * |
| 155 | * @resource |
| 156 | * @category DataMigration |
| 157 | */ |
| 158 | export const ConversionWorkspace = Resource<ConversionWorkspace>( |
| 159 | "GCP.DataMigration.ConversionWorkspace", |
| 160 | ); |
| 161 | |
| 162 | const resourceName = (project: string, location: string, conversionWorkspaceId: string) => |
| 163 | `${locationParent(project, location)}/conversionWorkspaces/${conversionWorkspaceId}`; |
| 164 | |
| 165 | const settingsOf = ( |
| 166 | value: Record<string, string | undefined> | null | undefined, |
| 167 | ): Record<string, string> => { |
| 168 | const next: Record<string, string> = {}; |
| 169 | for (const [key, item] of Object.entries(value ?? {})) { |
| 170 | if (item !== undefined) next[key] = item; |
| 171 | } |
| 172 | return next; |
| 173 | }; |
| 174 | |
| 175 | const toAttrs = (workspace: dm.ConversionWorkspace, project: string) => { |
| 176 | const name = workspace.name ?? ""; |
| 177 | const parsed = parseName(name, "conversionWorkspaces"); |
| 178 | const ownership = parseOwnership(workspace.displayName); |
| 179 | return { |
| 180 | name, |
| 181 | conversionWorkspaceId: parsed.id, |
| 182 | project: parsed.project || project, |
| 183 | location: parsed.location, |
| 184 | displayName: ownership.text, |
| 185 | source: workspace.source, |
| 186 | destination: workspace.destination, |
| 187 | sourceProvider: workspace.sourceProvider, |
| 188 | destinationProvider: workspace.destinationProvider, |
| 189 | globalSettings: settingsOf(workspace.globalSettings), |
| 190 | hasUncommittedChanges: workspace.hasUncommittedChanges, |
| 191 | latestCommitId: workspace.latestCommitId, |
| 192 | latestCommitTime: workspace.latestCommitTime, |
| 193 | createTime: workspace.createTime, |
| 194 | updateTime: workspace.updateTime, |
| 195 | }; |
| 196 | }; |
| 197 | |
| 198 | const getByName = (name: string) => |
| 199 | name.length === 0 |
| 200 | ? Effect.undefined |
| 201 | : dm |
| 202 | .getProjectsLocationsConversionWorkspaces({ name }) |
| 203 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 204 | |
| 205 | const listOwned = (project: string, region: string) => |
| 206 | dm.listProjectsLocationsConversionWorkspaces |
| 207 | .pages({ |
| 208 | parent: `projects/${project}/locations/-`, |
| 209 | pageSize: 1000, |
| 210 | }) |
| 211 | .pipe( |
| 212 | Stream.flatMap((page) => Stream.fromIterable(page.conversionWorkspaces ?? [])), |
| 213 | Stream.filter((item) => hasOwnershipMarker(item.displayName)), |
| 214 | Stream.runCollect, |
| 215 | Effect.map((chunk) => Array.from(chunk)), |
| 216 | Effect.catchTag("NotFound", () => |
| 217 | dm.listProjectsLocationsConversionWorkspaces |
| 218 | .pages({ |
| 219 | parent: locationParent(project, region), |
| 220 | pageSize: 1000, |
| 221 | }) |
| 222 | .pipe( |
| 223 | Stream.flatMap((page) => Stream.fromIterable(page.conversionWorkspaces ?? [])), |
| 224 | Stream.filter((item) => hasOwnershipMarker(item.displayName)), |
| 225 | Stream.runCollect, |
| 226 | Effect.map((chunk) => Array.from(chunk)), |
| 227 | Effect.catchTag("NotFound", () => Effect.succeed([] as dm.ConversionWorkspace[])), |
| 228 | ), |
| 229 | ), |
| 230 | ); |
| 231 | |
| 232 | const engineOf = (value: DatabaseEngineInfo | undefined) => |
| 233 | fingerprint({ |
| 234 | engine: value?.engine, |
| 235 | version: value?.version, |
| 236 | }); |
| 237 | |
| 238 | export const ConversionWorkspaceProvider = () => |
| 239 | Provider.succeed(ConversionWorkspace, { |
| 240 | stables: ["name", "conversionWorkspaceId", "project", "location", "createTime"], |
| 241 | |
| 242 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 243 | if (!isResolved(news)) return undefined; |
| 244 | const env = yield* GcpEnvironment.current; |
| 245 | return replaceOnIdentity({ |
| 246 | previousId: olds?.conversionWorkspaceId ?? output?.conversionWorkspaceId, |
| 247 | nextId: |
| 248 | news.conversionWorkspaceId ?? |
| 249 | olds?.conversionWorkspaceId ?? |
| 250 | output?.conversionWorkspaceId, |
| 251 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 252 | nextLocation: normalizeLocation( |
| 253 | news.location ?? olds?.location ?? output?.location, |
| 254 | env.region, |
| 255 | ), |
| 256 | extra: |
| 257 | engineOf(news.source) !== engineOf(olds?.source ?? output?.source) || |
| 258 | engineOf(news.destination) !== engineOf(olds?.destination ?? output?.destination) || |
| 259 | (news.sourceProvider ?? olds?.sourceProvider ?? output?.sourceProvider) !== |
| 260 | (olds?.sourceProvider ?? output?.sourceProvider) || |
| 261 | (news.destinationProvider ?? olds?.destinationProvider ?? output?.destinationProvider) !== |
| 262 | (olds?.destinationProvider ?? output?.destinationProvider), |
| 263 | }); |
| 264 | }), |
| 265 | |
| 266 | read: Effect.fn(function* ({ id, olds, output }) { |
| 267 | const env = yield* GcpEnvironment.current; |
| 268 | const conversionWorkspaceId = yield* toPhysicalId( |
| 269 | id, |
| 270 | olds?.conversionWorkspaceId, |
| 271 | output?.conversionWorkspaceId, |
| 272 | "workspace", |
| 273 | ); |
| 274 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 275 | const name = output?.name ?? resourceName(env.project, location, conversionWorkspaceId); |
| 276 | const existing = yield* getByName(name); |
| 277 | if (existing === undefined) return undefined; |
| 278 | const attrs = toAttrs(existing, env.project); |
| 279 | return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs); |
| 280 | }), |
| 281 | |
| 282 | list: () => |
| 283 | Effect.gen(function* () { |
| 284 | const env = yield* GcpEnvironment.current; |
| 285 | const items = yield* listOwned(env.project, env.region); |
| 286 | return items.map((item) => toAttrs(item, env.project)); |
| 287 | }), |
| 288 | |
| 289 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 290 | const env = yield* GcpEnvironment.current; |
| 291 | const conversionWorkspaceId = yield* toPhysicalId( |
| 292 | id, |
| 293 | news.conversionWorkspaceId, |
| 294 | output?.conversionWorkspaceId, |
| 295 | "workspace", |
| 296 | ); |
| 297 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 298 | const name = resourceName(env.project, location, conversionWorkspaceId); |
| 299 | const ownership = yield* createInternalLabels(id); |
| 300 | const displayName = encodeOwnershipLine(ownership, news.displayName); |
| 301 | const globalSettings = news.globalSettings; |
| 302 | |
| 303 | let current = yield* getByName(output?.name ?? name); |
| 304 | |
| 305 | if (current === undefined) { |
| 306 | const created = yield* dm |
| 307 | .createProjectsLocationsConversionWorkspaces({ |
| 308 | parent: locationParent(env.project, location), |
| 309 | conversionWorkspaceId, |
| 310 | body: { |
| 311 | displayName, |
| 312 | source: news.source, |
| 313 | destination: news.destination, |
| 314 | sourceProvider: news.sourceProvider, |
| 315 | destinationProvider: news.destinationProvider, |
| 316 | globalSettings, |
| 317 | }, |
| 318 | }) |
| 319 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 320 | if (created !== undefined) { |
| 321 | yield* waitForOperation(created); |
| 322 | } |
| 323 | current = yield* waitUntilExists(getByName(name), name); |
| 324 | } |
| 325 | |
| 326 | if (current === undefined) { |
| 327 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 328 | } |
| 329 | |
| 330 | const observedDisplay = current.displayName ?? ""; |
| 331 | const displayNameChanged = observedDisplay !== displayName; |
| 332 | const settingsChanged = |
| 333 | fingerprint(settingsOf(current.globalSettings)) !== fingerprint(globalSettings); |
| 334 | const mask = fieldMask([ |
| 335 | displayNameChanged && "displayName", |
| 336 | settingsChanged && "globalSettings", |
| 337 | ]); |
| 338 | |
| 339 | if (mask.length > 0) { |
| 340 | const operation = yield* dm.patchProjectsLocationsConversionWorkspaces({ |
| 341 | name: current.name ?? name, |
| 342 | updateMask: mask, |
| 343 | body: { |
| 344 | name: current.name ?? name, |
| 345 | displayName, |
| 346 | globalSettings, |
| 347 | }, |
| 348 | }); |
| 349 | yield* waitForOperation(operation); |
| 350 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 351 | } |
| 352 | |
| 353 | return toAttrs(current, env.project); |
| 354 | }), |
| 355 | |
| 356 | delete: Effect.fn(function* ({ output }) { |
| 357 | const operation = yield* dm |
| 358 | .deleteProjectsLocationsConversionWorkspaces({ |
| 359 | name: output.name, |
| 360 | force: true, |
| 361 | }) |
| 362 | .pipe( |
| 363 | Effect.retry({ |
| 364 | while: (error) => error._tag === "Conflict", |
| 365 | times: 8, |
| 366 | schedule: Schedule.spaced("2 seconds"), |
| 367 | }), |
| 368 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 369 | ); |
| 370 | if (operation !== undefined) { |
| 371 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 372 | } |
| 373 | yield* waitUntilGone(getByName(output.name), output.name); |
| 374 | }), |
| 375 | }); |