@happyvertical/smrt-core 0.43.9 → 0.44.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +24 -1
- package/agents/generators.md +78 -5
- package/agents/schema-paths.md +99 -0
- package/dist/change-feed.d.ts +4 -0
- package/dist/change-feed.d.ts.map +1 -1
- package/dist/change-feed.js +8 -4
- package/dist/change-feed.js.map +1 -1
- package/dist/collection.d.ts +10 -0
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +145 -18
- package/dist/collection.js.map +1 -1
- package/dist/embedded-write-queue.d.ts +17 -29
- package/dist/embedded-write-queue.d.ts.map +1 -1
- package/dist/embedded-write-queue.js +44 -6
- package/dist/embedded-write-queue.js.map +1 -1
- package/dist/generators/custom-action.d.ts.map +1 -1
- package/dist/generators/custom-action.js +1 -1
- package/dist/generators/custom-action.js.map +1 -1
- package/dist/generators/mcp-emit.d.ts +6 -7
- package/dist/generators/mcp-emit.d.ts.map +1 -1
- package/dist/generators/mcp-emit.js +17 -29
- package/dist/generators/mcp-emit.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/knowledge.d.ts +17 -1
- package/dist/knowledge.d.ts.map +1 -1
- package/dist/knowledge.js +33 -3
- package/dist/knowledge.js.map +1 -1
- package/dist/manifest/static-manifest.d.ts.map +1 -1
- package/dist/manifest/static-manifest.js +30 -2
- package/dist/manifest/static-manifest.js.map +1 -1
- package/dist/manifest/store.js +1 -1
- package/dist/manifest/store.js.map +1 -1
- package/dist/manifest.json +36 -2
- package/dist/migrations/differ.d.ts +49 -0
- package/dist/migrations/differ.d.ts.map +1 -1
- package/dist/migrations/differ.js +220 -4
- package/dist/migrations/differ.js.map +1 -1
- package/dist/migrations/orchestrate.d.ts +23 -0
- package/dist/migrations/orchestrate.d.ts.map +1 -1
- package/dist/migrations/orchestrate.js +12 -3
- package/dist/migrations/orchestrate.js.map +1 -1
- package/dist/object.d.ts +44 -2
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +207 -18
- package/dist/object.js.map +1 -1
- package/dist/registry/types.d.ts +8 -3
- package/dist/registry/types.d.ts.map +1 -1
- package/dist/schema/foreign-key-ddl.d.ts +7 -0
- package/dist/schema/foreign-key-ddl.d.ts.map +1 -1
- package/dist/schema/foreign-key-ddl.js +7 -1
- package/dist/schema/foreign-key-ddl.js.map +1 -1
- package/dist/schema/schema-manager.d.ts +15 -0
- package/dist/schema/schema-manager.d.ts.map +1 -1
- package/dist/schema/schema-manager.js +52 -9
- package/dist/schema/schema-manager.js.map +1 -1
- package/dist/schema/types.d.ts +10 -0
- package/dist/schema/types.d.ts.map +1 -1
- package/dist/schema/uuid-convergence.d.ts +142 -0
- package/dist/schema/uuid-convergence.d.ts.map +1 -0
- package/dist/schema/uuid-convergence.js +0 -0
- package/dist/schema/uuid-convergence.js.map +1 -0
- package/dist/smrt-knowledge.json +33 -11
- package/dist/utils/scanner-module.d.ts +44 -0
- package/dist/utils/scanner-module.d.ts.map +1 -1
- package/dist/vite-plugin/index.d.ts +14 -1
- package/dist/vite-plugin/index.d.ts.map +1 -1
- package/dist/vite-plugin/index.js +54 -2
- package/dist/vite-plugin/index.js.map +1 -1
- package/dist/vite-plugin.js +3 -2
- package/package.json +6 -5
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { SchemaDefinition } from './types.js';
|
|
2
|
+
/** One physical column participating in a manifest UUID relationship. */
|
|
3
|
+
export interface UuidConvergenceNode {
|
|
4
|
+
table: string;
|
|
5
|
+
column: string;
|
|
6
|
+
}
|
|
7
|
+
/** A planned, executable `text` → `uuid` conversion for one column. */
|
|
8
|
+
export interface UuidConvergenceConversion extends UuidConvergenceNode {
|
|
9
|
+
/** The live PostgreSQL type the column has today (e.g. `text`). */
|
|
10
|
+
liveType: string;
|
|
11
|
+
/** Ordered statements: optional `DROP DEFAULT`, then the type change. */
|
|
12
|
+
statements: string[];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A relationship group that must converge but cannot be converged safely.
|
|
16
|
+
* Reported, never executed.
|
|
17
|
+
*/
|
|
18
|
+
export interface UuidConvergenceBlock {
|
|
19
|
+
/** Every column in the affected relationship component, sorted. */
|
|
20
|
+
nodes: UuidConvergenceNode[];
|
|
21
|
+
/** Which columns would have had to change, sorted. */
|
|
22
|
+
targets: UuidConvergenceNode[];
|
|
23
|
+
/** Why the automatic conversion is refused. */
|
|
24
|
+
reason: string;
|
|
25
|
+
/** Manual remediation the operator can inspect and run deliberately. */
|
|
26
|
+
suggestedSql: string[];
|
|
27
|
+
}
|
|
28
|
+
/** Result of {@link planUuidConvergence}. */
|
|
29
|
+
export interface UuidConvergencePlan {
|
|
30
|
+
/** Executable conversions, ordered parent-first then by table/column. */
|
|
31
|
+
conversions: UuidConvergenceConversion[];
|
|
32
|
+
/** Components that need convergence but were refused. */
|
|
33
|
+
blocks: UuidConvergenceBlock[];
|
|
34
|
+
/**
|
|
35
|
+
* Type each column will have once this plan is applied: `UUID` for a
|
|
36
|
+
* converted or already-native column, the normalized live type otherwise.
|
|
37
|
+
* Callers use it to decide whether a foreign key is safe to emit in the
|
|
38
|
+
* same run.
|
|
39
|
+
*/
|
|
40
|
+
effectiveType(table: string, column: string): string | undefined;
|
|
41
|
+
}
|
|
42
|
+
/** Live shape of one column, as the planner needs it. */
|
|
43
|
+
export interface UuidConvergenceLiveColumn {
|
|
44
|
+
/** Normalized bucket — `UUID`, `TEXT`, or any other normalized type. */
|
|
45
|
+
normalizedType: string;
|
|
46
|
+
/** Raw live type as introspected, used verbatim in diagnostics. */
|
|
47
|
+
rawType: string;
|
|
48
|
+
/** True when the live column carries a DEFAULT that must be dropped first. */
|
|
49
|
+
hasDefault: boolean;
|
|
50
|
+
/** False when the table/column does not exist yet (it will be created). */
|
|
51
|
+
exists: boolean;
|
|
52
|
+
}
|
|
53
|
+
/** One live foreign key that already constrains a pair of columns. */
|
|
54
|
+
export interface UuidConvergenceLiveForeignKey {
|
|
55
|
+
table: string;
|
|
56
|
+
column: string;
|
|
57
|
+
referencesTable: string;
|
|
58
|
+
referencesColumn: string;
|
|
59
|
+
}
|
|
60
|
+
/** Outcome of probing a column for values that are not uuid-shaped. */
|
|
61
|
+
export type UuidShapeProbeResult = {
|
|
62
|
+
status: 'clean';
|
|
63
|
+
} | {
|
|
64
|
+
status: 'dirty';
|
|
65
|
+
count: number;
|
|
66
|
+
sample?: string;
|
|
67
|
+
} | {
|
|
68
|
+
status: 'unavailable';
|
|
69
|
+
reason: string;
|
|
70
|
+
};
|
|
71
|
+
/** One manifest-declared relationship considered for convergence. */
|
|
72
|
+
export interface UuidConvergenceRelationship {
|
|
73
|
+
childTable: string;
|
|
74
|
+
childColumn: string;
|
|
75
|
+
parentTable: string;
|
|
76
|
+
parentColumn: string;
|
|
77
|
+
}
|
|
78
|
+
/** Inputs {@link planUuidConvergence} needs from the live database. */
|
|
79
|
+
export interface UuidConvergenceInput {
|
|
80
|
+
manifestSchemas: Record<string, SchemaDefinition>;
|
|
81
|
+
/**
|
|
82
|
+
* Physical relationships declared by the manifest for this engine. The
|
|
83
|
+
* caller filters engine allowlists (`@foreignKey({ constraint: { engines }})`)
|
|
84
|
+
* before handing them over.
|
|
85
|
+
*/
|
|
86
|
+
relationships: readonly UuidConvergenceRelationship[];
|
|
87
|
+
/** Live column shape, or `undefined` when the column does not exist yet. */
|
|
88
|
+
getLiveColumn(table: string, column: string): UuidConvergenceLiveColumn | undefined;
|
|
89
|
+
/** Every live foreign key across the schemas under comparison. */
|
|
90
|
+
liveForeignKeys: readonly UuidConvergenceLiveForeignKey[];
|
|
91
|
+
/** Probe run once per column that would be rewritten. */
|
|
92
|
+
probeUuidShape(table: string, column: string): Promise<UuidShapeProbeResult>;
|
|
93
|
+
}
|
|
94
|
+
/** Stable key for one `table.column` node. */
|
|
95
|
+
export declare function uuidConvergenceNodeKey(table: string, column: string): string;
|
|
96
|
+
/**
|
|
97
|
+
* SQL that counts, and samples, values a `text` column holds that could not
|
|
98
|
+
* be reinterpreted as `uuid`. `0` rows means the conversion is safe.
|
|
99
|
+
*/
|
|
100
|
+
export declare function renderUuidShapeProbe(tableName: string, columnName: string): string;
|
|
101
|
+
/**
|
|
102
|
+
* Render the one-time PostgreSQL conversion of a legacy `text` identifier or
|
|
103
|
+
* reference column to native `uuid`.
|
|
104
|
+
*
|
|
105
|
+
* A live DEFAULT is dropped first: PostgreSQL refuses `ALTER COLUMN … TYPE`
|
|
106
|
+
* when the existing default cannot be cast automatically, and the differ's
|
|
107
|
+
* ordinary default comparison re-establishes the manifest default on the next
|
|
108
|
+
* run once the type is settled.
|
|
109
|
+
*/
|
|
110
|
+
export declare function renderUuidColumnConversion(tableName: string, columnName: string, options?: {
|
|
111
|
+
hasDefault?: boolean;
|
|
112
|
+
}): string[];
|
|
113
|
+
/**
|
|
114
|
+
* The `[SchemaManager]`/`[SchemaComparer]` sentence that describes one
|
|
115
|
+
* incompatible child/parent pair. Shared so the runtime guard and the status
|
|
116
|
+
* planner say exactly the same thing about the same drift.
|
|
117
|
+
*/
|
|
118
|
+
export declare function describeForeignKeyTypeConflict(input: {
|
|
119
|
+
childTable: string;
|
|
120
|
+
childColumn: string;
|
|
121
|
+
childType: string;
|
|
122
|
+
parentTable: string;
|
|
123
|
+
parentColumn: string;
|
|
124
|
+
parentType: string;
|
|
125
|
+
}): string;
|
|
126
|
+
/**
|
|
127
|
+
* Remediation text appended to every incompatible-type diagnostic: the
|
|
128
|
+
* planned convergence normally handles it, and this is the manual equivalent.
|
|
129
|
+
*/
|
|
130
|
+
export declare function renderUuidConvergenceRepairHint(nodes: readonly UuidConvergenceNode[]): string[];
|
|
131
|
+
/**
|
|
132
|
+
* Group manifest UUID relationships into connected components and decide,
|
|
133
|
+
* per component, whether the live database must converge on native `uuid`.
|
|
134
|
+
*
|
|
135
|
+
* Components — not individual pairs — are the unit of decision because a
|
|
136
|
+
* single legacy `text` primary key can be referenced by several children:
|
|
137
|
+
* converting the parent for one child would break every other member that is
|
|
138
|
+
* still `text`. Self-referential tables fall out of the same grouping (both
|
|
139
|
+
* endpoints land in one component).
|
|
140
|
+
*/
|
|
141
|
+
export declare function planUuidConvergence(input: UuidConvergenceInput): Promise<UuidConvergencePlan>;
|
|
142
|
+
//# sourceMappingURL=uuid-convergence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uuid-convergence.d.ts","sourceRoot":"","sources":["../../src/schema/uuid-convergence.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAIH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,yEAAyE;AACzE,MAAM,WAAW,mBAAmB;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,uEAAuE;AACvE,MAAM,WAAW,yBAA0B,SAAQ,mBAAmB;IACpE,mEAAmE;IACnE,QAAQ,EAAE,MAAM,CAAC;IACjB,yEAAyE;IACzE,UAAU,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC,mEAAmE;IACnE,KAAK,EAAE,mBAAmB,EAAE,CAAC;IAC7B,sDAAsD;IACtD,OAAO,EAAE,mBAAmB,EAAE,CAAC;IAC/B,+CAA+C;IAC/C,MAAM,EAAE,MAAM,CAAC;IACf,wEAAwE;IACxE,YAAY,EAAE,MAAM,EAAE,CAAC;CACxB;AAED,6CAA6C;AAC7C,MAAM,WAAW,mBAAmB;IAClC,yEAAyE;IACzE,WAAW,EAAE,yBAAyB,EAAE,CAAC;IACzC,yDAAyD;IACzD,MAAM,EAAE,oBAAoB,EAAE,CAAC;IAC/B;;;;;OAKG;IACH,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CAClE;AAED,yDAAyD;AACzD,MAAM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AoB,CAAC,CAAC;CAC9E;AAED,8CAA8C;AAC9C,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAE5E;AAMD;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,GACjB,MAAM,CAOR;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE;IAAE,UAAU,CAAC,EAAE,OAAO,CAAA;CAAO,GACrC,MAAM,EAAE,CAWV;AAED;;;;GAIG;AACH,wBAAgB,8BAA8B,CAAC,KAAK,EAAE;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;CACpB,GAAG,MAAM,CAMT;AAED;;;GAGG;AACH,wBAAgB,+BAA+B,CAC7C,KAAK,EAAE,SAAS,mBAAmB,EAAE,GACpC,MAAM,EAAE,CAKV;AAOD;;;;;;;;;GASG;AACH,wBAAsB,mBAAmB,CACvC,KAAK,EAAE,oBAAoB,GAC1B,OAAO,CAAC,mBAAmB,CAAC,CA4N9B"}
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uuid-convergence.js","names":[],"sources":["../../src/schema/uuid-convergence.ts"],"sourcesContent":["/**\n * UUID convergence for pre-R11 PostgreSQL databases (#2608).\n *\n * R11 made SMRT identifiers and references native `uuid` on PostgreSQL.\n * Databases created before that change still carry `text` id columns, while\n * every column added afterwards materializes as `uuid`. PostgreSQL cannot\n * implement a `FOREIGN KEY` across those two physical types — there is no\n * cast inside FK DDL — so `ALTER TABLE … ADD CONSTRAINT … NOT VALID` fails\n * with SQLSTATE 42804 and takes the rest of the migration batch with it.\n *\n * This module owns the deterministic half of the repair:\n *\n * - {@link renderUuidColumnConversion} renders the one-time\n * `ALTER COLUMN … TYPE uuid USING …::uuid` (plus the `DROP DEFAULT` a live\n * text default would otherwise block), and\n * - {@link planUuidConvergence} decides *which* columns must convert, by\n * grouping every manifest-declared UUID relationship into connected\n * components and converging a component only when the live database already\n * proves the target shape (at least one side is native `uuid`).\n *\n * The planner never coerces data: a component converges only when a caller\n * supplied probe confirms every non-null value is uuid-shaped, no live\n * foreign key depends on a column being rewritten, and no member carries some\n * third physical type. Anything else is returned as a *block* with an\n * operator-facing reason, so `db:status` can report \"blocked\" instead of\n * \"pending\" and `db:migrate` never attempts DDL PostgreSQL will reject.\n *\n * A database whose relationship is `text` on *both* sides is left alone: that\n * is the tolerated pre-R11 shape (see the R11 uuid/text equivalence in\n * `migrations/differ.ts`), and its foreign keys apply fine.\n */\n\nimport { CANONICAL_UUID_PATTERN } from './foreign-key-ddl.js';\nimport { quoteIdentifier } from './sql-identifiers.js';\nimport type { SchemaDefinition } from './types.js';\n\n/** One physical column participating in a manifest UUID relationship. */\nexport interface UuidConvergenceNode {\n table: string;\n column: string;\n}\n\n/** A planned, executable `text` → `uuid` conversion for one column. */\nexport interface UuidConvergenceConversion extends UuidConvergenceNode {\n /** The live PostgreSQL type the column has today (e.g. `text`). */\n liveType: string;\n /** Ordered statements: optional `DROP DEFAULT`, then the type change. */\n statements: string[];\n}\n\n/**\n * A relationship group that must converge but cannot be converged safely.\n * Reported, never executed.\n */\nexport interface UuidConvergenceBlock {\n /** Every column in the affected relationship component, sorted. */\n nodes: UuidConvergenceNode[];\n /** Which columns would have had to change, sorted. */\n targets: UuidConvergenceNode[];\n /** Why the automatic conversion is refused. */\n reason: string;\n /** Manual remediation the operator can inspect and run deliberately. */\n suggestedSql: string[];\n}\n\n/** Result of {@link planUuidConvergence}. */\nexport interface UuidConvergencePlan {\n /** Executable conversions, ordered parent-first then by table/column. */\n conversions: UuidConvergenceConversion[];\n /** Components that need convergence but were refused. */\n blocks: UuidConvergenceBlock[];\n /**\n * Type each column will have once this plan is applied: `UUID` for a\n * converted or already-native column, the normalized live type otherwise.\n * Callers use it to decide whether a foreign key is safe to emit in the\n * same run.\n */\n effectiveType(table: string, column: string): string | undefined;\n}\n\n/** Live shape of one column, as the planner needs it. */\nexport interface UuidConvergenceLiveColumn {\n /** Normalized bucket — `UUID`, `TEXT`, or any other normalized type. */\n normalizedType: string;\n /** Raw live type as introspected, used verbatim in diagnostics. */\n rawType: string;\n /** True when the live column carries a DEFAULT that must be dropped first. */\n hasDefault: boolean;\n /** False when the table/column does not exist yet (it will be created). */\n exists: boolean;\n}\n\n/** One live foreign key that already constrains a pair of columns. */\nexport interface UuidConvergenceLiveForeignKey {\n table: string;\n column: string;\n referencesTable: string;\n referencesColumn: string;\n}\n\n/** Outcome of probing a column for values that are not uuid-shaped. */\nexport type UuidShapeProbeResult =\n | { status: 'clean' }\n | { status: 'dirty'; count: number; sample?: string }\n | { status: 'unavailable'; reason: string };\n\n/** One manifest-declared relationship considered for convergence. */\nexport interface UuidConvergenceRelationship {\n childTable: string;\n childColumn: string;\n parentTable: string;\n parentColumn: string;\n}\n\n/** Inputs {@link planUuidConvergence} needs from the live database. */\nexport interface UuidConvergenceInput {\n manifestSchemas: Record<string, SchemaDefinition>;\n /**\n * Physical relationships declared by the manifest for this engine. The\n * caller filters engine allowlists (`@foreignKey({ constraint: { engines }})`)\n * before handing them over.\n */\n relationships: readonly UuidConvergenceRelationship[];\n /** Live column shape, or `undefined` when the column does not exist yet. */\n getLiveColumn(\n table: string,\n column: string,\n ): UuidConvergenceLiveColumn | undefined;\n /** Every live foreign key across the schemas under comparison. */\n liveForeignKeys: readonly UuidConvergenceLiveForeignKey[];\n /** Probe run once per column that would be rewritten. */\n probeUuidShape(table: string, column: string): Promise<UuidShapeProbeResult>;\n}\n\n/** Stable key for one `table.column` node. */\nexport function uuidConvergenceNodeKey(table: string, column: string): string {\n return `${table}\u0000${column}`;\n}\n\nfunction describeNode(node: UuidConvergenceNode): string {\n return `${node.table}.${node.column}`;\n}\n\n/**\n * SQL that counts, and samples, values a `text` column holds that could not\n * be reinterpreted as `uuid`. `0` rows means the conversion is safe.\n */\nexport function renderUuidShapeProbe(\n tableName: string,\n columnName: string,\n): string {\n const column = quoteIdentifier(columnName);\n return (\n `SELECT count(*) AS invalid_count, min(${column}::text) AS sample_value ` +\n `FROM ${quoteIdentifier(tableName)} ` +\n `WHERE ${column} IS NOT NULL AND ${column}::text !~* '${CANONICAL_UUID_PATTERN}'`\n );\n}\n\n/**\n * Render the one-time PostgreSQL conversion of a legacy `text` identifier or\n * reference column to native `uuid`.\n *\n * A live DEFAULT is dropped first: PostgreSQL refuses `ALTER COLUMN … TYPE`\n * when the existing default cannot be cast automatically, and the differ's\n * ordinary default comparison re-establishes the manifest default on the next\n * run once the type is settled.\n */\nexport function renderUuidColumnConversion(\n tableName: string,\n columnName: string,\n options: { hasDefault?: boolean } = {},\n): string[] {\n const table = quoteIdentifier(tableName);\n const column = quoteIdentifier(columnName);\n const statements: string[] = [];\n if (options.hasDefault) {\n statements.push(`ALTER TABLE ${table} ALTER COLUMN ${column} DROP DEFAULT`);\n }\n statements.push(\n `ALTER TABLE ${table} ALTER COLUMN ${column} TYPE uuid USING ${column}::uuid`,\n );\n return statements;\n}\n\n/**\n * The `[SchemaManager]`/`[SchemaComparer]` sentence that describes one\n * incompatible child/parent pair. Shared so the runtime guard and the status\n * planner say exactly the same thing about the same drift.\n */\nexport function describeForeignKeyTypeConflict(input: {\n childTable: string;\n childColumn: string;\n childType: string;\n parentTable: string;\n parentColumn: string;\n parentType: string;\n}): string {\n return (\n `${input.childTable}.${input.childColumn} is ${input.childType} but ` +\n `${input.parentTable}.${input.parentColumn} is ${input.parentType}; ` +\n 'PostgreSQL cannot cast inside FOREIGN KEY DDL (SQLSTATE 42804)'\n );\n}\n\n/**\n * Remediation text appended to every incompatible-type diagnostic: the\n * planned convergence normally handles it, and this is the manual equivalent.\n */\nexport function renderUuidConvergenceRepairHint(\n nodes: readonly UuidConvergenceNode[],\n): string[] {\n return nodes.flatMap((node) => [\n renderUuidShapeProbe(node.table, node.column),\n ...renderUuidColumnConversion(node.table, node.column),\n ]);\n}\n\ninterface ComponentNode extends UuidConvergenceNode {\n key: string;\n live: UuidConvergenceLiveColumn;\n}\n\n/**\n * Group manifest UUID relationships into connected components and decide,\n * per component, whether the live database must converge on native `uuid`.\n *\n * Components — not individual pairs — are the unit of decision because a\n * single legacy `text` primary key can be referenced by several children:\n * converting the parent for one child would break every other member that is\n * still `text`. Self-referential tables fall out of the same grouping (both\n * endpoints land in one component).\n */\nexport async function planUuidConvergence(\n input: UuidConvergenceInput,\n): Promise<UuidConvergencePlan> {\n const nodes = new Map<string, ComponentNode>();\n const parents = new Map<string, string>();\n\n const find = (key: string): string => {\n let root = key;\n while (parents.get(root) !== root) {\n const next = parents.get(root);\n if (next === undefined) break;\n root = next;\n }\n let cursor = key;\n while (parents.get(cursor) !== root) {\n const next = parents.get(cursor);\n if (next === undefined) break;\n parents.set(cursor, root);\n cursor = next;\n }\n return root;\n };\n const union = (a: string, b: string) => {\n const rootA = find(a);\n const rootB = find(b);\n if (rootA !== rootB) parents.set(rootB, rootA);\n };\n\n const addNode = (\n table: string,\n column: string,\n manifestType: string,\n ): string | undefined => {\n const key = uuidConvergenceNodeKey(table, column);\n const existing = nodes.get(key);\n if (existing) return key;\n const live = input.getLiveColumn(table, column) ?? {\n // A table or column the migration is about to create materializes with\n // the manifest type, so it is authoritative evidence for the component.\n normalizedType: manifestType,\n rawType: manifestType.toLowerCase(),\n hasDefault: false,\n exists: false,\n };\n nodes.set(key, { key, table, column, live });\n parents.set(key, key);\n return key;\n };\n\n for (const relationship of input.relationships) {\n const childColumn =\n input.manifestSchemas[relationship.childTable]?.columns[\n relationship.childColumn\n ];\n const parentColumn =\n input.manifestSchemas[relationship.parentTable]?.columns[\n relationship.parentColumn\n ];\n // Only relationships the manifest declares UUID on both sides can\n // converge on uuid; anything else is an ordinary type question the\n // column differ already owns.\n if (childColumn?.type !== 'UUID' || parentColumn?.type !== 'UUID') continue;\n const childKey = addNode(\n relationship.childTable,\n relationship.childColumn,\n 'UUID',\n );\n const parentKey = addNode(\n relationship.parentTable,\n relationship.parentColumn,\n 'UUID',\n );\n if (childKey && parentKey) union(childKey, parentKey);\n }\n\n const components = new Map<string, ComponentNode[]>();\n for (const node of nodes.values()) {\n const root = find(node.key);\n const bucket = components.get(root);\n if (bucket) bucket.push(node);\n else components.set(root, [node]);\n }\n\n const conversions: UuidConvergenceConversion[] = [];\n const blocks: UuidConvergenceBlock[] = [];\n const converted = new Set<string>();\n\n for (const members of components.values()) {\n members.sort((a, b) => a.key.localeCompare(b.key));\n const hasUuid = members.some((node) => node.live.normalizedType === 'UUID');\n const targets = members.filter(\n (node) => node.live.normalizedType === 'TEXT' && node.live.exists,\n );\n const foreign = members.filter(\n (node) =>\n node.live.normalizedType !== 'UUID' &&\n node.live.normalizedType !== 'TEXT',\n );\n\n // All-`text` component: the tolerated pre-R11 shape. Its foreign keys are\n // type-compatible today, so forcing native uuid here would be churn.\n if (!hasUuid || targets.length === 0) {\n if (hasUuid && foreign.length > 0) {\n blocks.push(\n buildBlock(\n members,\n [],\n `column${foreign.length === 1 ? '' : 's'} ${foreign\n .map(describeNode)\n .join(', ')} hold${foreign.length === 1 ? 's' : ''} an ` +\n `unexpected physical type (${foreign\n .map((node) => node.live.rawType)\n .join(', ')}) that SMRT will not rewrite automatically`,\n ),\n );\n }\n continue;\n }\n\n if (foreign.length > 0) {\n blocks.push(\n buildBlock(\n members,\n targets,\n `column${foreign.length === 1 ? '' : 's'} ${foreign\n .map(describeNode)\n .join(', ')} hold${foreign.length === 1 ? 's' : ''} an ` +\n `unexpected physical type (${foreign\n .map((node) => node.live.rawType)\n .join(', ')}) that SMRT will not rewrite automatically`,\n ),\n );\n continue;\n }\n\n const targetKeys = new Set(targets.map((node) => node.key));\n const dependents = input.liveForeignKeys.filter(\n (live) =>\n targetKeys.has(uuidConvergenceNodeKey(live.table, live.column)) ||\n targetKeys.has(\n uuidConvergenceNodeKey(live.referencesTable, live.referencesColumn),\n ),\n );\n if (dependents.length > 0) {\n blocks.push(\n buildBlock(\n members,\n targets,\n 'live foreign keys still depend on the columns that must change (' +\n dependents\n .map(\n (live) =>\n `${live.table}.${live.column} -> ${live.referencesTable}.${live.referencesColumn}`,\n )\n .join(', ') +\n '). Drop those constraints deliberately, rerun the migration to ' +\n 'converge the columns, and let SMRT re-add the manifest constraints',\n ),\n );\n continue;\n }\n\n let dirty: string | undefined;\n for (const node of targets) {\n const probe = await input.probeUuidShape(node.table, node.column);\n if (probe.status === 'clean') continue;\n dirty =\n probe.status === 'dirty'\n ? `${describeNode(node)} holds ${probe.count} value${\n probe.count === 1 ? '' : 's'\n } that ${probe.count === 1 ? 'is' : 'are'} not uuid-shaped` +\n (probe.sample\n ? ` (for example ${JSON.stringify(probe.sample)})`\n : '')\n : `${describeNode(node)} could not be probed for uuid-shaped values (${probe.reason})`;\n break;\n }\n if (dirty) {\n blocks.push(\n buildBlock(\n members,\n targets,\n `${dirty}. SMRT never coerces identifier values; repair or remove them, then rerun the migration`,\n ),\n );\n continue;\n }\n\n for (const node of targets) {\n converted.add(node.key);\n conversions.push({\n table: node.table,\n column: node.column,\n liveType: node.live.rawType,\n statements: renderUuidColumnConversion(node.table, node.column, {\n hasDefault: node.live.hasDefault,\n }),\n });\n }\n }\n\n // Parents before children: converting a referenced primary key first keeps\n // the statement stream valid even if an operator replays it by hand.\n conversions.sort((left, right) => {\n const leftIsId = left.column === 'id' ? 0 : 1;\n const rightIsId = right.column === 'id' ? 0 : 1;\n if (leftIsId !== rightIsId) return leftIsId - rightIsId;\n return (\n left.table.localeCompare(right.table) ||\n left.column.localeCompare(right.column)\n );\n });\n\n return {\n conversions,\n blocks,\n effectiveType(table: string, column: string): string | undefined {\n const key = uuidConvergenceNodeKey(table, column);\n if (converted.has(key)) return 'UUID';\n return nodes.get(key)?.live.normalizedType;\n },\n };\n}\n\nfunction buildBlock(\n members: readonly ComponentNode[],\n targets: readonly ComponentNode[],\n reason: string,\n): UuidConvergenceBlock {\n const plain = (node: UuidConvergenceNode) => ({\n table: node.table,\n column: node.column,\n });\n return {\n nodes: members.map(plain),\n targets: targets.map(plain),\n reason,\n suggestedSql: renderUuidConvergenceRepairHint(targets.map(plain)),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuIA,SAAgB,uBAAuB,OAAe,QAAwB;CAC5E,OAAO,GAAG,MAAM,GAAG;AACrB;AAEA,SAAS,aAAa,MAAmC;CACvD,OAAO,GAAG,KAAK,MAAM,GAAG,KAAK;AAC/B;;;;;AAMA,SAAgB,qBACd,WACA,YACQ;CACR,MAAM,SAAS,gBAAgB,UAAU;CACzC,OACE,yCAAyC,OAAO,+BACxC,gBAAgB,SAAS,EAAE,SAC1B,OAAO,mBAAmB,OAAO,cAAc,uBAAuB;AAEnF;;;;;;;;;;AAWA,SAAgB,2BACd,WACA,YACA,UAAoC,CAAC,GAC3B;CACV,MAAM,QAAQ,gBAAgB,SAAS;CACvC,MAAM,SAAS,gBAAgB,UAAU;CACzC,MAAM,aAAuB,CAAC;CAC9B,IAAI,QAAQ,YACV,WAAW,KAAK,eAAe,MAAM,gBAAgB,OAAO,cAAc;CAE5E,WAAW,KACT,eAAe,MAAM,gBAAgB,OAAO,mBAAmB,OAAO,OACxE;CACA,OAAO;AACT;;;;;;AAOA,SAAgB,+BAA+B,OAOpC;CACT,OACE,GAAG,MAAM,WAAW,GAAG,MAAM,YAAY,MAAM,MAAM,UAAU,OAC5D,MAAM,YAAY,GAAG,MAAM,aAAa,MAAM,MAAM,WAAW;AAGtE;;;;;AAMA,SAAgB,gCACd,OACU;CACV,OAAO,MAAM,SAAS,SAAS,CAC7B,qBAAqB,KAAK,OAAO,KAAK,MAAM,GAC5C,GAAG,2BAA2B,KAAK,OAAO,KAAK,MAAM,CACvD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