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.
Files changed (117) hide show
  1. package/dist/dialect/abstractSqlDialect.d.ts +1 -7
  2. package/dist/dialect/abstractSqlDialect.js +1 -9
  3. package/dist/dialect/mysqlLikeSqlDialect.d.ts +2 -0
  4. package/dist/dialect/mysqlLikeSqlDialect.js +4 -1
  5. package/dist/dialect/pgLikeSqlDialect.js +0 -1
  6. package/dist/migrate/acquireQuerierForMigrations.d.ts +4 -8
  7. package/dist/migrate/acquireQuerierForMigrations.js +10 -18
  8. package/dist/migrate/builder/columnBuilder.d.ts +1 -2
  9. package/dist/migrate/builder/columnBuilder.js +3 -10
  10. package/dist/migrate/builder/migrationBuilder.d.ts +6 -26
  11. package/dist/migrate/builder/migrationBuilder.js +21 -78
  12. package/dist/migrate/builder/tableBuilder.js +2 -3
  13. package/dist/migrate/builder/types.d.ts +59 -120
  14. package/dist/migrate/cli-config.d.ts +1 -0
  15. package/dist/migrate/cli-config.js +11 -26
  16. package/dist/migrate/cli.d.ts +0 -13
  17. package/dist/migrate/cli.js +55 -118
  18. package/dist/migrate/codegen/entityCodeGenerator.d.ts +7 -61
  19. package/dist/migrate/codegen/entityCodeGenerator.js +53 -174
  20. package/dist/migrate/codegen/fieldOptionsSource.d.ts +1 -3
  21. package/dist/migrate/codegen/fieldOptionsSource.js +3 -11
  22. package/dist/migrate/codegen/index.d.ts +2 -7
  23. package/dist/migrate/codegen/index.js +2 -8
  24. package/dist/migrate/codegen/migrationFile.d.ts +1 -8
  25. package/dist/migrate/codegen/migrationFile.js +9 -13
  26. package/dist/migrate/ddl/defaultSql.d.ts +2 -1
  27. package/dist/migrate/ddl/defaultSql.js +1 -3
  28. package/dist/migrate/ddl/indexCapabilities.d.ts +18 -0
  29. package/dist/migrate/ddl/indexCapabilities.js +60 -0
  30. package/dist/migrate/ddl/indexDdl.d.ts +11 -24
  31. package/dist/migrate/ddl/indexDdl.js +22 -28
  32. package/dist/migrate/ddl/mssqlIndexDdl.d.ts +1 -7
  33. package/dist/migrate/ddl/mssqlIndexDdl.js +1 -7
  34. package/dist/migrate/ddl/mssqlTableDdl.d.ts +6 -9
  35. package/dist/migrate/ddl/mssqlTableDdl.js +14 -17
  36. package/dist/migrate/ddl/mysqlIndexDdl.d.ts +0 -21
  37. package/dist/migrate/ddl/mysqlIndexDdl.js +4 -48
  38. package/dist/migrate/ddl/mysqlTableDdl.d.ts +6 -5
  39. package/dist/migrate/ddl/mysqlTableDdl.js +15 -10
  40. package/dist/migrate/ddl/pgIndexDdl.d.ts +0 -10
  41. package/dist/migrate/ddl/pgIndexDdl.js +5 -44
  42. package/dist/migrate/ddl/pgTableDdl.d.ts +6 -2
  43. package/dist/migrate/ddl/pgTableDdl.js +17 -1
  44. package/dist/migrate/ddl/sqliteIndexDdl.js +2 -7
  45. package/dist/migrate/ddl/tableDdl.d.ts +43 -24
  46. package/dist/migrate/ddl/tableDdl.js +56 -37
  47. package/dist/migrate/ddl/tableRebuild.d.ts +2 -1
  48. package/dist/migrate/drift/driftDetector.d.ts +29 -32
  49. package/dist/migrate/drift/driftDetector.js +155 -205
  50. package/dist/migrate/drift/index.d.ts +1 -6
  51. package/dist/migrate/drift/index.js +1 -6
  52. package/dist/migrate/generator/definitionToNode.d.ts +2 -2
  53. package/dist/migrate/generator/definitionToNode.js +6 -5
  54. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +56 -77
  55. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +75 -98
  56. package/dist/migrate/introspection/mssqlIntrospector.d.ts +10 -43
  57. package/dist/migrate/introspection/mssqlIntrospector.js +52 -77
  58. package/dist/migrate/introspection/mysqlIntrospector.d.ts +21 -37
  59. package/dist/migrate/introspection/mysqlIntrospector.js +75 -56
  60. package/dist/migrate/introspection/postgresIntrospector.d.ts +15 -60
  61. package/dist/migrate/introspection/postgresIntrospector.js +74 -77
  62. package/dist/migrate/introspection/sqliteIntrospector.d.ts +19 -53
  63. package/dist/migrate/introspection/sqliteIntrospector.js +73 -119
  64. package/dist/migrate/introspection/tableSchemaAST.d.ts +13 -0
  65. package/dist/migrate/introspection/tableSchemaAST.js +85 -0
  66. package/dist/migrate/migrationTarget.js +10 -2
  67. package/dist/migrate/migrator.d.ts +9 -44
  68. package/dist/migrate/migrator.js +72 -150
  69. package/dist/migrate/schemaChange.d.ts +8 -5
  70. package/dist/migrate/schemaChange.js +25 -8
  71. package/dist/migrate/schemaGenerator.d.ts +35 -109
  72. package/dist/migrate/schemaGenerator.js +179 -353
  73. package/dist/migrate/storage/databaseStorage.d.ts +2 -1
  74. package/dist/migrate/storage/databaseStorage.js +3 -2
  75. package/dist/migrate/storage/jsonStorage.d.ts +1 -2
  76. package/dist/migrate/storage/jsonStorage.js +11 -20
  77. package/dist/migrate/triggerSql.d.ts +7 -9
  78. package/dist/migrate/triggerSql.js +17 -18
  79. package/dist/mongo/index.d.ts +0 -1
  80. package/dist/mongo/index.js +0 -1
  81. package/dist/mongo/mongoDialect.js +0 -1
  82. package/dist/mongo/mongoIntrospector.d.ts +1 -6
  83. package/dist/mongo/mongoIntrospector.js +7 -18
  84. package/dist/mongo/mongoSchemaGenerator.d.ts +7 -26
  85. package/dist/mongo/mongoSchemaGenerator.js +51 -75
  86. package/dist/mssql/mssqlDialect.js +0 -1
  87. package/dist/schema/canonicalType.d.ts +15 -19
  88. package/dist/schema/canonicalType.js +20 -43
  89. package/dist/schema/dependencyGraph.d.ts +2 -3
  90. package/dist/schema/dependencyGraph.js +0 -4
  91. package/dist/schema/indexDifferences.js +2 -2
  92. package/dist/schema/schemaAST.d.ts +1 -1
  93. package/dist/schema/schemaAST.js +5 -12
  94. package/dist/schema/schemaASTBuilder.d.ts +5 -6
  95. package/dist/schema/schemaASTBuilder.js +17 -18
  96. package/dist/schema/schemaASTDiffer.d.ts +4 -5
  97. package/dist/schema/schemaASTDiffer.js +40 -73
  98. package/dist/schema/types.d.ts +24 -54
  99. package/dist/sqlite/localSqliteQuerierPool.d.ts +16 -2
  100. package/dist/sqlite/localSqliteQuerierPool.js +20 -4
  101. package/dist/sqlite/nodeSqliteQuerierPool.js +2 -2
  102. package/dist/sqlite/sqliteDialect.js +0 -1
  103. package/dist/sqlite/sqliteQuerierPool.js +3 -3
  104. package/dist/type/dialect.d.ts +0 -2
  105. package/dist/type/migration.d.ts +13 -73
  106. package/dist/util/ddlExpression.util.d.ts +13 -2
  107. package/dist/util/ddlExpression.util.js +27 -4
  108. package/dist/util/sql.util.d.ts +2 -12
  109. package/dist/util/sql.util.js +9 -18
  110. package/package.json +1 -1
  111. package/skills/uql-orm/SKILL.md +1 -1
  112. package/dist/migrate/introspection/baseSqlIntrospector.d.ts +0 -36
  113. package/dist/migrate/introspection/baseSqlIntrospector.js +0 -133
  114. package/dist/schema/index.d.ts +0 -2
  115. package/dist/schema/index.js +0 -1
  116. package/dist/sqlite/sqlitePragmas.d.ts +0 -3
  117. package/dist/sqlite/sqlitePragmas.js +0 -15
