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,16 +1,25 @@
|
|
|
1
1
|
import { SqlExpression } from '../../schema/sqlExpression.js';
|
|
2
2
|
import { FOREIGN_KEY_ACTIONS } from '../../schema/types.js';
|
|
3
|
-
import { isSqlQuerier } from '../../type/index.js';
|
|
4
3
|
import { isOwnedName } from '../../util/sql.util.js';
|
|
5
|
-
import {
|
|
4
|
+
import { withSqlQuerierForMigrations } from '../acquireQuerierForMigrations.js';
|
|
6
5
|
import { knownDefault } from '../ddl/defaultSql.js';
|
|
7
|
-
import {
|
|
8
|
-
/** A SQL introspector: an engine
|
|
9
|
-
export class AbstractSqlSchemaIntrospector
|
|
6
|
+
import { renamedTable, tableSchemasToAST } from './tableSchemaAST.js';
|
|
7
|
+
/** A SQL introspector: an engine reads each part of a table from its catalogue (`get*`), on a {@link TableRowReader}. */
|
|
8
|
+
export class AbstractSqlSchemaIntrospector {
|
|
10
9
|
pool;
|
|
10
|
+
schema;
|
|
11
|
+
/** Columns and uniqueness only; each introspector opts in to what its catalogue queries report. */
|
|
12
|
+
indexFacets = new Set();
|
|
13
|
+
dialect;
|
|
14
|
+
/**
|
|
15
|
+
* `schema` is the one these queries read, `undefined` for the connection's own default. Every table
|
|
16
|
+
* reported is stamped with it, so a diff compares like with like: entity and database both say
|
|
17
|
+
* `undefined` for "wherever the connection points", and name a schema only when one was asked for.
|
|
18
|
+
*/
|
|
11
19
|
constructor(pool, schema) {
|
|
12
|
-
super(pool.dialect, schema);
|
|
13
20
|
this.pool = pool;
|
|
21
|
+
this.schema = schema;
|
|
22
|
+
this.dialect = pool.dialect;
|
|
14
23
|
}
|
|
15
24
|
/**
|
|
16
25
|
* The schema every catalogue query filters on, as SQL: the one that was asked for, as the engine's
|
|
@@ -25,6 +34,25 @@ export class AbstractSqlSchemaIntrospector extends BaseSqlIntrospector {
|
|
|
25
34
|
* an engine with no schemas, whose catalogue queries never reference one.
|
|
26
35
|
*/
|
|
27
36
|
defaultSchemaExpr = '';
|
|
37
|
+
/**
|
|
38
|
+
* The database as a {@link SchemaAST}, or just the tables named. A name nothing matches is left out
|
|
39
|
+
* rather than raised: the point of naming them is to read a database other things are still
|
|
40
|
+
* changing, where scanning every table is both wasted work and a relation that can vanish mid-scan.
|
|
41
|
+
*/
|
|
42
|
+
async introspect(tables, renames) {
|
|
43
|
+
const schemas = [];
|
|
44
|
+
for (const tableName of tables ?? (await this.getTableNames())) {
|
|
45
|
+
const schema = await this.getTableSchema(tableName);
|
|
46
|
+
if (schema) {
|
|
47
|
+
schemas.push(renames ? renamedTable(schema, renames, this.schema) : schema);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return tableSchemasToAST(schemas, {
|
|
51
|
+
dialectName: this.dialect.dialectName,
|
|
52
|
+
schema: this.schema,
|
|
53
|
+
indexFacets: this.indexFacets,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
28
56
|
async getTableSchema(tableName) {
|
|
29
57
|
return this.withSqlQuerier(async (querier) => {
|
|
30
58
|
const read = createTableRowReader(querier);
|
|
@@ -41,9 +69,14 @@ export class AbstractSqlSchemaIntrospector extends BaseSqlIntrospector {
|
|
|
41
69
|
this.getTriggers(read, tableName),
|
|
42
70
|
this.getDefinition(read, tableName),
|
|
43
71
|
]);
|
|
72
|
+
const unique = uniqueColumns(indexes);
|
|
44
73
|
return {
|
|
45
74
|
name: tableName,
|
|
46
|
-
columns
|
|
75
|
+
columns: columns.map((column) => ({
|
|
76
|
+
...column,
|
|
77
|
+
isPrimaryKey: primaryKey?.columns.includes(column.name) ?? false,
|
|
78
|
+
isUnique: unique.has(column.name),
|
|
79
|
+
})),
|
|
47
80
|
primaryKey,
|
|
48
81
|
indexes,
|
|
49
82
|
foreignKeys,
|
|
@@ -53,16 +86,23 @@ export class AbstractSqlSchemaIntrospector extends BaseSqlIntrospector {
|
|
|
53
86
|
};
|
|
54
87
|
});
|
|
55
88
|
}
|
|
56
|
-
/** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
|
|
57
|
-
async getDefinition(_read, _tableName) {
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
89
|
async getTableNames() {
|
|
61
90
|
return this.withSqlQuerier(async (querier) => {
|
|
62
|
-
const
|
|
63
|
-
return
|
|
91
|
+
const rows = await querier.all(this.getTableNamesQuery());
|
|
92
|
+
return rows.map((row) => row.table_name);
|
|
64
93
|
});
|
|
65
94
|
}
|
|
95
|
+
async tableExists(tableName) {
|
|
96
|
+
return this.withSqlQuerier((querier) => this.tableExistsInternal(createTableRowReader(querier), tableName));
|
|
97
|
+
}
|
|
98
|
+
/** On the querier migrations run on, which is not the app's on a libSQL embedded replica. */
|
|
99
|
+
withSqlQuerier(task) {
|
|
100
|
+
return withSqlQuerierForMigrations(this.pool, this.constructor.name, task);
|
|
101
|
+
}
|
|
102
|
+
/** Whether the base table exists: its query answers a row for it, none for a view or nothing. */
|
|
103
|
+
async tableExistsInternal(read, tableName) {
|
|
104
|
+
return (await read(this.tableExistsQuery(), [tableName])).length > 0;
|
|
105
|
+
}
|
|
66
106
|
/**
|
|
67
107
|
* The triggers uql installed on the table, each with the statements recreating it as the engine keeps it.
|
|
68
108
|
* Ownership is matched here rather than with `LIKE`, whose `_` wildcard would take in a hand-written one.
|
|
@@ -71,56 +111,19 @@ export class AbstractSqlSchemaIntrospector extends BaseSqlIntrospector {
|
|
|
71
111
|
const rows = await read(this.triggersQuery(), [tableName]);
|
|
72
112
|
return rows.flatMap(({ name, requires, definition }) => isOwnedName(name) ? [{ name, statements: [requires, definition].filter((sql) => typeof sql === 'string') }] : []);
|
|
73
113
|
}
|
|
74
|
-
|
|
75
|
-
|
|
114
|
+
/** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
|
|
115
|
+
async getDefinition(_read, _tableName) {
|
|
116
|
+
return undefined;
|
|
76
117
|
}
|
|
77
118
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
119
|
+
* The key `sql` lists a row of for each column, in key order: its `column_name`, and the `constraint_name`
|
|
120
|
+
* where the engine names the key's constraint. Only a `DROP` needs that name, and only the reported one will do.
|
|
80
121
|
*/
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return task(querier);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
async tableExistsInternal(read, tableName) {
|
|
90
|
-
const results = await read(this.tableExistsQuery(), this.tableExistsParams(tableName));
|
|
91
|
-
return this.parseTableExistsResult(results);
|
|
92
|
-
}
|
|
93
|
-
async getColumns(read, tableName) {
|
|
94
|
-
const results = await read(this.getColumnsQuery(tableName), this.getColumnsParams(tableName));
|
|
95
|
-
return this.mapColumnsResult(read, tableName, results);
|
|
96
|
-
}
|
|
97
|
-
async getIndexes(read, tableName) {
|
|
98
|
-
const results = await read(this.getIndexesQuery(tableName), this.getIndexesParams(tableName));
|
|
99
|
-
return this.mapIndexesResult(read, tableName, results);
|
|
100
|
-
}
|
|
101
|
-
async getForeignKeys(read, tableName) {
|
|
102
|
-
const results = await read(this.getForeignKeysQuery(tableName), this.getForeignKeysParams(tableName));
|
|
103
|
-
return this.mapForeignKeysResult(read, tableName, results);
|
|
104
|
-
}
|
|
105
|
-
async getPrimaryKey(read, tableName) {
|
|
106
|
-
const results = await read(this.getPrimaryKeyQuery(tableName), this.getPrimaryKeyParams(tableName));
|
|
107
|
-
const columns = this.mapPrimaryKeyResult(results);
|
|
108
|
-
return columns && { columns, name: this.mapPrimaryKeyName(results) };
|
|
109
|
-
}
|
|
110
|
-
tableExistsParams(tableName) {
|
|
111
|
-
return [tableName];
|
|
112
|
-
}
|
|
113
|
-
getColumnsParams(tableName) {
|
|
114
|
-
return [tableName];
|
|
115
|
-
}
|
|
116
|
-
getIndexesParams(tableName) {
|
|
117
|
-
return [tableName];
|
|
118
|
-
}
|
|
119
|
-
getForeignKeysParams(tableName) {
|
|
120
|
-
return [tableName];
|
|
121
|
-
}
|
|
122
|
-
getPrimaryKeyParams(tableName) {
|
|
123
|
-
return [tableName];
|
|
122
|
+
async readPrimaryKey(read, sql, tableName) {
|
|
123
|
+
const rows = await read(sql, [tableName]);
|
|
124
|
+
return rows.length
|
|
125
|
+
? { columns: rows.map((row) => row.column_name), name: rows[0].constraint_name ?? undefined }
|
|
126
|
+
: undefined;
|
|
124
127
|
}
|
|
125
128
|
/** The {@link ForeignKeyAction} a catalogue names, whatever its case, and `SET_NULL` as SQL Server spells it. */
|
|
126
129
|
normalizeReferentialAction(action) {
|
|
@@ -137,42 +140,13 @@ export class AbstractSqlSchemaIntrospector extends BaseSqlIntrospector {
|
|
|
137
140
|
onUpdate: this.normalizeReferentialAction(row.update_rule),
|
|
138
141
|
}));
|
|
139
142
|
}
|
|
140
|
-
/**
|
|
141
|
-
* Convert bigint/null values to number safely.
|
|
142
|
-
*/
|
|
143
|
+
/** A catalogue's number, which a driver may answer as a `bigint`, or `undefined` for none. */
|
|
143
144
|
toNumber(value) {
|
|
144
145
|
if (value == null || value === '') {
|
|
145
146
|
return undefined;
|
|
146
147
|
}
|
|
147
148
|
return Number(value);
|
|
148
149
|
}
|
|
149
|
-
/**
|
|
150
|
-
* Extract table name from a row returned by getTableNamesQuery.
|
|
151
|
-
*
|
|
152
|
-
* Defaults to `information_schema`'s own column, which is what every engine with an
|
|
153
|
-
* `information_schema` returns and what Postgres and MySQL both restated identically. SQLite reads
|
|
154
|
-
* `sqlite_master` instead and overrides.
|
|
155
|
-
*/
|
|
156
|
-
mapTableNameRow(row) {
|
|
157
|
-
return row['table_name'];
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Map primary key query results to column names, in key order. `information_schema` gives every SQL
|
|
161
|
-
* engine here a `column_name` per row; SQLite reads its key off `PRAGMA table_info` instead and
|
|
162
|
-
* overrides this.
|
|
163
|
-
*/
|
|
164
|
-
mapPrimaryKeyResult(results) {
|
|
165
|
-
const columns = results.map((row) => String(row['column_name']));
|
|
166
|
-
return columns.length ? columns : undefined;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* What the engine calls the key's constraint, where the query reported one. Only a `DROP` needs it,
|
|
170
|
-
* and only the reported name will do - see {@link PrimaryKeySchema.name}.
|
|
171
|
-
*/
|
|
172
|
-
mapPrimaryKeyName(results) {
|
|
173
|
-
const name = results[0]?.['constraint_name'];
|
|
174
|
-
return name === undefined || name === null ? undefined : String(name);
|
|
175
|
-
}
|
|
176
150
|
/**
|
|
177
151
|
* A SQL default, wrapped in parentheses, or the value uql exports that it spells (`currentTimestamp`), so it
|
|
178
152
|
* compares equal on every engine and `generate:from-db` writes it by name.
|
|
@@ -181,6 +155,10 @@ export class AbstractSqlSchemaIntrospector extends BaseSqlIntrospector {
|
|
|
181
155
|
return knownDefault(SqlExpression.parenthesized(sql), this.dialect);
|
|
182
156
|
}
|
|
183
157
|
}
|
|
158
|
+
/** The columns a unique index makes unique alone: over that one plain column, unfiltered. The key's is not among them. */
|
|
159
|
+
function uniqueColumns(indexes) {
|
|
160
|
+
return new Set(indexes.flatMap(({ unique, where, entries }) => unique && !where && entries.length === 1 && !entries[0].expression ? [entries[0].column] : []));
|
|
161
|
+
}
|
|
184
162
|
/** A {@link TableRowReader} over one querier: the same statement is only ever sent once. */
|
|
185
163
|
function createTableRowReader(querier) {
|
|
186
164
|
const sent = new Map();
|
|
@@ -1,57 +1,22 @@
|
|
|
1
1
|
import type { CheckSchema } from '../../schema/types.js';
|
|
2
|
-
import type {
|
|
3
|
-
import { AbstractSqlSchemaIntrospector, type
|
|
4
|
-
/**
|
|
5
|
-
* SQL Server schema introspector.
|
|
6
|
-
*
|
|
7
|
-
* `INFORMATION_SCHEMA` answers columns and foreign keys, but not indexes: it has no view for them at
|
|
8
|
-
* all, and its `CONSTRAINT_COLUMN_USAGE` conflates a unique index with a unique constraint. Those
|
|
9
|
-
* come from `sys.indexes` instead, which is also the only place the filtered-index predicate and the
|
|
10
|
-
* included columns are readable.
|
|
11
|
-
*/
|
|
2
|
+
import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema } from '../../type/index.js';
|
|
3
|
+
import { AbstractSqlSchemaIntrospector, type ReadColumn, type TableRowReader } from './abstractSqlSchemaIntrospector.js';
|
|
4
|
+
/** SQL Server schema introspector: `INFORMATION_SCHEMA` has no view of indexes, which come from `sys.indexes`. */
|
|
12
5
|
export declare class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
13
6
|
protected readonly defaultSchemaExpr = "SCHEMA_NAME()";
|
|
14
7
|
protected triggersQuery(): string;
|
|
15
8
|
protected getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
|
|
16
9
|
protected getTableNamesQuery(): string;
|
|
17
10
|
protected tableExistsQuery(): string;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}[]): boolean;
|
|
21
|
-
/**
|
|
22
|
-
* From `sys` rather than `INFORMATION_SCHEMA`, which has no identity flag and no per-column view of
|
|
23
|
-
* the key or of a unique index. A key column is not also `isUnique`, as on MySQL: only a unique index
|
|
24
|
-
* of its own, other than the key's, makes it so.
|
|
25
|
-
*/
|
|
26
|
-
protected getColumnsQuery(_tableName: string): string;
|
|
11
|
+
/** From `sys` rather than `INFORMATION_SCHEMA`, which has no identity flag. */
|
|
12
|
+
protected getColumns(read: TableRowReader, tableName: string): Promise<ReadColumn[]>;
|
|
27
13
|
/** `is_primary_key` is excluded: the key is read separately, the way every other engine reads it. */
|
|
28
|
-
protected
|
|
29
|
-
protected
|
|
30
|
-
protected
|
|
31
|
-
protected mapColumnsResult(_read: TableRowReader, _tableName: string, results: MsSqlColumnRow[]): Promise<ColumnSchema[]>;
|
|
32
|
-
protected mapIndexesResult(_read: TableRowReader, _tableName: string, results: {
|
|
33
|
-
index_name: string;
|
|
34
|
-
columns: string;
|
|
35
|
-
is_unique: boolean;
|
|
36
|
-
}[]): Promise<IndexSchema[]>;
|
|
37
|
-
protected mapForeignKeysResult(_read: TableRowReader, _tableName: string, results: JoinedForeignKeyRow[]): Promise<ForeignKeySchema[]>;
|
|
14
|
+
protected getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
|
|
15
|
+
protected getForeignKeys(read: TableRowReader, tableName: string): Promise<ForeignKeySchema[]>;
|
|
16
|
+
protected getPrimaryKey(read: TableRowReader, tableName: string): Promise<PrimaryKeySchema | undefined>;
|
|
38
17
|
/**
|
|
39
18
|
* The engine reprints a default from its own parse tree: all of it in one pair of parentheses, and a
|
|
40
19
|
* number in a second, so `((0))`, `((1)+(2))`, `(N'x')` and `(getdate())`.
|
|
41
20
|
*/
|
|
42
21
|
protected parseDefaultValue(defaultValue: string | null): unknown;
|
|
43
22
|
}
|
|
44
|
-
type MsSqlColumnRow = {
|
|
45
|
-
column_name: string;
|
|
46
|
-
data_type: string;
|
|
47
|
-
max_length: number | null;
|
|
48
|
-
numeric_precision: number | null;
|
|
49
|
-
numeric_scale: number | null;
|
|
50
|
-
is_nullable: boolean;
|
|
51
|
-
is_identity: boolean;
|
|
52
|
-
column_default: string | null;
|
|
53
|
-
is_primary_key: number | null;
|
|
54
|
-
is_unique: number | null;
|
|
55
|
-
generated_as: string | null;
|
|
56
|
-
};
|
|
57
|
-
export {};
|
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
import { AbstractSqlSchemaIntrospector, } from './abstractSqlSchemaIntrospector.js';
|
|
2
|
-
/**
|
|
3
|
-
* SQL Server schema introspector.
|
|
4
|
-
*
|
|
5
|
-
* `INFORMATION_SCHEMA` answers columns and foreign keys, but not indexes: it has no view for them at
|
|
6
|
-
* all, and its `CONSTRAINT_COLUMN_USAGE` conflates a unique index with a unique constraint. Those
|
|
7
|
-
* come from `sys.indexes` instead, which is also the only place the filtered-index predicate and the
|
|
8
|
-
* included columns are readable.
|
|
9
|
-
*/
|
|
2
|
+
/** SQL Server schema introspector: `INFORMATION_SCHEMA` has no view of indexes, which come from `sys.indexes`. */
|
|
10
3
|
export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
11
4
|
defaultSchemaExpr = 'SCHEMA_NAME()';
|
|
12
5
|
triggersQuery() {
|
|
@@ -37,22 +30,14 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
37
30
|
}
|
|
38
31
|
tableExistsQuery() {
|
|
39
32
|
return /*sql*/ `
|
|
40
|
-
SELECT
|
|
41
|
-
|
|
42
|
-
WHERE TABLE_SCHEMA = ${this.schemaExpr}
|
|
43
|
-
AND TABLE_NAME = @p1
|
|
33
|
+
SELECT 1 FROM INFORMATION_SCHEMA.TABLES
|
|
34
|
+
WHERE TABLE_SCHEMA = ${this.schemaExpr} AND TABLE_NAME = @p1 AND TABLE_TYPE = 'BASE TABLE'
|
|
44
35
|
`;
|
|
45
36
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
* From `sys` rather than `INFORMATION_SCHEMA`, which has no identity flag and no per-column view of
|
|
51
|
-
* the key or of a unique index. A key column is not also `isUnique`, as on MySQL: only a unique index
|
|
52
|
-
* of its own, other than the key's, makes it so.
|
|
53
|
-
*/
|
|
54
|
-
getColumnsQuery(_tableName) {
|
|
55
|
-
return /*sql*/ `
|
|
37
|
+
/** From `sys` rather than `INFORMATION_SCHEMA`, which has no identity flag. */
|
|
38
|
+
async getColumns(read, tableName) {
|
|
39
|
+
const rows = await read(
|
|
40
|
+
/*sql*/ `
|
|
56
41
|
SELECT
|
|
57
42
|
c.name as column_name,
|
|
58
43
|
t.name as data_type,
|
|
@@ -62,8 +47,6 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
62
47
|
c.is_nullable as is_nullable,
|
|
63
48
|
c.is_identity as is_identity,
|
|
64
49
|
d.definition as column_default,
|
|
65
|
-
f.is_primary_key,
|
|
66
|
-
f.is_unique,
|
|
67
50
|
CASE WHEN cc.is_persisted = 1 THEN cc.definition END as generated_as
|
|
68
51
|
FROM sys.columns c
|
|
69
52
|
JOIN sys.objects o ON o.object_id = c.object_id
|
|
@@ -71,29 +54,33 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
71
54
|
JOIN sys.types t ON t.user_type_id = c.user_type_id
|
|
72
55
|
LEFT JOIN sys.default_constraints d ON d.object_id = c.default_object_id
|
|
73
56
|
LEFT JOIN sys.computed_columns cc ON cc.object_id = c.object_id AND cc.column_id = c.column_id
|
|
74
|
-
OUTER APPLY (
|
|
75
|
-
SELECT
|
|
76
|
-
MAX(CAST(i.is_primary_key AS INT)) as is_primary_key,
|
|
77
|
-
MAX(CASE WHEN i.is_primary_key = 0 AND n.key_columns = 1 THEN 1 ELSE 0 END) as is_unique
|
|
78
|
-
FROM sys.index_columns ic
|
|
79
|
-
JOIN sys.indexes i ON i.object_id = ic.object_id AND i.index_id = ic.index_id
|
|
80
|
-
CROSS APPLY (
|
|
81
|
-
SELECT COUNT(*) as key_columns FROM sys.index_columns k
|
|
82
|
-
WHERE k.object_id = i.object_id AND k.index_id = i.index_id AND k.is_included_column = 0
|
|
83
|
-
) n
|
|
84
|
-
WHERE ic.object_id = c.object_id AND ic.column_id = c.column_id
|
|
85
|
-
AND ic.is_included_column = 0 AND i.is_unique = 1
|
|
86
|
-
) f
|
|
87
57
|
WHERE s.name = ${this.schemaExpr} AND o.name = @p1
|
|
88
58
|
ORDER BY c.column_id
|
|
89
|
-
|
|
59
|
+
`, [tableName]);
|
|
60
|
+
return rows.map((row) => {
|
|
61
|
+
const type = row.data_type.toUpperCase();
|
|
62
|
+
const bytes = this.toNumber(row.max_length);
|
|
63
|
+
return {
|
|
64
|
+
name: row.column_name,
|
|
65
|
+
type: spelledType(type, bytes, this.toNumber(row.numeric_scale)),
|
|
66
|
+
nullable: Boolean(row.is_nullable),
|
|
67
|
+
defaultValue: this.parseDefaultValue(row.column_default),
|
|
68
|
+
isAutoIncrement: Boolean(row.is_identity),
|
|
69
|
+
length: widthOf(type, bytes),
|
|
70
|
+
precision: NUMERIC_TYPES.has(type) ? this.toNumber(row.numeric_precision) : undefined,
|
|
71
|
+
scale: NUMERIC_TYPES.has(type) ? this.toNumber(row.numeric_scale) : undefined,
|
|
72
|
+
generatedAs: row.generated_as ?? undefined,
|
|
73
|
+
};
|
|
74
|
+
});
|
|
90
75
|
}
|
|
91
76
|
/** `is_primary_key` is excluded: the key is read separately, the way every other engine reads it. */
|
|
92
|
-
|
|
93
|
-
|
|
77
|
+
async getIndexes(read, tableName) {
|
|
78
|
+
const rows = await read(
|
|
79
|
+
/*sql*/ `
|
|
94
80
|
SELECT
|
|
95
81
|
i.name as index_name,
|
|
96
82
|
i.is_unique as is_unique,
|
|
83
|
+
i.filter_definition as filter_definition,
|
|
97
84
|
STRING_AGG(c.name, ',') WITHIN GROUP (ORDER BY ic.key_ordinal) as columns
|
|
98
85
|
FROM sys.indexes i
|
|
99
86
|
JOIN sys.objects o ON o.object_id = i.object_id
|
|
@@ -102,12 +89,19 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
102
89
|
JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
|
|
103
90
|
WHERE s.name = ${this.schemaExpr} AND o.name = @p1
|
|
104
91
|
AND i.is_primary_key = 0 AND i.name IS NOT NULL AND ic.is_included_column = 0
|
|
105
|
-
GROUP BY i.name, i.is_unique
|
|
92
|
+
GROUP BY i.name, i.is_unique, i.filter_definition
|
|
106
93
|
ORDER BY i.name
|
|
107
|
-
|
|
94
|
+
`, [tableName]);
|
|
95
|
+
return rows.map((row) => ({
|
|
96
|
+
name: row.index_name,
|
|
97
|
+
entries: row.columns.split(',').map((column) => ({ column })),
|
|
98
|
+
unique: Boolean(row.is_unique),
|
|
99
|
+
where: row.filter_definition ?? undefined,
|
|
100
|
+
}));
|
|
108
101
|
}
|
|
109
|
-
|
|
110
|
-
|
|
102
|
+
async getForeignKeys(read, tableName) {
|
|
103
|
+
const rows = await read(
|
|
104
|
+
/*sql*/ `
|
|
111
105
|
SELECT
|
|
112
106
|
fk.name as constraint_name,
|
|
113
107
|
STRING_AGG(pc.name, ',') WITHIN GROUP (ORDER BY fkc.constraint_column_id) as columns,
|
|
@@ -125,10 +119,12 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
125
119
|
WHERE s.name = ${this.schemaExpr} AND o.name = @p1
|
|
126
120
|
GROUP BY fk.name, rt.name, fk.delete_referential_action_desc, fk.update_referential_action_desc
|
|
127
121
|
ORDER BY fk.name
|
|
128
|
-
|
|
122
|
+
`, [tableName]);
|
|
123
|
+
return this.joinedForeignKeys(rows);
|
|
129
124
|
}
|
|
130
|
-
|
|
131
|
-
return
|
|
125
|
+
getPrimaryKey(read, tableName) {
|
|
126
|
+
return this.readPrimaryKey(read,
|
|
127
|
+
/*sql*/ `
|
|
132
128
|
SELECT c.name as column_name, i.name as constraint_name
|
|
133
129
|
FROM sys.indexes i
|
|
134
130
|
JOIN sys.objects o ON o.object_id = i.object_id
|
|
@@ -137,36 +133,7 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
137
133
|
JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
|
|
138
134
|
WHERE s.name = ${this.schemaExpr} AND o.name = @p1 AND i.is_primary_key = 1
|
|
139
135
|
ORDER BY ic.key_ordinal
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
async mapColumnsResult(_read, _tableName, results) {
|
|
143
|
-
return results.map((row) => {
|
|
144
|
-
const type = row.data_type.toUpperCase();
|
|
145
|
-
const bytes = this.toNumber(row.max_length);
|
|
146
|
-
return {
|
|
147
|
-
name: row.column_name,
|
|
148
|
-
type: spelledType(type, bytes, this.toNumber(row.numeric_scale)),
|
|
149
|
-
nullable: Boolean(row.is_nullable),
|
|
150
|
-
defaultValue: this.parseDefaultValue(row.column_default),
|
|
151
|
-
isAutoIncrement: Boolean(row.is_identity),
|
|
152
|
-
isPrimaryKey: Boolean(row.is_primary_key),
|
|
153
|
-
isUnique: Boolean(row.is_unique),
|
|
154
|
-
length: widthOf(type, bytes),
|
|
155
|
-
precision: NUMERIC_TYPES.has(type) ? this.toNumber(row.numeric_precision) : undefined,
|
|
156
|
-
scale: NUMERIC_TYPES.has(type) ? this.toNumber(row.numeric_scale) : undefined,
|
|
157
|
-
generatedAs: row.generated_as ?? undefined,
|
|
158
|
-
};
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
async mapIndexesResult(_read, _tableName, results) {
|
|
162
|
-
return results.map((row) => ({
|
|
163
|
-
name: row.index_name,
|
|
164
|
-
entries: row.columns.split(',').map((column) => ({ column })),
|
|
165
|
-
unique: Boolean(row.is_unique),
|
|
166
|
-
}));
|
|
167
|
-
}
|
|
168
|
-
async mapForeignKeysResult(_read, _tableName, results) {
|
|
169
|
-
return this.joinedForeignKeys(results);
|
|
136
|
+
`, tableName);
|
|
170
137
|
}
|
|
171
138
|
/**
|
|
172
139
|
* The engine reprints a default from its own parse tree: all of it in one pair of parentheses, and a
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IndexFacet } from '../../schema/indexDifferences.js';
|
|
2
2
|
import type { CheckSchema } from '../../schema/types.js';
|
|
3
|
-
import type {
|
|
4
|
-
import { AbstractSqlSchemaIntrospector, type
|
|
3
|
+
import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema } from '../../type/index.js';
|
|
4
|
+
import { AbstractSqlSchemaIntrospector, type ReadColumn, type TableRowReader } from './abstractSqlSchemaIntrospector.js';
|
|
5
5
|
/**
|
|
6
6
|
* MySQL/MariaDB schema introspector.
|
|
7
7
|
* Works with both MySQL and MariaDB as they share the same information_schema structure.
|
|
@@ -16,16 +16,11 @@ export declare class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospect
|
|
|
16
16
|
protected getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
|
|
17
17
|
protected getTableNamesQuery(): string;
|
|
18
18
|
protected tableExistsQuery(): string;
|
|
19
|
-
protected
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
protected
|
|
24
|
-
protected getForeignKeysQuery(_tableName: string): string;
|
|
25
|
-
protected getPrimaryKeyQuery(_tableName: string): string;
|
|
26
|
-
protected mapColumnsResult(_read: TableRowReader, _tableName: string, results: MysqlColumnRow[]): Promise<ColumnSchema[]>;
|
|
27
|
-
protected mapIndexesResult(_read: TableRowReader, _tableName: string, results: MysqlIndexRow[]): Promise<IndexSchema[]>;
|
|
28
|
-
protected mapForeignKeysResult(_read: TableRowReader, _tableName: string, results: JoinedForeignKeyRow[]): Promise<ForeignKeySchema[]>;
|
|
19
|
+
protected getColumns(read: TableRowReader, tableName: string): Promise<ReadColumn[]>;
|
|
20
|
+
protected getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
|
|
21
|
+
protected getForeignKeys(read: TableRowReader, tableName: string): Promise<ForeignKeySchema[]>;
|
|
22
|
+
/** Unnamed: MySQL calls every key `PRIMARY`, and drops one by no name. */
|
|
23
|
+
protected getPrimaryKey(read: TableRowReader, tableName: string): Promise<PrimaryKeySchema | undefined>;
|
|
29
24
|
/**
|
|
30
25
|
* MySQL prints a literal default unquoted, except one wrapped as `DEFAULT ('x')`, the form a `TEXT`
|
|
31
26
|
* column takes. That one comes with a charset introducer and every quote and backslash escaped again:
|
|
@@ -38,21 +33,6 @@ export declare class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospect
|
|
|
38
33
|
*/
|
|
39
34
|
protected sqlText(defaultValue: string, extra: string): string | undefined;
|
|
40
35
|
}
|
|
41
|
-
type MysqlColumnRow = {
|
|
42
|
-
column_name: string;
|
|
43
|
-
data_type: string;
|
|
44
|
-
column_type: string;
|
|
45
|
-
is_nullable: string;
|
|
46
|
-
column_default: string | null;
|
|
47
|
-
column_key: string;
|
|
48
|
-
extra: string;
|
|
49
|
-
character_maximum_length: number | bigint | null;
|
|
50
|
-
numeric_precision: number | bigint | null;
|
|
51
|
-
datetime_precision: number | bigint | null;
|
|
52
|
-
numeric_scale: number | null;
|
|
53
|
-
column_comment: string | null;
|
|
54
|
-
generated_as: string | null;
|
|
55
|
-
};
|
|
56
36
|
/**
|
|
57
37
|
* MariaDB reads out of the same `information_schema` as MySQL, save for one column type it does not
|
|
58
38
|
* have: `JSON` there is an alias for `LONGTEXT` plus a `json_valid()` check constraint named after
|
|
@@ -64,24 +44,17 @@ type MysqlColumnRow = {
|
|
|
64
44
|
export declare class MariadbSchemaIntrospector extends MysqlSchemaIntrospector {
|
|
65
45
|
/**
|
|
66
46
|
* A check's name is unique only in its table on MariaDB, whose `CHECK_CONSTRAINTS` names the table. A
|
|
67
|
-
* JSON column's `json_valid()` is its type, read by {@link
|
|
47
|
+
* JSON column's `json_valid()` is its type, read by {@link getColumns}, so it is left out.
|
|
68
48
|
*/
|
|
69
49
|
protected getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
|
|
70
50
|
/** MariaDB prints a literal quoted, as SQL reads it (`'it''s'`), so an unquoted default is SQL. */
|
|
71
51
|
protected sqlText(defaultValue: string): string | undefined;
|
|
72
52
|
/** Whether an index is MariaDB's vector index, and the distance it was built for. */
|
|
73
|
-
readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
53
|
+
protected readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
74
54
|
/**
|
|
75
55
|
* A vector index's distance is kept only in the table's own definition, ``VECTOR KEY `ix` (`vec`)
|
|
76
56
|
* `DISTANCE`='cosine'``, and left out there for MariaDB's default, euclidean.
|
|
77
57
|
*/
|
|
78
|
-
protected
|
|
79
|
-
protected
|
|
58
|
+
protected getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
|
|
59
|
+
protected getColumns(read: TableRowReader, tableName: string): Promise<ReadColumn[]>;
|
|
80
60
|
}
|
|
81
|
-
type MysqlIndexRow = {
|
|
82
|
-
index_name: string;
|
|
83
|
-
columns: string;
|
|
84
|
-
is_unique: number;
|
|
85
|
-
method: string;
|
|
86
|
-
};
|
|
87
|
-
export {};
|