uql-orm 0.96.1 → 0.98.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +59 -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 +53 -174
- package/dist/migrate/codegen/fieldOptionsSource.d.ts +1 -3
- package/dist/migrate/codegen/fieldOptionsSource.js +3 -11
- 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 +155 -205
- package/dist/migrate/drift/index.d.ts +1 -6
- package/dist/migrate/drift/index.js +1 -6
- package/dist/migrate/generator/definitionToNode.d.ts +2 -2
- package/dist/migrate/generator/definitionToNode.js +6 -5
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +56 -77
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +75 -98
- package/dist/migrate/introspection/mssqlIntrospector.d.ts +10 -43
- package/dist/migrate/introspection/mssqlIntrospector.js +52 -77
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +21 -37
- package/dist/migrate/introspection/mysqlIntrospector.js +75 -56
- package/dist/migrate/introspection/postgresIntrospector.d.ts +15 -60
- package/dist/migrate/introspection/postgresIntrospector.js +74 -77
- package/dist/migrate/introspection/sqliteIntrospector.d.ts +19 -53
- package/dist/migrate/introspection/sqliteIntrospector.js +73 -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 +9 -44
- package/dist/migrate/migrator.js +72 -150
- package/dist/migrate/schemaChange.d.ts +8 -5
- package/dist/migrate/schemaChange.js +25 -8
- package/dist/migrate/schemaGenerator.d.ts +35 -109
- package/dist/migrate/schemaGenerator.js +179 -353
- 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 +7 -9
- package/dist/migrate/triggerSql.js +17 -18
- 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 +1 -6
- package/dist/mongo/mongoIntrospector.js +7 -18
- package/dist/mongo/mongoSchemaGenerator.d.ts +7 -26
- package/dist/mongo/mongoSchemaGenerator.js +51 -75
- 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 +5 -12
- package/dist/schema/schemaASTBuilder.d.ts +5 -6
- package/dist/schema/schemaASTBuilder.js +17 -18
- package/dist/schema/schemaASTDiffer.d.ts +4 -5
- package/dist/schema/schemaASTDiffer.js +40 -73
- package/dist/schema/types.d.ts +24 -54
- 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 +13 -73
- package/dist/util/ddlExpression.util.d.ts +13 -2
- package/dist/util/ddlExpression.util.js +27 -4
- package/dist/util/sql.util.d.ts +2 -12
- package/dist/util/sql.util.js +9 -18
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +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,18 +1,17 @@
|
|
|
1
|
+
import type { AbstractSqlDialect } from '../../dialect/index.js';
|
|
2
|
+
import type { IndexFacet } from '../../schema/indexDifferences.js';
|
|
3
|
+
import type { SchemaAST } from '../../schema/schemaAST.js';
|
|
1
4
|
import { SqlExpression } from '../../schema/sqlExpression.js';
|
|
5
|
+
import type { CheckSchema, TriggerSchema } from '../../schema/types.js';
|
|
2
6
|
import { type ForeignKeyAction } from '../../schema/types.js';
|
|
3
|
-
import type { ColumnSchema,
|
|
4
|
-
import { BaseSqlIntrospector } from './baseSqlIntrospector.js';
|
|
7
|
+
import type { ColumnRenames, ColumnSchema, Except, ForeignKeySchema, IndexSchema, PrimaryKeySchema, QuerierPool, RawRow, SchemaIntrospector, SqlQuerier, StoredDefinition, TableSchema } from '../../type/index.js';
|
|
5
8
|
/**
|
|
6
|
-
* Reads the rows of one statement while introspecting a table
|
|
7
|
-
*
|
|
8
|
-
* Identical statements share a single round trip, which is what the mappers get instead of a querier:
|
|
9
|
-
* describing one table needs four facts, and on SQLite three of them come out of the same two PRAGMAs
|
|
10
|
-
* (`table_info` is both the column list and the primary key; the column mapper walks `index_list` and
|
|
11
|
-
* `index_info` for single-column uniqueness while the index mapper is walking them too). That was four
|
|
12
|
-
* redundant statements out of ten per table - on D1 and Turso, where every PRAGMA is an HTTP round trip,
|
|
13
|
-
* it is four avoidable ones.
|
|
9
|
+
* Reads the rows of one statement while introspecting a table, an identical statement sent once: SQLite's
|
|
10
|
+
* `table_info` is both the column list and the key, and on D1 and Turso every PRAGMA is a round trip.
|
|
14
11
|
*/
|
|
15
12
|
export type TableRowReader = <T extends RawRow>(sql: string, params?: unknown[]) => Promise<T[]>;
|
|
13
|
+
/** A column as its engine's catalogue reads it; the key and the indexes say which it belongs to. */
|
|
14
|
+
export type ReadColumn = Except<ColumnSchema, 'isPrimaryKey' | 'isUnique'>;
|
|
16
15
|
/** A foreign key as MySQL and SQL Server list one: a row, its column lists comma-joined. */
|
|
17
16
|
export type JoinedForeignKeyRow = {
|
|
18
17
|
readonly constraint_name: string;
|
|
@@ -22,10 +21,19 @@ export type JoinedForeignKeyRow = {
|
|
|
22
21
|
readonly delete_rule: string;
|
|
23
22
|
readonly update_rule: string;
|
|
24
23
|
};
|
|
25
|
-
/** A SQL introspector: an engine
|
|
26
|
-
export declare abstract class AbstractSqlSchemaIntrospector
|
|
24
|
+
/** A SQL introspector: an engine reads each part of a table from its catalogue (`get*`), on a {@link TableRowReader}. */
|
|
25
|
+
export declare abstract class AbstractSqlSchemaIntrospector implements SchemaIntrospector {
|
|
27
26
|
protected readonly pool: QuerierPool;
|
|
28
|
-
|
|
27
|
+
readonly schema?: string | undefined;
|
|
28
|
+
/** Columns and uniqueness only; each introspector opts in to what its catalogue queries report. */
|
|
29
|
+
protected readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
30
|
+
protected readonly dialect: AbstractSqlDialect;
|
|
31
|
+
/**
|
|
32
|
+
* `schema` is the one these queries read, `undefined` for the connection's own default. Every table
|
|
33
|
+
* reported is stamped with it, so a diff compares like with like: entity and database both say
|
|
34
|
+
* `undefined` for "wherever the connection points", and name a schema only when one was asked for.
|
|
35
|
+
*/
|
|
36
|
+
constructor(pool: QuerierPool, schema?: string | undefined);
|
|
29
37
|
/**
|
|
30
38
|
* The schema every catalogue query filters on, as SQL: the one that was asked for, as the engine's
|
|
31
39
|
* own literal, or its expression for the connection's default. A literal rather than a bind
|
|
@@ -37,85 +45,56 @@ export declare abstract class AbstractSqlSchemaIntrospector extends BaseSqlIntro
|
|
|
37
45
|
* an engine with no schemas, whose catalogue queries never reference one.
|
|
38
46
|
*/
|
|
39
47
|
protected readonly defaultSchemaExpr: string;
|
|
40
|
-
getTableSchema(tableName: string): Promise<TableSchema | undefined>;
|
|
41
|
-
/** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
|
|
42
|
-
protected getDefinition(_read: TableRowReader, _tableName: string): Promise<StoredDefinition[] | undefined>;
|
|
43
|
-
getTableNames(): Promise<string[]>;
|
|
44
48
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
49
|
+
* The database as a {@link SchemaAST}, or just the tables named. A name nothing matches is left out
|
|
50
|
+
* rather than raised: the point of naming them is to read a database other things are still
|
|
51
|
+
* changing, where scanning every table is both wasted work and a relation that can vanish mid-scan.
|
|
48
52
|
*/
|
|
49
|
-
|
|
53
|
+
introspect(tables?: readonly string[], renames?: ColumnRenames): Promise<SchemaAST>;
|
|
54
|
+
getTableSchema(tableName: string): Promise<TableSchema | undefined>;
|
|
55
|
+
getTableNames(): Promise<string[]>;
|
|
50
56
|
tableExists(tableName: string): Promise<boolean>;
|
|
51
|
-
/**
|
|
52
|
-
* Introspection reads, so `withQuerier` rather than `transaction`: the pool owns the release either
|
|
53
|
-
* way, and wrapping catalogue queries in a transaction would hold one open for nothing.
|
|
54
|
-
*/
|
|
57
|
+
/** On the querier migrations run on, which is not the app's on a libSQL embedded replica. */
|
|
55
58
|
protected withSqlQuerier<T>(task: (querier: SqlQuerier) => Promise<T>): Promise<T>;
|
|
59
|
+
/** Whether the base table exists: its query answers a row for it, none for a view or nothing. */
|
|
56
60
|
protected tableExistsInternal(read: TableRowReader, tableName: string): Promise<boolean>;
|
|
57
|
-
|
|
58
|
-
protected getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
|
|
59
|
-
protected getForeignKeys(read: TableRowReader, tableName: string): Promise<ForeignKeySchema[]>;
|
|
60
|
-
protected getPrimaryKey(read: TableRowReader, tableName: string): Promise<PrimaryKeySchema | undefined>;
|
|
61
|
-
protected tableExistsParams(tableName: string): unknown[];
|
|
62
|
-
protected getColumnsParams(tableName: string): unknown[];
|
|
63
|
-
protected getIndexesParams(tableName: string): unknown[];
|
|
64
|
-
protected getForeignKeysParams(tableName: string): unknown[];
|
|
65
|
-
protected getPrimaryKeyParams(tableName: string): unknown[];
|
|
66
|
-
/** The {@link ForeignKeyAction} a catalogue names, whatever its case, and `SET_NULL` as SQL Server spells it. */
|
|
67
|
-
protected normalizeReferentialAction(action: string): ForeignKeyAction | undefined;
|
|
68
|
-
/** Foreign keys read one row each, as {@link JoinedForeignKeyRow} lists them. */
|
|
69
|
-
protected joinedForeignKeys(rows: readonly JoinedForeignKeyRow[]): ForeignKeySchema[];
|
|
70
|
-
/**
|
|
71
|
-
* Convert bigint/null values to number safely.
|
|
72
|
-
*/
|
|
73
|
-
protected toNumber(value: unknown): number | undefined;
|
|
74
|
-
/** SQL query to list all table names. */
|
|
61
|
+
/** SQL listing the base tables' names, as `table_name`. */
|
|
75
62
|
protected abstract getTableNamesQuery(): string;
|
|
76
|
-
/** SQL
|
|
63
|
+
/** SQL answering a row where the base table its one parameter names exists. */
|
|
77
64
|
protected abstract tableExistsQuery(): string;
|
|
78
|
-
/**
|
|
79
|
-
protected abstract
|
|
80
|
-
/**
|
|
81
|
-
protected abstract
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
65
|
+
/** The table's columns, in their order. */
|
|
66
|
+
protected abstract getColumns(read: TableRowReader, tableName: string): Promise<ReadColumn[]>;
|
|
67
|
+
/** The table's indexes, but its key's own. */
|
|
68
|
+
protected abstract getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
|
|
69
|
+
protected abstract getForeignKeys(read: TableRowReader, tableName: string): Promise<ForeignKeySchema[]>;
|
|
70
|
+
/** The table's key, its columns in key order, or `undefined` for a table without one. */
|
|
71
|
+
protected abstract getPrimaryKey(read: TableRowReader, tableName: string): Promise<PrimaryKeySchema | undefined>;
|
|
72
|
+
/** Every check on the table, each `expression` as the engine reprints it, which is what a rollback restores. */
|
|
73
|
+
protected abstract getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
|
|
74
|
+
/**
|
|
75
|
+
* The triggers uql installed on the table, each with the statements recreating it as the engine keeps it.
|
|
76
|
+
* Ownership is matched here rather than with `LIKE`, whose `_` wildcard would take in a hand-written one.
|
|
77
|
+
*/
|
|
78
|
+
protected getTriggers(read: TableRowReader, tableName: string): Promise<TriggerSchema[]>;
|
|
88
79
|
/**
|
|
89
80
|
* SQL listing the triggers on the table named by its single parameter: each one's `name`, its `definition`
|
|
90
81
|
* as the engine reprints it, and what it `requires` to be recreated first. It reads what is installed, not
|
|
91
82
|
* what uql wrote, which is exactly what a rollback restores.
|
|
92
83
|
*/
|
|
93
84
|
protected abstract triggersQuery(): string;
|
|
85
|
+
/** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
|
|
86
|
+
protected getDefinition(_read: TableRowReader, _tableName: string): Promise<StoredDefinition[] | undefined>;
|
|
94
87
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* Defaults to `information_schema`'s own column, which is what every engine with an
|
|
98
|
-
* `information_schema` returns and what Postgres and MySQL both restated identically. SQLite reads
|
|
99
|
-
* `sqlite_master` instead and overrides.
|
|
100
|
-
*/
|
|
101
|
-
protected mapTableNameRow(row: RawRow): string;
|
|
102
|
-
/** Map column query results to ColumnSchema array. Allows async for SQLite's unique column check. */
|
|
103
|
-
protected abstract mapColumnsResult(read: TableRowReader, tableName: string, results: RawRow[]): Promise<ColumnSchema[]>;
|
|
104
|
-
/** Map index query results to IndexSchema array. Allows async for SQLite's index_info calls. */
|
|
105
|
-
protected abstract mapIndexesResult(read: TableRowReader, tableName: string, results: RawRow[]): Promise<IndexSchema[]>;
|
|
106
|
-
/** Map foreign key query results to ForeignKeySchema array. */
|
|
107
|
-
protected abstract mapForeignKeysResult(read: TableRowReader, tableName: string, results: RawRow[]): Promise<ForeignKeySchema[]>;
|
|
108
|
-
/**
|
|
109
|
-
* Map primary key query results to column names, in key order. `information_schema` gives every SQL
|
|
110
|
-
* engine here a `column_name` per row; SQLite reads its key off `PRAGMA table_info` instead and
|
|
111
|
-
* overrides this.
|
|
112
|
-
*/
|
|
113
|
-
protected mapPrimaryKeyResult(results: RawRow[]): string[] | undefined;
|
|
114
|
-
/**
|
|
115
|
-
* What the engine calls the key's constraint, where the query reported one. Only a `DROP` needs it,
|
|
116
|
-
* and only the reported name will do - see {@link PrimaryKeySchema.name}.
|
|
88
|
+
* The key `sql` lists a row of for each column, in key order: its `column_name`, and the `constraint_name`
|
|
89
|
+
* where the engine names the key's constraint. Only a `DROP` needs that name, and only the reported one will do.
|
|
117
90
|
*/
|
|
118
|
-
protected
|
|
91
|
+
protected readPrimaryKey(read: TableRowReader, sql: string, tableName: string): Promise<PrimaryKeySchema | undefined>;
|
|
92
|
+
/** The {@link ForeignKeyAction} a catalogue names, whatever its case, and `SET_NULL` as SQL Server spells it. */
|
|
93
|
+
protected normalizeReferentialAction(action: string): ForeignKeyAction | undefined;
|
|
94
|
+
/** Foreign keys read one row each, as {@link JoinedForeignKeyRow} lists them. */
|
|
95
|
+
protected joinedForeignKeys(rows: readonly JoinedForeignKeyRow[]): ForeignKeySchema[];
|
|
96
|
+
/** A catalogue's number, which a driver may answer as a `bigint`, or `undefined` for none. */
|
|
97
|
+
protected toNumber(value: unknown): number | undefined;
|
|
119
98
|
/** Parses a default as the catalogue reports it: to its literal value, or through {@link sqlDefault} if SQL. */
|
|
120
99
|
protected abstract parseDefaultValue(defaultValue: string | null): unknown;
|
|
121
100
|
/**
|
|
@@ -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);
|
|
@@ -32,96 +60,70 @@ export class AbstractSqlSchemaIntrospector extends BaseSqlIntrospector {
|
|
|
32
60
|
if (!exists) {
|
|
33
61
|
return undefined;
|
|
34
62
|
}
|
|
35
|
-
const [columns, indexes, foreignKeys, primaryKey, definition] = await Promise.all([
|
|
63
|
+
const [columns, indexes, foreignKeys, primaryKey, checks, triggers, definition] = await Promise.all([
|
|
36
64
|
this.getColumns(read, tableName),
|
|
37
65
|
this.getIndexes(read, tableName),
|
|
38
66
|
this.getForeignKeys(read, tableName),
|
|
39
67
|
this.getPrimaryKey(read, tableName),
|
|
68
|
+
this.getChecks(read, tableName),
|
|
69
|
+
this.getTriggers(read, tableName),
|
|
40
70
|
this.getDefinition(read, tableName),
|
|
41
71
|
]);
|
|
72
|
+
const unique = uniqueColumns(indexes);
|
|
42
73
|
return {
|
|
43
74
|
name: tableName,
|
|
44
|
-
columns
|
|
75
|
+
columns: columns.map((column) => ({
|
|
76
|
+
...column,
|
|
77
|
+
isPrimaryKey: primaryKey?.columns.includes(column.name) ?? false,
|
|
78
|
+
isUnique: unique.has(column.name),
|
|
79
|
+
})),
|
|
45
80
|
primaryKey,
|
|
46
81
|
indexes,
|
|
47
82
|
foreignKeys,
|
|
83
|
+
checks,
|
|
84
|
+
triggers,
|
|
48
85
|
definition,
|
|
49
86
|
};
|
|
50
87
|
});
|
|
51
88
|
}
|
|
52
|
-
/** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
|
|
53
|
-
async getDefinition(_read, _tableName) {
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
89
|
async getTableNames() {
|
|
57
90
|
return this.withSqlQuerier(async (querier) => {
|
|
58
|
-
const
|
|
59
|
-
return
|
|
91
|
+
const rows = await querier.all(this.getTableNamesQuery());
|
|
92
|
+
return rows.map((row) => row.table_name);
|
|
60
93
|
});
|
|
61
94
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Every trigger uql installed in this schema, by table and then by name, with the statements recreating
|
|
64
|
-
* it: the whole schema in one read, since a table whose entity stopped declaring one still has one to
|
|
65
|
-
* drop. Ownership is matched here rather than with `LIKE`, whose `_` wildcard would take in a hand-written one.
|
|
66
|
-
*/
|
|
67
|
-
async ownedTriggers(table) {
|
|
68
|
-
const rows = await this.withSqlQuerier((querier) => querier.all(this.triggersQuery(), [table]));
|
|
69
|
-
return new Map(rows.flatMap((row) => {
|
|
70
|
-
const name = String(row['name']);
|
|
71
|
-
const statements = [row['requires'], row['definition']].filter(Boolean).map((sql) => String(sql));
|
|
72
|
-
return isOwnedName(name) ? [[name, statements]] : [];
|
|
73
|
-
}));
|
|
74
|
-
}
|
|
75
95
|
async tableExists(tableName) {
|
|
76
96
|
return this.withSqlQuerier((querier) => this.tableExistsInternal(createTableRowReader(querier), tableName));
|
|
77
97
|
}
|
|
78
|
-
/**
|
|
79
|
-
* Introspection reads, so `withQuerier` rather than `transaction`: the pool owns the release either
|
|
80
|
-
* way, and wrapping catalogue queries in a transaction would hold one open for nothing.
|
|
81
|
-
*/
|
|
98
|
+
/** On the querier migrations run on, which is not the app's on a libSQL embedded replica. */
|
|
82
99
|
withSqlQuerier(task) {
|
|
83
|
-
return this.pool.
|
|
84
|
-
if (!isSqlQuerier(querier)) {
|
|
85
|
-
throw new UqlUsageError(`${this.constructor.name} requires a SQL-based querier`);
|
|
86
|
-
}
|
|
87
|
-
return task(querier);
|
|
88
|
-
});
|
|
100
|
+
return withSqlQuerierForMigrations(this.pool, this.constructor.name, task);
|
|
89
101
|
}
|
|
102
|
+
/** Whether the base table exists: its query answers a row for it, none for a view or nothing. */
|
|
90
103
|
async tableExistsInternal(read, tableName) {
|
|
91
|
-
|
|
92
|
-
return this.parseTableExistsResult(results);
|
|
93
|
-
}
|
|
94
|
-
async getColumns(read, tableName) {
|
|
95
|
-
const results = await read(this.getColumnsQuery(tableName), this.getColumnsParams(tableName));
|
|
96
|
-
return this.mapColumnsResult(read, tableName, results);
|
|
97
|
-
}
|
|
98
|
-
async getIndexes(read, tableName) {
|
|
99
|
-
const results = await read(this.getIndexesQuery(tableName), this.getIndexesParams(tableName));
|
|
100
|
-
return this.mapIndexesResult(read, tableName, results);
|
|
101
|
-
}
|
|
102
|
-
async getForeignKeys(read, tableName) {
|
|
103
|
-
const results = await read(this.getForeignKeysQuery(tableName), this.getForeignKeysParams(tableName));
|
|
104
|
-
return this.mapForeignKeysResult(read, tableName, results);
|
|
104
|
+
return (await read(this.tableExistsQuery(), [tableName])).length > 0;
|
|
105
105
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return [
|
|
113
|
-
}
|
|
114
|
-
getColumnsParams(tableName) {
|
|
115
|
-
return [tableName];
|
|
116
|
-
}
|
|
117
|
-
getIndexesParams(tableName) {
|
|
118
|
-
return [tableName];
|
|
106
|
+
/**
|
|
107
|
+
* The triggers uql installed on the table, each with the statements recreating it as the engine keeps it.
|
|
108
|
+
* Ownership is matched here rather than with `LIKE`, whose `_` wildcard would take in a hand-written one.
|
|
109
|
+
*/
|
|
110
|
+
async getTriggers(read, tableName) {
|
|
111
|
+
const rows = await read(this.triggersQuery(), [tableName]);
|
|
112
|
+
return rows.flatMap(({ name, requires, definition }) => isOwnedName(name) ? [{ name, statements: [requires, definition].filter((sql) => typeof sql === 'string') }] : []);
|
|
119
113
|
}
|
|
120
|
-
|
|
121
|
-
|
|
114
|
+
/** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
|
|
115
|
+
async getDefinition(_read, _tableName) {
|
|
116
|
+
return undefined;
|
|
122
117
|
}
|
|
123
|
-
|
|
124
|
-
|
|
118
|
+
/**
|
|
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.
|
|
121
|
+
*/
|
|
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;
|
|
125
127
|
}
|
|
126
128
|
/** The {@link ForeignKeyAction} a catalogue names, whatever its case, and `SET_NULL` as SQL Server spells it. */
|
|
127
129
|
normalizeReferentialAction(action) {
|
|
@@ -138,42 +140,13 @@ export class AbstractSqlSchemaIntrospector extends BaseSqlIntrospector {
|
|
|
138
140
|
onUpdate: this.normalizeReferentialAction(row.update_rule),
|
|
139
141
|
}));
|
|
140
142
|
}
|
|
141
|
-
/**
|
|
142
|
-
* Convert bigint/null values to number safely.
|
|
143
|
-
*/
|
|
143
|
+
/** A catalogue's number, which a driver may answer as a `bigint`, or `undefined` for none. */
|
|
144
144
|
toNumber(value) {
|
|
145
145
|
if (value == null || value === '') {
|
|
146
146
|
return undefined;
|
|
147
147
|
}
|
|
148
148
|
return Number(value);
|
|
149
149
|
}
|
|
150
|
-
/**
|
|
151
|
-
* Extract table name from a row returned by getTableNamesQuery.
|
|
152
|
-
*
|
|
153
|
-
* Defaults to `information_schema`'s own column, which is what every engine with an
|
|
154
|
-
* `information_schema` returns and what Postgres and MySQL both restated identically. SQLite reads
|
|
155
|
-
* `sqlite_master` instead and overrides.
|
|
156
|
-
*/
|
|
157
|
-
mapTableNameRow(row) {
|
|
158
|
-
return row['table_name'];
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Map primary key query results to column names, in key order. `information_schema` gives every SQL
|
|
162
|
-
* engine here a `column_name` per row; SQLite reads its key off `PRAGMA table_info` instead and
|
|
163
|
-
* overrides this.
|
|
164
|
-
*/
|
|
165
|
-
mapPrimaryKeyResult(results) {
|
|
166
|
-
const columns = results.map((row) => String(row['column_name']));
|
|
167
|
-
return columns.length ? columns : undefined;
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* What the engine calls the key's constraint, where the query reported one. Only a `DROP` needs it,
|
|
171
|
-
* and only the reported name will do - see {@link PrimaryKeySchema.name}.
|
|
172
|
-
*/
|
|
173
|
-
mapPrimaryKeyName(results) {
|
|
174
|
-
const name = results[0]?.['constraint_name'];
|
|
175
|
-
return name === undefined || name === null ? undefined : String(name);
|
|
176
|
-
}
|
|
177
150
|
/**
|
|
178
151
|
* A SQL default, wrapped in parentheses, or the value uql exports that it spells (`currentTimestamp`), so it
|
|
179
152
|
* compares equal on every engine and `generate:from-db` writes it by name.
|
|
@@ -182,6 +155,10 @@ export class AbstractSqlSchemaIntrospector extends BaseSqlIntrospector {
|
|
|
182
155
|
return knownDefault(SqlExpression.parenthesized(sql), this.dialect);
|
|
183
156
|
}
|
|
184
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
|
+
}
|
|
185
162
|
/** A {@link TableRowReader} over one querier: the same statement is only ever sent once. */
|
|
186
163
|
function createTableRowReader(querier) {
|
|
187
164
|
const sent = new Map();
|
|
@@ -1,55 +1,22 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* `INFORMATION_SCHEMA` answers columns and foreign keys, but not indexes: it has no view for them at
|
|
7
|
-
* all, and its `CONSTRAINT_COLUMN_USAGE` conflates a unique index with a unique constraint. Those
|
|
8
|
-
* come from `sys.indexes` instead, which is also the only place the filtered-index predicate and the
|
|
9
|
-
* included columns are readable.
|
|
10
|
-
*/
|
|
1
|
+
import type { CheckSchema } from '../../schema/types.js';
|
|
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`. */
|
|
11
5
|
export declare class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
12
6
|
protected readonly defaultSchemaExpr = "SCHEMA_NAME()";
|
|
13
7
|
protected triggersQuery(): string;
|
|
8
|
+
protected getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
|
|
14
9
|
protected getTableNamesQuery(): string;
|
|
15
10
|
protected tableExistsQuery(): string;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}[]): boolean;
|
|
19
|
-
/**
|
|
20
|
-
* From `sys` rather than `INFORMATION_SCHEMA`, which has no identity flag and no per-column view of
|
|
21
|
-
* the key or of a unique index. A key column is not also `isUnique`, as on MySQL: only a unique index
|
|
22
|
-
* of its own, other than the key's, makes it so.
|
|
23
|
-
*/
|
|
24
|
-
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[]>;
|
|
25
13
|
/** `is_primary_key` is excluded: the key is read separately, the way every other engine reads it. */
|
|
26
|
-
protected
|
|
27
|
-
protected
|
|
28
|
-
protected
|
|
29
|
-
protected mapColumnsResult(_read: TableRowReader, _tableName: string, results: MsSqlColumnRow[]): Promise<ColumnSchema[]>;
|
|
30
|
-
protected mapIndexesResult(_read: TableRowReader, _tableName: string, results: {
|
|
31
|
-
index_name: string;
|
|
32
|
-
columns: string;
|
|
33
|
-
is_unique: boolean;
|
|
34
|
-
}[]): Promise<IndexSchema[]>;
|
|
35
|
-
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>;
|
|
36
17
|
/**
|
|
37
18
|
* The engine reprints a default from its own parse tree: all of it in one pair of parentheses, and a
|
|
38
19
|
* number in a second, so `((0))`, `((1)+(2))`, `(N'x')` and `(getdate())`.
|
|
39
20
|
*/
|
|
40
21
|
protected parseDefaultValue(defaultValue: string | null): unknown;
|
|
41
22
|
}
|
|
42
|
-
type MsSqlColumnRow = {
|
|
43
|
-
column_name: string;
|
|
44
|
-
data_type: string;
|
|
45
|
-
max_length: number | null;
|
|
46
|
-
numeric_precision: number | null;
|
|
47
|
-
numeric_scale: number | null;
|
|
48
|
-
is_nullable: boolean;
|
|
49
|
-
is_identity: boolean;
|
|
50
|
-
column_default: string | null;
|
|
51
|
-
is_primary_key: number | null;
|
|
52
|
-
is_unique: number | null;
|
|
53
|
-
generated_as: string | null;
|
|
54
|
-
};
|
|
55
|
-
export {};
|