@@ -0,0 +1,85 @@
1
+ import { storedType } from '../../schema/canonicalType.js';
2
+ import { createTableNode, SchemaAST } from '../../schema/schemaAST.js';
3
+ import { DEFAULT_FOREIGN_KEY_ACTION, } from '../../schema/types.js';
4
+ import { derivedForeignKeyName, qualifyName } from '../../util/sql.util.js';
5
+ import { renameIndexEntries } from '../schemaChange.js';
6
+ /** The tables an introspector read, as a {@link SchemaAST}: every table first, so a foreign key reaches one read after it. */
7
+ export function tableSchemasToAST(schemas, from) {
8
+ const ast = new SchemaAST();
9
+ const tables = schemas.map((schema) => ({ schema, table: tableNode(schema, from) }));
10
+ const nodes = new Map(tables.map(({ schema, table }) => [schema.name, table]));
11
+ for (const { table } of tables) {
12
+ ast.addTable(table);
13
+ }
14
+ for (const { schema, table } of tables) {
15
+ addRelationships(ast, nodes, schema, table);
16
+ addIndexes(ast, schema, table);
17
+ }
18
+ return ast;
19
+ }
20
+ /** `table` with each column `renames` names under its new name, wherever the table names it. */
21
+ export function renamedTable(table, renames, schema) {
22
+ const nameIn = (tableName) => (column) => renames.get(qualifyName(tableName, schema))?.find((rename) => rename.from === column)?.to ?? column;
23
+ const own = nameIn(table.name);
24
+ return {
25
+ ...table,
26
+ columns: table.columns.map((column) => ({ ...column, name: own(column.name) })),
27
+ primaryKey: table.primaryKey && { ...table.primaryKey, columns: table.primaryKey.columns.map(own) },
28
+ indexes: table.indexes?.map((index) => ({ ...index, entries: renameIndexEntries(index.entries, own) })),
29
+ foreignKeys: table.foreignKeys?.map((foreignKey) => ({
30
+ ...foreignKey,
31
+ columns: foreignKey.columns.map(own),
32
+ references: {
33
+ ...foreignKey.references,
34
+ columns: foreignKey.references.columns.map(nameIn(foreignKey.references.table)),
35
+ },
36
+ })),
37
+ };
38
+ }
39
+ function tableNode(schema, { dialectName, schema: namespace, indexFacets }) {
40
+ const table = createTableNode(schema.name, namespace, indexFacets);
41
+ for (const col of schema.columns) {
42
+ // Spread, not field by field: a `ColumnSchema` is a `ColumnNode` minus the graph links, so
43
+ // everything but the type crosses unchanged and a field either shape gains cannot be dropped here.
44
+ const { type, length: _length, precision: _precision, scale: _scale, ...rest } = col;
45
+ const column = { ...rest, type: storedType(dialectName, type, col), table };
46
+ table.columns.set(col.name, column);
47
+ }
48
+ // The ordered key the query returned: `(a, b)` is a different key from `(b, a)`.
49
+ table.primaryKey = schema.primaryKey;
50
+ table.checks.push(...(schema.checks ?? []));
51
+ table.triggers.push(...(schema.triggers ?? []));
52
+ table.definition = schema.definition;
53
+ return table;
54
+ }
55
+ function addRelationships(ast, nodes, schema, fromTable) {
56
+ for (const fk of schema.foreignKeys ?? []) {
57
+ const toTable = nodes.get(fk.references.table);
58
+ if (!toTable) {
59
+ fromTable.externalForeignKeys.push(fk);
60
+ continue;
61
+ }
62
+ const fromColumns = fk.columns.flatMap((name) => fromTable.columns.get(name) ?? []);
63
+ const toColumns = fk.references.columns.flatMap((name) => toTable.columns.get(name) ?? []);
64
+ if (fromColumns.length > 0 && toColumns.length > 0) {
65
+ const relationship = {
66
+ name: fk.name ?? derivedForeignKeyName(schema.name, fk.columns),
67
+ type: fromColumns[0].isUnique ? 'OneToOne' : 'ManyToOne',
68
+ from: { table: fromTable, columns: fromColumns },
69
+ to: { table: toTable, columns: toColumns },
70
+ onDelete: fk.onDelete || DEFAULT_FOREIGN_KEY_ACTION,
71
+ onUpdate: fk.onUpdate || DEFAULT_FOREIGN_KEY_ACTION,
72
+ };
73
+ ast.addRelationship(relationship);
74
+ }
75
+ }
76
+ }
77
+ /** An expression has no column to resolve; an entry naming a column the table lacks is dropped, as the entity side does. */
78
+ function addIndexes(ast, schema, table) {
79
+ for (const index of schema.indexes ?? []) {
80
+ const entries = index.entries.filter((entry) => entry.expression || table.columns.has(entry.column));
81
+ if (entries.length > 0) {
82
+ ast.addIndex({ ...index, table, entries });
83
+ }
84
+ }
85
+ }
@@ -69,10 +69,18 @@ export function migrationTargetFor(pool, defaultForeignKeyAction) {
69
69
  /** A builder running each operation on `querier`, as SQL or as MongoDB driver commands. */
70
70
  export async function migrationBuilderFor(querier) {
71
71
  if (isSqlQuerier(querier)) {
72
- return new MigrationBuilder(new SqlSchemaGenerator(querier.dialect), sqlSession(querier).run);
72
+ return builderOn(new SqlSchemaGenerator(querier.dialect), sqlSession(querier).run);
73
73
  }
74
74
  if (isMongoQuerier(querier)) {
75
- return new MigrationBuilder(await mongoSchemaGenerator(), mongoSession(querier).run);
75
+ return builderOn(await mongoSchemaGenerator(), mongoSession(querier).run);
76
76
  }
77
77
  throw new UqlUsageError('A migration builder needs a SQL or a MongoDB querier');
78
78
  }
79
+ /** A builder running each statement `generator` writes for an operation through `run`. */
80
+ function builderOn(generator, run) {
81
+ return new MigrationBuilder(async (operation) => {
82
+ for (const statement of generator.generateOperation(operation)) {
83
+ await run(statement);
84
+ }
85
+ });
86
+ }
@@ -22,9 +22,9 @@ export declare class Migrator {
22
22
  * Get all discovered migrations from the migrations directory
23
23
  */
24
24
  getMigrations(): Promise<Migration<Querier>[]>;
25
- /**
26
- * Get list of pending migrations (not yet executed)
27
- */
25
+ /** The migrations on disk the storage records as run (`applied`) or not (`pending`), and every name it records. */
26
+ private journal;
27
+ /** The migrations not yet run. */
28
28
  pending(): Promise<Migration<Querier>[]>;
29
29
  /**
30
30
  * Get list of executed migrations
@@ -61,29 +61,6 @@ export declare class Migrator {
61
61
  * Generate a migration based on entity schema differences
62
62
  */
63
63
  generateFromEntities(name: string): Promise<string>;
64
- /**
65
- * Each entity on a table this plan does not create - a new one carries its triggers in its `CREATE` -
66
- * beside the triggers uql has installed there, read once per schema. Kept only where either side has
67
- * any: one declaring none on a table holding none has nothing to reconcile.
68
- */
69
- private installedTriggers;
70
- /**
71
- * The alters, with the triggers reconciled around them. Postgres refuses to retype or drop a column a
72
- * trigger names, so every trigger on a table whose columns change comes off before the alters and what
73
- * its entity declares goes back on after. `down` is lazy: SQLite cannot express every alter's inverse.
74
- */
75
- private alterPlan;
76
- /**
77
- * Each entity's triggers taken from what the catalogue holds to what it declares. Against the catalogue
78
- * rather than a diff, because a trigger hangs off a table whose columns may be unchanged: a body edited
79
- * on a settled table appears in no diff at all.
80
- */
81
- private reconcileTriggers;
82
- /**
83
- * The inverse: what the reconcile created dropped, and what it dropped restored as the engine reprints
84
- * it. Read off the catalogue rather than recorded by uql, and exactly right for restoring one.
85
- */
86
- private revertedTriggers;
87
64
  /**
88
65
  * What a generated migration does that its reader must not miss: each column it drops or retypes, which
89
66
  * can lose data, and each table it creates empty while the database holds one no entity names with the
@@ -108,6 +85,11 @@ export declare class Migrator {
108
85
  getDiffs(options?: {
109
86
  renames?: boolean;
110
87
  }): Promise<SchemaDiff[]>;
88
+ /**
89
+ * Each check uql did not install on a table whose entity declares checks: never changed or dropped, so
90
+ * one an owned check replaces, such as an enum's from before checks were named, goes on enforcing itself.
91
+ */
92
+ private noteForeignChecks;
111
93
  /**
112
94
  * The tables `entities` name, read a schema at a time so each is keyed as its entity spells it. Those
113
95
  * alone: nothing else is diffed, and another table can be dropped mid-scan by whatever else is running.
@@ -123,8 +105,6 @@ export declare class Migrator {
123
105
  * settle instead of colliding; an existing one still pays for introspection, as a diff needs columns.
124
106
  */
125
107
  private planEntity;
126
- /** The diff for one entity against the table it already has, and none where the two agree. */
127
- private alterFromEntity;
128
108
  /** The configured entities, with `entity` among them however the migrator was built. */
129
109
  private entitiesWith;
130
110
  /** The introspector for a claimed schema, which is the connection's own where none is claimed. */
@@ -148,10 +128,7 @@ export declare class Migrator {
148
128
  * Without `drop`, a column's drop is held too. A rebuilt table applies its diff whole, so holding any
149
129
  * part of it holds the rebuild, and only what an `ALTER` adds goes ahead: a plain column, an index.
150
130
  */
151
- protected filterDiff(diff: SchemaDiff, options: {
152
- safe?: boolean;
153
- drop?: boolean;
154
- }): SchemaDiff;
131
+ private filterDiff;
155
132
  /** Runs the statements a generator wrote, in one transaction where the engine takes DDL in one. */
156
133
  executeSyncStatements(statements: string[], options: {
157
134
  logging?: boolean;
@@ -175,18 +152,6 @@ export declare class Migrator {
175
152
  * Check if an object is a valid migration
176
153
  */
177
154
  isMigration(obj: unknown): obj is MigrationDefinition<Querier>;
178
- /**
179
- * Extract migration name from filename
180
- */
181
- getMigrationName(fileName: string): string;
182
- /**
183
- * Generate timestamp string for migration names
184
- */
185
- protected getTimestamp(): string;
186
- /**
187
- * Convert a string to a slug for filenames
188
- */
189
- protected slugify(text: string): string;
190
155
  }
191
156
  /**
192
157
  * Helper function to define a migration with proper typing
@@ -2,10 +2,10 @@ import { mkdir, readdir, writeFile } from 'node:fs/promises';
2
2
  import { basename, extname, join } from 'node:path';
3
3
  import { pathToFileURL } from 'node:url';
4
4
  import { getEntities, getMeta } from '../entity/index.js';
5
- import { SchemaAST } from '../schema/index.js';
5
+ import { SchemaAST } from '../schema/schemaAST.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';
@@ -59,13 +59,19 @@ export class Migrator {
59
59
  // Sort by name (which typically includes timestamp)
60
60
  return migrations.sort((a, b) => a.name.localeCompare(b.name));
61
61
  }
62
- /**
63
- * Get list of pending migrations (not yet executed)
64
- */
65
- async pending() {
62
+ /** The migrations on disk the storage records as run (`applied`) or not (`pending`), and every name it records. */
63
+ async journal() {
66
64
  const [migrations, executed] = await Promise.all([this.getMigrations(), this.storage.executed()]);
67
- const executedSet = new Set(executed);
68
- return migrations.filter((m) => !executedSet.has(m.name));
65
+ const ran = new Set(executed);
66
+ return {
67
+ executed,
68
+ applied: migrations.filter((migration) => ran.has(migration.name)),
69
+ pending: migrations.filter((migration) => !ran.has(migration.name)),
70
+ };
71
+ }
72
+ /** The migrations not yet run. */
73
+ async pending() {
74
+ return (await this.journal()).pending;
69
75
  }
70
76
  /**
71
77
  * Get list of executed migrations
@@ -83,10 +89,7 @@ export class Migrator {
83
89
  * Rollback migrations
84
90
  */
85
91
  async down(options = {}) {
86
- const [migrations, executed] = await Promise.all([this.getMigrations(), this.storage.executed()]);
87
- const executedSet = new Set(executed);
88
- const executedMigrations = migrations.filter((m) => executedSet.has(m.name)).reverse(); // Rollback in reverse order
89
- return this.runInOrder(executedMigrations, 'down', options);
92
+ return this.runInOrder((await this.journal()).applied.reverse(), 'down', options);
90
93
  }
91
94
  /** Runs the list narrowed by `to`/`step`, stopping at the first failure: `up` over the pending, `down` over the executed reversed. */
92
95
  async runInOrder(migrations, direction, options) {
@@ -117,6 +120,13 @@ export class Migrator {
117
120
  */
118
121
  async runMigration(migration, direction) {
119
122
  const startTime = Date.now();
123
+ const finished = (error) => ({
124
+ name: migration.name,
125
+ direction,
126
+ duration: Date.now() - startTime,
127
+ success: error === undefined,
128
+ ...(error && { error }),
129
+ });
120
130
  return this.target.withSession(async ({ querier, transaction }) => {
121
131
  try {
122
132
  this.logger.logMigration(`${direction === 'up' ? 'Running' : 'Reverting'} migration: ${migration.name}`);
@@ -131,25 +141,13 @@ export class Migrator {
131
141
  }
132
142
  };
133
143
  await (migration.transaction === false ? work() : transaction(work));
134
- const duration = Date.now() - startTime;
135
- this.logger.logMigration(`Migration ${migration.name} ${direction === 'up' ? 'applied' : 'reverted'} in ${duration}ms`);
136
- return {
137
- name: migration.name,
138
- direction,
139
- duration,
140
- success: true,
141
- };
144
+ const result = finished();
145
+ this.logger.logMigration(`Migration ${migration.name} ${direction === 'up' ? 'applied' : 'reverted'} in ${result.duration}ms`);
146
+ return result;
142
147
  }
143
148
  catch (error) {
144
- const duration = Date.now() - startTime;
145
149
  this.logger.logError(`Migration ${migration.name} failed: ${error.message}`, error);
146
- return {
147
- name: migration.name,
148
- direction,
149
- duration,
150
- success: false,
151
- error: error,
152
- };
150
+ return finished(error);
153
151
  }
154
152
  });
155
153
  }
@@ -164,7 +162,7 @@ export class Migrator {
164
162
  }
165
163
  /** Writes a migration module on this dialect's querier to a new timestamped file, and returns its path. */
166
164
  async writeMigration(name, body) {
167
- const filePath = join(this.migrationsPath, `${this.getTimestamp()}_${this.slugify(name)}.ts`);
165
+ const filePath = join(this.migrationsPath, `${timestamp()}_${slugify(name)}.ts`);
168
166
  const content = buildMigrationModule({
169
167
  migrationName: name,
170
168
  createdAt: new Date(),
@@ -182,19 +180,19 @@ export class Migrator {
182
180
  const generator = await this.getSchemaGenerator();
183
181
  const { created, altered } = await this.pendingChanges({ renames: true });
184
182
  await this.assertFillable(altered);
185
- const plan = this.alterPlan(generator, altered, await this.installedTriggers(created));
186
183
  await this.noteChanges(generator, created, altered);
187
- const up = [...this.createSchema(generator, created), ...plan.up];
184
+ const up = [
185
+ ...this.createSchema(generator, created),
186
+ ...altered.flatMap((diff) => generator.generateAlterTable(diff)),
187
+ ];
188
188
  if (up.length === 0) {
189
189
  this.logger.logInfo('No schema changes detected.');
190
190
  return '';
191
191
  }
192
192
  // Diff by diff in reverse, each rolled back in the order its generator wrote it.
193
193
  const down = [
194
- ...plan.down(),
195
- // A table's drop takes its triggers along, but not the function the Postgres family keeps each body in.
196
- ...this.createdEntities(created).flatMap((entity) => generator.generateTriggersDown(entity)),
197
- ...created.toReversed().map((tableName) => generator.generateDropTable(tableName, { ifExists: true })),
194
+ ...altered.toReversed().flatMap((diff) => generator.generateAlterTable(reverseDiff(diff))),
195
+ ...generator.generateDropSchema(this.createdEntities(created), { ifExists: true }),
198
196
  ];
199
197
  const { emit } = this.target.source;
200
198
  const filePath = await this.writeMigration(name, {
@@ -205,70 +203,6 @@ export class Migrator {
205
203
  this.logger.logInfo(`Created migration from entities: ${filePath}`);
206
204
  return filePath;
207
205
  }
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
206
  /**
273
207
  * What a generated migration does that its reader must not miss: each column it drops or retypes, which
274
208
  * can lose data, and each table it creates empty while the database holds one no entity names with the
@@ -354,12 +288,28 @@ export class Migrator {
354
288
  // Read again under the names the entities give them, so the rest compares as the columns they become.
355
289
  ast = await this.introspectEntities(this.entities, renames);
356
290
  }
291
+ if (desiredAst) {
292
+ this.noteForeignChecks(desiredAst, ast);
293
+ }
357
294
  return this.entities.flatMap((entity) => {
358
- const tableName = generator.resolveTableName(getMeta(entity));
295
+ const tableName = this.tableOf(entity);
359
296
  const diff = generator.diffSchema(entity, ast.getTable(tableName), desiredAst, renames.get(tableName));
360
297
  return diff ? [diff] : [];
361
298
  });
362
299
  }
300
+ /**
301
+ * Each check uql did not install on a table whose entity declares checks: never changed or dropped, so
302
+ * one an owned check replaces, such as an enum's from before checks were named, goes on enforcing itself.
303
+ */
304
+ noteForeignChecks(desired, actual) {
305
+ for (const table of desired.getTables()) {
306
+ const foreign = (actual.getTable(qualifyName(table.name, table.schema))?.checks ?? []).filter((check) => !isOwnedName(check.name));
307
+ if (table.checks.length && foreign.length) {
308
+ this.logger.logWarn(`"${table.name}" holds checks uql did not install, which it leaves as they are: ` +
309
+ `${foreign.map((check) => check.name).join(', ')}. Drop each one a declared check replaces.`);
310
+ }
311
+ }
312
+ }
363
313
  /**
364
314
  * The tables `entities` name, read a schema at a time so each is keyed as its entity spells it. Those
365
315
  * alone: nothing else is diffed, and another table can be dropped mid-scan by whatever else is running.
@@ -403,7 +353,7 @@ export class Migrator {
403
353
  const meta = getMeta(entity);
404
354
  const { dialect } = this.pool;
405
355
  const introspector = this.schemaIntrospectorFor(dialect.resolveSchema(meta));
406
- const tableName = generator.resolveTableName(meta);
356
+ const tableName = this.tableOf(entity);
407
357
  if (!(await introspector.tableExists(dialect.resolveTableAlias(meta)))) {
408
358
  // Spanning the whole set, so a foreign key resolves against the tables it points at, and always
409
359
  // including this entity: `only` is what keeps the statements to this table.
@@ -411,17 +361,10 @@ export class Migrator {
411
361
  }
412
362
  // With the tables it references, which its foreign keys resolve against.
413
363
  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
- const altered = this.alterFromEntity(generator, entity, ast.getTable(tableName), options);
364
+ const diff = generator.diffSchema(entity, ast.getTable(tableName), generator.buildAST?.(this.entitiesWith(entity)));
365
+ const altered = diff?.type === 'alter' ? [this.filterDiff(diff, options)] : [];
416
366
  await this.assertFillable(altered);
417
- return this.alterPlan(generator, altered, await this.installedTriggers([], [entity])).up;
418
- }
419
- /** The diff for one entity against the table it already has, and none where the two agree. */
420
- alterFromEntity(generator, entity, table, options) {
421
- // Spanning the set for the reason `planEntity` spells out: a foreign key needs the table it
422
- // points at, which a sync of one entity outside the configured list would not otherwise have.
423
- const diff = generator.diffSchema(entity, table, generator.buildAST?.(this.entitiesWith(entity)));
424
- return diff?.type === 'alter' ? [this.filterDiff(diff, options)] : [];
367
+ return altered.flatMap((change) => generator.generateAlterTable(change));
425
368
  }
426
369
  /** The configured entities, with `entity` among them however the migrator was built. */
427
370
  entitiesWith(entity) {
@@ -449,7 +392,7 @@ export class Migrator {
449
392
  await this.assertFillable(filtered);
450
393
  return [
451
394
  ...this.createSchema(generator, created),
452
- ...this.alterPlan(generator, filtered, await this.installedTriggers(created)).up,
395
+ ...filtered.flatMap((diff) => generator.generateAlterTable(diff)),
453
396
  ];
454
397
  }
455
398
  /** The new tables, created together so a foreign key between them, cyclic included, resolves. Empty in, empty out. */
@@ -504,6 +447,7 @@ export class Migrator {
504
447
  columns: options.drop ? columns : nonEmpty((columns ?? []).filter((change) => change.to !== undefined)),
505
448
  indexes: additive('index', diff.indexes, (index) => index.name),
506
449
  foreignKeys: additive('foreign key', diff.foreignKeys, (foreignKey) => constraintNameOf(diff.tableName, foreignKey)),
450
+ checks: additive('check', diff.checks, (check) => check.name),
507
451
  };
508
452
  if (!diff.rebuild || !held) {
509
453
  return filtered;
@@ -527,8 +471,8 @@ export class Migrator {
527
471
  * Get migration status
528
472
  */
529
473
  async status() {
530
- const [pending, executed] = await Promise.all([this.pending().then((m) => m.map((x) => x.name)), this.executed()]);
531
- return { pending, executed };
474
+ const { pending, executed } = await this.journal();
475
+ return { pending: pending.map((migration) => migration.name), executed };
532
476
  }
533
477
  /**
534
478
  * Get migration files from the migrations directory
@@ -559,7 +503,7 @@ export class Migrator {
559
503
  const migration = module.default ?? module;
560
504
  if (this.isMigration(migration)) {
561
505
  return {
562
- name: this.getMigrationName(fileName),
506
+ name: basename(fileName, extname(fileName)),
563
507
  up: migration.up.bind(migration),
564
508
  down: migration.down.bind(migration),
565
509
  transaction: migration.transaction,
@@ -579,35 +523,19 @@ export class Migrator {
579
523
  isMigration(obj) {
580
524
  return isRecord(obj) && typeof obj['up'] === 'function' && typeof obj['down'] === 'function';
581
525
  }
582
- /**
583
- * Extract migration name from filename
584
- */
585
- getMigrationName(fileName) {
586
- return basename(fileName, extname(fileName));
587
- }
588
- /**
589
- * Generate timestamp string for migration names
590
- */
591
- getTimestamp() {
592
- const now = new Date();
593
- return [
594
- now.getFullYear(),
595
- String(now.getMonth() + 1).padStart(2, '0'),
596
- String(now.getDate()).padStart(2, '0'),
597
- String(now.getHours()).padStart(2, '0'),
598
- String(now.getMinutes()).padStart(2, '0'),
599
- String(now.getSeconds()).padStart(2, '0'),
600
- ].join('');
601
- }
602
- /**
603
- * Convert a string to a slug for filenames
604
- */
605
- slugify(text) {
606
- return text
607
- .toLowerCase()
608
- .replace(/[^a-z0-9]+/g, '_')
609
- .replace(/^_+|_+$/g, '');
610
- }
526
+ }
527
+ /** The local time a migration file is named by, `YYYYMMDDHHmmss`, so names sort in the order they were made. */
528
+ function timestamp() {
529
+ const now = new Date();
530
+ const parts = [now.getMonth() + 1, now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds()];
531
+ return `${now.getFullYear()}${parts.map((part) => String(part).padStart(2, '0')).join('')}`;
532
+ }
533
+ /** `text` as a file name's part: lower case, each run of anything else an underscore. */
534
+ function slugify(text) {
535
+ return text
536
+ .toLowerCase()
537
+ .replace(/[^a-z0-9]+/g, '_')
538
+ .replace(/^_+|_+$/g, '');
611
539
  }
612
540
  /**
613
541
  * Helper function to define a migration with proper typing
@@ -631,9 +559,3 @@ function referencedEntities(meta) {
631
559
  const fields = definedEntries(meta.fields).flatMap(([, field]) => field.references?.() ?? []);
632
560
  return [...fields, ...definedEntries(meta.relations).map(([, relation]) => relation.entity())];
633
561
  }
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,13 +14,16 @@ 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 foreign key included. */
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;
21
21
  /** `items`, or nothing where it has none, so an empty change list is left off a diff. */
22
22
  export declare function nonEmpty<T>(items: readonly T[]): readonly T[] | undefined;
23
- /** `change` undone: an add becomes a drop, a drop an add, and an alter runs the other way. */
24
- export declare function swap<T>({ from, to }: Change<T>): Change<T>;
25
- /** `diff` undone: every change swapped, which is what a migration's `down` runs. */
23
+ /**
24
+ * `diff` undone: every change swapped, which is what a migration's `down` runs. Renames run before the adds
25
+ * either way, so a key, index or foreign key put back names a renamed column as the rename back leaves it.
26
+ */
26
27
  export declare function reverseDiff(diff: SchemaDiff): SchemaDiff;
28
+ /** Each column entry named by `nameOf`; an expression entry is SQL, left as written. */
29
+ 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 foreign key included. */
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
  };
@@ -48,18 +49,34 @@ export function nonEmpty(items) {
48
49
  return items.length ? items : undefined;
49
50
  }
50
51
  /** `change` undone: an add becomes a drop, a drop an add, and an alter runs the other way. */
51
- export function swap({ from, to }) {
52
+ function swap({ from, to }) {
52
53
  return { from: to, to: from };
53
54
  }
54
- /** `diff` undone: every change swapped, which is what a migration's `down` runs. */
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 && swap(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(swap),
61
- foreignKeys: diff.foreignKeys?.map(swap),
62
- renamedColumns: diff.renamedColumns?.map(({ from, to }) => ({ from: to, to: from })),
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
+ }