@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 460 lines 17.5 KB
Raw
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 conversionWorkspaceOf,
14 encodeOwnershipLine,
15 fingerprint,
16 hasOwnershipMarker,
17 listConversionWorkspaces,
18 normalizeLocation,
19 ownedByAlchemy,
20 parseName,
21 parseOwnership,
22 replaceOnIdentity,
23 ResourceNotResolved,
24 toPhysicalId,
25 waitUntilExists,
26 waitUntilGone,
27 } from "./internal.ts";
28
29 export type MappingRuleRuleScope = dm.MappingRuleRuleScopeEnum | (string & {});
30 export type MappingRuleState = dm.MappingRuleStateEnum | (string & {});
31 export type MappingRuleFilter = dm.MappingRuleFilter;
32 export type FilterTableColumns = dm.FilterTableColumns;
33 export type SinglePackageChange = dm.SinglePackageChange;
34 export type SingleEntityRename = dm.SingleEntityRename;
35 export type SetTablePrimaryKey = dm.SetTablePrimaryKey;
36 export type MultiEntityRename = dm.MultiEntityRename;
37 export type EntityMove = dm.EntityMove;
38 export type SourceSqlChange = dm.SourceSqlChange;
39 export type MultiColumnDatatypeChange = dm.MultiColumnDatatypeChange;
40 export type ConvertRowIdToColumn = dm.ConvertRowIdToColumn;
41 export type SingleColumnChange = dm.SingleColumnChange;
42 export type ConditionalColumnSetValue = dm.ConditionalColumnSetValue;
43
44 export type ConversionWorkspacesMappingRuleProps = {
45 /**
46 * Parent conversion workspace. Full name
47 * `projects/{project}/locations/{location}/conversionWorkspaces/{conversionWorkspace}`
48 * or the workspace id (combined with `location`). Immutable — changing
49 * it replaces the rule.
50 */
51 conversionWorkspace: string;
52 /**
53 * Region used when `conversionWorkspace` is a bare id.
54 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
55 */
56 location?: string;
57 /**
58 * Mapping rule id (the `{mappingRule}` segment). If omitted, a unique
59 * RFC1035 name is generated. Immutable — changing it replaces the rule.
60 */
61 mappingRuleId?: string;
62 /**
63 * Rule scope (`DATABASE_ENTITY_TYPE_SCHEMA`,
64 * `DATABASE_ENTITY_TYPE_TABLE`, …). The API has no patch method —
65 * changing the rule body replaces the rule.
66 */
67 ruleScope: MappingRuleRuleScope;
68 /**
69 * Entities the rule applies to.
70 */
71 filter?: MappingRuleFilter;
72 /**
73 * Application order. Lower values run first.
74 * @default "1"
75 */
76 ruleOrder?: string;
77 /**
78 * User-friendly display name. Mapping rules have no labels field, so
79 * ownership is stored in a `[alchemy …]` prefix and stripped from
80 * attributes. The API has no patch method — changing display name
81 * replaces the rule.
82 */
83 displayName?: string;
84 /**
85 * Mapping rule state (`ENABLED` or `DISABLED`).
86 * @default "ENABLED"
87 */
88 state?: MappingRuleState;
89 /** Rename a single entity. */
90 singleEntityRename?: SingleEntityRename;
91 /** Rename multiple entities. */
92 multiEntityRename?: MultiEntityRename;
93 /** Move entities into another schema. */
94 entityMove?: EntityMove;
95 /** Change SQL for a function or procedure. */
96 sourceSqlChange?: SourceSqlChange;
97 /** Change a single package. */
98 singlePackageChange?: SinglePackageChange;
99 /** Set a table primary key. */
100 setTablePrimaryKey?: SetTablePrimaryKey;
101 /** Filter table columns. */
102 filterTableColumns?: FilterTableColumns;
103 /** Change multiple column data types. */
104 multiColumnDataTypeChange?: MultiColumnDatatypeChange;
105 /** Convert ROWID to a column. */
106 convertRowidColumn?: ConvertRowIdToColumn;
107 /** Change a single column. */
108 singleColumnChange?: SingleColumnChange;
109 /** Conditionally transform column values. */
110 conditionalColumnSetValue?: ConditionalColumnSetValue;
111 };
112
113 export type ConversionWorkspacesMappingRule = Resource<
114 "GCP.DataMigration.ConversionWorkspacesMappingRule",
115 ConversionWorkspacesMappingRuleProps,
116 {
117 /** Full resource name. */
118 name: string;
119 /** Mapping rule id (last path segment). */
120 mappingRuleId: string;
121 /** Parent conversion workspace resource name. */
122 conversionWorkspace: string;
123 /** Project id. */
124 project: string;
125 /** Location id. */
126 location: string;
127 /** Rule scope. */
128 ruleScope: string | undefined;
129 /** Entity filter. */
130 filter: MappingRuleFilter | undefined;
131 /** Application order. */
132 ruleOrder: string | undefined;
133 /** User display name with the Alchemy ownership prefix stripped. */
134 displayName: string | undefined;
135 /** Mapping rule state. */
136 state: string | undefined;
137 /** Single-entity rename. */
138 singleEntityRename: SingleEntityRename | undefined;
139 /** Multi-entity rename. */
140 multiEntityRename: MultiEntityRename | undefined;
141 /** Entity move. */
142 entityMove: EntityMove | undefined;
143 /** Source SQL change. */
144 sourceSqlChange: SourceSqlChange | undefined;
145 /** Package change. */
146 singlePackageChange: SinglePackageChange | undefined;
147 /** Primary key change. */
148 setTablePrimaryKey: SetTablePrimaryKey | undefined;
149 /** Table column filter. */
150 filterTableColumns: FilterTableColumns | undefined;
151 /** Multi-column data type change. */
152 multiColumnDataTypeChange: MultiColumnDatatypeChange | undefined;
153 /** ROWID-to-column conversion. */
154 convertRowidColumn: ConvertRowIdToColumn | undefined;
155 /** Single-column change. */
156 singleColumnChange: SingleColumnChange | undefined;
157 /** Conditional column value transform. */
158 conditionalColumnSetValue: ConditionalColumnSetValue | undefined;
159 /** Revision id. */
160 revisionId: string | undefined;
161 /** RFC3339 revision timestamp. */
162 revisionCreateTime: string | undefined;
163 },
164 never,
165 Providers
166 >;
167
168 /**
169 * A mapping rule attached to a Database Migration Service conversion
170 * workspace. Rules transform source schema entities into the destination
171 * schema (rename, move, type change, …).
172 *
173 * Mapping rules have no labels field and no patch method — Alchemy stamps
174 * ownership into the display name, and any change to the rule body
175 * replaces the rule.
176 *
177 * ### Creating a Mapping Rule
178 * **Example:** Rename a schema
179 * ```typescript
180 * const workspace = yield* GCP.DataMigration.ConversionWorkspace("MysqlToPg", {
181 * source: { engine: "MYSQL", version: "8.0" },
182 * destination: { engine: "POSTGRESQL", version: "14" },
183 * });
184 * const rule = yield* GCP.DataMigration.ConversionWorkspacesMappingRule("Rename", {
185 * conversionWorkspace: workspace.name,
186 * ruleScope: "DATABASE_ENTITY_TYPE_SCHEMA",
187 * filter: { entities: ["src_schema"] },
188 * singleEntityRename: { newName: "dst_schema" },
189 * });
190 * ```
191 *
192 * @resource
193 * @category DataMigration
194 */
195 export const ConversionWorkspacesMappingRule = Resource<ConversionWorkspacesMappingRule>(
196 "GCP.DataMigration.ConversionWorkspacesMappingRule",
197 );
198
199 const DEFAULT_ORDER = "1";
200
201 const resourceName = (conversionWorkspace: string, mappingRuleId: string) =>
202 `${conversionWorkspace}/mappingRules/${mappingRuleId}`;
203
204 // The API omits `name` from mapping rules (create and get alike), so callers
205 // that know the rule's name pass it in.
206 const toAttrs = (item: dm.MappingRule, project: string, knownName?: string) => {
207 const name = item.name || knownName || "";
208 const parsed = parseName(name, "mappingRules");
209 const ownership = parseOwnership(item.displayName);
210 return {
211 name,
212 mappingRuleId: parsed.id,
213 conversionWorkspace: parsed.parent,
214 project: parsed.project || project,
215 location: parsed.location,
216 ruleScope: item.ruleScope,
217 filter: item.filter,
218 ruleOrder: item.ruleOrder,
219 displayName: ownership.text,
220 state: item.state,
221 singleEntityRename: item.singleEntityRename,
222 multiEntityRename: item.multiEntityRename,
223 entityMove: item.entityMove,
224 sourceSqlChange: item.sourceSqlChange,
225 singlePackageChange: item.singlePackageChange,
226 setTablePrimaryKey: item.setTablePrimaryKey,
227 filterTableColumns: item.filterTableColumns,
228 multiColumnDataTypeChange: item.multiColumnDataTypeChange,
229 convertRowidColumn: item.convertRowidColumn,
230 singleColumnChange: item.singleColumnChange,
231 conditionalColumnSetValue: item.conditionalColumnSetValue,
232 revisionId: item.revisionId,
233 revisionCreateTime: item.revisionCreateTime,
234 };
235 };
236
237 const ruleBody = (item: {
238 ruleScope?: string;
239 filter?: MappingRuleFilter;
240 ruleOrder?: string;
241 displayName?: string;
242 state?: string;
243 singleEntityRename?: SingleEntityRename;
244 multiEntityRename?: MultiEntityRename;
245 entityMove?: EntityMove;
246 sourceSqlChange?: SourceSqlChange;
247 singlePackageChange?: SinglePackageChange;
248 setTablePrimaryKey?: SetTablePrimaryKey;
249 filterTableColumns?: FilterTableColumns;
250 multiColumnDataTypeChange?: MultiColumnDatatypeChange;
251 convertRowidColumn?: ConvertRowIdToColumn;
252 singleColumnChange?: SingleColumnChange;
253 conditionalColumnSetValue?: ConditionalColumnSetValue;
254 }) =>
255 fingerprint({
256 ruleScope: item.ruleScope,
257 filter: item.filter ?? {},
258 ruleOrder: item.ruleOrder ?? DEFAULT_ORDER,
259 displayName: item.displayName,
260 state: item.state,
261 singleEntityRename: item.singleEntityRename,
262 multiEntityRename: item.multiEntityRename,
263 entityMove: item.entityMove,
264 sourceSqlChange: item.sourceSqlChange,
265 singlePackageChange: item.singlePackageChange,
266 setTablePrimaryKey: item.setTablePrimaryKey,
267 filterTableColumns: item.filterTableColumns,
268 multiColumnDataTypeChange: item.multiColumnDataTypeChange,
269 convertRowidColumn: item.convertRowidColumn,
270 singleColumnChange: item.singleColumnChange,
271 conditionalColumnSetValue: item.conditionalColumnSetValue,
272 });
273
274 // A name with an empty segment means the parent workspace never resolved
275 // (e.g. its create failed), so there is no rule to observe or delete.
276 const isWellFormed = (name: string) => name.length > 0 && !name.includes("//");
277
278 const getByName = (name: string) =>
279 !isWellFormed(name)
280 ? Effect.undefined
281 : dm.getProjectsLocationsConversionWorkspacesMappingRules({ name }).pipe(
282 // Deleted rules stay readable as a `DELETED` revision.
283 Effect.map((rule) => (rule.state === "DELETED" ? undefined : rule)),
284 Effect.catchTag("NotFound", () => Effect.undefined),
285 );
286
287 const listRules = (parent: string) =>
288 dm.listProjectsLocationsConversionWorkspacesMappingRules.pages({ parent, pageSize: 1000 }).pipe(
289 Stream.flatMap((page) => Stream.fromIterable(page.mappingRules ?? [])),
290 Stream.runCollect,
291 Effect.map((chunk) => Array.from(chunk)),
292 Effect.catchTag("NotFound", () => Effect.succeed([] as dm.MappingRule[])),
293 );
294
295 const listOwned = (project: string, region: string) =>
296 Effect.gen(function* () {
297 const workspaces = yield* listConversionWorkspaces(project, region);
298 const groups = yield* Effect.forEach(
299 workspaces.filter((item) => (item.name ?? "").length > 0),
300 (workspace) => listRules(workspace.name!),
301 { concurrency: 4 },
302 );
303 return groups.flat().filter((item) => hasOwnershipMarker(item.displayName));
304 });
305
306 export const ConversionWorkspacesMappingRuleProvider = () =>
307 Provider.succeed(ConversionWorkspacesMappingRule, {
308 stables: ["name", "mappingRuleId", "conversionWorkspace", "project", "location"],
309
310 diff: Effect.fn(function* ({ news, olds, output }) {
311 if (!isResolved(news)) return undefined;
312 const env = yield* GcpEnvironment.current;
313 const previous = {
314 ruleScope: olds?.ruleScope ?? output?.ruleScope,
315 filter: olds?.filter ?? output?.filter,
316 ruleOrder: olds?.ruleOrder ?? output?.ruleOrder,
317 displayName: olds?.displayName ?? output?.displayName,
318 state: olds?.state ?? output?.state,
319 singleEntityRename: olds?.singleEntityRename ?? output?.singleEntityRename,
320 multiEntityRename: olds?.multiEntityRename ?? output?.multiEntityRename,
321 entityMove: olds?.entityMove ?? output?.entityMove,
322 sourceSqlChange: olds?.sourceSqlChange ?? output?.sourceSqlChange,
323 singlePackageChange: olds?.singlePackageChange ?? output?.singlePackageChange,
324 setTablePrimaryKey: olds?.setTablePrimaryKey ?? output?.setTablePrimaryKey,
325 filterTableColumns: olds?.filterTableColumns ?? output?.filterTableColumns,
326 multiColumnDataTypeChange:
327 olds?.multiColumnDataTypeChange ?? output?.multiColumnDataTypeChange,
328 convertRowidColumn: olds?.convertRowidColumn ?? output?.convertRowidColumn,
329 singleColumnChange: olds?.singleColumnChange ?? output?.singleColumnChange,
330 conditionalColumnSetValue:
331 olds?.conditionalColumnSetValue ?? output?.conditionalColumnSetValue,
332 };
333 return replaceOnIdentity({
334 previousId: olds?.mappingRuleId ?? output?.mappingRuleId,
335 nextId: news.mappingRuleId ?? olds?.mappingRuleId ?? output?.mappingRuleId,
336 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
337 nextLocation: normalizeLocation(
338 news.location ?? olds?.location ?? output?.location,
339 env.region,
340 ),
341 previousParent: olds?.conversionWorkspace ?? output?.conversionWorkspace,
342 nextParent: news.conversionWorkspace,
343 extra:
344 (olds !== undefined || output !== undefined) && ruleBody(news) !== ruleBody(previous),
345 });
346 }),
347
348 read: Effect.fn(function* ({ id, olds, output }) {
349 const env = yield* GcpEnvironment.current;
350 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
351 const conversionWorkspace = conversionWorkspaceOf(
352 olds?.conversionWorkspace ?? output?.conversionWorkspace ?? "",
353 env.project,
354 location,
355 );
356 const mappingRuleId = yield* toPhysicalId(
357 id,
358 olds?.mappingRuleId,
359 output?.mappingRuleId,
360 "rule",
361 );
362 const name =
363 output?.name ??
364 (conversionWorkspace.length > 0 ? resourceName(conversionWorkspace, mappingRuleId) : "");
365 const existing = yield* getByName(name);
366 if (existing === undefined) return undefined;
367 const attrs = toAttrs(existing, env.project, name);
368 return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);
369 }),
370
371 list: () =>
372 Effect.gen(function* () {
373 const env = yield* GcpEnvironment.current;
374 const items = yield* listOwned(env.project, env.region);
375 return items.map((item) => toAttrs(item, env.project));
376 }),
377
378 reconcile: Effect.fn(function* ({ id, news, output }) {
379 const env = yield* GcpEnvironment.current;
380 const location = normalizeLocation(news.location ?? output?.location, env.region);
381 const conversionWorkspace = conversionWorkspaceOf(
382 news.conversionWorkspace,
383 env.project,
384 location,
385 );
386 const mappingRuleId = yield* toPhysicalId(
387 id,
388 news.mappingRuleId,
389 output?.mappingRuleId,
390 "rule",
391 );
392 const name = resourceName(conversionWorkspace, mappingRuleId);
393 const ownership = yield* createInternalLabels(id);
394 const displayName = encodeOwnershipLine(ownership, news.displayName);
395 const ruleOrder = news.ruleOrder ?? DEFAULT_ORDER;
396 const filter = news.filter ?? {};
397
398 let current = yield* getByName(output?.name ?? name);
399
400 if (current === undefined) {
401 const created = yield* dm
402 .createProjectsLocationsConversionWorkspacesMappingRules({
403 parent: conversionWorkspace,
404 mappingRuleId,
405 body: {
406 displayName,
407 ruleScope: news.ruleScope,
408 filter,
409 ruleOrder,
410 state: news.state ?? "ENABLED",
411 singleEntityRename: news.singleEntityRename,
412 multiEntityRename: news.multiEntityRename,
413 entityMove: news.entityMove,
414 sourceSqlChange: news.sourceSqlChange,
415 singlePackageChange: news.singlePackageChange,
416 setTablePrimaryKey: news.setTablePrimaryKey,
417 filterTableColumns: news.filterTableColumns,
418 multiColumnDataTypeChange: news.multiColumnDataTypeChange,
419 convertRowidColumn: news.convertRowidColumn,
420 singleColumnChange: news.singleColumnChange,
421 conditionalColumnSetValue: news.conditionalColumnSetValue,
422 },
423 })
424 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
425 if (created !== undefined) {
426 current = created;
427 } else {
428 current = yield* waitUntilExists(getByName(name), name);
429 }
430 }
431
432 if (current === undefined) {
433 return yield* new ResourceNotResolved({ resourceName: name });
434 }
435
436 return toAttrs(current, env.project, output?.name ?? name);
437 }),
438
439 delete: Effect.fn(function* ({ output }) {
440 if (!isWellFormed(output.name)) return;
441 const deleted = yield* dm
442 .deleteProjectsLocationsConversionWorkspacesMappingRules({
443 name: output.name,
444 })
445 .pipe(
446 Effect.as(true),
447 Effect.retry({
448 while: (error) => error._tag === "Conflict",
449 times: 8,
450 schedule: Schedule.spaced("2 seconds"),
451 }),
452 // The API answers NotFound for rules it still serves on GET (they
453 // only go away with their workspace), so there is nothing to wait on.
454 Effect.catchTag("NotFound", () => Effect.succeed(false)),
455 );
456 if (deleted) {
457 yield* waitUntilGone(getByName(output.name), output.name);
458 }
459 }),
460 });