uql-orm 0.97.0 → 0.98.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dialect/abstractSqlDialect.d.ts +1 -7
- package/dist/dialect/abstractSqlDialect.js +1 -9
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +2 -0
- package/dist/dialect/mysqlLikeSqlDialect.js +4 -1
- package/dist/dialect/pgLikeSqlDialect.js +0 -1
- package/dist/migrate/acquireQuerierForMigrations.d.ts +4 -8
- package/dist/migrate/acquireQuerierForMigrations.js +10 -18
- package/dist/migrate/builder/columnBuilder.d.ts +1 -2
- package/dist/migrate/builder/columnBuilder.js +3 -10
- package/dist/migrate/builder/migrationBuilder.d.ts +6 -26
- package/dist/migrate/builder/migrationBuilder.js +21 -78
- package/dist/migrate/builder/tableBuilder.js +2 -3
- package/dist/migrate/builder/types.d.ts +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 +4 -21
- package/dist/migrate/migrator.js +47 -71
- 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,4 +1,4 @@
|
|
|
1
|
-
import { AbstractSqlSchemaIntrospector } from './abstractSqlSchemaIntrospector.js';
|
|
1
|
+
import { AbstractSqlSchemaIntrospector, } from './abstractSqlSchemaIntrospector.js';
|
|
2
2
|
/**
|
|
3
3
|
* SQLite schema introspector
|
|
4
4
|
*/
|
|
@@ -11,7 +11,7 @@ export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
11
11
|
/** User tables only: skips SQLite's own and libSQL's vector index tables (its metadata and `<index>_shadow`). */
|
|
12
12
|
getTableNamesQuery() {
|
|
13
13
|
return /*sql*/ `
|
|
14
|
-
SELECT name
|
|
14
|
+
SELECT name AS table_name
|
|
15
15
|
FROM sqlite_master
|
|
16
16
|
WHERE type = 'table'
|
|
17
17
|
AND name NOT LIKE 'sqlite_%'
|
|
@@ -21,116 +21,69 @@ export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
21
21
|
`;
|
|
22
22
|
}
|
|
23
23
|
tableExistsQuery() {
|
|
24
|
-
return /*sql*/ `
|
|
25
|
-
SELECT COUNT(*) as count
|
|
26
|
-
FROM sqlite_master
|
|
27
|
-
WHERE type = 'table'
|
|
28
|
-
AND name = ?
|
|
29
|
-
`;
|
|
30
|
-
}
|
|
31
|
-
parseTableExistsResult([row]) {
|
|
32
|
-
return Number(row.count) > 0;
|
|
24
|
+
return /*sql*/ `SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?`;
|
|
33
25
|
}
|
|
34
26
|
/**
|
|
35
27
|
* `table_xinfo`, not `table_info`: the latter omits generated columns entirely, so a table carrying
|
|
36
28
|
* one read back without it and every sync offered to add a column that was already there - which
|
|
37
|
-
* SQLite cannot do to an existing table anyway.
|
|
29
|
+
* SQLite cannot do to an existing table anyway. Also the key, by each column's `pk`: one statement for both.
|
|
38
30
|
*/
|
|
39
|
-
|
|
40
|
-
return /*sql*/ `PRAGMA table_xinfo(${this.dialect.escapeId(tableName)})
|
|
41
|
-
}
|
|
42
|
-
getIndexesQuery(tableName) {
|
|
43
|
-
return /*sql*/ `PRAGMA index_list(${this.dialect.escapeId(tableName)})`;
|
|
44
|
-
}
|
|
45
|
-
getForeignKeysQuery(tableName) {
|
|
46
|
-
return /*sql*/ `PRAGMA foreign_key_list(${this.dialect.escapeId(tableName)})`;
|
|
31
|
+
tableInfo(read, tableName) {
|
|
32
|
+
return read(/*sql*/ `PRAGMA table_xinfo(${this.dialect.escapeId(tableName)})`);
|
|
47
33
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
getColumnsParams(_tableName) {
|
|
52
|
-
return [];
|
|
53
|
-
}
|
|
54
|
-
getIndexesParams(_tableName) {
|
|
55
|
-
return [];
|
|
56
|
-
}
|
|
57
|
-
getForeignKeysParams(_tableName) {
|
|
58
|
-
return [];
|
|
59
|
-
}
|
|
60
|
-
getPrimaryKeyParams(_tableName) {
|
|
61
|
-
return [];
|
|
62
|
-
}
|
|
63
|
-
/** `sqlite_master`, not `information_schema`, so the column is `name`. */
|
|
64
|
-
mapTableNameRow(row) {
|
|
65
|
-
return row.name;
|
|
66
|
-
}
|
|
67
|
-
async mapColumnsResult(read, tableName, results) {
|
|
68
|
-
const uniqueColumns = await this.getUniqueColumns(read, tableName);
|
|
34
|
+
async getColumns(read, tableName) {
|
|
35
|
+
const rows = await this.tableInfo(read, tableName);
|
|
69
36
|
// Only a sole `INTEGER PRIMARY KEY` is the rowid, which is what numbers itself.
|
|
70
|
-
const soleKey =
|
|
71
|
-
const [table] =
|
|
37
|
+
const soleKey = rows.filter((row) => row.pk > 0).length === 1;
|
|
38
|
+
const [table] = rows.some((row) => row.hidden === STORED_GENERATED)
|
|
72
39
|
? await this.getDefinition(read, tableName)
|
|
73
40
|
: [];
|
|
74
41
|
const ddl = table?.sql ?? '';
|
|
75
|
-
return
|
|
42
|
+
return rows.map((row) => ({
|
|
76
43
|
name: row.name,
|
|
77
44
|
type: this.normalizeType(row.type),
|
|
78
45
|
nullable: row.notnull === 0,
|
|
79
46
|
defaultValue: this.parseDefaultValue(row.dflt_value),
|
|
80
|
-
isPrimaryKey: row.pk > 0,
|
|
81
47
|
isAutoIncrement: soleKey && row.pk > 0 && row.type.toUpperCase() === 'INTEGER',
|
|
82
|
-
isUnique: uniqueColumns.has(row.name),
|
|
83
48
|
length: this.extractLength(row.type),
|
|
84
|
-
precision: undefined,
|
|
85
|
-
scale: undefined,
|
|
86
|
-
comment: undefined, // SQLite doesn't support column comments
|
|
87
49
|
generatedAs: row.hidden === STORED_GENERATED ? generatedExpression(ddl, row.name) : undefined,
|
|
88
50
|
}));
|
|
89
51
|
}
|
|
90
|
-
async
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const vectorIndex = await this.getVectorIndex(read, index.name);
|
|
111
|
-
if (vectorIndex) {
|
|
112
|
-
indexSchemas.push(vectorIndex);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
52
|
+
async getPrimaryKey(read, tableName) {
|
|
53
|
+
const key = (await this.tableInfo(read, tableName)).filter((row) => row.pk > 0).sort((a, b) => a.pk - b.pk);
|
|
54
|
+
return key.length ? { columns: key.map((row) => row.name) } : undefined;
|
|
55
|
+
}
|
|
56
|
+
async getIndexes(read, tableName) {
|
|
57
|
+
const list = await read(/*sql*/ `PRAGMA index_list(${this.dialect.escapeId(tableName)})`);
|
|
58
|
+
const statements = new Map((await this.getDefinition(read, tableName)).map(({ name, sql }) => [name, sql]));
|
|
59
|
+
const indexes = [];
|
|
60
|
+
// The key's own index ('pk') is left out; a unique constraint's ('u') is reported as every engine reports one.
|
|
61
|
+
for (const index of list.filter((it) => it.origin !== 'pk')) {
|
|
62
|
+
const sql = statements.get(index.name) ?? '';
|
|
63
|
+
// `PRAGMA index_info` names an expression entry `null`, its text kept only in `sqlite_master.sql`: it is
|
|
64
|
+
// reported as the expression it is, as MySQL reports one, and libSQL's vector index is read off that text.
|
|
65
|
+
const entries = (await this.getIndexColumns(read, index.name)).map(({ name }) => name === null ? { column: '', expression: true } : { column: name });
|
|
66
|
+
indexes.push(this.vectorIndex(index.name, sql) ?? {
|
|
67
|
+
name: index.name,
|
|
68
|
+
entries,
|
|
69
|
+
unique: Boolean(index.unique),
|
|
70
|
+
where: index.partial ? indexPredicate(sql) : undefined,
|
|
71
|
+
});
|
|
115
72
|
}
|
|
116
|
-
return
|
|
73
|
+
return indexes;
|
|
117
74
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return Array.from(grouped.entries()).map(([, rows]) => {
|
|
128
|
-
const first = rows[0];
|
|
129
|
-
const columns = rows.map((r) => r.from);
|
|
130
|
-
// Unnamed: `PRAGMA foreign_key_list` reports none, so the AST derives one from the columns, as the entity side does.
|
|
75
|
+
/**
|
|
76
|
+
* A row per column of each key, grouped by its id. Unnamed: `PRAGMA foreign_key_list` reports none, so the
|
|
77
|
+
* AST derives one from the columns, as the entity side does.
|
|
78
|
+
*/
|
|
79
|
+
async getForeignKeys(read, tableName) {
|
|
80
|
+
const rows = await read(
|
|
81
|
+
/*sql*/ `PRAGMA foreign_key_list(${this.dialect.escapeId(tableName)})`);
|
|
82
|
+
return [...Map.groupBy(rows, (row) => row.id).values()].map((key) => {
|
|
83
|
+
const [first] = key;
|
|
131
84
|
return {
|
|
132
|
-
columns,
|
|
133
|
-
references: { table: first.table, columns:
|
|
85
|
+
columns: key.map((row) => row.from),
|
|
86
|
+
references: { table: first.table, columns: key.map((row) => row.to) },
|
|
134
87
|
onDelete: this.normalizeReferentialAction(first.on_delete),
|
|
135
88
|
onUpdate: this.normalizeReferentialAction(first.on_update),
|
|
136
89
|
};
|
|
@@ -146,40 +99,15 @@ export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
146
99
|
return read(
|
|
147
100
|
/*sql*/ `SELECT type AS kind, name, sql FROM sqlite_master WHERE tbl_name = ? AND sql IS NOT NULL ORDER BY type <> 'table'`, [tableName]);
|
|
148
101
|
}
|
|
149
|
-
mapPrimaryKeyResult(results) {
|
|
150
|
-
const pkColumns = results.filter((r) => r.pk > 0).sort((a, b) => a.pk - b.pk);
|
|
151
|
-
if (pkColumns.length === 0) {
|
|
152
|
-
return undefined;
|
|
153
|
-
}
|
|
154
|
-
return pkColumns.map((r) => r.name);
|
|
155
|
-
}
|
|
156
|
-
async getUniqueColumns(read, tableName) {
|
|
157
|
-
const indexes = await read(this.getIndexesQuery(tableName));
|
|
158
|
-
const uniqueColumns = new Set();
|
|
159
|
-
// The key's own index is left out: a key column is unique already, which the entity side never states.
|
|
160
|
-
for (const index of indexes) {
|
|
161
|
-
if (index.unique && index.origin !== 'pk') {
|
|
162
|
-
const columns = await this.getIndexColumns(read, index.name);
|
|
163
|
-
// Only single-column unique constraints, and only over a real column (not an expression)
|
|
164
|
-
const [column] = columns;
|
|
165
|
-
if (columns.length === 1 && column.name !== null) {
|
|
166
|
-
uniqueColumns.add(column.name);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
return uniqueColumns;
|
|
171
|
-
}
|
|
172
102
|
/** libSQL's `libsql_vector_idx(col, 'metric=...')`, read back from the statement that created it. */
|
|
173
|
-
|
|
174
|
-
const
|
|
175
|
-
/*sql*/ `SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ?`, [indexName]);
|
|
176
|
-
const column = row?.sql?.match(/libsql_vector_idx\s*\(\s*[`"[]?([^`"\],\s)]+)/i)?.[1];
|
|
103
|
+
vectorIndex(name, sql) {
|
|
104
|
+
const column = sql.match(/libsql_vector_idx\s*\(\s*[`"[]?([^`"\],\s)]+)/i)?.[1];
|
|
177
105
|
if (!column) {
|
|
178
106
|
return undefined;
|
|
179
107
|
}
|
|
180
|
-
const metric =
|
|
108
|
+
const metric = sql.match(/'metric=(\w+)'/i)?.[1]?.toLowerCase();
|
|
181
109
|
return {
|
|
182
|
-
name
|
|
110
|
+
name,
|
|
183
111
|
entries: [{ column }],
|
|
184
112
|
unique: false,
|
|
185
113
|
type: 'vector',
|
|
@@ -250,6 +178,18 @@ function tableEntries(ddl) {
|
|
|
250
178
|
});
|
|
251
179
|
return [...entries, body.slice(start)];
|
|
252
180
|
}
|
|
181
|
+
/** A partial index's predicate: what follows `WHERE` after its column list, as the statement gave it. */
|
|
182
|
+
function indexPredicate(sql) {
|
|
183
|
+
let rest = '';
|
|
184
|
+
scan(sql, (char, index, depth) => {
|
|
185
|
+
const closes = char === ')' && depth === 0;
|
|
186
|
+
if (closes) {
|
|
187
|
+
rest = sql.slice(index + 1);
|
|
188
|
+
}
|
|
189
|
+
return closes;
|
|
190
|
+
});
|
|
191
|
+
return /^\s*WHERE\s+([\s\S]+)$/i.exec(rest)?.[1].trim();
|
|
192
|
+
}
|
|
253
193
|
/** What a leading `(` encloses, its own nesting and quoting respected. */
|
|
254
194
|
function parenthesized(text) {
|
|
255
195
|
let end = text.length;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { IndexFacet } from '../../schema/indexDifferences.js';
|
|
2
|
+
import { SchemaAST } from '../../schema/schemaAST.js';
|
|
3
|
+
import type { ColumnRenames, DialectName, TableSchema } from '../../type/index.js';
|
|
4
|
+
/** Where the tables were read: the engine, the one schema read, and what its catalogue reports about an index. */
|
|
5
|
+
export type ReadFrom = {
|
|
6
|
+
readonly dialectName: DialectName;
|
|
7
|
+
readonly schema?: string;
|
|
8
|
+
readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
9
|
+
};
|
|
10
|
+
/** The tables an introspector read, as a {@link SchemaAST}: every table first, so a foreign key reaches one read after it. */
|
|
11
|
+
export declare function tableSchemasToAST(schemas: readonly TableSchema[], from: ReadFrom): SchemaAST;
|
|
12
|
+
/** `table` with each column `renames` names under its new name, wherever the table names it. */
|
|
13
|
+
export declare function renamedTable(table: TableSchema, renames: ColumnRenames, schema?: string): TableSchema;
|
|
@@ -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
|
|
72
|
+
return builderOn(new SqlSchemaGenerator(querier.dialect), sqlSession(querier).run);
|
|
73
73
|
}
|
|
74
74
|
if (isMongoQuerier(querier)) {
|
|
75
|
-
return
|
|
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
|
-
|
|
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
|
|
@@ -105,8 +105,6 @@ export declare class Migrator {
|
|
|
105
105
|
* settle instead of colliding; an existing one still pays for introspection, as a diff needs columns.
|
|
106
106
|
*/
|
|
107
107
|
private planEntity;
|
|
108
|
-
/** The diff for one entity against the table it already has, and none where the two agree. */
|
|
109
|
-
private alterFromEntity;
|
|
110
108
|
/** The configured entities, with `entity` among them however the migrator was built. */
|
|
111
109
|
private entitiesWith;
|
|
112
110
|
/** The introspector for a claimed schema, which is the connection's own where none is claimed. */
|
|
@@ -130,10 +128,7 @@ export declare class Migrator {
|
|
|
130
128
|
* Without `drop`, a column's drop is held too. A rebuilt table applies its diff whole, so holding any
|
|
131
129
|
* part of it holds the rebuild, and only what an `ALTER` adds goes ahead: a plain column, an index.
|
|
132
130
|
*/
|
|
133
|
-
|
|
134
|
-
safe?: boolean;
|
|
135
|
-
drop?: boolean;
|
|
136
|
-
}): SchemaDiff;
|
|
131
|
+
private filterDiff;
|
|
137
132
|
/** Runs the statements a generator wrote, in one transaction where the engine takes DDL in one. */
|
|
138
133
|
executeSyncStatements(statements: string[], options: {
|
|
139
134
|
logging?: boolean;
|
|
@@ -157,18 +152,6 @@ export declare class Migrator {
|
|
|
157
152
|
* Check if an object is a valid migration
|
|
158
153
|
*/
|
|
159
154
|
isMigration(obj: unknown): obj is MigrationDefinition<Querier>;
|
|
160
|
-
/**
|
|
161
|
-
* Extract migration name from filename
|
|
162
|
-
*/
|
|
163
|
-
getMigrationName(fileName: string): string;
|
|
164
|
-
/**
|
|
165
|
-
* Generate timestamp string for migration names
|
|
166
|
-
*/
|
|
167
|
-
protected getTimestamp(): string;
|
|
168
|
-
/**
|
|
169
|
-
* Convert a string to a slug for filenames
|
|
170
|
-
*/
|
|
171
|
-
protected slugify(text: string): string;
|
|
172
155
|
}
|
|
173
156
|
/**
|
|
174
157
|
* Helper function to define a migration with proper typing
|
package/dist/migrate/migrator.js
CHANGED
|
@@ -2,7 +2,7 @@ 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/
|
|
5
|
+
import { SchemaAST } from '../schema/schemaAST.js';
|
|
6
6
|
import { columnRenames, tableRenameCandidates } from '../schema/schemaASTDiffer.js';
|
|
7
7
|
import { definedEntries, isRecord, LoggerWrapper } from '../util/index.js';
|
|
8
8
|
import { isOwnedName, qualifyName } from '../util/sql.util.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
|
-
|
|
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
|
|
68
|
-
return
|
|
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
|
-
|
|
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
|
|
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, `${
|
|
165
|
+
const filePath = join(this.migrationsPath, `${timestamp()}_${slugify(name)}.ts`);
|
|
168
166
|
const content = buildMigrationModule({
|
|
169
167
|
migrationName: name,
|
|
170
168
|
createdAt: new Date(),
|
|
@@ -294,7 +292,7 @@ export class Migrator {
|
|
|
294
292
|
this.noteForeignChecks(desiredAst, ast);
|
|
295
293
|
}
|
|
296
294
|
return this.entities.flatMap((entity) => {
|
|
297
|
-
const tableName =
|
|
295
|
+
const tableName = this.tableOf(entity);
|
|
298
296
|
const diff = generator.diffSchema(entity, ast.getTable(tableName), desiredAst, renames.get(tableName));
|
|
299
297
|
return diff ? [diff] : [];
|
|
300
298
|
});
|
|
@@ -355,7 +353,7 @@ export class Migrator {
|
|
|
355
353
|
const meta = getMeta(entity);
|
|
356
354
|
const { dialect } = this.pool;
|
|
357
355
|
const introspector = this.schemaIntrospectorFor(dialect.resolveSchema(meta));
|
|
358
|
-
const tableName =
|
|
356
|
+
const tableName = this.tableOf(entity);
|
|
359
357
|
if (!(await introspector.tableExists(dialect.resolveTableAlias(meta)))) {
|
|
360
358
|
// Spanning the whole set, so a foreign key resolves against the tables it points at, and always
|
|
361
359
|
// including this entity: `only` is what keeps the statements to this table.
|
|
@@ -363,16 +361,10 @@ export class Migrator {
|
|
|
363
361
|
}
|
|
364
362
|
// With the tables it references, which its foreign keys resolve against.
|
|
365
363
|
const ast = await this.introspectEntities([entity, ...referencedEntities(meta)]);
|
|
366
|
-
const
|
|
364
|
+
const diff = generator.diffSchema(entity, ast.getTable(tableName), generator.buildAST?.(this.entitiesWith(entity)));
|
|
365
|
+
const altered = diff?.type === 'alter' ? [this.filterDiff(diff, options)] : [];
|
|
367
366
|
await this.assertFillable(altered);
|
|
368
|
-
return altered.flatMap((
|
|
369
|
-
}
|
|
370
|
-
/** The diff for one entity against the table it already has, and none where the two agree. */
|
|
371
|
-
alterFromEntity(generator, entity, table, options) {
|
|
372
|
-
// Spanning the set for the reason `planEntity` spells out: a foreign key needs the table it
|
|
373
|
-
// points at, which a sync of one entity outside the configured list would not otherwise have.
|
|
374
|
-
const diff = generator.diffSchema(entity, table, generator.buildAST?.(this.entitiesWith(entity)));
|
|
375
|
-
return diff?.type === 'alter' ? [this.filterDiff(diff, options)] : [];
|
|
367
|
+
return altered.flatMap((change) => generator.generateAlterTable(change));
|
|
376
368
|
}
|
|
377
369
|
/** The configured entities, with `entity` among them however the migrator was built. */
|
|
378
370
|
entitiesWith(entity) {
|
|
@@ -479,8 +471,8 @@ export class Migrator {
|
|
|
479
471
|
* Get migration status
|
|
480
472
|
*/
|
|
481
473
|
async status() {
|
|
482
|
-
const
|
|
483
|
-
return { pending, executed };
|
|
474
|
+
const { pending, executed } = await this.journal();
|
|
475
|
+
return { pending: pending.map((migration) => migration.name), executed };
|
|
484
476
|
}
|
|
485
477
|
/**
|
|
486
478
|
* Get migration files from the migrations directory
|
|
@@ -511,7 +503,7 @@ export class Migrator {
|
|
|
511
503
|
const migration = module.default ?? module;
|
|
512
504
|
if (this.isMigration(migration)) {
|
|
513
505
|
return {
|
|
514
|
-
name:
|
|
506
|
+
name: basename(fileName, extname(fileName)),
|
|
515
507
|
up: migration.up.bind(migration),
|
|
516
508
|
down: migration.down.bind(migration),
|
|
517
509
|
transaction: migration.transaction,
|
|
@@ -531,35 +523,19 @@ export class Migrator {
|
|
|
531
523
|
isMigration(obj) {
|
|
532
524
|
return isRecord(obj) && typeof obj['up'] === 'function' && typeof obj['down'] === 'function';
|
|
533
525
|
}
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
String(now.getMonth() + 1).padStart(2, '0'),
|
|
548
|
-
String(now.getDate()).padStart(2, '0'),
|
|
549
|
-
String(now.getHours()).padStart(2, '0'),
|
|
550
|
-
String(now.getMinutes()).padStart(2, '0'),
|
|
551
|
-
String(now.getSeconds()).padStart(2, '0'),
|
|
552
|
-
].join('');
|
|
553
|
-
}
|
|
554
|
-
/**
|
|
555
|
-
* Convert a string to a slug for filenames
|
|
556
|
-
*/
|
|
557
|
-
slugify(text) {
|
|
558
|
-
return text
|
|
559
|
-
.toLowerCase()
|
|
560
|
-
.replace(/[^a-z0-9]+/g, '_')
|
|
561
|
-
.replace(/^_+|_+$/g, '');
|
|
562
|
-
}
|
|
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, '');
|
|
563
539
|
}
|
|
564
540
|
/**
|
|
565
541
|
* Helper function to define a migration with proper typing
|
|
@@ -20,8 +20,6 @@ export declare function needsRebuild(diff: SchemaDiff): boolean;
|
|
|
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
23
|
/**
|
|
26
24
|
* `diff` undone: every change swapped, which is what a migration's `down` runs. Renames run before the adds
|
|
27
25
|
* either way, so a key, index or foreign key put back names a renamed column as the rename back leaves it.
|
|
@@ -49,7 +49,7 @@ export function nonEmpty(items) {
|
|
|
49
49
|
return items.length ? items : undefined;
|
|
50
50
|
}
|
|
51
51
|
/** `change` undone: an add becomes a drop, a drop an add, and an alter runs the other way. */
|
|
52
|
-
|
|
52
|
+
function swap({ from, to }) {
|
|
53
53
|
return { from: to, to: from };
|
|
54
54
|
}
|
|
55
55
|
/**
|