uql-orm 0.97.0 → 0.99.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 +57 -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 +51 -168
- package/dist/migrate/codegen/fieldOptionsSource.js +3 -6
- 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 +139 -205
- package/dist/migrate/drift/index.d.ts +1 -6
- package/dist/migrate/drift/index.js +1 -6
- package/dist/migrate/generator/definitionToNode.js +3 -3
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +53 -76
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +68 -90
- package/dist/migrate/introspection/mssqlIntrospector.d.ts +8 -43
- package/dist/migrate/introspection/mssqlIntrospector.js +44 -77
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +11 -38
- package/dist/migrate/introspection/mysqlIntrospector.js +48 -57
- package/dist/migrate/introspection/postgresIntrospector.d.ts +12 -60
- package/dist/migrate/introspection/postgresIntrospector.js +62 -77
- package/dist/migrate/introspection/sqliteIntrospector.d.ts +14 -53
- package/dist/migrate/introspection/sqliteIntrospector.js +59 -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 +14 -37
- package/dist/migrate/migrator.js +89 -126
- package/dist/migrate/schemaChange.d.ts +0 -2
- package/dist/migrate/schemaChange.js +1 -1
- package/dist/migrate/schemaGenerator.d.ts +22 -97
- package/dist/migrate/schemaGenerator.js +113 -298
- 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 +3 -3
- package/dist/migrate/triggerSql.js +5 -4
- 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 +0 -3
- package/dist/mongo/mongoIntrospector.js +7 -14
- package/dist/mongo/mongoSchemaGenerator.d.ts +7 -20
- package/dist/mongo/mongoSchemaGenerator.js +51 -70
- 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 +4 -12
- package/dist/schema/schemaASTBuilder.d.ts +2 -5
- package/dist/schema/schemaASTBuilder.js +11 -14
- package/dist/schema/schemaASTDiffer.d.ts +4 -5
- package/dist/schema/schemaASTDiffer.js +22 -70
- package/dist/schema/types.d.ts +1 -48
- 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 +3 -54
- package/dist/util/ddlExpression.util.d.ts +2 -2
- package/dist/util/ddlExpression.util.js +3 -3
- package/dist/util/sql.util.d.ts +0 -8
- package/dist/util/sql.util.js +7 -12
- package/package.json +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,52 +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),
|
|
43
16
|
...detectOwnedDrifts('Trigger', diff.triggers),
|
|
44
|
-
...
|
|
17
|
+
...diff.relationships.map(relationshipDrift),
|
|
45
18
|
];
|
|
19
|
+
const count = (severity) => drifts.filter((drift) => drift.severity === severity).length;
|
|
20
|
+
const summary = { critical: count('critical'), warning: count('warning'), info: count('info') };
|
|
46
21
|
return {
|
|
47
|
-
status:
|
|
22
|
+
status: drifts.length === 0 ? 'in_sync' : summary.critical ? 'critical' : 'drifted',
|
|
48
23
|
drifts,
|
|
49
|
-
summary
|
|
24
|
+
summary,
|
|
50
25
|
generatedAt: new Date(),
|
|
51
26
|
};
|
|
52
27
|
}
|
|
@@ -83,11 +58,8 @@ function detectOwnedDrifts(kind, changes) {
|
|
|
83
58
|
function keyColumns(key) {
|
|
84
59
|
return key?.columns.join(', ') || 'none';
|
|
85
60
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
*/
|
|
89
|
-
function detectTableDrifts(diff) {
|
|
90
|
-
return diff.tables.map((change) => change.from === undefined
|
|
61
|
+
function tableDrift(change) {
|
|
62
|
+
return change.from === undefined
|
|
91
63
|
? {
|
|
92
64
|
type: 'missing_table',
|
|
93
65
|
severity: 'critical',
|
|
@@ -101,166 +73,149 @@ function detectTableDrifts(diff) {
|
|
|
101
73
|
table: change.from.name,
|
|
102
74
|
details: `Table "${change.from.name}" exists in database but no matching entity`,
|
|
103
75
|
suggestion: 'Create entity or drop table',
|
|
104
|
-
}
|
|
76
|
+
};
|
|
105
77
|
}
|
|
106
|
-
/**
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (colDiff.from === undefined) {
|
|
113
|
-
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
|
+
{
|
|
114
84
|
type: 'missing_column',
|
|
115
85
|
severity: 'critical',
|
|
116
|
-
table
|
|
117
|
-
column
|
|
118
|
-
details: `Column "${
|
|
86
|
+
table,
|
|
87
|
+
column,
|
|
88
|
+
details: `Column "${column}" expected but not found in database`,
|
|
119
89
|
suggestion: 'Run migration to add column',
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
90
|
+
},
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
if (change.to === undefined) {
|
|
94
|
+
return [
|
|
95
|
+
{
|
|
124
96
|
type: 'unexpected_column',
|
|
125
97
|
severity: 'warning',
|
|
126
|
-
table
|
|
127
|
-
column
|
|
128
|
-
details: `Column "${
|
|
98
|
+
table,
|
|
99
|
+
column,
|
|
100
|
+
details: `Column "${column}" exists in database but not in entity`,
|
|
129
101
|
suggestion: 'Add to entity or create migration to drop',
|
|
130
|
-
}
|
|
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
|
+
];
|
|
131
125
|
}
|
|
132
|
-
|
|
133
|
-
|
|
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
|
+
];
|
|
134
139
|
}
|
|
135
|
-
|
|
136
|
-
|
|
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
|
+
});
|
|
137
153
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const expectedType = canonicalToSql(colDiff.to.type, opts.dialect);
|
|
144
|
-
const actualType = canonicalToSql(colDiff.from.type, opts.dialect);
|
|
145
|
-
drifts.push({
|
|
146
|
-
type: 'type_mismatch',
|
|
147
|
-
severity: colDiff.isBreaking ? 'critical' : 'warning',
|
|
148
|
-
table: colDiff.table,
|
|
149
|
-
column: colDiff.column,
|
|
150
|
-
expected: expectedType,
|
|
151
|
-
actual: actualType,
|
|
152
|
-
details: `Type mismatch for "${colDiff.column}": expected ${expectedType}, got ${actualType}`,
|
|
153
|
-
suggestion: colDiff.isBreaking
|
|
154
|
-
? 'Data truncation risk! Create migration to fix.'
|
|
155
|
-
: 'Create migration to align types',
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
if (opts.checkNullable && changed.includes('nullable')) {
|
|
159
|
-
drifts.push({
|
|
160
|
-
type: 'constraint_mismatch',
|
|
154
|
+
function indexDrift(change) {
|
|
155
|
+
const { table, name: index } = change;
|
|
156
|
+
if (change.from === undefined) {
|
|
157
|
+
return {
|
|
158
|
+
type: 'missing_index',
|
|
161
159
|
severity: 'warning',
|
|
162
|
-
table
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
suggestion: 'Align nullable setting in entity or database',
|
|
168
|
-
});
|
|
160
|
+
table,
|
|
161
|
+
index,
|
|
162
|
+
details: `Index "${index}" expected but not found in database`,
|
|
163
|
+
suggestion: 'Create index via migration',
|
|
164
|
+
};
|
|
169
165
|
}
|
|
170
|
-
if (
|
|
171
|
-
|
|
172
|
-
type: '
|
|
166
|
+
if (change.to === undefined) {
|
|
167
|
+
return {
|
|
168
|
+
type: 'unexpected_index',
|
|
173
169
|
severity: 'info',
|
|
174
|
-
table
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
suggestion: 'Align the default in the entity or the database',
|
|
180
|
-
});
|
|
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
|
+
};
|
|
181
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
|
+
};
|
|
182
186
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
index: idxDiff.name,
|
|
195
|
-
details: `Index "${idxDiff.name}" expected but not found in database`,
|
|
196
|
-
suggestion: 'Create index via migration',
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
else if (idxDiff.to === undefined) {
|
|
200
|
-
drifts.push({
|
|
201
|
-
type: 'unexpected_index',
|
|
202
|
-
severity: 'info',
|
|
203
|
-
table: idxDiff.table,
|
|
204
|
-
index: idxDiff.name,
|
|
205
|
-
details: `Index "${idxDiff.name}" exists in database but not defined in entity`,
|
|
206
|
-
suggestion: 'Declare it, or drop it via migration: generate:entities drops one uql named',
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
// No `expected`/`actual` here: the CLI prints those by interpolation, where an `IndexNode`
|
|
211
|
-
// renders as `[object Object]`. What differs is already spelled out in `description`.
|
|
212
|
-
drifts.push({
|
|
213
|
-
type: 'index_mismatch',
|
|
214
|
-
severity: 'warning',
|
|
215
|
-
table: idxDiff.table,
|
|
216
|
-
index: idxDiff.name,
|
|
217
|
-
details: `Index "${idxDiff.name}" differs from the entity (${idxDiff.description})`,
|
|
218
|
-
suggestion: 'Recreate it via migration, which generate:entities writes',
|
|
219
|
-
});
|
|
220
|
-
}
|
|
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
|
+
};
|
|
221
198
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
drifts.push({
|
|
232
|
-
type: 'missing_relationship',
|
|
233
|
-
severity: 'warning',
|
|
234
|
-
table: relDiff.fromTable,
|
|
235
|
-
relationship: relDiff.name,
|
|
236
|
-
details: `FK "${relDiff.name}" expected but not found in database`,
|
|
237
|
-
suggestion: 'Add FK constraint or remove relation from entity',
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
else if (relDiff.to === undefined) {
|
|
241
|
-
drifts.push({
|
|
242
|
-
type: 'unexpected_relationship',
|
|
243
|
-
severity: 'info',
|
|
244
|
-
table: relDiff.fromTable,
|
|
245
|
-
relationship: relDiff.name,
|
|
246
|
-
details: `FK "${relDiff.name}" exists in database but not in entity`,
|
|
247
|
-
suggestion: 'Add relation to entity or drop FK',
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
else {
|
|
251
|
-
drifts.push({
|
|
252
|
-
type: 'relationship_mismatch',
|
|
253
|
-
severity: 'warning',
|
|
254
|
-
table: relDiff.fromTable,
|
|
255
|
-
relationship: relDiff.name,
|
|
256
|
-
expected: formatActions(relDiff.to),
|
|
257
|
-
actual: formatActions(relDiff.from),
|
|
258
|
-
details: `FK "${relDiff.name}" has other referential actions in the database than in the entity`,
|
|
259
|
-
suggestion: 'Generate a migration, which drops and re-adds the constraint',
|
|
260
|
-
});
|
|
261
|
-
}
|
|
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
|
+
};
|
|
262
208
|
}
|
|
263
|
-
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
|
+
};
|
|
264
219
|
}
|
|
265
220
|
/** A default as a report shows it: SQL as its SQL, a JSON document as JSON rather than `[object Object]`. */
|
|
266
221
|
function shownDefault(value) {
|
|
@@ -271,24 +226,3 @@ function formatActions(rel) {
|
|
|
271
226
|
const { onDelete, onUpdate } = referentialActions(rel);
|
|
272
227
|
return `ON DELETE ${onDelete} ON UPDATE ${onUpdate}`;
|
|
273
228
|
}
|
|
274
|
-
/**
|
|
275
|
-
* Calculate overall status based on drifts.
|
|
276
|
-
*/
|
|
277
|
-
function calculateStatus(drifts) {
|
|
278
|
-
if (drifts.length === 0)
|
|
279
|
-
return 'in_sync';
|
|
280
|
-
const hasCritical = drifts.some((d) => d.severity === 'critical');
|
|
281
|
-
if (hasCritical)
|
|
282
|
-
return 'critical';
|
|
283
|
-
return 'drifted';
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* Create a summary of drifts by severity.
|
|
287
|
-
*/
|
|
288
|
-
function createSummary(drifts) {
|
|
289
|
-
return {
|
|
290
|
-
critical: drifts.filter((d) => d.severity === 'critical').length,
|
|
291
|
-
warning: drifts.filter((d) => d.severity === 'warning').length,
|
|
292
|
-
info: drifts.filter((d) => d.severity === 'info').length,
|
|
293
|
-
};
|
|
294
|
-
}
|
|
@@ -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';
|
|
@@ -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,7 +16,7 @@ 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
21
|
table.checks.push(...def.columns.flatMap((column) => enumCheck(name, column, render)));
|
|
22
22
|
// A declared key keeps only the columns the table has, in its own order.
|
|
@@ -57,7 +57,7 @@ export function indexDefinition(tableName, columns, { name, unique = false, ...o
|
|
|
57
57
|
const entries = columns.map(normalizeIndexColumn);
|
|
58
58
|
return {
|
|
59
59
|
...options,
|
|
60
|
-
name: name
|
|
60
|
+
name: declaredIndexName(name, tableName, entries, uniqueName),
|
|
61
61
|
entries,
|
|
62
62
|
unique,
|
|
63
63
|
};
|
|
@@ -1,19 +1,17 @@
|
|
|
1
|
+
import type { AbstractSqlDialect } from '../../dialect/index.js';
|
|
2
|
+
import type { IndexFacet } from '../../schema/indexDifferences.js';
|
|
3
|
+
import type { SchemaAST } from '../../schema/schemaAST.js';
|
|
1
4
|
import { SqlExpression } from '../../schema/sqlExpression.js';
|
|
2
5
|
import type { CheckSchema, TriggerSchema } from '../../schema/types.js';
|
|
3
6
|
import { type ForeignKeyAction } from '../../schema/types.js';
|
|
4
|
-
import type { ColumnSchema, ForeignKeySchema, IndexSchema, PrimaryKeySchema, QuerierPool, RawRow, SchemaIntrospector, SqlQuerier, StoredDefinition, TableSchema } from '../../type/index.js';
|
|
5
|
-
import { BaseSqlIntrospector } from './baseSqlIntrospector.js';
|
|
7
|
+
import type { ColumnRenames, ColumnSchema, Except, ForeignKeySchema, IndexSchema, PrimaryKeySchema, QuerierPool, RawRow, SchemaIntrospector, SqlQuerier, StoredDefinition, TableSchema } from '../../type/index.js';
|
|
6
8
|
/**
|
|
7
|
-
* Reads the rows of one statement while introspecting a table
|
|
8
|
-
*
|
|
9
|
-
* Identical statements share a single round trip, which is what the mappers get instead of a querier:
|
|
10
|
-
* describing one table needs four facts, and on SQLite three of them come out of the same two PRAGMAs
|
|
11
|
-
* (`table_info` is both the column list and the primary key; the column mapper walks `index_list` and
|
|
12
|
-
* `index_info` for single-column uniqueness while the index mapper is walking them too). That was four
|
|
13
|
-
* redundant statements out of ten per table - on D1 and Turso, where every PRAGMA is an HTTP round trip,
|
|
14
|
-
* it is four avoidable ones.
|
|
9
|
+
* Reads the rows of one statement while introspecting a table, an identical statement sent once: SQLite's
|
|
10
|
+
* `table_info` is both the column list and the key, and on D1 and Turso every PRAGMA is a round trip.
|
|
15
11
|
*/
|
|
16
12
|
export type TableRowReader = <T extends RawRow>(sql: string, params?: unknown[]) => Promise<T[]>;
|
|
13
|
+
/** A column as its engine's catalogue reads it; the key and the indexes say which it belongs to. */
|
|
14
|
+
export type ReadColumn = Except<ColumnSchema, 'isPrimaryKey' | 'isUnique'>;
|
|
17
15
|
/** A foreign key as MySQL and SQL Server list one: a row, its column lists comma-joined. */
|
|
18
16
|
export type JoinedForeignKeyRow = {
|
|
19
17
|
readonly constraint_name: string;
|
|
@@ -23,10 +21,19 @@ export type JoinedForeignKeyRow = {
|
|
|
23
21
|
readonly delete_rule: string;
|
|
24
22
|
readonly update_rule: string;
|
|
25
23
|
};
|
|
26
|
-
/** A SQL introspector: an engine
|
|
27
|
-
export declare abstract class AbstractSqlSchemaIntrospector
|
|
24
|
+
/** A SQL introspector: an engine reads each part of a table from its catalogue (`get*`), on a {@link TableRowReader}. */
|
|
25
|
+
export declare abstract class AbstractSqlSchemaIntrospector implements SchemaIntrospector {
|
|
28
26
|
protected readonly pool: QuerierPool;
|
|
29
|
-
|
|
27
|
+
readonly schema?: string | undefined;
|
|
28
|
+
/** Columns and uniqueness only; each introspector opts in to what its catalogue queries report. */
|
|
29
|
+
protected readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
30
|
+
protected readonly dialect: AbstractSqlDialect;
|
|
31
|
+
/**
|
|
32
|
+
* `schema` is the one these queries read, `undefined` for the connection's own default. Every table
|
|
33
|
+
* reported is stamped with it, so a diff compares like with like: entity and database both say
|
|
34
|
+
* `undefined` for "wherever the connection points", and name a schema only when one was asked for.
|
|
35
|
+
*/
|
|
36
|
+
constructor(pool: QuerierPool, schema?: string | undefined);
|
|
30
37
|
/**
|
|
31
38
|
* The schema every catalogue query filters on, as SQL: the one that was asked for, as the engine's
|
|
32
39
|
* own literal, or its expression for the connection's default. A literal rather than a bind
|
|
@@ -38,86 +45,56 @@ export declare abstract class AbstractSqlSchemaIntrospector extends BaseSqlIntro
|
|
|
38
45
|
* an engine with no schemas, whose catalogue queries never reference one.
|
|
39
46
|
*/
|
|
40
47
|
protected readonly defaultSchemaExpr: string;
|
|
41
|
-
getTableSchema(tableName: string): Promise<TableSchema | undefined>;
|
|
42
|
-
/** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
|
|
43
|
-
protected getDefinition(_read: TableRowReader, _tableName: string): Promise<StoredDefinition[] | undefined>;
|
|
44
|
-
getTableNames(): Promise<string[]>;
|
|
45
48
|
/**
|
|
46
|
-
* The
|
|
47
|
-
*
|
|
49
|
+
* The database as a {@link SchemaAST}, or just the tables named. A name nothing matches is left out
|
|
50
|
+
* rather than raised: the point of naming them is to read a database other things are still
|
|
51
|
+
* changing, where scanning every table is both wasted work and a relation that can vanish mid-scan.
|
|
48
52
|
*/
|
|
49
|
-
|
|
53
|
+
introspect(tables?: readonly string[], renames?: ColumnRenames): Promise<SchemaAST>;
|
|
54
|
+
getTableSchema(tableName: string): Promise<TableSchema | undefined>;
|
|
55
|
+
getTableNames(): Promise<string[]>;
|
|
50
56
|
tableExists(tableName: string): Promise<boolean>;
|
|
51
|
-
/**
|
|
52
|
-
* Introspection reads, so `withQuerier` rather than `transaction`: the pool owns the release either
|
|
53
|
-
* way, and wrapping catalogue queries in a transaction would hold one open for nothing.
|
|
54
|
-
*/
|
|
57
|
+
/** On the querier migrations run on, which is not the app's on a libSQL embedded replica. */
|
|
55
58
|
protected withSqlQuerier<T>(task: (querier: SqlQuerier) => Promise<T>): Promise<T>;
|
|
59
|
+
/** Whether the base table exists: its query answers a row for it, none for a view or nothing. */
|
|
56
60
|
protected tableExistsInternal(read: TableRowReader, tableName: string): Promise<boolean>;
|
|
57
|
-
|
|
58
|
-
protected getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
|
|
59
|
-
protected getForeignKeys(read: TableRowReader, tableName: string): Promise<ForeignKeySchema[]>;
|
|
60
|
-
protected getPrimaryKey(read: TableRowReader, tableName: string): Promise<PrimaryKeySchema | undefined>;
|
|
61
|
-
protected tableExistsParams(tableName: string): unknown[];
|
|
62
|
-
protected getColumnsParams(tableName: string): unknown[];
|
|
63
|
-
protected getIndexesParams(tableName: string): unknown[];
|
|
64
|
-
protected getForeignKeysParams(tableName: string): unknown[];
|
|
65
|
-
protected getPrimaryKeyParams(tableName: string): unknown[];
|
|
66
|
-
/** The {@link ForeignKeyAction} a catalogue names, whatever its case, and `SET_NULL` as SQL Server spells it. */
|
|
67
|
-
protected normalizeReferentialAction(action: string): ForeignKeyAction | undefined;
|
|
68
|
-
/** Foreign keys read one row each, as {@link JoinedForeignKeyRow} lists them. */
|
|
69
|
-
protected joinedForeignKeys(rows: readonly JoinedForeignKeyRow[]): ForeignKeySchema[];
|
|
70
|
-
/**
|
|
71
|
-
* Convert bigint/null values to number safely.
|
|
72
|
-
*/
|
|
73
|
-
protected toNumber(value: unknown): number | undefined;
|
|
74
|
-
/** SQL query to list all table names. */
|
|
61
|
+
/** SQL listing the base tables' names, as `table_name`. */
|
|
75
62
|
protected abstract getTableNamesQuery(): string;
|
|
76
|
-
/** SQL
|
|
63
|
+
/** SQL answering a row where the base table its one parameter names exists. */
|
|
77
64
|
protected abstract tableExistsQuery(): string;
|
|
78
|
-
/**
|
|
79
|
-
protected abstract
|
|
80
|
-
/**
|
|
81
|
-
protected abstract
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
protected abstract getForeignKeysQuery(tableName: string): string;
|
|
86
|
-
/** SQL query to get primary key columns. Parameter: tableName (for PRAGMA-style). */
|
|
87
|
-
protected abstract getPrimaryKeyQuery(tableName: string): string;
|
|
65
|
+
/** The table's columns, in their order. */
|
|
66
|
+
protected abstract getColumns(read: TableRowReader, tableName: string): Promise<ReadColumn[]>;
|
|
67
|
+
/** The table's indexes, but its key's own. */
|
|
68
|
+
protected abstract getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
|
|
69
|
+
protected abstract getForeignKeys(read: TableRowReader, tableName: string): Promise<ForeignKeySchema[]>;
|
|
70
|
+
/** The table's key, its columns in key order, or `undefined` for a table without one. */
|
|
71
|
+
protected abstract getPrimaryKey(read: TableRowReader, tableName: string): Promise<PrimaryKeySchema | undefined>;
|
|
88
72
|
/** Every check on the table, each `expression` as the engine reprints it, which is what a rollback restores. */
|
|
89
73
|
protected abstract getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
|
|
74
|
+
/**
|
|
75
|
+
* The triggers uql installed on the table, each with the statements recreating it as the engine keeps it.
|
|
76
|
+
* Ownership is matched here rather than with `LIKE`, whose `_` wildcard would take in a hand-written one.
|
|
77
|
+
*/
|
|
78
|
+
protected getTriggers(read: TableRowReader, tableName: string): Promise<TriggerSchema[]>;
|
|
90
79
|
/**
|
|
91
80
|
* SQL listing the triggers on the table named by its single parameter: each one's `name`, its `definition`
|
|
92
81
|
* as the engine reprints it, and what it `requires` to be recreated first. It reads what is installed, not
|
|
93
82
|
* what uql wrote, which is exactly what a rollback restores.
|
|
94
83
|
*/
|
|
95
84
|
protected abstract triggersQuery(): string;
|
|
85
|
+
/** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
|
|
86
|
+
protected getDefinition(_read: TableRowReader, _tableName: string): Promise<StoredDefinition[] | undefined>;
|
|
96
87
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
* Defaults to `information_schema`'s own column, which is what every engine with an
|
|
100
|
-
* `information_schema` returns and what Postgres and MySQL both restated identically. SQLite reads
|
|
101
|
-
* `sqlite_master` instead and overrides.
|
|
102
|
-
*/
|
|
103
|
-
protected mapTableNameRow(row: RawRow): string;
|
|
104
|
-
/** Map column query results to ColumnSchema array. Allows async for SQLite's unique column check. */
|
|
105
|
-
protected abstract mapColumnsResult(read: TableRowReader, tableName: string, results: RawRow[]): Promise<ColumnSchema[]>;
|
|
106
|
-
/** Map index query results to IndexSchema array. Allows async for SQLite's index_info calls. */
|
|
107
|
-
protected abstract mapIndexesResult(read: TableRowReader, tableName: string, results: RawRow[]): Promise<IndexSchema[]>;
|
|
108
|
-
/** Map foreign key query results to ForeignKeySchema array. */
|
|
109
|
-
protected abstract mapForeignKeysResult(read: TableRowReader, tableName: string, results: RawRow[]): Promise<ForeignKeySchema[]>;
|
|
110
|
-
/**
|
|
111
|
-
* Map primary key query results to column names, in key order. `information_schema` gives every SQL
|
|
112
|
-
* engine here a `column_name` per row; SQLite reads its key off `PRAGMA table_info` instead and
|
|
113
|
-
* overrides this.
|
|
114
|
-
*/
|
|
115
|
-
protected mapPrimaryKeyResult(results: RawRow[]): string[] | undefined;
|
|
116
|
-
/**
|
|
117
|
-
* What the engine calls the key's constraint, where the query reported one. Only a `DROP` needs it,
|
|
118
|
-
* and only the reported name will do - see {@link PrimaryKeySchema.name}.
|
|
88
|
+
* The key `sql` lists a row of for each column, in key order: its `column_name`, and the `constraint_name`
|
|
89
|
+
* where the engine names the key's constraint. Only a `DROP` needs that name, and only the reported one will do.
|
|
119
90
|
*/
|
|
120
|
-
protected
|
|
91
|
+
protected readPrimaryKey(read: TableRowReader, sql: string, tableName: string): Promise<PrimaryKeySchema | undefined>;
|
|
92
|
+
/** The {@link ForeignKeyAction} a catalogue names, whatever its case, and `SET_NULL` as SQL Server spells it. */
|
|
93
|
+
protected normalizeReferentialAction(action: string): ForeignKeyAction | undefined;
|
|
94
|
+
/** Foreign keys read one row each, as {@link JoinedForeignKeyRow} lists them. */
|
|
95
|
+
protected joinedForeignKeys(rows: readonly JoinedForeignKeyRow[]): ForeignKeySchema[];
|
|
96
|
+
/** A catalogue's number, which a driver may answer as a `bigint`, or `undefined` for none. */
|
|
97
|
+
protected toNumber(value: unknown): number | undefined;
|
|
121
98
|
/** Parses a default as the catalogue reports it: to its literal value, or through {@link sqlDefault} if SQL. */
|
|
122
99
|
protected abstract parseDefaultValue(defaultValue: string | null): unknown;
|
|
123
100
|
/**
|