uql-orm 0.97.0 → 0.99.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dialect/abstractSqlDialect.d.ts +1 -7
- package/dist/dialect/abstractSqlDialect.js +1 -9
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +2 -0
- package/dist/dialect/mysqlLikeSqlDialect.js +4 -1
- package/dist/dialect/pgLikeSqlDialect.js +0 -1
- package/dist/migrate/acquireQuerierForMigrations.d.ts +4 -8
- package/dist/migrate/acquireQuerierForMigrations.js +10 -18
- package/dist/migrate/builder/columnBuilder.d.ts +1 -2
- package/dist/migrate/builder/columnBuilder.js +3 -10
- package/dist/migrate/builder/migrationBuilder.d.ts +6 -26
- package/dist/migrate/builder/migrationBuilder.js +21 -78
- package/dist/migrate/builder/tableBuilder.js +2 -3
- package/dist/migrate/builder/types.d.ts +57 -120
- package/dist/migrate/cli-config.d.ts +1 -0
- package/dist/migrate/cli-config.js +11 -26
- package/dist/migrate/cli.d.ts +0 -13
- package/dist/migrate/cli.js +55 -118
- package/dist/migrate/codegen/entityCodeGenerator.d.ts +7 -61
- package/dist/migrate/codegen/entityCodeGenerator.js +51 -168
- package/dist/migrate/codegen/fieldOptionsSource.js +3 -6
- package/dist/migrate/codegen/index.d.ts +2 -7
- package/dist/migrate/codegen/index.js +2 -8
- package/dist/migrate/codegen/migrationFile.d.ts +1 -8
- package/dist/migrate/codegen/migrationFile.js +9 -13
- package/dist/migrate/ddl/defaultSql.d.ts +2 -1
- package/dist/migrate/ddl/defaultSql.js +1 -3
- package/dist/migrate/ddl/indexCapabilities.d.ts +18 -0
- package/dist/migrate/ddl/indexCapabilities.js +60 -0
- package/dist/migrate/ddl/indexDdl.d.ts +11 -24
- package/dist/migrate/ddl/indexDdl.js +22 -28
- package/dist/migrate/ddl/mssqlIndexDdl.d.ts +1 -7
- package/dist/migrate/ddl/mssqlIndexDdl.js +1 -7
- package/dist/migrate/ddl/mssqlTableDdl.d.ts +6 -9
- package/dist/migrate/ddl/mssqlTableDdl.js +14 -17
- package/dist/migrate/ddl/mysqlIndexDdl.d.ts +0 -21
- package/dist/migrate/ddl/mysqlIndexDdl.js +4 -48
- package/dist/migrate/ddl/mysqlTableDdl.d.ts +6 -5
- package/dist/migrate/ddl/mysqlTableDdl.js +15 -10
- package/dist/migrate/ddl/pgIndexDdl.d.ts +0 -10
- package/dist/migrate/ddl/pgIndexDdl.js +5 -44
- package/dist/migrate/ddl/pgTableDdl.d.ts +6 -2
- package/dist/migrate/ddl/pgTableDdl.js +17 -1
- package/dist/migrate/ddl/sqliteIndexDdl.js +2 -7
- package/dist/migrate/ddl/tableDdl.d.ts +43 -24
- package/dist/migrate/ddl/tableDdl.js +56 -37
- package/dist/migrate/ddl/tableRebuild.d.ts +2 -1
- package/dist/migrate/drift/driftDetector.d.ts +29 -32
- package/dist/migrate/drift/driftDetector.js +139 -205
- package/dist/migrate/drift/index.d.ts +1 -6
- package/dist/migrate/drift/index.js +1 -6
- package/dist/migrate/generator/definitionToNode.js +3 -3
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +53 -76
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +68 -90
- package/dist/migrate/introspection/mssqlIntrospector.d.ts +8 -43
- package/dist/migrate/introspection/mssqlIntrospector.js +44 -77
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +11 -38
- package/dist/migrate/introspection/mysqlIntrospector.js +48 -57
- package/dist/migrate/introspection/postgresIntrospector.d.ts +12 -60
- package/dist/migrate/introspection/postgresIntrospector.js +62 -77
- package/dist/migrate/introspection/sqliteIntrospector.d.ts +14 -53
- package/dist/migrate/introspection/sqliteIntrospector.js +59 -119
- package/dist/migrate/introspection/tableSchemaAST.d.ts +13 -0
- package/dist/migrate/introspection/tableSchemaAST.js +85 -0
- package/dist/migrate/migrationTarget.js +10 -2
- package/dist/migrate/migrator.d.ts +14 -37
- package/dist/migrate/migrator.js +89 -126
- package/dist/migrate/schemaChange.d.ts +0 -2
- package/dist/migrate/schemaChange.js +1 -1
- package/dist/migrate/schemaGenerator.d.ts +22 -97
- package/dist/migrate/schemaGenerator.js +113 -298
- package/dist/migrate/storage/databaseStorage.d.ts +2 -1
- package/dist/migrate/storage/databaseStorage.js +3 -2
- package/dist/migrate/storage/jsonStorage.d.ts +1 -2
- package/dist/migrate/storage/jsonStorage.js +11 -20
- package/dist/migrate/triggerSql.d.ts +3 -3
- package/dist/migrate/triggerSql.js +5 -4
- package/dist/mongo/index.d.ts +0 -1
- package/dist/mongo/index.js +0 -1
- package/dist/mongo/mongoDialect.js +0 -1
- package/dist/mongo/mongoIntrospector.d.ts +0 -3
- package/dist/mongo/mongoIntrospector.js +7 -14
- package/dist/mongo/mongoSchemaGenerator.d.ts +7 -20
- package/dist/mongo/mongoSchemaGenerator.js +51 -70
- package/dist/mssql/mssqlDialect.js +0 -1
- package/dist/schema/canonicalType.d.ts +15 -19
- package/dist/schema/canonicalType.js +20 -43
- package/dist/schema/dependencyGraph.d.ts +2 -3
- package/dist/schema/dependencyGraph.js +0 -4
- package/dist/schema/indexDifferences.js +2 -2
- package/dist/schema/schemaAST.d.ts +1 -1
- package/dist/schema/schemaAST.js +4 -12
- package/dist/schema/schemaASTBuilder.d.ts +2 -5
- package/dist/schema/schemaASTBuilder.js +11 -14
- package/dist/schema/schemaASTDiffer.d.ts +4 -5
- package/dist/schema/schemaASTDiffer.js +22 -70
- package/dist/schema/types.d.ts +1 -48
- package/dist/sqlite/localSqliteQuerierPool.d.ts +16 -2
- package/dist/sqlite/localSqliteQuerierPool.js +20 -4
- package/dist/sqlite/nodeSqliteQuerierPool.js +2 -2
- package/dist/sqlite/sqliteDialect.js +0 -1
- package/dist/sqlite/sqliteQuerierPool.js +3 -3
- package/dist/type/dialect.d.ts +0 -2
- package/dist/type/migration.d.ts +3 -54
- package/dist/util/ddlExpression.util.d.ts +2 -2
- package/dist/util/ddlExpression.util.js +3 -3
- package/dist/util/sql.util.d.ts +0 -8
- package/dist/util/sql.util.js +7 -12
- package/package.json +1 -1
- package/dist/migrate/introspection/baseSqlIntrospector.d.ts +0 -36
- package/dist/migrate/introspection/baseSqlIntrospector.js +0 -133
- package/dist/schema/index.d.ts +0 -2
- package/dist/schema/index.js +0 -1
- package/dist/sqlite/sqlitePragmas.d.ts +0 -3
- package/dist/sqlite/sqlitePragmas.js +0 -15
|
@@ -1,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
|
+
}
|
|
@@ -19,13 +19,12 @@ export declare class Migrator {
|
|
|
19
19
|
/** The schema generator, loaded on first use: MongoDB's needs its optional peer. */
|
|
20
20
|
getSchemaGenerator(): Promise<SchemaGenerator>;
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* The migration files by name, the names the storage records as run, and those on disk it does not, all in
|
|
23
|
+
* name order. Read from names alone: a file is imported only when it is about to run.
|
|
23
24
|
*/
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
*/
|
|
28
|
-
pending(): Promise<Migration<Querier>[]>;
|
|
25
|
+
private journal;
|
|
26
|
+
/** The names of the migrations not yet run, in the order `up` runs them. */
|
|
27
|
+
pending(): Promise<string[]>;
|
|
29
28
|
/**
|
|
30
29
|
* Get list of executed migrations
|
|
31
30
|
*/
|
|
@@ -44,7 +43,10 @@ export declare class Migrator {
|
|
|
44
43
|
to?: string;
|
|
45
44
|
step?: number;
|
|
46
45
|
}): Promise<MigrationResult[]>;
|
|
47
|
-
/**
|
|
46
|
+
/**
|
|
47
|
+
* Runs the names narrowed by `to`/`step`, stopping at the first failure: `up` over the pending, `down` over
|
|
48
|
+
* the executed reversed. Every one is loaded before any runs, so a file that cannot be leaves the database as it was.
|
|
49
|
+
*/
|
|
48
50
|
private runInOrder;
|
|
49
51
|
/**
|
|
50
52
|
* Run a single migration, in a transaction where the dialect has one for it and the migration has not
|
|
@@ -105,8 +107,6 @@ export declare class Migrator {
|
|
|
105
107
|
* settle instead of colliding; an existing one still pays for introspection, as a diff needs columns.
|
|
106
108
|
*/
|
|
107
109
|
private planEntity;
|
|
108
|
-
/** The diff for one entity against the table it already has, and none where the two agree. */
|
|
109
|
-
private alterFromEntity;
|
|
110
110
|
/** The configured entities, with `entity` among them however the migrator was built. */
|
|
111
111
|
private entitiesWith;
|
|
112
112
|
/** The introspector for a claimed schema, which is the connection's own where none is claimed. */
|
|
@@ -130,10 +130,7 @@ export declare class Migrator {
|
|
|
130
130
|
* Without `drop`, a column's drop is held too. A rebuilt table applies its diff whole, so holding any
|
|
131
131
|
* part of it holds the rebuild, and only what an `ALTER` adds goes ahead: a plain column, an index.
|
|
132
132
|
*/
|
|
133
|
-
|
|
134
|
-
safe?: boolean;
|
|
135
|
-
drop?: boolean;
|
|
136
|
-
}): SchemaDiff;
|
|
133
|
+
private filterDiff;
|
|
137
134
|
/** Runs the statements a generator wrote, in one transaction where the engine takes DDL in one. */
|
|
138
135
|
executeSyncStatements(statements: string[], options: {
|
|
139
136
|
logging?: boolean;
|
|
@@ -145,30 +142,10 @@ export declare class Migrator {
|
|
|
145
142
|
pending: string[];
|
|
146
143
|
executed: string[];
|
|
147
144
|
}>;
|
|
148
|
-
/**
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Load a migration from a file
|
|
154
|
-
*/
|
|
155
|
-
loadMigration(fileName: string): Promise<Migration<Querier> | undefined>;
|
|
156
|
-
/**
|
|
157
|
-
* Check if an object is a valid migration
|
|
158
|
-
*/
|
|
159
|
-
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;
|
|
145
|
+
/** The migration files in the migrations directory, by the name each runs as; none where it is missing. */
|
|
146
|
+
private migrationFiles;
|
|
147
|
+
/** The migration `name`, from its `file`: a missing file, one that fails to import, or one exporting no migration throws. */
|
|
148
|
+
private loadMigration;
|
|
172
149
|
}
|
|
173
150
|
/**
|
|
174
151
|
* Helper function to define a migration with proper typing
|