uql-orm 0.96.1 → 0.97.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/migrate/builder/types.d.ts +2 -0
- package/dist/migrate/codegen/entityCodeGenerator.js +2 -6
- package/dist/migrate/codegen/fieldOptionsSource.d.ts +1 -3
- package/dist/migrate/codegen/fieldOptionsSource.js +0 -5
- package/dist/migrate/drift/driftDetector.js +16 -0
- package/dist/migrate/generator/definitionToNode.d.ts +2 -2
- package/dist/migrate/generator/definitionToNode.js +4 -3
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +7 -5
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +10 -11
- package/dist/migrate/introspection/baseSqlIntrospector.js +4 -4
- package/dist/migrate/introspection/mssqlIntrospector.d.ts +2 -0
- package/dist/migrate/introspection/mssqlIntrospector.js +8 -0
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +11 -0
- package/dist/migrate/introspection/mysqlIntrospector.js +28 -0
- package/dist/migrate/introspection/postgresIntrospector.d.ts +3 -0
- package/dist/migrate/introspection/postgresIntrospector.js +12 -0
- package/dist/migrate/introspection/sqliteIntrospector.d.ts +5 -0
- package/dist/migrate/introspection/sqliteIntrospector.js +14 -0
- package/dist/migrate/migrator.d.ts +5 -23
- package/dist/migrate/migrator.js +26 -80
- package/dist/migrate/schemaChange.d.ts +8 -3
- package/dist/migrate/schemaChange.js +24 -7
- package/dist/migrate/schemaGenerator.d.ts +20 -19
- package/dist/migrate/schemaGenerator.js +87 -76
- package/dist/migrate/triggerSql.d.ts +6 -8
- package/dist/migrate/triggerSql.js +14 -16
- package/dist/mongo/mongoIntrospector.d.ts +1 -3
- package/dist/mongo/mongoIntrospector.js +0 -4
- package/dist/mongo/mongoSchemaGenerator.d.ts +0 -6
- package/dist/mongo/mongoSchemaGenerator.js +0 -5
- package/dist/schema/schemaAST.js +1 -0
- package/dist/schema/schemaASTBuilder.d.ts +3 -1
- package/dist/schema/schemaASTBuilder.js +6 -4
- package/dist/schema/schemaASTDiffer.js +20 -5
- package/dist/schema/types.d.ts +23 -6
- package/dist/type/migration.d.ts +10 -19
- package/dist/util/ddlExpression.util.d.ts +11 -0
- package/dist/util/ddlExpression.util.js +24 -1
- package/dist/util/sql.util.d.ts +4 -6
- package/dist/util/sql.util.js +2 -6
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +1 -1
package/dist/migrate/migrator.js
CHANGED
|
@@ -4,8 +4,8 @@ import { pathToFileURL } from 'node:url';
|
|
|
4
4
|
import { getEntities, getMeta } from '../entity/index.js';
|
|
5
5
|
import { SchemaAST } from '../schema/index.js';
|
|
6
6
|
import { columnRenames, tableRenameCandidates } from '../schema/schemaASTDiffer.js';
|
|
7
|
-
import { hasTriggers } from '../util/field.util.js';
|
|
8
7
|
import { definedEntries, isRecord, LoggerWrapper } from '../util/index.js';
|
|
8
|
+
import { isOwnedName, qualifyName } from '../util/sql.util.js';
|
|
9
9
|
import { UqlUsageError } from '../util/uqlError.js';
|
|
10
10
|
import { withSqlQuerierForMigrations } from './acquireQuerierForMigrations.js';
|
|
11
11
|
import { buildMigrationModule } from './codegen/migrationFile.js';
|
|
@@ -182,19 +182,19 @@ export class Migrator {
|
|
|
182
182
|
const generator = await this.getSchemaGenerator();
|
|
183
183
|
const { created, altered } = await this.pendingChanges({ renames: true });
|
|
184
184
|
await this.assertFillable(altered);
|
|
185
|
-
const plan = this.alterPlan(generator, altered, await this.installedTriggers(created));
|
|
186
185
|
await this.noteChanges(generator, created, altered);
|
|
187
|
-
const up = [
|
|
186
|
+
const up = [
|
|
187
|
+
...this.createSchema(generator, created),
|
|
188
|
+
...altered.flatMap((diff) => generator.generateAlterTable(diff)),
|
|
189
|
+
];
|
|
188
190
|
if (up.length === 0) {
|
|
189
191
|
this.logger.logInfo('No schema changes detected.');
|
|
190
192
|
return '';
|
|
191
193
|
}
|
|
192
194
|
// Diff by diff in reverse, each rolled back in the order its generator wrote it.
|
|
193
195
|
const down = [
|
|
194
|
-
...
|
|
195
|
-
|
|
196
|
-
...this.createdEntities(created).flatMap((entity) => generator.generateTriggersDown(entity)),
|
|
197
|
-
...created.toReversed().map((tableName) => generator.generateDropTable(tableName, { ifExists: true })),
|
|
196
|
+
...altered.toReversed().flatMap((diff) => generator.generateAlterTable(reverseDiff(diff))),
|
|
197
|
+
...generator.generateDropSchema(this.createdEntities(created), { ifExists: true }),
|
|
198
198
|
];
|
|
199
199
|
const { emit } = this.target.source;
|
|
200
200
|
const filePath = await this.writeMigration(name, {
|
|
@@ -205,70 +205,6 @@ export class Migrator {
|
|
|
205
205
|
this.logger.logInfo(`Created migration from entities: ${filePath}`);
|
|
206
206
|
return filePath;
|
|
207
207
|
}
|
|
208
|
-
/**
|
|
209
|
-
* Each entity on a table this plan does not create - a new one carries its triggers in its `CREATE` -
|
|
210
|
-
* beside the triggers uql has installed there, read once per schema. Kept only where either side has
|
|
211
|
-
* any: one declaring none on a table holding none has nothing to reconcile.
|
|
212
|
-
*/
|
|
213
|
-
async installedTriggers(created, entities = this.entities) {
|
|
214
|
-
const { dialect } = this.pool;
|
|
215
|
-
const fresh = new Set(created);
|
|
216
|
-
// Tables this plan creates are left out: their `CREATE` carries their triggers, and asking the
|
|
217
|
-
// catalogue about a table that is not there yet fails outright on some engines.
|
|
218
|
-
const wanted = entities.filter((entity) => !fresh.has(this.tableOf(entity)));
|
|
219
|
-
const state = [];
|
|
220
|
-
for (const entity of wanted) {
|
|
221
|
-
const meta = getMeta(entity);
|
|
222
|
-
const installed = await this.schemaIntrospectorFor(dialect.resolveSchema(meta)).ownedTriggers(dialect.resolveTableAlias(meta));
|
|
223
|
-
// An installed trigger alone keeps it: an entity that stopped declaring one has it to drop.
|
|
224
|
-
if (installed.size || hasTriggers(meta)) {
|
|
225
|
-
state.push({ entity, installed });
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return state;
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* The alters, with the triggers reconciled around them. Postgres refuses to retype or drop a column a
|
|
232
|
-
* trigger names, so every trigger on a table whose columns change comes off before the alters and what
|
|
233
|
-
* its entity declares goes back on after. `down` is lazy: SQLite cannot express every alter's inverse.
|
|
234
|
-
*/
|
|
235
|
-
alterPlan(generator, altered, state) {
|
|
236
|
-
const changing = new Set(altered
|
|
237
|
-
.filter((diff) => sides(diff.columns, 'from').length || diff.renamedColumns?.length || diff.rebuild)
|
|
238
|
-
.map((diff) => diff.tableName));
|
|
239
|
-
const cleared = state.filter(({ entity }) => changing.has(this.tableOf(entity)));
|
|
240
|
-
const after = state.map((it) => (cleared.includes(it) ? { entity: it.entity, installed: new Map() } : it));
|
|
241
|
-
return {
|
|
242
|
-
up: [
|
|
243
|
-
...cleared.flatMap(({ entity, installed }) => generator.generateTriggerDrops(entity, [...installed.keys()])),
|
|
244
|
-
...altered.flatMap((diff) => [...renameStatements(generator, diff), ...generator.generateAlterTable(diff)]),
|
|
245
|
-
...this.reconcileTriggers(generator, after),
|
|
246
|
-
],
|
|
247
|
-
down: () => [
|
|
248
|
-
...this.revertedTriggers(generator, after),
|
|
249
|
-
...altered.toReversed().flatMap((diff) => {
|
|
250
|
-
const reversed = reverseDiff(diff);
|
|
251
|
-
return [...generator.generateAlterTable(reversed), ...renameStatements(generator, reversed)];
|
|
252
|
-
}),
|
|
253
|
-
...cleared.flatMap(({ installed }) => [...installed.values()].flat().map((sql) => `${sql};`)),
|
|
254
|
-
],
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* Each entity's triggers taken from what the catalogue holds to what it declares. Against the catalogue
|
|
259
|
-
* rather than a diff, because a trigger hangs off a table whose columns may be unchanged: a body edited
|
|
260
|
-
* on a settled table appears in no diff at all.
|
|
261
|
-
*/
|
|
262
|
-
reconcileTriggers(generator, state) {
|
|
263
|
-
return state.flatMap(({ entity, installed }) => generator.generateTriggers(entity, installed));
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* The inverse: what the reconcile created dropped, and what it dropped restored as the engine reprints
|
|
267
|
-
* it. Read off the catalogue rather than recorded by uql, and exactly right for restoring one.
|
|
268
|
-
*/
|
|
269
|
-
revertedTriggers(generator, state) {
|
|
270
|
-
return state.flatMap(({ entity, installed }) => generator.generateTriggersDown(entity, installed));
|
|
271
|
-
}
|
|
272
208
|
/**
|
|
273
209
|
* What a generated migration does that its reader must not miss: each column it drops or retypes, which
|
|
274
210
|
* can lose data, and each table it creates empty while the database holds one no entity names with the
|
|
@@ -354,12 +290,28 @@ export class Migrator {
|
|
|
354
290
|
// Read again under the names the entities give them, so the rest compares as the columns they become.
|
|
355
291
|
ast = await this.introspectEntities(this.entities, renames);
|
|
356
292
|
}
|
|
293
|
+
if (desiredAst) {
|
|
294
|
+
this.noteForeignChecks(desiredAst, ast);
|
|
295
|
+
}
|
|
357
296
|
return this.entities.flatMap((entity) => {
|
|
358
297
|
const tableName = generator.resolveTableName(getMeta(entity));
|
|
359
298
|
const diff = generator.diffSchema(entity, ast.getTable(tableName), desiredAst, renames.get(tableName));
|
|
360
299
|
return diff ? [diff] : [];
|
|
361
300
|
});
|
|
362
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* Each check uql did not install on a table whose entity declares checks: never changed or dropped, so
|
|
304
|
+
* one an owned check replaces, such as an enum's from before checks were named, goes on enforcing itself.
|
|
305
|
+
*/
|
|
306
|
+
noteForeignChecks(desired, actual) {
|
|
307
|
+
for (const table of desired.getTables()) {
|
|
308
|
+
const foreign = (actual.getTable(qualifyName(table.name, table.schema))?.checks ?? []).filter((check) => !isOwnedName(check.name));
|
|
309
|
+
if (table.checks.length && foreign.length) {
|
|
310
|
+
this.logger.logWarn(`"${table.name}" holds checks uql did not install, which it leaves as they are: ` +
|
|
311
|
+
`${foreign.map((check) => check.name).join(', ')}. Drop each one a declared check replaces.`);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
363
315
|
/**
|
|
364
316
|
* The tables `entities` name, read a schema at a time so each is keyed as its entity spells it. Those
|
|
365
317
|
* alone: nothing else is diffed, and another table can be dropped mid-scan by whatever else is running.
|
|
@@ -411,10 +363,9 @@ export class Migrator {
|
|
|
411
363
|
}
|
|
412
364
|
// With the tables it references, which its foreign keys resolve against.
|
|
413
365
|
const ast = await this.introspectEntities([entity, ...referencedEntities(meta)]);
|
|
414
|
-
// The table is already there, so its triggers are reconciled rather than carried by a `CREATE`.
|
|
415
366
|
const altered = this.alterFromEntity(generator, entity, ast.getTable(tableName), options);
|
|
416
367
|
await this.assertFillable(altered);
|
|
417
|
-
return
|
|
368
|
+
return altered.flatMap((diff) => generator.generateAlterTable(diff));
|
|
418
369
|
}
|
|
419
370
|
/** The diff for one entity against the table it already has, and none where the two agree. */
|
|
420
371
|
alterFromEntity(generator, entity, table, options) {
|
|
@@ -449,7 +400,7 @@ export class Migrator {
|
|
|
449
400
|
await this.assertFillable(filtered);
|
|
450
401
|
return [
|
|
451
402
|
...this.createSchema(generator, created),
|
|
452
|
-
...
|
|
403
|
+
...filtered.flatMap((diff) => generator.generateAlterTable(diff)),
|
|
453
404
|
];
|
|
454
405
|
}
|
|
455
406
|
/** The new tables, created together so a foreign key between them, cyclic included, resolves. Empty in, empty out. */
|
|
@@ -504,6 +455,7 @@ export class Migrator {
|
|
|
504
455
|
columns: options.drop ? columns : nonEmpty((columns ?? []).filter((change) => change.to !== undefined)),
|
|
505
456
|
indexes: additive('index', diff.indexes, (index) => index.name),
|
|
506
457
|
foreignKeys: additive('foreign key', diff.foreignKeys, (foreignKey) => constraintNameOf(diff.tableName, foreignKey)),
|
|
458
|
+
checks: additive('check', diff.checks, (check) => check.name),
|
|
507
459
|
};
|
|
508
460
|
if (!diff.rebuild || !held) {
|
|
509
461
|
return filtered;
|
|
@@ -631,9 +583,3 @@ function referencedEntities(meta) {
|
|
|
631
583
|
const fields = definedEntries(meta.fields).flatMap(([, field]) => field.references?.() ?? []);
|
|
632
584
|
return [...fields, ...definedEntries(meta.relations).map(([, relation]) => relation.entity())];
|
|
633
585
|
}
|
|
634
|
-
/** A diff's column renames, through the builder operation every SQL generator already renders. */
|
|
635
|
-
function renameStatements(generator, { tableName, renamedColumns = [], rebuild }) {
|
|
636
|
-
return rebuild
|
|
637
|
-
? []
|
|
638
|
-
: renamedColumns.flatMap(({ from, to }) => generator.generateOperation({ type: 'renameColumn', tableName, oldName: from, newName: to }));
|
|
639
|
-
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Alteration, Change, ColumnSchema, SchemaDiff } from '../type/index.js';
|
|
1
|
+
import type { Alteration, Change, ColumnSchema, IndexColumnSchema, SchemaDiff } from '../type/index.js';
|
|
2
2
|
/** Each change's end on `side`, where it has one: what a drop half removes (`from`), or an add half creates (`to`). */
|
|
3
3
|
export declare function sides<T>(changes: readonly Change<T>[] | undefined, side: 'from' | 'to'): T[];
|
|
4
4
|
/** What the changes add: each `to` with no `from`. */
|
|
@@ -14,7 +14,7 @@ export declare function newlyRequired(changes?: readonly Change<ColumnSchema>[])
|
|
|
14
14
|
}[];
|
|
15
15
|
/** Whether a row already in the table would hold nothing in `column`: required, with no default, and not one the engine fills. */
|
|
16
16
|
export declare function lacksValue(column: ColumnSchema): boolean;
|
|
17
|
-
/** Whether `diff` holds anything an engine that rebuilds tables makes no other way, a key or a
|
|
17
|
+
/** Whether `diff` holds anything an engine that rebuilds tables makes no other way: a key, a foreign key or a check included. */
|
|
18
18
|
export declare function needsRebuild(diff: SchemaDiff): boolean;
|
|
19
19
|
/** `diff` less its rebuild and everything only a rebuild makes: what an `ALTER` can still apply alone. */
|
|
20
20
|
export declare function withoutRebuild(diff: SchemaDiff): SchemaDiff;
|
|
@@ -22,5 +22,10 @@ export declare function withoutRebuild(diff: SchemaDiff): SchemaDiff;
|
|
|
22
22
|
export declare function nonEmpty<T>(items: readonly T[]): readonly T[] | undefined;
|
|
23
23
|
/** `change` undone: an add becomes a drop, a drop an add, and an alter runs the other way. */
|
|
24
24
|
export declare function swap<T>({ from, to }: Change<T>): Change<T>;
|
|
25
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* `diff` undone: every change swapped, which is what a migration's `down` runs. Renames run before the adds
|
|
27
|
+
* either way, so a key, index or foreign key put back names a renamed column as the rename back leaves it.
|
|
28
|
+
*/
|
|
26
29
|
export declare function reverseDiff(diff: SchemaDiff): SchemaDiff;
|
|
30
|
+
/** Each column entry named by `nameOf`; an expression entry is SQL, left as written. */
|
|
31
|
+
export declare function renameIndexEntries(entries: readonly IndexColumnSchema[], nameOf: (column: string) => string): IndexColumnSchema[];
|
|
@@ -29,9 +29,9 @@ export function lacksValue(column) {
|
|
|
29
29
|
function onlyRebuilt({ from, to }) {
|
|
30
30
|
return from === undefined ? Boolean(to?.generatedAs) : to !== undefined;
|
|
31
31
|
}
|
|
32
|
-
/** Whether `diff` holds anything an engine that rebuilds tables makes no other way, a key or a
|
|
32
|
+
/** Whether `diff` holds anything an engine that rebuilds tables makes no other way: a key, a foreign key or a check included. */
|
|
33
33
|
export function needsRebuild(diff) {
|
|
34
|
-
return Boolean(diff.primaryKey || diff.foreignKeys || diff.columns?.some(onlyRebuilt));
|
|
34
|
+
return Boolean(diff.primaryKey || diff.foreignKeys || diff.checks || diff.columns?.some(onlyRebuilt));
|
|
35
35
|
}
|
|
36
36
|
/** `diff` less its rebuild and everything only a rebuild makes: what an `ALTER` can still apply alone. */
|
|
37
37
|
export function withoutRebuild(diff) {
|
|
@@ -39,6 +39,7 @@ export function withoutRebuild(diff) {
|
|
|
39
39
|
...diff,
|
|
40
40
|
primaryKey: undefined,
|
|
41
41
|
foreignKeys: undefined,
|
|
42
|
+
checks: undefined,
|
|
42
43
|
columns: nonEmpty((diff.columns ?? []).filter((change) => !onlyRebuilt(change))),
|
|
43
44
|
rebuild: undefined,
|
|
44
45
|
};
|
|
@@ -51,15 +52,31 @@ export function nonEmpty(items) {
|
|
|
51
52
|
export function swap({ from, to }) {
|
|
52
53
|
return { from: to, to: from };
|
|
53
54
|
}
|
|
54
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
* `diff` undone: every change swapped, which is what a migration's `down` runs. Renames run before the adds
|
|
57
|
+
* either way, so a key, index or foreign key put back names a renamed column as the rename back leaves it.
|
|
58
|
+
*/
|
|
55
59
|
export function reverseDiff(diff) {
|
|
60
|
+
const renamedColumns = diff.renamedColumns?.map(({ from, to }) => ({ from: to, to: from }));
|
|
61
|
+
const renamed = new Map(renamedColumns?.map(({ from, to }) => [from, to]));
|
|
62
|
+
const nameOf = (column) => renamed.get(column) ?? column;
|
|
56
63
|
return {
|
|
57
64
|
...diff,
|
|
58
|
-
primaryKey: diff.primaryKey &&
|
|
65
|
+
primaryKey: diff.primaryKey && putBack(diff.primaryKey, (key) => ({ ...key, columns: key.columns.map(nameOf) })),
|
|
59
66
|
columns: diff.columns?.map(swap),
|
|
60
|
-
indexes: diff.indexes?.map(
|
|
61
|
-
foreignKeys: diff.foreignKeys?.map(
|
|
62
|
-
|
|
67
|
+
indexes: diff.indexes?.map((change) => putBack(change, (index) => ({ ...index, entries: renameIndexEntries(index.entries, nameOf) }))),
|
|
68
|
+
foreignKeys: diff.foreignKeys?.map((change) => putBack(change, (foreignKey) => ({ ...foreignKey, columns: foreignKey.columns.map(nameOf) }))),
|
|
69
|
+
checks: diff.checks?.map(swap),
|
|
70
|
+
triggers: diff.triggers?.map(swap),
|
|
71
|
+
renamedColumns,
|
|
63
72
|
rebuild: diff.rebuild && { from: diff.rebuild.to, to: diff.rebuild.from },
|
|
64
73
|
};
|
|
65
74
|
}
|
|
75
|
+
/** `change` swapped, what it puts back passed through `restore`. */
|
|
76
|
+
function putBack({ from, to }, restore) {
|
|
77
|
+
return { from: to, to: from && restore(from) };
|
|
78
|
+
}
|
|
79
|
+
/** Each column entry named by `nameOf`; an expression entry is SQL, left as written. */
|
|
80
|
+
export function renameIndexEntries(entries, nameOf) {
|
|
81
|
+
return entries.map((entry) => (entry.expression ? entry : { ...entry, column: nameOf(entry.column) }));
|
|
82
|
+
}
|
|
@@ -3,7 +3,7 @@ import type { SchemaAST } from '../schema/schemaAST.js';
|
|
|
3
3
|
import { type BuildSchemaASTOptions } from '../schema/schemaASTBuilder.js';
|
|
4
4
|
import { type DiffOptions } from '../schema/schemaASTDiffer.js';
|
|
5
5
|
import { type CanonicalType, type ColumnNode, type ForeignKeyAction, type IndexNode, type TableNode } from '../schema/types.js';
|
|
6
|
-
import type { ColumnSchema, CreateSchemaOptions, DialectFeatures, DropSchemaOptions, EntityMeta,
|
|
6
|
+
import type { ColumnSchema, CreateSchemaOptions, DialectFeatures, DropSchemaOptions, EntityMeta, EntityWhereMeta, FieldOptions, ForeignKeySchema, IndexSchema, Rename, NamingStrategy, SchemaDiff, SchemaGenerator, Type } from '../type/index.js';
|
|
7
7
|
import type { AnyMigrationOperation, FullColumnDefinition, TableDefinition } from './builder/types.js';
|
|
8
8
|
import { type IndexDdl, type TableDdl } from './ddl/index.js';
|
|
9
9
|
/**
|
|
@@ -43,23 +43,16 @@ export declare class SqlSchemaGenerator implements SchemaGenerator {
|
|
|
43
43
|
* cyclic. SQLite keeps them inline: it cannot add one later, and resolves a forward reference lazily.
|
|
44
44
|
*/
|
|
45
45
|
generateCreateSchema(entities: readonly Type<object>[], options?: CreateSchemaOptions): string[];
|
|
46
|
-
/**
|
|
47
|
-
* The installed triggers on `entity`'s table it does not declare, and the declared ones not installed,
|
|
48
|
-
* compared by name alone: a name carries a hash of the trigger's SQL, so an edited one is a new name.
|
|
49
|
-
* Every trigger counts - the ones it authored, and one per event each stamp names.
|
|
50
|
-
*/
|
|
51
|
-
private triggerChanges;
|
|
52
|
-
generateTriggers(entity: Type<object>, installed?: InstalledTriggers): string[];
|
|
53
|
-
generateTriggersDown(entity: Type<object>, installed?: InstalledTriggers): string[];
|
|
54
|
-
/** `dropped` taken off `entity`'s table and `created` put on, which is a reconcile read either way. */
|
|
55
|
-
private swapTriggers;
|
|
56
|
-
generateTriggerDrops(entity: Type<object>, names: readonly string[]): string[];
|
|
57
46
|
/**
|
|
58
47
|
* One statement per distinct schema the tables being created live in, in first-seen order. Only
|
|
59
48
|
* the tables actually being created, so a narrowed `only` does not declare namespaces it is not
|
|
60
49
|
* about to fill. Empty on an engine without schemas, whose tables are never qualified.
|
|
61
50
|
*/
|
|
62
51
|
private generateCreateSchemas;
|
|
52
|
+
/**
|
|
53
|
+
* The inverse of {@link generateCreateSchema}: a table's drop takes its triggers along, so only what they
|
|
54
|
+
* leave behind follows it.
|
|
55
|
+
*/
|
|
63
56
|
generateDropSchema(entities: readonly Type<object>[], options?: DropSchemaOptions): string[];
|
|
64
57
|
/**
|
|
65
58
|
* The tables of `entities` in dependency order, optionally narrowed to `only`. The AST always spans
|
|
@@ -72,9 +65,14 @@ export declare class SqlSchemaGenerator implements SchemaGenerator {
|
|
|
72
65
|
* The statements taking a table through `diff`, in the one order both directions need: whatever holds
|
|
73
66
|
* something down goes before it and comes back after it. A foreign key holds its columns and the key it
|
|
74
67
|
* points at, so it goes first and comes back last; the key holds its columns; and some engines drop an
|
|
75
|
-
* index along with its column, which would leave nothing to name.
|
|
68
|
+
* index along with its column, which would leave nothing to name. A check holds the columns it names, which
|
|
69
|
+
* MySQL refuses to rename or drop under one. An alter is its drop, then its add.
|
|
76
70
|
*/
|
|
77
71
|
generateAlterTable(diff: SchemaDiff): string[];
|
|
72
|
+
/** `CONSTRAINT <name> CHECK (...)`, in a `CREATE TABLE`, after `ADD`, or inline where a column brings it. */
|
|
73
|
+
private checkConstraint;
|
|
74
|
+
private addCheckSql;
|
|
75
|
+
private dropCheckSql;
|
|
78
76
|
/**
|
|
79
77
|
* Each column the changes make required while declaring a default, and that default as SQL: the rows
|
|
80
78
|
* already there hold a null it has to replace, and it is the only value the entity says it may take.
|
|
@@ -84,7 +82,10 @@ export declare class SqlSchemaGenerator implements SchemaGenerator {
|
|
|
84
82
|
private addForeignKeyStatements;
|
|
85
83
|
/** An index added to a table that may already have rows: its `CREATE`, then what the engine needs after. */
|
|
86
84
|
private addIndexStatements;
|
|
87
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* A column added to a table that exists, and its comment where the engine keeps one apart. `checks` go
|
|
87
|
+
* inline, which is the one way to constrain a table that is rebuilt to alter.
|
|
88
|
+
*/
|
|
88
89
|
private addColumnStatements;
|
|
89
90
|
generateCreateIndex(tableName: string, index: IndexSchema, options?: {
|
|
90
91
|
ifNotExists?: boolean;
|
|
@@ -96,14 +97,14 @@ export declare class SqlSchemaGenerator implements SchemaGenerator {
|
|
|
96
97
|
generateDropIndex(tableName: string, indexName: string, schema?: string): string;
|
|
97
98
|
/**
|
|
98
99
|
* A column definition from a {@link ColumnSchema}, whose type is already the engine's spelling. Apart from
|
|
99
|
-
* {@link generateColumnFromNode}, which
|
|
100
|
+
* {@link generateColumnFromNode}, which sizes a canonical type; both render through {@link renderColumn}.
|
|
100
101
|
*/
|
|
101
102
|
generateColumnDefinitionFromSchema(column: ColumnSchema): string;
|
|
102
103
|
/**
|
|
103
104
|
* The one place a column definition is spelled, so the `ColumnSchema` and `ColumnNode` paths cannot
|
|
104
105
|
* drift. A key column states `NOT NULL` rather than leave it to the key: SQLite lets a key column hold
|
|
105
106
|
* NULL otherwise, and SQL Server adds no key over a nullable column. Never `UNIQUE`: a unique column is
|
|
106
|
-
* a unique index, which the table creates beside it.
|
|
107
|
+
* a unique index, which the table creates beside it. Nor an enum's `CHECK`, which is the table's, named.
|
|
107
108
|
*/
|
|
108
109
|
private renderColumn;
|
|
109
110
|
/** The inline ` COMMENT '...'` a column declaration carries, where the engine takes one there. */
|
|
@@ -122,8 +123,8 @@ export declare class SqlSchemaGenerator implements SchemaGenerator {
|
|
|
122
123
|
diffSchema(entity: Type<object>, currentTable: TableNode | undefined, desiredAst?: SchemaAST, renamedColumns?: readonly Rename[]): SchemaDiff | undefined;
|
|
123
124
|
/**
|
|
124
125
|
* Both ends of rebuilding `actual` as `desired`. The new table is the entity's, keeping what it cannot
|
|
125
|
-
* know of: the indexes and triggers uql did not make, and foreign keys to tables no entity names.
|
|
126
|
-
* old one is the engine's own statements, so a rollback restores it exactly
|
|
126
|
+
* know of: the indexes, checks and triggers uql did not make, and foreign keys to tables no entity names.
|
|
127
|
+
* The old one is the engine's own statements, so a rollback restores it exactly.
|
|
127
128
|
*/
|
|
128
129
|
private rebuildOf;
|
|
129
130
|
diffOptions(): DiffOptions;
|
|
@@ -196,4 +197,4 @@ export declare function constraintNameOf(tableName: string, foreignKey: ForeignK
|
|
|
196
197
|
* The entities as an AST, named by `generator`'s resolvers rather than a naming strategy, which would
|
|
197
198
|
* also rename an explicit `@Entity({ name })` and so compare each table under another name.
|
|
198
199
|
*/
|
|
199
|
-
export declare function buildEntityAST(generator: Pick<SchemaGenerator, 'resolveTableAlias' | 'resolveSchema' | 'resolveColumnName' | 'compileDdl' | 'compileIndexPredicate'>, entities: readonly Type<object>[], options?: Pick<BuildSchemaASTOptions, 'defaultForeignKeyAction' | 'textScoreIndexes' | 'vectorIndexRequiresNotNull'>): SchemaAST;
|
|
200
|
+
export declare function buildEntityAST(generator: Pick<SchemaGenerator, 'resolveTableAlias' | 'resolveSchema' | 'resolveColumnName' | 'compileDdl' | 'compileIndexPredicate'>, entities: readonly Type<object>[], options?: Pick<BuildSchemaASTOptions, 'defaultForeignKeyAction' | 'textScoreIndexes' | 'vectorIndexRequiresNotNull' | 'renderTriggers'>): SchemaAST;
|