uql-orm 0.96.1 → 0.98.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/dist/dialect/abstractSqlDialect.d.ts +1 -7
- package/dist/dialect/abstractSqlDialect.js +1 -9
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +2 -0
- package/dist/dialect/mysqlLikeSqlDialect.js +4 -1
- package/dist/dialect/pgLikeSqlDialect.js +0 -1
- package/dist/migrate/acquireQuerierForMigrations.d.ts +4 -8
- package/dist/migrate/acquireQuerierForMigrations.js +10 -18
- package/dist/migrate/builder/columnBuilder.d.ts +1 -2
- package/dist/migrate/builder/columnBuilder.js +3 -10
- package/dist/migrate/builder/migrationBuilder.d.ts +6 -26
- package/dist/migrate/builder/migrationBuilder.js +21 -78
- package/dist/migrate/builder/tableBuilder.js +2 -3
- package/dist/migrate/builder/types.d.ts +59 -120
- package/dist/migrate/cli-config.d.ts +1 -0
- package/dist/migrate/cli-config.js +11 -26
- package/dist/migrate/cli.d.ts +0 -13
- package/dist/migrate/cli.js +55 -118
- package/dist/migrate/codegen/entityCodeGenerator.d.ts +7 -61
- package/dist/migrate/codegen/entityCodeGenerator.js +53 -174
- package/dist/migrate/codegen/fieldOptionsSource.d.ts +1 -3
- package/dist/migrate/codegen/fieldOptionsSource.js +3 -11
- package/dist/migrate/codegen/index.d.ts +2 -7
- package/dist/migrate/codegen/index.js +2 -8
- package/dist/migrate/codegen/migrationFile.d.ts +1 -8
- package/dist/migrate/codegen/migrationFile.js +9 -13
- package/dist/migrate/ddl/defaultSql.d.ts +2 -1
- package/dist/migrate/ddl/defaultSql.js +1 -3
- package/dist/migrate/ddl/indexCapabilities.d.ts +18 -0
- package/dist/migrate/ddl/indexCapabilities.js +60 -0
- package/dist/migrate/ddl/indexDdl.d.ts +11 -24
- package/dist/migrate/ddl/indexDdl.js +22 -28
- package/dist/migrate/ddl/mssqlIndexDdl.d.ts +1 -7
- package/dist/migrate/ddl/mssqlIndexDdl.js +1 -7
- package/dist/migrate/ddl/mssqlTableDdl.d.ts +6 -9
- package/dist/migrate/ddl/mssqlTableDdl.js +14 -17
- package/dist/migrate/ddl/mysqlIndexDdl.d.ts +0 -21
- package/dist/migrate/ddl/mysqlIndexDdl.js +4 -48
- package/dist/migrate/ddl/mysqlTableDdl.d.ts +6 -5
- package/dist/migrate/ddl/mysqlTableDdl.js +15 -10
- package/dist/migrate/ddl/pgIndexDdl.d.ts +0 -10
- package/dist/migrate/ddl/pgIndexDdl.js +5 -44
- package/dist/migrate/ddl/pgTableDdl.d.ts +6 -2
- package/dist/migrate/ddl/pgTableDdl.js +17 -1
- package/dist/migrate/ddl/sqliteIndexDdl.js +2 -7
- package/dist/migrate/ddl/tableDdl.d.ts +43 -24
- package/dist/migrate/ddl/tableDdl.js +56 -37
- package/dist/migrate/ddl/tableRebuild.d.ts +2 -1
- package/dist/migrate/drift/driftDetector.d.ts +29 -32
- package/dist/migrate/drift/driftDetector.js +155 -205
- package/dist/migrate/drift/index.d.ts +1 -6
- package/dist/migrate/drift/index.js +1 -6
- package/dist/migrate/generator/definitionToNode.d.ts +2 -2
- package/dist/migrate/generator/definitionToNode.js +6 -5
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +56 -77
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +75 -98
- package/dist/migrate/introspection/mssqlIntrospector.d.ts +10 -43
- package/dist/migrate/introspection/mssqlIntrospector.js +52 -77
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +21 -37
- package/dist/migrate/introspection/mysqlIntrospector.js +75 -56
- package/dist/migrate/introspection/postgresIntrospector.d.ts +15 -60
- package/dist/migrate/introspection/postgresIntrospector.js +74 -77
- package/dist/migrate/introspection/sqliteIntrospector.d.ts +19 -53
- package/dist/migrate/introspection/sqliteIntrospector.js +73 -119
- package/dist/migrate/introspection/tableSchemaAST.d.ts +13 -0
- package/dist/migrate/introspection/tableSchemaAST.js +85 -0
- package/dist/migrate/migrationTarget.js +10 -2
- package/dist/migrate/migrator.d.ts +9 -44
- package/dist/migrate/migrator.js +72 -150
- package/dist/migrate/schemaChange.d.ts +8 -5
- package/dist/migrate/schemaChange.js +25 -8
- package/dist/migrate/schemaGenerator.d.ts +35 -109
- package/dist/migrate/schemaGenerator.js +179 -353
- package/dist/migrate/storage/databaseStorage.d.ts +2 -1
- package/dist/migrate/storage/databaseStorage.js +3 -2
- package/dist/migrate/storage/jsonStorage.d.ts +1 -2
- package/dist/migrate/storage/jsonStorage.js +11 -20
- package/dist/migrate/triggerSql.d.ts +7 -9
- package/dist/migrate/triggerSql.js +17 -18
- package/dist/mongo/index.d.ts +0 -1
- package/dist/mongo/index.js +0 -1
- package/dist/mongo/mongoDialect.js +0 -1
- package/dist/mongo/mongoIntrospector.d.ts +1 -6
- package/dist/mongo/mongoIntrospector.js +7 -18
- package/dist/mongo/mongoSchemaGenerator.d.ts +7 -26
- package/dist/mongo/mongoSchemaGenerator.js +51 -75
- package/dist/mssql/mssqlDialect.js +0 -1
- package/dist/schema/canonicalType.d.ts +15 -19
- package/dist/schema/canonicalType.js +20 -43
- package/dist/schema/dependencyGraph.d.ts +2 -3
- package/dist/schema/dependencyGraph.js +0 -4
- package/dist/schema/indexDifferences.js +2 -2
- package/dist/schema/schemaAST.d.ts +1 -1
- package/dist/schema/schemaAST.js +5 -12
- package/dist/schema/schemaASTBuilder.d.ts +5 -6
- package/dist/schema/schemaASTBuilder.js +17 -18
- package/dist/schema/schemaASTDiffer.d.ts +4 -5
- package/dist/schema/schemaASTDiffer.js +40 -73
- package/dist/schema/types.d.ts +24 -54
- package/dist/sqlite/localSqliteQuerierPool.d.ts +16 -2
- package/dist/sqlite/localSqliteQuerierPool.js +20 -4
- package/dist/sqlite/nodeSqliteQuerierPool.js +2 -2
- package/dist/sqlite/sqliteDialect.js +0 -1
- package/dist/sqlite/sqliteQuerierPool.js +3 -3
- package/dist/type/dialect.d.ts +0 -2
- package/dist/type/migration.d.ts +13 -73
- package/dist/util/ddlExpression.util.d.ts +13 -2
- package/dist/util/ddlExpression.util.js +27 -4
- package/dist/util/sql.util.d.ts +2 -12
- package/dist/util/sql.util.js +9 -18
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +1 -1
- package/dist/migrate/introspection/baseSqlIntrospector.d.ts +0 -36
- package/dist/migrate/introspection/baseSqlIntrospector.js +0 -133
- package/dist/schema/index.d.ts +0 -2
- package/dist/schema/index.js +0 -1
- package/dist/sqlite/sqlitePragmas.d.ts +0 -3
- package/dist/sqlite/sqlitePragmas.js +0 -15
|
@@ -1,50 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* Detects schema drift between expected schema (from entities) and
|
|
5
|
-
* actual database schema.
|
|
6
|
-
*/
|
|
7
|
-
import { canonicalToSql, engineType } from '../../schema/canonicalType.js';
|
|
8
|
-
import { defaultsEqualAsWritten, diffSchemas, referentialActions } from '../../schema/schemaASTDiffer.js';
|
|
1
|
+
import { canonicalToSql } from '../../schema/canonicalType.js';
|
|
2
|
+
import { diffSchemas, referentialActions } from '../../schema/schemaASTDiffer.js';
|
|
9
3
|
import { SqlExpression, writtenDefault } from '../../schema/sqlExpression.js';
|
|
10
|
-
function resolveOptions(options) {
|
|
11
|
-
return {
|
|
12
|
-
checkTypes: options.checkTypes ?? true,
|
|
13
|
-
checkNullable: options.checkNullable ?? true,
|
|
14
|
-
checkIndexes: options.checkIndexes ?? true,
|
|
15
|
-
checkForeignKeys: options.checkForeignKeys ?? true,
|
|
16
|
-
checkDefaults: options.checkDefaults ?? options.defaultsEqual !== undefined,
|
|
17
|
-
defaultsEqual: options.defaultsEqual ?? defaultsEqualAsWritten,
|
|
18
|
-
excludeTables: options.excludeTables ?? [],
|
|
19
|
-
dialect: options.dialect,
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
4
|
/**
|
|
23
5
|
* Compare an expected schema (from entities) with an actual one (from the database) and report every
|
|
24
6
|
* way they have drifted apart.
|
|
25
7
|
*/
|
|
26
8
|
export function detectDrift(expectedAST, actualAST, options = {}) {
|
|
27
|
-
const
|
|
28
|
-
const { dialect } = opts;
|
|
29
|
-
const diff = diffSchemas(expectedAST, actualAST, {
|
|
30
|
-
compareIndexes: opts.checkIndexes,
|
|
31
|
-
compareRelationships: opts.checkForeignKeys,
|
|
32
|
-
excludeTables: opts.excludeTables,
|
|
33
|
-
defaultsEqual: opts.defaultsEqual,
|
|
34
|
-
// Types are normalized through the dialect's engine; without a dialect, no type drift is reported.
|
|
35
|
-
...(dialect && { normalizeType: engineType(dialect) }),
|
|
36
|
-
});
|
|
9
|
+
const diff = diffSchemas(expectedAST, actualAST, options);
|
|
37
10
|
const drifts = [
|
|
38
|
-
...
|
|
39
|
-
...
|
|
40
|
-
...
|
|
11
|
+
...diff.tables.map(tableDrift),
|
|
12
|
+
...diff.columns.flatMap((change) => columnDrifts(change, options.dialect)),
|
|
13
|
+
...diff.indexes.map(indexDrift),
|
|
41
14
|
...detectPrimaryKeyDrifts(diff),
|
|
42
|
-
...
|
|
15
|
+
...detectOwnedDrifts('Check', diff.checks),
|
|
16
|
+
...detectOwnedDrifts('Trigger', diff.triggers),
|
|
17
|
+
...diff.relationships.map(relationshipDrift),
|
|
43
18
|
];
|
|
19
|
+
const count = (severity) => drifts.filter((drift) => drift.severity === severity).length;
|
|
20
|
+
const summary = { critical: count('critical'), warning: count('warning'), info: count('info') };
|
|
44
21
|
return {
|
|
45
|
-
status:
|
|
22
|
+
status: drifts.length === 0 ? 'in_sync' : summary.critical ? 'critical' : 'drifted',
|
|
46
23
|
drifts,
|
|
47
|
-
summary
|
|
24
|
+
summary,
|
|
48
25
|
generatedAt: new Date(),
|
|
49
26
|
};
|
|
50
27
|
}
|
|
@@ -63,15 +40,26 @@ function detectPrimaryKeyDrifts(diff) {
|
|
|
63
40
|
suggestion: 'Generate a migration to change the primary key',
|
|
64
41
|
}));
|
|
65
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* A check or trigger the database lacks, critical since rows are written otherwise than the entity says, or
|
|
45
|
+
* one uql installed that nothing declares.
|
|
46
|
+
*/
|
|
47
|
+
function detectOwnedDrifts(kind, changes) {
|
|
48
|
+
return changes.map(({ table, from, to }) => ({
|
|
49
|
+
type: 'constraint_mismatch',
|
|
50
|
+
table,
|
|
51
|
+
suggestion: 'Generate a migration to match the entity',
|
|
52
|
+
...(to
|
|
53
|
+
? { severity: 'critical', details: `${kind} "${to.name}" expected but not found in database` }
|
|
54
|
+
: { severity: 'warning', details: `${kind} "${from?.name}" exists in database but not in entity` }),
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
66
57
|
/** A key's columns as a drift names them, `none` where there is no key. */
|
|
67
58
|
function keyColumns(key) {
|
|
68
59
|
return key?.columns.join(', ') || 'none';
|
|
69
60
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
*/
|
|
73
|
-
function detectTableDrifts(diff) {
|
|
74
|
-
return diff.tables.map((change) => change.from === undefined
|
|
61
|
+
function tableDrift(change) {
|
|
62
|
+
return change.from === undefined
|
|
75
63
|
? {
|
|
76
64
|
type: 'missing_table',
|
|
77
65
|
severity: 'critical',
|
|
@@ -85,166 +73,149 @@ function detectTableDrifts(diff) {
|
|
|
85
73
|
table: change.from.name,
|
|
86
74
|
details: `Table "${change.from.name}" exists in database but no matching entity`,
|
|
87
75
|
suggestion: 'Create entity or drop table',
|
|
88
|
-
}
|
|
76
|
+
};
|
|
89
77
|
}
|
|
90
|
-
/**
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (colDiff.from === undefined) {
|
|
97
|
-
drifts.push({
|
|
78
|
+
/** A column the database lacks or no entity declares, or each part of one the differ found changed. */
|
|
79
|
+
function columnDrifts(change, dialect) {
|
|
80
|
+
const { table, column } = change;
|
|
81
|
+
if (change.from === undefined) {
|
|
82
|
+
return [
|
|
83
|
+
{
|
|
98
84
|
type: 'missing_column',
|
|
99
85
|
severity: 'critical',
|
|
100
|
-
table
|
|
101
|
-
column
|
|
102
|
-
details: `Column "${
|
|
86
|
+
table,
|
|
87
|
+
column,
|
|
88
|
+
details: `Column "${column}" expected but not found in database`,
|
|
103
89
|
suggestion: 'Run migration to add column',
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
90
|
+
},
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
if (change.to === undefined) {
|
|
94
|
+
return [
|
|
95
|
+
{
|
|
108
96
|
type: 'unexpected_column',
|
|
109
97
|
severity: 'warning',
|
|
110
|
-
table
|
|
111
|
-
column
|
|
112
|
-
details: `Column "${
|
|
98
|
+
table,
|
|
99
|
+
column,
|
|
100
|
+
details: `Column "${column}" exists in database but not in entity`,
|
|
113
101
|
suggestion: 'Add to entity or create migration to drop',
|
|
114
|
-
}
|
|
102
|
+
},
|
|
103
|
+
];
|
|
104
|
+
}
|
|
105
|
+
const { from, to, isBreaking } = change;
|
|
106
|
+
return change.changed.flatMap((facet) => {
|
|
107
|
+
if (facet === 'type') {
|
|
108
|
+
// Type drift needs a dialect to render both types.
|
|
109
|
+
if (!dialect) {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
const [expected, actual] = [canonicalToSql(to.type, dialect), canonicalToSql(from.type, dialect)];
|
|
113
|
+
return [
|
|
114
|
+
{
|
|
115
|
+
type: 'type_mismatch',
|
|
116
|
+
severity: isBreaking ? 'critical' : 'warning',
|
|
117
|
+
table,
|
|
118
|
+
column,
|
|
119
|
+
expected,
|
|
120
|
+
actual,
|
|
121
|
+
details: `Type mismatch for "${column}": expected ${expected}, got ${actual}`,
|
|
122
|
+
suggestion: isBreaking ? 'Data truncation risk! Create migration to fix.' : 'Create migration to align types',
|
|
123
|
+
},
|
|
124
|
+
];
|
|
115
125
|
}
|
|
116
|
-
|
|
117
|
-
|
|
126
|
+
if (facet === 'nullable') {
|
|
127
|
+
return [
|
|
128
|
+
{
|
|
129
|
+
type: 'constraint_mismatch',
|
|
130
|
+
severity: 'warning',
|
|
131
|
+
table,
|
|
132
|
+
column,
|
|
133
|
+
expected: to.nullable ? 'NULLABLE' : 'NOT NULL',
|
|
134
|
+
actual: from.nullable ? 'NULLABLE' : 'NOT NULL',
|
|
135
|
+
details: `Nullable mismatch for "${column}"`,
|
|
136
|
+
suggestion: 'Align nullable setting in entity or database',
|
|
137
|
+
},
|
|
138
|
+
];
|
|
118
139
|
}
|
|
119
|
-
|
|
120
|
-
|
|
140
|
+
return [
|
|
141
|
+
{
|
|
142
|
+
type: 'constraint_mismatch',
|
|
143
|
+
severity: 'info',
|
|
144
|
+
table,
|
|
145
|
+
column,
|
|
146
|
+
expected: shownDefault(to.defaultValue),
|
|
147
|
+
actual: shownDefault(from.defaultValue),
|
|
148
|
+
details: `Default mismatch for "${column}"`,
|
|
149
|
+
suggestion: 'Align the default in the entity or the database',
|
|
150
|
+
},
|
|
151
|
+
];
|
|
152
|
+
});
|
|
121
153
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const expectedType = canonicalToSql(colDiff.to.type, opts.dialect);
|
|
128
|
-
const actualType = canonicalToSql(colDiff.from.type, opts.dialect);
|
|
129
|
-
drifts.push({
|
|
130
|
-
type: 'type_mismatch',
|
|
131
|
-
severity: colDiff.isBreaking ? 'critical' : 'warning',
|
|
132
|
-
table: colDiff.table,
|
|
133
|
-
column: colDiff.column,
|
|
134
|
-
expected: expectedType,
|
|
135
|
-
actual: actualType,
|
|
136
|
-
details: `Type mismatch for "${colDiff.column}": expected ${expectedType}, got ${actualType}`,
|
|
137
|
-
suggestion: colDiff.isBreaking
|
|
138
|
-
? 'Data truncation risk! Create migration to fix.'
|
|
139
|
-
: 'Create migration to align types',
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
if (opts.checkNullable && changed.includes('nullable')) {
|
|
143
|
-
drifts.push({
|
|
144
|
-
type: 'constraint_mismatch',
|
|
154
|
+
function indexDrift(change) {
|
|
155
|
+
const { table, name: index } = change;
|
|
156
|
+
if (change.from === undefined) {
|
|
157
|
+
return {
|
|
158
|
+
type: 'missing_index',
|
|
145
159
|
severity: 'warning',
|
|
146
|
-
table
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
suggestion: 'Align nullable setting in entity or database',
|
|
152
|
-
});
|
|
160
|
+
table,
|
|
161
|
+
index,
|
|
162
|
+
details: `Index "${index}" expected but not found in database`,
|
|
163
|
+
suggestion: 'Create index via migration',
|
|
164
|
+
};
|
|
153
165
|
}
|
|
154
|
-
if (
|
|
155
|
-
|
|
156
|
-
type: '
|
|
166
|
+
if (change.to === undefined) {
|
|
167
|
+
return {
|
|
168
|
+
type: 'unexpected_index',
|
|
157
169
|
severity: 'info',
|
|
158
|
-
table
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
suggestion: 'Align the default in the entity or the database',
|
|
164
|
-
});
|
|
170
|
+
table,
|
|
171
|
+
index,
|
|
172
|
+
details: `Index "${index}" exists in database but not defined in entity`,
|
|
173
|
+
suggestion: 'Declare it, or drop it via migration: generate:entities drops one uql named',
|
|
174
|
+
};
|
|
165
175
|
}
|
|
176
|
+
// No `expected`/`actual`: the CLI prints those by interpolation, where an `IndexNode` renders as
|
|
177
|
+
// `[object Object]`. What differs is already spelled out in `description`.
|
|
178
|
+
return {
|
|
179
|
+
type: 'index_mismatch',
|
|
180
|
+
severity: 'warning',
|
|
181
|
+
table,
|
|
182
|
+
index,
|
|
183
|
+
details: `Index "${index}" differs from the entity (${change.description})`,
|
|
184
|
+
suggestion: 'Recreate it via migration, which generate:entities writes',
|
|
185
|
+
};
|
|
166
186
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
index: idxDiff.name,
|
|
179
|
-
details: `Index "${idxDiff.name}" expected but not found in database`,
|
|
180
|
-
suggestion: 'Create index via migration',
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
else if (idxDiff.to === undefined) {
|
|
184
|
-
drifts.push({
|
|
185
|
-
type: 'unexpected_index',
|
|
186
|
-
severity: 'info',
|
|
187
|
-
table: idxDiff.table,
|
|
188
|
-
index: idxDiff.name,
|
|
189
|
-
details: `Index "${idxDiff.name}" exists in database but not defined in entity`,
|
|
190
|
-
suggestion: 'Declare it, or drop it via migration: generate:entities drops one uql named',
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
// No `expected`/`actual` here: the CLI prints those by interpolation, where an `IndexNode`
|
|
195
|
-
// renders as `[object Object]`. What differs is already spelled out in `description`.
|
|
196
|
-
drifts.push({
|
|
197
|
-
type: 'index_mismatch',
|
|
198
|
-
severity: 'warning',
|
|
199
|
-
table: idxDiff.table,
|
|
200
|
-
index: idxDiff.name,
|
|
201
|
-
details: `Index "${idxDiff.name}" differs from the entity (${idxDiff.description})`,
|
|
202
|
-
suggestion: 'Recreate it via migration, which generate:entities writes',
|
|
203
|
-
});
|
|
204
|
-
}
|
|
187
|
+
function relationshipDrift(change) {
|
|
188
|
+
const { fromTable: table, name: relationship } = change;
|
|
189
|
+
if (change.from === undefined) {
|
|
190
|
+
return {
|
|
191
|
+
type: 'missing_relationship',
|
|
192
|
+
severity: 'warning',
|
|
193
|
+
table,
|
|
194
|
+
relationship,
|
|
195
|
+
details: `FK "${relationship}" expected but not found in database`,
|
|
196
|
+
suggestion: 'Add FK constraint or remove relation from entity',
|
|
197
|
+
};
|
|
205
198
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
drifts.push({
|
|
216
|
-
type: 'missing_relationship',
|
|
217
|
-
severity: 'warning',
|
|
218
|
-
table: relDiff.fromTable,
|
|
219
|
-
relationship: relDiff.name,
|
|
220
|
-
details: `FK "${relDiff.name}" expected but not found in database`,
|
|
221
|
-
suggestion: 'Add FK constraint or remove relation from entity',
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
else if (relDiff.to === undefined) {
|
|
225
|
-
drifts.push({
|
|
226
|
-
type: 'unexpected_relationship',
|
|
227
|
-
severity: 'info',
|
|
228
|
-
table: relDiff.fromTable,
|
|
229
|
-
relationship: relDiff.name,
|
|
230
|
-
details: `FK "${relDiff.name}" exists in database but not in entity`,
|
|
231
|
-
suggestion: 'Add relation to entity or drop FK',
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
else {
|
|
235
|
-
drifts.push({
|
|
236
|
-
type: 'relationship_mismatch',
|
|
237
|
-
severity: 'warning',
|
|
238
|
-
table: relDiff.fromTable,
|
|
239
|
-
relationship: relDiff.name,
|
|
240
|
-
expected: formatActions(relDiff.to),
|
|
241
|
-
actual: formatActions(relDiff.from),
|
|
242
|
-
details: `FK "${relDiff.name}" has other referential actions in the database than in the entity`,
|
|
243
|
-
suggestion: 'Generate a migration, which drops and re-adds the constraint',
|
|
244
|
-
});
|
|
245
|
-
}
|
|
199
|
+
if (change.to === undefined) {
|
|
200
|
+
return {
|
|
201
|
+
type: 'unexpected_relationship',
|
|
202
|
+
severity: 'info',
|
|
203
|
+
table,
|
|
204
|
+
relationship,
|
|
205
|
+
details: `FK "${relationship}" exists in database but not in entity`,
|
|
206
|
+
suggestion: 'Add relation to entity or drop FK',
|
|
207
|
+
};
|
|
246
208
|
}
|
|
247
|
-
return
|
|
209
|
+
return {
|
|
210
|
+
type: 'relationship_mismatch',
|
|
211
|
+
severity: 'warning',
|
|
212
|
+
table,
|
|
213
|
+
relationship,
|
|
214
|
+
expected: formatActions(change.to),
|
|
215
|
+
actual: formatActions(change.from),
|
|
216
|
+
details: `FK "${relationship}" has other referential actions in the database than in the entity`,
|
|
217
|
+
suggestion: 'Generate a migration, which drops and re-adds the constraint',
|
|
218
|
+
};
|
|
248
219
|
}
|
|
249
220
|
/** A default as a report shows it: SQL as its SQL, a JSON document as JSON rather than `[object Object]`. */
|
|
250
221
|
function shownDefault(value) {
|
|
@@ -255,24 +226,3 @@ function formatActions(rel) {
|
|
|
255
226
|
const { onDelete, onUpdate } = referentialActions(rel);
|
|
256
227
|
return `ON DELETE ${onDelete} ON UPDATE ${onUpdate}`;
|
|
257
228
|
}
|
|
258
|
-
/**
|
|
259
|
-
* Calculate overall status based on drifts.
|
|
260
|
-
*/
|
|
261
|
-
function calculateStatus(drifts) {
|
|
262
|
-
if (drifts.length === 0)
|
|
263
|
-
return 'in_sync';
|
|
264
|
-
const hasCritical = drifts.some((d) => d.severity === 'critical');
|
|
265
|
-
if (hasCritical)
|
|
266
|
-
return 'critical';
|
|
267
|
-
return 'drifted';
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* Create a summary of drifts by severity.
|
|
271
|
-
*/
|
|
272
|
-
function createSummary(drifts) {
|
|
273
|
-
return {
|
|
274
|
-
critical: drifts.filter((d) => d.severity === 'critical').length,
|
|
275
|
-
warning: drifts.filter((d) => d.severity === 'warning').length,
|
|
276
|
-
info: drifts.filter((d) => d.severity === 'info').length,
|
|
277
|
-
};
|
|
278
|
-
}
|
|
@@ -1,6 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Drift Detection Module
|
|
3
|
-
*
|
|
4
|
-
* Detects schema drift between expected and actual database schemas.
|
|
5
|
-
*/
|
|
6
|
-
export { type DriftDetectorOptions, detectDrift } from './driftDetector.js';
|
|
1
|
+
export { type Drift, type DriftDetectorOptions, type DriftReport, type DriftSeverity, type DriftStatus, type DriftType, detectDrift, } from './driftDetector.js';
|
|
@@ -9,8 +9,8 @@ import type { ColumnDefinition, FullColumnDefinition, IndexDefinition, TableDefi
|
|
|
9
9
|
* `render`. Free functions and not generator methods: the dialect reaches them only through `render`.
|
|
10
10
|
*/
|
|
11
11
|
export declare function tableDefinitionToNode(def: TableDefinition, render: (sql: QueryRaw) => string): TableNode;
|
|
12
|
-
/** A builder's column without its `index` and `
|
|
13
|
-
export declare function bareColumn({ index: _index, foreignKey: _foreignKey, ...column }: FullColumnDefinition): ColumnDefinition;
|
|
12
|
+
/** A builder's column without its `index`, `foreignKey` and `enum`, which are lifted onto the table. */
|
|
13
|
+
export declare function bareColumn({ index: _index, foreignKey: _foreignKey, enum: _enum, ...column }: FullColumnDefinition): ColumnDefinition;
|
|
14
14
|
/**
|
|
15
15
|
* The index a column-level `index` or `unique` declares, or nothing: a unique column is a unique index.
|
|
16
16
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createTableNode, keyOfColumns } from '../../schema/schemaAST.js';
|
|
2
|
-
import {
|
|
2
|
+
import { declaredIndexName, enumCheck, normalizeIndexColumn, renderIndexColumn, } from '../../util/ddlExpression.util.js';
|
|
3
3
|
import { derivedIndexName, splitQualifiedName } from '../../util/sql.util.js';
|
|
4
4
|
/**
|
|
5
5
|
* A migration builder's table definition as the AST nodes the generators render from, so a hand-written
|
|
@@ -16,8 +16,9 @@ export function tableDefinitionToNode(def, render) {
|
|
|
16
16
|
};
|
|
17
17
|
const { columns } = table;
|
|
18
18
|
for (const colDef of def.columns) {
|
|
19
|
-
columns.set(colDef.name, { ...bareColumn(colDef), table
|
|
19
|
+
columns.set(colDef.name, { ...bareColumn(colDef), table });
|
|
20
20
|
}
|
|
21
|
+
table.checks.push(...def.columns.flatMap((column) => enumCheck(name, column, render)));
|
|
21
22
|
// A declared key keeps only the columns the table has, in its own order.
|
|
22
23
|
table.primaryKey = def.primaryKey
|
|
23
24
|
? { columns: def.primaryKey.filter((name) => columns.has(name)) }
|
|
@@ -27,8 +28,8 @@ export function tableDefinitionToNode(def, render) {
|
|
|
27
28
|
}
|
|
28
29
|
return table;
|
|
29
30
|
}
|
|
30
|
-
/** A builder's column without its `index` and `
|
|
31
|
-
export function bareColumn({ index: _index, foreignKey: _foreignKey, ...column }) {
|
|
31
|
+
/** A builder's column without its `index`, `foreignKey` and `enum`, which are lifted onto the table. */
|
|
32
|
+
export function bareColumn({ index: _index, foreignKey: _foreignKey, enum: _enum, ...column }) {
|
|
32
33
|
return column;
|
|
33
34
|
}
|
|
34
35
|
/**
|
|
@@ -56,7 +57,7 @@ export function indexDefinition(tableName, columns, { name, unique = false, ...o
|
|
|
56
57
|
const entries = columns.map(normalizeIndexColumn);
|
|
57
58
|
return {
|
|
58
59
|
...options,
|
|
59
|
-
name: name
|
|
60
|
+
name: declaredIndexName(name, tableName, entries, uniqueName),
|
|
60
61
|
entries,
|
|
61
62
|
unique,
|
|
62
63
|
};
|