The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { GcpEnvironment } from "../Environment.ts"; |
| 11 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { |
| 14 | connectionProfileOf, |
| 15 | fieldMask, |
| 16 | fingerprint, |
| 17 | hasAlchemyLabelMap, |
| 18 | lastSegment, |
| 19 | locationParent, |
| 20 | normalizeLocation, |
| 21 | parseName, |
| 22 | replaceOnIdentity, |
| 23 | ResourceNotResolved, |
| 24 | toPhysicalId, |
| 25 | userLabels, |
| 26 | waitForOperation, |
| 27 | waitUntilExists, |
| 28 | waitUntilGone, |
| 29 | } from "./internal.ts"; |
| 30 | |
| 31 | export type MigrationJobType = dm.MigrationJobTypeEnum | (string & {}); |
| 32 | export type MigrationJobState = dm.MigrationJobStateEnum | (string & {}); |
| 33 | export type MigrationJobPhase = dm.MigrationJobPhaseEnum | (string & {}); |
| 34 | export type MigrationJobPurpose = dm.MigrationJobPurposeEnum | (string & {}); |
| 35 | export type MigrationJobDumpType = dm.MigrationJobDumpTypeEnum | (string & {}); |
| 36 | export type DatabaseType = dm.DatabaseType; |
| 37 | export type DumpFlags = dm.DumpFlags; |
| 38 | export type ConversionWorkspaceInfo = dm.ConversionWorkspaceInfo; |
| 39 | export type VpcPeeringConnectivity = dm.VpcPeeringConnectivity; |
| 40 | export type ReverseSshConnectivity = dm.ReverseSshConnectivity; |
| 41 | export type PerformanceConfig = dm.PerformanceConfig; |
| 42 | export type MigrationJobObjectsConfig = dm.MigrationJobObjectsConfig; |
| 43 | export type SqlServerToPostgresConfig = dm.SqlServerToPostgresConfig; |
| 44 | export type OracleToPostgresConfig = dm.OracleToPostgresConfig; |
| 45 | export type PostgresToSqlServerConfig = dm.PostgresToSqlServerConfig; |
| 46 | export type MySqlHomogeneousConfig = dm.MySqlHomogeneousConfig; |
| 47 | export type PostgresHomogeneousConfig = dm.PostgresHomogeneousConfig; |
| 48 | export type SqlServerHomogeneousMigrationJobConfig = dm.SqlServerHomogeneousMigrationJobConfig; |
| 49 | type Status = dm.Status; |
| 50 | |
| 51 | export type MigrationJobProps = { |
| 52 | /** |
| 53 | * Migration job id (the `{migrationJob}` segment of |
| 54 | * `projects/{project}/locations/{location}/migrationJobs/{migrationJob}`). |
| 55 | * If omitted, a unique RFC1035 name is generated. Immutable — changing |
| 56 | * it replaces the job. |
| 57 | */ |
| 58 | migrationJobId?: string; |
| 59 | /** |
| 60 | * Region (`us-central1`, …). Immutable — changing it replaces the job. |
| 61 | * `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 62 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 63 | */ |
| 64 | location?: string; |
| 65 | /** |
| 66 | * User-friendly display name. |
| 67 | */ |
| 68 | displayName?: string; |
| 69 | /** |
| 70 | * User labels. Alchemy ownership labels are merged in automatically. |
| 71 | */ |
| 72 | labels?: Record<string, string>; |
| 73 | /** |
| 74 | * Migration type (`ONE_TIME` or `CONTINUOUS`). Immutable — changing it |
| 75 | * replaces the job. |
| 76 | */ |
| 77 | type: MigrationJobType; |
| 78 | /** |
| 79 | * Source connection profile. Full name or id (combined with |
| 80 | * `location`). Immutable — changing it replaces the job. |
| 81 | */ |
| 82 | source: string; |
| 83 | /** |
| 84 | * Destination connection profile. Full name or id (combined with |
| 85 | * `location`). Immutable — changing it replaces the job. |
| 86 | */ |
| 87 | destination: string; |
| 88 | /** |
| 89 | * Source database engine/provider metadata. |
| 90 | */ |
| 91 | sourceDatabase?: DatabaseType; |
| 92 | /** |
| 93 | * Destination database engine/provider metadata. |
| 94 | */ |
| 95 | destinationDatabase?: DatabaseType; |
| 96 | /** |
| 97 | * Conversion workspace used by heterogeneous migrations. |
| 98 | */ |
| 99 | conversionWorkspace?: ConversionWorkspaceInfo; |
| 100 | /** |
| 101 | * Static IP connectivity (default). Mutually exclusive with VPC peering |
| 102 | * and reverse SSH. |
| 103 | */ |
| 104 | staticIpConnectivity?: Record<string, never>; |
| 105 | /** |
| 106 | * VPC peering connectivity to the source. |
| 107 | */ |
| 108 | vpcPeeringConnectivity?: VpcPeeringConnectivity; |
| 109 | /** |
| 110 | * Reverse SSH tunnel connectivity to the source. |
| 111 | */ |
| 112 | reverseSshConnectivity?: ReverseSshConnectivity; |
| 113 | /** |
| 114 | * Initial dump flags. Mutually exclusive with `dumpPath`. |
| 115 | */ |
| 116 | dumpFlags?: DumpFlags; |
| 117 | /** |
| 118 | * GCS dump path (`gs://bucket/object`). Mutually exclusive with |
| 119 | * `dumpFlags`. |
| 120 | */ |
| 121 | dumpPath?: string; |
| 122 | /** |
| 123 | * Dump type for MySQL to Cloud SQL for MySQL. |
| 124 | */ |
| 125 | dumpType?: MigrationJobDumpType; |
| 126 | /** |
| 127 | * Dump parallelism settings. |
| 128 | */ |
| 129 | performanceConfig?: PerformanceConfig; |
| 130 | /** |
| 131 | * AIP-160 entity filter when a conversion workspace is attached. |
| 132 | */ |
| 133 | filter?: string; |
| 134 | /** |
| 135 | * Objects that need to be migrated. |
| 136 | */ |
| 137 | objectsConfig?: MigrationJobObjectsConfig; |
| 138 | /** |
| 139 | * CMEK used for the job. Immutable — changing it replaces the job. |
| 140 | */ |
| 141 | cmekKeyName?: string; |
| 142 | /** |
| 143 | * Failback pointer to the original migration job. |
| 144 | */ |
| 145 | originalMigrationName?: string; |
| 146 | /** |
| 147 | * Heterogeneous SQL Server to PostgreSQL config. |
| 148 | */ |
| 149 | sqlserverToPostgresConfig?: SqlServerToPostgresConfig; |
| 150 | /** |
| 151 | * Heterogeneous Oracle to PostgreSQL config. |
| 152 | */ |
| 153 | oracleToPostgresConfig?: OracleToPostgresConfig; |
| 154 | /** |
| 155 | * Heterogeneous PostgreSQL to SQL Server config. |
| 156 | */ |
| 157 | postgresToSqlserverConfig?: PostgresToSqlServerConfig; |
| 158 | /** |
| 159 | * Homogeneous MySQL config. |
| 160 | */ |
| 161 | mysqlHomogeneousConfig?: MySqlHomogeneousConfig; |
| 162 | /** |
| 163 | * Homogeneous PostgreSQL config. |
| 164 | */ |
| 165 | postgresHomogeneousConfig?: PostgresHomogeneousConfig; |
| 166 | /** |
| 167 | * Homogeneous SQL Server config. |
| 168 | */ |
| 169 | sqlserverHomogeneousMigrationJobConfig?: SqlServerHomogeneousMigrationJobConfig; |
| 170 | }; |
| 171 | |
| 172 | export type MigrationJob = Resource< |
| 173 | "GCP.DataMigration.MigrationJob", |
| 174 | MigrationJobProps, |
| 175 | { |
| 176 | /** Full resource name. */ |
| 177 | name: string; |
| 178 | /** Migration job id (last path segment). */ |
| 179 | migrationJobId: string; |
| 180 | /** Project id. */ |
| 181 | project: string; |
| 182 | /** Location id. */ |
| 183 | location: string; |
| 184 | /** User-friendly display name. */ |
| 185 | displayName: string | undefined; |
| 186 | /** User labels (Alchemy ownership labels stripped). */ |
| 187 | labels: Record<string, string>; |
| 188 | /** Migration type. */ |
| 189 | type: string | undefined; |
| 190 | /** Source connection profile name. */ |
| 191 | source: string | undefined; |
| 192 | /** Destination connection profile name. */ |
| 193 | destination: string | undefined; |
| 194 | /** Source database metadata. */ |
| 195 | sourceDatabase: DatabaseType | undefined; |
| 196 | /** Destination database metadata. */ |
| 197 | destinationDatabase: DatabaseType | undefined; |
| 198 | /** Conversion workspace info. */ |
| 199 | conversionWorkspace: ConversionWorkspaceInfo | undefined; |
| 200 | /** Static IP connectivity. */ |
| 201 | staticIpConnectivity: Record<string, never> | undefined; |
| 202 | /** VPC peering connectivity. */ |
| 203 | vpcPeeringConnectivity: VpcPeeringConnectivity | undefined; |
| 204 | /** Reverse SSH connectivity. */ |
| 205 | reverseSshConnectivity: ReverseSshConnectivity | undefined; |
| 206 | /** Dump flags. */ |
| 207 | dumpFlags: DumpFlags | undefined; |
| 208 | /** GCS dump path. */ |
| 209 | dumpPath: string | undefined; |
| 210 | /** Dump type. */ |
| 211 | dumpType: string | undefined; |
| 212 | /** Dump parallelism. */ |
| 213 | performanceConfig: PerformanceConfig | undefined; |
| 214 | /** Entity filter. */ |
| 215 | filter: string | undefined; |
| 216 | /** Objects config. */ |
| 217 | objectsConfig: MigrationJobObjectsConfig | undefined; |
| 218 | /** CMEK name. */ |
| 219 | cmekKeyName: string | undefined; |
| 220 | /** Original migration job for failback. */ |
| 221 | originalMigrationName: string | undefined; |
| 222 | /** SQL Server to PostgreSQL config. */ |
| 223 | sqlserverToPostgresConfig: SqlServerToPostgresConfig | undefined; |
| 224 | /** Oracle to PostgreSQL config. */ |
| 225 | oracleToPostgresConfig: OracleToPostgresConfig | undefined; |
| 226 | /** PostgreSQL to SQL Server config. */ |
| 227 | postgresToSqlserverConfig: PostgresToSqlServerConfig | undefined; |
| 228 | /** Homogeneous MySQL config. */ |
| 229 | mysqlHomogeneousConfig: MySqlHomogeneousConfig | undefined; |
| 230 | /** Homogeneous PostgreSQL config. */ |
| 231 | postgresHomogeneousConfig: PostgresHomogeneousConfig | undefined; |
| 232 | /** Homogeneous SQL Server config. */ |
| 233 | sqlserverHomogeneousMigrationJobConfig: SqlServerHomogeneousMigrationJobConfig | undefined; |
| 234 | /** Server-reported state. */ |
| 235 | state: string | undefined; |
| 236 | /** Current phase. */ |
| 237 | phase: string | undefined; |
| 238 | /** Forward or failback purpose. */ |
| 239 | purpose: string | undefined; |
| 240 | /** Duration of the job. */ |
| 241 | duration: string | undefined; |
| 242 | /** Failure details when `state` is `FAILED`. */ |
| 243 | error: Status | undefined; |
| 244 | /** RFC3339 creation timestamp. */ |
| 245 | createTime: string | undefined; |
| 246 | /** RFC3339 last-update timestamp. */ |
| 247 | updateTime: string | undefined; |
| 248 | /** RFC3339 completion timestamp. */ |
| 249 | endTime: string | undefined; |
| 250 | }, |
| 251 | never, |
| 252 | Providers |
| 253 | >; |
| 254 | |
| 255 | /** |
| 256 | * A Database Migration Service migration job: copies data from a source |
| 257 | * connection profile to a destination (Cloud SQL or AlloyDB). |
| 258 | * |
| 259 | * `migrationJobId`, `location`, `type`, `source`, `destination`, and |
| 260 | * `cmekKeyName` are replacement triggers. Display name, labels, dump |
| 261 | * flags, connectivity, and performance config update in place. Delete |
| 262 | * force-deletes the destination Cloud SQL replica. |
| 263 | * |
| 264 | * ### Creating a Migration Job |
| 265 | * **Example:** Continuous MySQL to Cloud SQL |
| 266 | * ```typescript |
| 267 | * const source = yield* GCP.DataMigration.ConnectionProfile("MysqlSrc", { |
| 268 | * mysql: { |
| 269 | * host: "10.0.0.8", |
| 270 | * port: 3306, |
| 271 | * username: "dms", |
| 272 | * password: process.env.MYSQL_PASSWORD, |
| 273 | * }, |
| 274 | * }); |
| 275 | * const dest = yield* GCP.DataMigration.ConnectionProfile("MysqlDest", { |
| 276 | * cloudsql: { |
| 277 | * settings: { |
| 278 | * sourceId: source.name, |
| 279 | * databaseVersion: "MYSQL_8_0", |
| 280 | * tier: "db-n1-standard-1", |
| 281 | * rootPassword: process.env.MYSQL_ROOT_PASSWORD, |
| 282 | * }, |
| 283 | * }, |
| 284 | * }); |
| 285 | * const job = yield* GCP.DataMigration.MigrationJob("Replica", { |
| 286 | * type: "CONTINUOUS", |
| 287 | * source: source.name, |
| 288 | * destination: dest.name, |
| 289 | * staticIpConnectivity: {}, |
| 290 | * }); |
| 291 | * ``` |
| 292 | * |
| 293 | * @resource |
| 294 | * @category DataMigration |
| 295 | */ |
| 296 | export const MigrationJob = Resource<MigrationJob>("GCP.DataMigration.MigrationJob"); |
| 297 | |
| 298 | const resourceName = (project: string, location: string, migrationJobId: string) => |
| 299 | `${locationParent(project, location)}/migrationJobs/${migrationJobId}`; |
| 300 | |
| 301 | const emptyConnectivity = (value: unknown) => (value === undefined ? undefined : {}); |
| 302 | |
| 303 | const publicReverseSsh = (value: ReverseSshConnectivity | undefined) => |
| 304 | value === undefined |
| 305 | ? undefined |
| 306 | : { |
| 307 | vmIp: value.vmIp, |
| 308 | vmPort: value.vmPort, |
| 309 | vpc: value.vpc, |
| 310 | }; |
| 311 | |
| 312 | const toAttrs = (job: dm.MigrationJob, project: string) => { |
| 313 | const name = job.name ?? ""; |
| 314 | const parsed = parseName(name, "migrationJobs"); |
| 315 | return { |
| 316 | name, |
| 317 | migrationJobId: parsed.id, |
| 318 | project: parsed.project || project, |
| 319 | location: parsed.location, |
| 320 | displayName: job.displayName, |
| 321 | labels: userLabels(job.labels), |
| 322 | type: job.type, |
| 323 | source: job.source, |
| 324 | destination: job.destination, |
| 325 | sourceDatabase: job.sourceDatabase, |
| 326 | destinationDatabase: job.destinationDatabase, |
| 327 | conversionWorkspace: job.conversionWorkspace, |
| 328 | staticIpConnectivity: emptyConnectivity(job.staticIpConnectivity), |
| 329 | vpcPeeringConnectivity: job.vpcPeeringConnectivity, |
| 330 | reverseSshConnectivity: publicReverseSsh(job.reverseSshConnectivity), |
| 331 | dumpFlags: job.dumpFlags, |
| 332 | dumpPath: job.dumpPath, |
| 333 | dumpType: job.dumpType, |
| 334 | performanceConfig: job.performanceConfig, |
| 335 | filter: job.filter, |
| 336 | objectsConfig: job.objectsConfig, |
| 337 | cmekKeyName: job.cmekKeyName, |
| 338 | originalMigrationName: job.originalMigrationName, |
| 339 | sqlserverToPostgresConfig: job.sqlserverToPostgresConfig, |
| 340 | oracleToPostgresConfig: job.oracleToPostgresConfig, |
| 341 | postgresToSqlserverConfig: job.postgresToSqlserverConfig, |
| 342 | mysqlHomogeneousConfig: job.mysqlHomogeneousConfig, |
| 343 | postgresHomogeneousConfig: job.postgresHomogeneousConfig, |
| 344 | sqlserverHomogeneousMigrationJobConfig: job.sqlserverHomogeneousMigrationJobConfig, |
| 345 | state: job.state, |
| 346 | phase: job.phase, |
| 347 | purpose: job.purpose, |
| 348 | duration: job.duration, |
| 349 | error: job.error, |
| 350 | createTime: job.createTime, |
| 351 | updateTime: job.updateTime, |
| 352 | endTime: job.endTime, |
| 353 | }; |
| 354 | }; |
| 355 | |
| 356 | const getByName = (name: string) => |
| 357 | name.length === 0 |
| 358 | ? Effect.undefined |
| 359 | : dm |
| 360 | .getProjectsLocationsMigrationJobs({ name }) |
| 361 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 362 | |
| 363 | const listOwned = (project: string, region: string) => |
| 364 | dm.listProjectsLocationsMigrationJobs |
| 365 | .pages({ |
| 366 | parent: `projects/${project}/locations/-`, |
| 367 | pageSize: 1000, |
| 368 | }) |
| 369 | .pipe( |
| 370 | Stream.flatMap((page) => Stream.fromIterable(page.migrationJobs ?? [])), |
| 371 | Stream.filter((item) => hasAlchemyLabelMap(item.labels)), |
| 372 | Stream.runCollect, |
| 373 | Effect.map((chunk) => Array.from(chunk)), |
| 374 | Effect.catchTag("NotFound", () => |
| 375 | dm.listProjectsLocationsMigrationJobs |
| 376 | .pages({ |
| 377 | parent: locationParent(project, region), |
| 378 | pageSize: 1000, |
| 379 | }) |
| 380 | .pipe( |
| 381 | Stream.flatMap((page) => Stream.fromIterable(page.migrationJobs ?? [])), |
| 382 | Stream.filter((item) => hasAlchemyLabelMap(item.labels)), |
| 383 | Stream.runCollect, |
| 384 | Effect.map((chunk) => Array.from(chunk)), |
| 385 | Effect.catchTag("NotFound", () => Effect.succeed([] as dm.MigrationJob[])), |
| 386 | ), |
| 387 | ), |
| 388 | ); |
| 389 | |
| 390 | const connectivityKind = (value: { |
| 391 | staticIpConnectivity?: unknown; |
| 392 | vpcPeeringConnectivity?: unknown; |
| 393 | reverseSshConnectivity?: unknown; |
| 394 | }) => |
| 395 | value.vpcPeeringConnectivity |
| 396 | ? "vpc" |
| 397 | : value.reverseSshConnectivity |
| 398 | ? "ssh" |
| 399 | : value.staticIpConnectivity !== undefined |
| 400 | ? "static" |
| 401 | : ""; |
| 402 | |
| 403 | export const MigrationJobProvider = () => |
| 404 | Provider.succeed(MigrationJob, { |
| 405 | stables: ["name", "migrationJobId", "project", "location", "createTime"], |
| 406 | |
| 407 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 408 | if (!isResolved(news)) return undefined; |
| 409 | const env = yield* GcpEnvironment.current; |
| 410 | const previousType = olds?.type ?? output?.type; |
| 411 | const nextType = news.type ?? previousType; |
| 412 | const previousCmek = olds?.cmekKeyName ?? output?.cmekKeyName; |
| 413 | const nextCmek = news.cmekKeyName ?? previousCmek; |
| 414 | const previousSource = lastSegment(olds?.source ?? output?.source ?? ""); |
| 415 | const nextSource = lastSegment(news.source); |
| 416 | const previousDestination = lastSegment(olds?.destination ?? output?.destination ?? ""); |
| 417 | const nextDestination = lastSegment(news.destination); |
| 418 | return replaceOnIdentity({ |
| 419 | previousId: olds?.migrationJobId ?? output?.migrationJobId, |
| 420 | nextId: news.migrationJobId ?? olds?.migrationJobId ?? output?.migrationJobId, |
| 421 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 422 | nextLocation: normalizeLocation( |
| 423 | news.location ?? olds?.location ?? output?.location, |
| 424 | env.region, |
| 425 | ), |
| 426 | extra: |
| 427 | previousType !== nextType || |
| 428 | previousCmek !== nextCmek || |
| 429 | (previousSource.length > 0 && nextSource.length > 0 && previousSource !== nextSource) || |
| 430 | (previousDestination.length > 0 && |
| 431 | nextDestination.length > 0 && |
| 432 | previousDestination !== nextDestination), |
| 433 | }); |
| 434 | }), |
| 435 | |
| 436 | read: Effect.fn(function* ({ id, olds, output }) { |
| 437 | const env = yield* GcpEnvironment.current; |
| 438 | const migrationJobId = yield* toPhysicalId( |
| 439 | id, |
| 440 | olds?.migrationJobId, |
| 441 | output?.migrationJobId, |
| 442 | "job", |
| 443 | ); |
| 444 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 445 | const name = output?.name ?? resourceName(env.project, location, migrationJobId); |
| 446 | const existing = yield* getByName(name); |
| 447 | if (existing === undefined) return undefined; |
| 448 | const attrs = toAttrs(existing, env.project); |
| 449 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 450 | }), |
| 451 | |
| 452 | list: () => |
| 453 | Effect.gen(function* () { |
| 454 | const env = yield* GcpEnvironment.current; |
| 455 | const items = yield* listOwned(env.project, env.region); |
| 456 | return items.map((item) => toAttrs(item, env.project)); |
| 457 | }), |
| 458 | |
| 459 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 460 | const env = yield* GcpEnvironment.current; |
| 461 | const migrationJobId = yield* toPhysicalId( |
| 462 | id, |
| 463 | news.migrationJobId, |
| 464 | output?.migrationJobId, |
| 465 | "job", |
| 466 | ); |
| 467 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 468 | const name = resourceName(env.project, location, migrationJobId); |
| 469 | const desiredLabels = { |
| 470 | ...toLabels(news.labels), |
| 471 | ...(yield* createInternalLabels(id)), |
| 472 | }; |
| 473 | const displayName = news.displayName ?? migrationJobId; |
| 474 | const source = connectionProfileOf(news.source, env.project, location); |
| 475 | const destination = connectionProfileOf(news.destination, env.project, location); |
| 476 | const staticIpConnectivity = |
| 477 | news.staticIpConnectivity !== undefined || |
| 478 | (news.vpcPeeringConnectivity === undefined && news.reverseSshConnectivity === undefined) |
| 479 | ? {} |
| 480 | : undefined; |
| 481 | const body: dm.MigrationJob = { |
| 482 | displayName, |
| 483 | labels: desiredLabels, |
| 484 | type: news.type, |
| 485 | source, |
| 486 | destination, |
| 487 | sourceDatabase: news.sourceDatabase, |
| 488 | destinationDatabase: news.destinationDatabase, |
| 489 | conversionWorkspace: news.conversionWorkspace, |
| 490 | staticIpConnectivity, |
| 491 | vpcPeeringConnectivity: news.vpcPeeringConnectivity, |
| 492 | reverseSshConnectivity: news.reverseSshConnectivity, |
| 493 | dumpFlags: news.dumpFlags, |
| 494 | dumpPath: news.dumpPath, |
| 495 | dumpType: news.dumpType, |
| 496 | performanceConfig: news.performanceConfig, |
| 497 | filter: news.filter, |
| 498 | objectsConfig: news.objectsConfig, |
| 499 | cmekKeyName: news.cmekKeyName, |
| 500 | originalMigrationName: news.originalMigrationName, |
| 501 | sqlserverToPostgresConfig: news.sqlserverToPostgresConfig, |
| 502 | oracleToPostgresConfig: news.oracleToPostgresConfig, |
| 503 | postgresToSqlserverConfig: news.postgresToSqlserverConfig, |
| 504 | mysqlHomogeneousConfig: news.mysqlHomogeneousConfig, |
| 505 | postgresHomogeneousConfig: news.postgresHomogeneousConfig, |
| 506 | sqlserverHomogeneousMigrationJobConfig: news.sqlserverHomogeneousMigrationJobConfig, |
| 507 | }; |
| 508 | |
| 509 | let current = yield* getByName(output?.name ?? name); |
| 510 | |
| 511 | if (current === undefined) { |
| 512 | const created = yield* dm |
| 513 | .createProjectsLocationsMigrationJobs({ |
| 514 | parent: locationParent(env.project, location), |
| 515 | migrationJobId, |
| 516 | body, |
| 517 | }) |
| 518 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 519 | if (created !== undefined) { |
| 520 | yield* waitForOperation(created); |
| 521 | } |
| 522 | current = yield* waitUntilExists(getByName(name), name); |
| 523 | } |
| 524 | |
| 525 | if (current === undefined) { |
| 526 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 527 | } |
| 528 | |
| 529 | const observedLabels = tagRecord(current.labels); |
| 530 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 531 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 532 | const displayNameChanged = (current.displayName ?? "") !== displayName; |
| 533 | const dumpFlagsChanged = fingerprint(current.dumpFlags) !== fingerprint(news.dumpFlags); |
| 534 | const dumpPathChanged = (current.dumpPath ?? "") !== (news.dumpPath ?? ""); |
| 535 | const dumpTypeChanged = |
| 536 | news.dumpType !== undefined && (current.dumpType ?? "") !== news.dumpType; |
| 537 | const performanceChanged = |
| 538 | fingerprint(current.performanceConfig) !== fingerprint(news.performanceConfig); |
| 539 | const filterChanged = (current.filter ?? "") !== (news.filter ?? ""); |
| 540 | const objectsChanged = fingerprint(current.objectsConfig) !== fingerprint(news.objectsConfig); |
| 541 | const conversionChanged = |
| 542 | fingerprint(current.conversionWorkspace) !== fingerprint(news.conversionWorkspace); |
| 543 | const vpcChanged = |
| 544 | fingerprint(current.vpcPeeringConnectivity) !== fingerprint(news.vpcPeeringConnectivity); |
| 545 | const sshChanged = |
| 546 | fingerprint(publicReverseSsh(current.reverseSshConnectivity)) !== |
| 547 | fingerprint(publicReverseSsh(news.reverseSshConnectivity)); |
| 548 | const nextKind = connectivityKind({ |
| 549 | staticIpConnectivity, |
| 550 | vpcPeeringConnectivity: news.vpcPeeringConnectivity, |
| 551 | reverseSshConnectivity: news.reverseSshConnectivity, |
| 552 | }); |
| 553 | const previousKind = connectivityKind(current); |
| 554 | const staticChanged = nextKind === "static" && previousKind !== "static"; |
| 555 | const homogeneousChanged = |
| 556 | fingerprint(current.mysqlHomogeneousConfig) !== fingerprint(news.mysqlHomogeneousConfig) || |
| 557 | fingerprint(current.postgresHomogeneousConfig) !== |
| 558 | fingerprint(news.postgresHomogeneousConfig) || |
| 559 | fingerprint(current.sqlserverHomogeneousMigrationJobConfig) !== |
| 560 | fingerprint(news.sqlserverHomogeneousMigrationJobConfig) || |
| 561 | fingerprint(current.sqlserverToPostgresConfig) !== |
| 562 | fingerprint(news.sqlserverToPostgresConfig) || |
| 563 | fingerprint(current.oracleToPostgresConfig) !== fingerprint(news.oracleToPostgresConfig) || |
| 564 | fingerprint(current.postgresToSqlserverConfig) !== |
| 565 | fingerprint(news.postgresToSqlserverConfig); |
| 566 | const mask = fieldMask([ |
| 567 | labelsChanged && "labels", |
| 568 | displayNameChanged && "displayName", |
| 569 | dumpFlagsChanged && "dumpFlags", |
| 570 | dumpPathChanged && "dumpPath", |
| 571 | dumpTypeChanged && "dumpType", |
| 572 | performanceChanged && "performanceConfig", |
| 573 | filterChanged && "filter", |
| 574 | objectsChanged && "objectsConfig", |
| 575 | conversionChanged && "conversionWorkspace", |
| 576 | vpcChanged && "vpcPeeringConnectivity", |
| 577 | sshChanged && "reverseSshConnectivity", |
| 578 | staticChanged && "staticIpConnectivity", |
| 579 | homogeneousChanged && "mysqlHomogeneousConfig", |
| 580 | homogeneousChanged && "postgresHomogeneousConfig", |
| 581 | homogeneousChanged && "sqlserverHomogeneousMigrationJobConfig", |
| 582 | homogeneousChanged && "sqlserverToPostgresConfig", |
| 583 | homogeneousChanged && "oracleToPostgresConfig", |
| 584 | homogeneousChanged && "postgresToSqlserverConfig", |
| 585 | ]); |
| 586 | |
| 587 | if (mask.length > 0) { |
| 588 | const operation = yield* dm.patchProjectsLocationsMigrationJobs({ |
| 589 | name: current.name ?? name, |
| 590 | updateMask: mask, |
| 591 | body: { |
| 592 | ...body, |
| 593 | name: current.name ?? name, |
| 594 | }, |
| 595 | }); |
| 596 | yield* waitForOperation(operation); |
| 597 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 598 | } |
| 599 | |
| 600 | return toAttrs(current, env.project); |
| 601 | }), |
| 602 | |
| 603 | delete: Effect.fn(function* ({ output }) { |
| 604 | const operation = yield* dm |
| 605 | .deleteProjectsLocationsMigrationJobs({ |
| 606 | name: output.name, |
| 607 | force: true, |
| 608 | }) |
| 609 | .pipe( |
| 610 | Effect.retry({ |
| 611 | while: (error) => error._tag === "Conflict", |
| 612 | times: 8, |
| 613 | schedule: Schedule.spaced("2 seconds"), |
| 614 | }), |
| 615 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 616 | ); |
| 617 | if (operation !== undefined) { |
| 618 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 619 | } |
| 620 | yield* waitUntilGone(getByName(output.name), output.name); |
| 621 | }), |
| 622 | }); |