uql-orm 0.99.0 → 0.100.1
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/LICENSE.md +22 -0
- package/README.md +1 -1
- package/dist/browser/uql-browser.min.js +2 -2
- package/dist/browser/uql-browser.min.js.map +5 -5
- package/dist/bunSql/bunSql.util.js +2 -2
- package/dist/bunSql/bunSqlQuerier.js +0 -1
- package/dist/bunSql/bunSqlQuerierPool.js +1 -1
- package/dist/cockroachdb/cockroachDialect.js +1 -0
- package/dist/context/context.browser.d.ts +5 -0
- package/dist/context/context.browser.js +21 -11
- package/dist/context/context.d.ts +5 -0
- package/dist/context/context.js +8 -3
- package/dist/dialect/abstractDialect.js +4 -2
- package/dist/dialect/abstractSqlDialect.d.ts +5 -5
- package/dist/dialect/abstractSqlDialect.js +26 -18
- package/dist/dialect/aliases.d.ts +0 -2
- package/dist/dialect/aliases.js +0 -2
- package/dist/dialect/hydrateColumn.d.ts +3 -3
- package/dist/dialect/hydrateColumn.js +1 -0
- package/dist/dialect/index.js +0 -2
- package/dist/dialect/jsonSql.d.ts +2 -3
- package/dist/dialect/jsonSql.js +4 -6
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +1 -1
- package/dist/dialect/mysqlLikeSqlDialect.js +5 -3
- package/dist/dialect/operators.d.ts +2 -3
- package/dist/dialect/operators.js +3 -4
- package/dist/dialect/pgLikeSqlDialect.js +12 -12
- package/dist/entity/decorator/members.d.ts +4 -12
- package/dist/entity/metadata/definition.js +8 -5
- package/dist/http/handler.d.ts +6 -6
- package/dist/http/handler.js +20 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +6 -5
- package/dist/{maria → mariadb}/mariadbQuerier.js +1 -1
- package/dist/migrate/builder/columnBuilder.d.ts +3 -3
- package/dist/migrate/builder/columnBuilder.js +1 -1
- package/dist/migrate/builder/index.d.ts +0 -3
- package/dist/migrate/builder/index.js +0 -3
- package/dist/migrate/builder/migrationBuilder.d.ts +6 -6
- package/dist/migrate/builder/migrationBuilder.js +7 -7
- package/dist/migrate/builder/tableBuilder.d.ts +25 -25
- package/dist/migrate/builder/tableBuilder.js +5 -5
- package/dist/migrate/builder/types.d.ts +37 -37
- package/dist/migrate/cli-config.d.ts +11 -1
- package/dist/migrate/cli-config.js +20 -6
- package/dist/migrate/cli.js +9 -18
- package/dist/migrate/codegen/entityCodeGenerator.js +1 -1
- package/dist/migrate/codegen/migrationFile.d.ts +7 -7
- package/dist/migrate/codegen/migrationFile.js +11 -12
- package/dist/migrate/ddl/indexCapabilities.d.ts +2 -0
- package/dist/migrate/ddl/indexCapabilities.js +6 -0
- package/dist/migrate/ddl/indexDdl.d.ts +8 -0
- package/dist/migrate/ddl/indexDdl.js +22 -6
- package/dist/migrate/generator/definitionToNode.d.ts +1 -1
- package/dist/migrate/generator/definitionToNode.js +1 -1
- package/dist/migrate/index.d.ts +1 -3
- package/dist/migrate/index.js +0 -3
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +11 -15
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +13 -15
- package/dist/migrate/introspection/mssqlIntrospector.d.ts +5 -5
- package/dist/migrate/introspection/mssqlIntrospector.js +24 -28
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +5 -5
- package/dist/migrate/introspection/mysqlIntrospector.js +31 -37
- package/dist/migrate/introspection/postgresIntrospector.d.ts +13 -13
- package/dist/migrate/introspection/postgresIntrospector.js +35 -39
- package/dist/migrate/introspection/registry.js +1 -1
- package/dist/migrate/introspection/sqliteIntrospector.d.ts +6 -4
- package/dist/migrate/introspection/sqliteIntrospector.js +15 -12
- package/dist/migrate/migrationLock.d.ts +38 -0
- package/dist/migrate/migrationLock.js +103 -0
- package/dist/migrate/migrationTarget.d.ts +6 -2
- package/dist/migrate/migrationTarget.js +30 -14
- package/dist/migrate/migrator.d.ts +39 -23
- package/dist/migrate/migrator.js +110 -74
- package/dist/migrate/schemaGenerator.d.ts +3 -1
- package/dist/migrate/schemaGenerator.js +14 -5
- package/dist/migrate/storage/databaseStorage.d.ts +28 -20
- package/dist/migrate/storage/databaseStorage.js +36 -54
- package/dist/migrate/storage/index.d.ts +0 -1
- package/dist/migrate/storage/index.js +0 -1
- package/dist/{mongo → mongodb}/index.d.ts +0 -1
- package/dist/{mongo → mongodb}/index.js +0 -1
- package/dist/{mongo → mongodb}/mongoCommand.d.ts +17 -1
- package/dist/{mongo → mongodb}/mongoCommand.js +15 -2
- package/dist/{mongo → mongodb}/mongoDialect.js +8 -12
- package/dist/{mongo → mongodb}/mongoIntrospector.js +12 -3
- package/dist/{mongo → mongodb}/mongoMigrationStorage.d.ts +5 -5
- package/dist/{mongo → mongodb}/mongoMigrationStorage.js +8 -12
- package/dist/{mongo → mongodb}/mongoQuerier.d.ts +3 -1
- package/dist/{mongo → mongodb}/mongoSchemaGenerator.d.ts +11 -1
- package/dist/{mongo → mongodb}/mongoSchemaGenerator.js +56 -11
- package/dist/{mongo → mongodb}/mongodbQuerier.d.ts +7 -18
- package/dist/{mongo → mongodb}/mongodbQuerier.js +44 -78
- package/dist/mssql/mssqlDialect.d.ts +6 -4
- package/dist/mssql/mssqlDialect.js +20 -10
- package/dist/mssql/mssqlQuerier.d.ts +2 -2
- package/dist/mssql/mssqlQuerier.js +3 -3
- package/dist/mssql/mssqlQuerierPool.d.ts +1 -1
- package/dist/mssql/mssqlQuerierPool.js +2 -2
- package/dist/mysql/mysql2Querier.js +1 -5
- package/dist/namingStrategy/defaultNamingStrategy.d.ts +0 -1
- package/dist/namingStrategy/defaultNamingStrategy.js +0 -3
- package/dist/pglite/pgliteDialect.d.ts +3 -0
- package/dist/pglite/pgliteDialect.js +3 -1
- package/dist/pglite/pgliteQuerier.d.ts +2 -0
- package/dist/pglite/pgliteQuerier.js +4 -0
- package/dist/postgres/postgresDialect.d.ts +3 -4
- package/dist/postgres/postgresDialect.js +5 -6
- package/dist/querier/abstractQuerier.d.ts +119 -36
- package/dist/querier/abstractQuerier.js +465 -161
- package/dist/querier/abstractQuerierPool.d.ts +5 -6
- package/dist/querier/abstractQuerierPool.js +20 -6
- package/dist/querier/abstractSqlQuerier.d.ts +15 -10
- package/dist/querier/abstractSqlQuerier.js +54 -47
- package/dist/querier/abstractSqlQuerierPool.d.ts +5 -3
- package/dist/querier/abstractSqlQuerierPool.js +7 -4
- package/dist/querier/transaction.d.ts +46 -0
- package/dist/querier/transaction.js +56 -0
- package/dist/schema/canonicalType.js +2 -2
- package/dist/schema/schemaASTDiffer.d.ts +11 -3
- package/dist/schema/schemaASTDiffer.js +20 -8
- package/dist/sqlite/nodeSqliteQuerierPool.d.ts +1 -1
- package/dist/sqlite/nodeSqliteQuerierPool.js +1 -1
- package/dist/sqlite/sqliteDialect.d.ts +1 -1
- package/dist/sqlite/sqliteDialect.js +3 -1
- package/dist/sqlite/sqliteQuerier.d.ts +2 -0
- package/dist/sqlite/sqliteQuerier.js +4 -0
- package/dist/sqlite/sqliteQuerierPool.d.ts +4 -4
- package/dist/sqlite/sqliteQuerierPool.js +1 -1
- package/dist/type/dialect.d.ts +16 -2
- package/dist/type/entity.d.ts +32 -32
- package/dist/type/migration.d.ts +10 -27
- package/dist/type/namingStrategy.d.ts +0 -4
- package/dist/type/querier.d.ts +18 -10
- package/dist/type/query.d.ts +7 -22
- package/dist/type/queryRaw.d.ts +11 -6
- package/dist/type/queryRaw.js +2 -3
- package/dist/type/universalQuerier.d.ts +26 -28
- package/dist/type/utility.d.ts +7 -2
- package/dist/util/dialect.util.d.ts +2 -2
- package/dist/util/dialect.util.js +4 -1
- package/dist/util/field.util.d.ts +12 -0
- package/dist/util/field.util.js +24 -0
- package/dist/util/hook.util.d.ts +3 -2
- package/dist/util/query.util.d.ts +5 -0
- package/dist/util/query.util.js +34 -1
- package/dist/util/raw.d.ts +8 -2
- package/dist/util/raw.js +22 -0
- package/dist/util/relationQuery.util.d.ts +2 -0
- package/dist/util/relationQuery.util.js +4 -0
- package/dist/util/sql.util.d.ts +0 -5
- package/dist/util/sql.util.js +3 -9
- package/dist/util/uqlError.d.ts +0 -6
- package/dist/util/uqlError.js +0 -5
- package/package.json +20 -26
- package/skills/uql-orm/SKILL.md +13 -10
- package/dist/migrate/storage/jsonStorage.d.ts +0 -14
- package/dist/migrate/storage/jsonStorage.js +0 -33
- /package/dist/{maria → mariadb}/index.d.ts +0 -0
- /package/dist/{maria → mariadb}/index.js +0 -0
- /package/dist/{maria → mariadb}/mariaDialect.d.ts +0 -0
- /package/dist/{maria → mariadb}/mariaDialect.js +0 -0
- /package/dist/{maria → mariadb}/mariadbQuerier.d.ts +0 -0
- /package/dist/{maria → mariadb}/mariadbQuerierPool.d.ts +0 -0
- /package/dist/{maria → mariadb}/mariadbQuerierPool.js +0 -0
- /package/dist/{mongo → mongodb}/mongoDialect.d.ts +0 -0
- /package/dist/{mongo → mongodb}/mongoIntrospector.d.ts +0 -0
- /package/dist/{mongo → mongodb}/mongoQuerier.js +0 -0
- /package/dist/{mongo → mongodb}/mongodbQuerierPool.d.ts +0 -0
- /package/dist/{mongo → mongodb}/mongodbQuerierPool.js +0 -0
- /package/dist/{mongo → mongodb}/textLanguage.d.ts +0 -0
- /package/dist/{mongo → mongodb}/textLanguage.js +0 -0
- /package/dist/{mongo → mongodb}/vectorDistance.d.ts +0 -0
- /package/dist/{mongo → mongodb}/vectorDistance.js +0 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SqlExpression } from '../../schema/sqlExpression.js';
|
|
2
|
+
import { raw } from '../../util/raw.js';
|
|
2
3
|
import { unescapeMysqlString } from '../../util/sqlLiteral.js';
|
|
3
4
|
import { AbstractSqlSchemaIntrospector, } from './abstractSqlSchemaIntrospector.js';
|
|
4
5
|
/**
|
|
@@ -7,14 +8,14 @@ import { AbstractSqlSchemaIntrospector, } from './abstractSqlSchemaIntrospector.
|
|
|
7
8
|
*/
|
|
8
9
|
export class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
9
10
|
// A MySQL "schema" is a database, so the connection's own is what `DATABASE()` reports.
|
|
10
|
-
defaultSchemaExpr =
|
|
11
|
-
triggersQuery() {
|
|
12
|
-
return
|
|
11
|
+
defaultSchemaExpr = raw `DATABASE()`;
|
|
12
|
+
triggersQuery(tableName) {
|
|
13
|
+
return raw `
|
|
13
14
|
SELECT TRIGGER_NAME AS name,
|
|
14
15
|
CONCAT('CREATE TRIGGER \`', TRIGGER_SCHEMA, '\`.\`', TRIGGER_NAME, '\` ', ACTION_TIMING, ' ', EVENT_MANIPULATION,
|
|
15
16
|
' ON \`', EVENT_OBJECT_SCHEMA, '\`.\`', EVENT_OBJECT_TABLE, '\` FOR EACH ROW ', ACTION_STATEMENT) AS definition
|
|
16
17
|
FROM information_schema.TRIGGERS
|
|
17
|
-
WHERE TRIGGER_SCHEMA = ${this.schemaExpr} AND EVENT_OBJECT_TABLE = ${
|
|
18
|
+
WHERE TRIGGER_SCHEMA = ${this.schemaExpr} AND EVENT_OBJECT_TABLE = ${tableName}
|
|
18
19
|
`;
|
|
19
20
|
}
|
|
20
21
|
/**
|
|
@@ -22,18 +23,17 @@ export class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
22
23
|
* table comes from `TABLE_CONSTRAINTS`. It reports the clause escaped as a literal's body (`\'a\'`).
|
|
23
24
|
*/
|
|
24
25
|
async getChecks(read, tableName) {
|
|
25
|
-
const rows = await read(
|
|
26
|
-
/*sql*/ `
|
|
26
|
+
const rows = await read(raw `
|
|
27
27
|
SELECT k.CONSTRAINT_NAME AS name, c.CHECK_CLAUSE AS expression
|
|
28
28
|
FROM information_schema.TABLE_CONSTRAINTS k
|
|
29
29
|
JOIN information_schema.CHECK_CONSTRAINTS c
|
|
30
30
|
ON c.CONSTRAINT_SCHEMA = k.CONSTRAINT_SCHEMA AND c.CONSTRAINT_NAME = k.CONSTRAINT_NAME
|
|
31
|
-
WHERE k.CONSTRAINT_TYPE = 'CHECK' AND k.TABLE_SCHEMA = ${this.schemaExpr} AND k.TABLE_NAME =
|
|
32
|
-
|
|
31
|
+
WHERE k.CONSTRAINT_TYPE = 'CHECK' AND k.TABLE_SCHEMA = ${this.schemaExpr} AND k.TABLE_NAME = ${tableName}
|
|
32
|
+
`);
|
|
33
33
|
return rows.map(({ name, expression }) => ({ name, expression: unescapeMysqlString(expression) }));
|
|
34
34
|
}
|
|
35
35
|
getTableNamesQuery() {
|
|
36
|
-
return
|
|
36
|
+
return raw `
|
|
37
37
|
SELECT TABLE_NAME as table_name
|
|
38
38
|
FROM information_schema.TABLES
|
|
39
39
|
WHERE TABLE_SCHEMA = ${this.schemaExpr}
|
|
@@ -41,15 +41,14 @@ export class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
41
41
|
ORDER BY TABLE_NAME
|
|
42
42
|
`;
|
|
43
43
|
}
|
|
44
|
-
tableExistsQuery() {
|
|
45
|
-
return
|
|
44
|
+
tableExistsQuery(tableName) {
|
|
45
|
+
return raw `
|
|
46
46
|
SELECT 1 FROM information_schema.TABLES
|
|
47
|
-
WHERE TABLE_SCHEMA = ${this.schemaExpr} AND TABLE_NAME =
|
|
47
|
+
WHERE TABLE_SCHEMA = ${this.schemaExpr} AND TABLE_NAME = ${tableName} AND TABLE_TYPE = 'BASE TABLE'
|
|
48
48
|
`;
|
|
49
49
|
}
|
|
50
50
|
async getColumns(read, tableName) {
|
|
51
|
-
const rows = await read(
|
|
52
|
-
/*sql*/ `
|
|
51
|
+
const rows = await read(raw `
|
|
53
52
|
SELECT
|
|
54
53
|
COLUMN_NAME as column_name,
|
|
55
54
|
DATA_TYPE as data_type,
|
|
@@ -65,9 +64,9 @@ export class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
65
64
|
COLUMN_COMMENT as column_comment
|
|
66
65
|
FROM information_schema.COLUMNS
|
|
67
66
|
WHERE TABLE_SCHEMA = ${this.schemaExpr}
|
|
68
|
-
AND TABLE_NAME =
|
|
67
|
+
AND TABLE_NAME = ${tableName}
|
|
69
68
|
ORDER BY ORDINAL_POSITION
|
|
70
|
-
|
|
69
|
+
`);
|
|
71
70
|
return rows.map((row) => ({
|
|
72
71
|
name: row.column_name,
|
|
73
72
|
type: row.column_type.toUpperCase(),
|
|
@@ -84,8 +83,7 @@ export class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
84
83
|
}));
|
|
85
84
|
}
|
|
86
85
|
async getIndexes(read, tableName) {
|
|
87
|
-
const rows = await read(
|
|
88
|
-
/*sql*/ `
|
|
86
|
+
const rows = await read(raw `
|
|
89
87
|
SELECT
|
|
90
88
|
INDEX_NAME as index_name,
|
|
91
89
|
GROUP_CONCAT(COALESCE(COLUMN_NAME, '') ORDER BY SEQ_IN_INDEX) as columns,
|
|
@@ -93,11 +91,11 @@ export class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
93
91
|
MAX(INDEX_TYPE) as method
|
|
94
92
|
FROM information_schema.STATISTICS
|
|
95
93
|
WHERE TABLE_SCHEMA = ${this.schemaExpr}
|
|
96
|
-
AND TABLE_NAME =
|
|
94
|
+
AND TABLE_NAME = ${tableName}
|
|
97
95
|
AND INDEX_NAME != 'PRIMARY'
|
|
98
96
|
GROUP BY INDEX_NAME, NON_UNIQUE
|
|
99
97
|
ORDER BY INDEX_NAME
|
|
100
|
-
|
|
98
|
+
`);
|
|
101
99
|
return rows.map((row) => ({
|
|
102
100
|
name: row.index_name,
|
|
103
101
|
...(row.method === 'VECTOR' && { type: 'vector' }),
|
|
@@ -109,8 +107,7 @@ export class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
109
107
|
}));
|
|
110
108
|
}
|
|
111
109
|
async getForeignKeys(read, tableName) {
|
|
112
|
-
const rows = await read(
|
|
113
|
-
/*sql*/ `
|
|
110
|
+
const rows = await read(raw `
|
|
114
111
|
SELECT
|
|
115
112
|
kcu.CONSTRAINT_NAME as constraint_name,
|
|
116
113
|
GROUP_CONCAT(kcu.COLUMN_NAME ORDER BY kcu.ORDINAL_POSITION) as columns,
|
|
@@ -123,24 +120,23 @@ export class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
123
120
|
ON kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
|
|
124
121
|
AND kcu.TABLE_SCHEMA = rc.CONSTRAINT_SCHEMA
|
|
125
122
|
WHERE kcu.TABLE_SCHEMA = ${this.schemaExpr}
|
|
126
|
-
AND kcu.TABLE_NAME =
|
|
123
|
+
AND kcu.TABLE_NAME = ${tableName}
|
|
127
124
|
AND kcu.REFERENCED_TABLE_NAME IS NOT NULL
|
|
128
125
|
GROUP BY kcu.CONSTRAINT_NAME, kcu.REFERENCED_TABLE_NAME, rc.DELETE_RULE, rc.UPDATE_RULE
|
|
129
126
|
ORDER BY kcu.CONSTRAINT_NAME
|
|
130
|
-
|
|
127
|
+
`);
|
|
131
128
|
return this.joinedForeignKeys(rows);
|
|
132
129
|
}
|
|
133
130
|
/** Unnamed: MySQL calls every key `PRIMARY`, and drops one by no name. */
|
|
134
131
|
getPrimaryKey(read, tableName) {
|
|
135
|
-
return this.readPrimaryKey(read,
|
|
136
|
-
/*sql*/ `
|
|
132
|
+
return this.readPrimaryKey(read, raw `
|
|
137
133
|
SELECT COLUMN_NAME as column_name
|
|
138
134
|
FROM information_schema.KEY_COLUMN_USAGE
|
|
139
135
|
WHERE TABLE_SCHEMA = ${this.schemaExpr}
|
|
140
|
-
AND TABLE_NAME =
|
|
136
|
+
AND TABLE_NAME = ${tableName}
|
|
141
137
|
AND CONSTRAINT_NAME = 'PRIMARY'
|
|
142
138
|
ORDER BY ORDINAL_POSITION
|
|
143
|
-
|
|
139
|
+
`);
|
|
144
140
|
}
|
|
145
141
|
/**
|
|
146
142
|
* MySQL prints a literal default unquoted, except one wrapped as `DEFAULT ('x')`, the form a `TEXT`
|
|
@@ -194,13 +190,12 @@ export class MariadbSchemaIntrospector extends MysqlSchemaIntrospector {
|
|
|
194
190
|
* JSON column's `json_valid()` is its type, read by {@link getColumns}, so it is left out.
|
|
195
191
|
*/
|
|
196
192
|
async getChecks(read, tableName) {
|
|
197
|
-
return read(
|
|
198
|
-
/*sql*/ `
|
|
193
|
+
return read(raw `
|
|
199
194
|
SELECT CONSTRAINT_NAME AS name, CHECK_CLAUSE AS expression
|
|
200
195
|
FROM information_schema.CHECK_CONSTRAINTS
|
|
201
|
-
WHERE CONSTRAINT_SCHEMA = ${this.schemaExpr} AND TABLE_NAME =
|
|
196
|
+
WHERE CONSTRAINT_SCHEMA = ${this.schemaExpr} AND TABLE_NAME = ${tableName}
|
|
202
197
|
AND CHECK_CLAUSE <> CONCAT('json_valid(\`', CONSTRAINT_NAME, '\`)')
|
|
203
|
-
|
|
198
|
+
`);
|
|
204
199
|
}
|
|
205
200
|
/** MariaDB prints a literal quoted, as SQL reads it (`'it''s'`), so an unquoted default is SQL. */
|
|
206
201
|
sqlText(defaultValue) {
|
|
@@ -220,7 +215,7 @@ export class MariadbSchemaIntrospector extends MysqlSchemaIntrospector {
|
|
|
220
215
|
const qualified = [this.schema, tableName]
|
|
221
216
|
.filter((name) => name !== undefined)
|
|
222
217
|
.map((name) => this.dialect.escapeId(name));
|
|
223
|
-
const [row] = await read(
|
|
218
|
+
const [row] = await read(raw.text(`SHOW CREATE TABLE ${qualified.join('.')}`));
|
|
224
219
|
const lines = row['Create Table'].split('\n');
|
|
225
220
|
return indexes.map((index) => {
|
|
226
221
|
if (index.type !== 'vector') {
|
|
@@ -233,14 +228,13 @@ export class MariadbSchemaIntrospector extends MysqlSchemaIntrospector {
|
|
|
233
228
|
}
|
|
234
229
|
async getColumns(read, tableName) {
|
|
235
230
|
const columns = await super.getColumns(read, tableName);
|
|
236
|
-
const checks = await read(
|
|
237
|
-
/*sql*/ `
|
|
231
|
+
const checks = await read(raw `
|
|
238
232
|
SELECT CONSTRAINT_NAME as column_name
|
|
239
233
|
FROM information_schema.CHECK_CONSTRAINTS
|
|
240
234
|
WHERE CONSTRAINT_SCHEMA = ${this.schemaExpr}
|
|
241
|
-
AND TABLE_NAME =
|
|
235
|
+
AND TABLE_NAME = ${tableName}
|
|
242
236
|
AND CHECK_CLAUSE = CONCAT('json_valid(\`', CONSTRAINT_NAME, '\`)')
|
|
243
|
-
|
|
237
|
+
`);
|
|
244
238
|
const jsonColumns = new Set(checks.map((row) => row.column_name));
|
|
245
239
|
// The reported `LONGTEXT` length is that type's maximum, which means nothing for a JSON column.
|
|
246
240
|
return columns.map((column) => jsonColumns.has(column.name) ? { ...column, type: 'JSON', length: undefined } : column);
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import type { IndexFacet } from '../../schema/indexDifferences.js';
|
|
2
2
|
import { type CheckSchema } from '../../schema/types.js';
|
|
3
|
-
import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema } from '../../type/index.js';
|
|
3
|
+
import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema, QueryRaw } from '../../type/index.js';
|
|
4
4
|
import { AbstractSqlSchemaIntrospector, type ReadColumn, type TableRowReader } from './abstractSqlSchemaIntrospector.js';
|
|
5
5
|
/**
|
|
6
6
|
* PostgreSQL schema introspector
|
|
7
7
|
*/
|
|
8
8
|
export declare class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
9
|
-
protected readonly defaultSchemaExpr
|
|
9
|
+
protected readonly defaultSchemaExpr: QueryRaw;
|
|
10
10
|
/**
|
|
11
11
|
* Expressions and predicates are read back too, for `generate:from-db`, but they are text the
|
|
12
12
|
* database reprints in its own words, so they are not comparable and are not claimed here.
|
|
13
13
|
*/
|
|
14
14
|
protected readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
15
|
-
protected triggersQuery():
|
|
15
|
+
protected triggersQuery(tableName: string): QueryRaw;
|
|
16
16
|
/** `pg_get_expr` answers NULL for a table dropped mid-read, where `pg_get_constraintdef` raises. */
|
|
17
17
|
protected getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
|
|
18
|
-
protected getTableNamesQuery():
|
|
19
|
-
protected tableExistsQuery():
|
|
18
|
+
protected getTableNamesQuery(): QueryRaw;
|
|
19
|
+
protected tableExistsQuery(tableName: string): QueryRaw;
|
|
20
20
|
/**
|
|
21
21
|
* The comment reads through `to_regclass` rather than a `::regclass` cast: a name resolves against
|
|
22
22
|
* the live catalogue while `information_schema` answers from this statement's snapshot, so a table
|
|
@@ -30,7 +30,7 @@ export declare class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrosp
|
|
|
30
30
|
*/
|
|
31
31
|
protected getColumns(read: TableRowReader, tableName: string): Promise<ReadColumn[]>;
|
|
32
32
|
/** Whether `information_schema.columns c` lists a column the table shows: every one, on Postgres. */
|
|
33
|
-
protected readonly visibleColumnSql:
|
|
33
|
+
protected readonly visibleColumnSql: QueryRaw;
|
|
34
34
|
/**
|
|
35
35
|
* `attname` where the entry is a column, `pg_get_indexdef` for that one position where it is an
|
|
36
36
|
* expression. Neither alone will do: an expression entry has `attnum = 0`, so joining `pg_attribute`
|
|
@@ -44,11 +44,11 @@ export declare class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrosp
|
|
|
44
44
|
*/
|
|
45
45
|
protected getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
|
|
46
46
|
/** Whether an entry sorts nulls first, which Postgres states on every entry. */
|
|
47
|
-
protected readonly nullsFirstSql:
|
|
47
|
+
protected readonly nullsFirstSql: QueryRaw;
|
|
48
48
|
/** An index's access method, which is the type it declares. */
|
|
49
|
-
protected readonly indexMethodSql:
|
|
49
|
+
protected readonly indexMethodSql: QueryRaw;
|
|
50
50
|
/** An entry's operator class, where it is not the default for its type. */
|
|
51
|
-
protected readonly opsClassSql:
|
|
51
|
+
protected readonly opsClassSql: QueryRaw;
|
|
52
52
|
/** From `pg_constraint`, whose key arrays keep each column paired with the one it references. */
|
|
53
53
|
protected getForeignKeys(read: TableRowReader, tableName: string): Promise<ForeignKeySchema[]>;
|
|
54
54
|
protected getPrimaryKey(read: TableRowReader, tableName: string): Promise<PrimaryKeySchema | undefined>;
|
|
@@ -76,13 +76,13 @@ export declare class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrosp
|
|
|
76
76
|
export declare class CockroachSchemaIntrospector extends PostgresSchemaIntrospector {
|
|
77
77
|
protected readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
78
78
|
/** Not `rowid`, the `NOT VISIBLE` key it gives a table declared without one, which no entity can name. */
|
|
79
|
-
protected readonly visibleColumnSql
|
|
79
|
+
protected readonly visibleColumnSql: QueryRaw;
|
|
80
80
|
/** None: it rejects a stated nulls order, so reading one back gives an index it would refuse to rebuild. */
|
|
81
|
-
protected readonly nullsFirstSql
|
|
81
|
+
protected readonly nullsFirstSql: QueryRaw;
|
|
82
82
|
/**
|
|
83
83
|
* Every index reports the access method `prefix` and no operator class, so a vector index is read off
|
|
84
84
|
* its definition, `USING cspann (vec vector_cosine_ops)`: its type, and its last key's class.
|
|
85
85
|
*/
|
|
86
|
-
protected readonly indexMethodSql
|
|
87
|
-
protected readonly opsClassSql
|
|
86
|
+
protected readonly indexMethodSql: QueryRaw;
|
|
87
|
+
protected readonly opsClassSql: QueryRaw;
|
|
88
88
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { INDEX_TYPES } from '../../schema/types.js';
|
|
2
2
|
import { isVectorIndexType } from '../../type/vector.js';
|
|
3
|
+
import { raw } from '../../util/raw.js';
|
|
3
4
|
import { AbstractSqlSchemaIntrospector, } from './abstractSqlSchemaIntrospector.js';
|
|
4
5
|
/**
|
|
5
6
|
* PostgreSQL schema introspector
|
|
6
7
|
*/
|
|
7
8
|
export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
8
|
-
defaultSchemaExpr =
|
|
9
|
+
defaultSchemaExpr = raw `current_schema()`;
|
|
9
10
|
/**
|
|
10
11
|
* Expressions and predicates are read back too, for `generate:from-db`, but they are text the
|
|
11
12
|
* database reprints in its own words, so they are not comparable and are not claimed here.
|
|
@@ -18,29 +19,28 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
18
19
|
'include',
|
|
19
20
|
'distance',
|
|
20
21
|
]);
|
|
21
|
-
triggersQuery() {
|
|
22
|
-
return
|
|
22
|
+
triggersQuery(tableName) {
|
|
23
|
+
return raw `
|
|
23
24
|
SELECT t.tgname AS name, pg_get_triggerdef(t.oid) AS definition, pg_get_functiondef(t.tgfoid) AS requires
|
|
24
25
|
FROM pg_trigger t
|
|
25
26
|
JOIN pg_class c ON c.oid = t.tgrelid
|
|
26
27
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
27
|
-
WHERE NOT t.tgisinternal AND n.nspname = ${this.schemaExpr} AND c.relname = ${
|
|
28
|
+
WHERE NOT t.tgisinternal AND n.nspname = ${this.schemaExpr} AND c.relname = ${tableName}
|
|
28
29
|
`;
|
|
29
30
|
}
|
|
30
31
|
/** `pg_get_expr` answers NULL for a table dropped mid-read, where `pg_get_constraintdef` raises. */
|
|
31
32
|
async getChecks(read, tableName) {
|
|
32
|
-
const rows = await read(
|
|
33
|
-
/*sql*/ `
|
|
33
|
+
const rows = await read(raw `
|
|
34
34
|
SELECT con.conname AS name, pg_get_expr(con.conbin, con.conrelid) AS expression
|
|
35
35
|
FROM pg_constraint con
|
|
36
36
|
JOIN pg_class t ON t.oid = con.conrelid
|
|
37
37
|
JOIN pg_namespace n ON n.oid = t.relnamespace
|
|
38
|
-
WHERE con.contype = 'c' AND t.relname = $
|
|
39
|
-
|
|
38
|
+
WHERE con.contype = 'c' AND t.relname = ${tableName} AND n.nspname = ${this.schemaExpr}
|
|
39
|
+
`);
|
|
40
40
|
return rows.flatMap(({ name, expression }) => (expression === null ? [] : [{ name, expression }]));
|
|
41
41
|
}
|
|
42
42
|
getTableNamesQuery() {
|
|
43
|
-
return
|
|
43
|
+
return raw `
|
|
44
44
|
SELECT table_name
|
|
45
45
|
FROM information_schema.tables
|
|
46
46
|
WHERE table_schema = ${this.schemaExpr}
|
|
@@ -48,10 +48,10 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
48
48
|
ORDER BY table_name
|
|
49
49
|
`;
|
|
50
50
|
}
|
|
51
|
-
tableExistsQuery() {
|
|
52
|
-
return
|
|
51
|
+
tableExistsQuery(tableName) {
|
|
52
|
+
return raw `
|
|
53
53
|
SELECT 1 FROM information_schema.tables
|
|
54
|
-
WHERE table_schema = ${this.schemaExpr} AND table_name = $
|
|
54
|
+
WHERE table_schema = ${this.schemaExpr} AND table_name = ${tableName} AND table_type = 'BASE TABLE'
|
|
55
55
|
`;
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
@@ -66,8 +66,7 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
66
66
|
* CockroachDB names that type `vector` where Postgres says `USER-DEFINED`.
|
|
67
67
|
*/
|
|
68
68
|
async getColumns(read, tableName) {
|
|
69
|
-
const rows = await read(
|
|
70
|
-
/*sql*/ `
|
|
69
|
+
const rows = await read(raw `
|
|
71
70
|
SELECT
|
|
72
71
|
c.column_name,
|
|
73
72
|
c.data_type,
|
|
@@ -91,10 +90,10 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
91
90
|
ON a.attrelid = to_regclass(quote_ident(c.table_schema) || '.' || quote_ident(c.table_name))
|
|
92
91
|
AND a.attname = c.column_name
|
|
93
92
|
WHERE c.table_schema = ${this.schemaExpr}
|
|
94
|
-
AND c.table_name = $
|
|
93
|
+
AND c.table_name = ${tableName}
|
|
95
94
|
AND ${this.visibleColumnSql}
|
|
96
95
|
ORDER BY c.ordinal_position
|
|
97
|
-
|
|
96
|
+
`);
|
|
98
97
|
return rows.map((row) => ({
|
|
99
98
|
name: row.column_name,
|
|
100
99
|
type: row.formatted_type?.toUpperCase() ?? this.normalizeType(row.data_type, row.udt_name),
|
|
@@ -109,7 +108,7 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
109
108
|
}));
|
|
110
109
|
}
|
|
111
110
|
/** Whether `information_schema.columns c` lists a column the table shows: every one, on Postgres. */
|
|
112
|
-
visibleColumnSql =
|
|
111
|
+
visibleColumnSql = raw `TRUE`;
|
|
113
112
|
/**
|
|
114
113
|
* `attname` where the entry is a column, `pg_get_indexdef` for that one position where it is an
|
|
115
114
|
* expression. Neither alone will do: an expression entry has `attnum = 0`, so joining `pg_attribute`
|
|
@@ -122,8 +121,7 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
122
121
|
* uniqueness, and one over several as the unique `@Index` it is.
|
|
123
122
|
*/
|
|
124
123
|
async getIndexes(read, tableName) {
|
|
125
|
-
const rows = await read(
|
|
126
|
-
/*sql*/ `
|
|
124
|
+
const rows = await read(raw `
|
|
127
125
|
SELECT
|
|
128
126
|
i.relname AS index_name,
|
|
129
127
|
ix.indisunique AS is_unique,
|
|
@@ -143,14 +141,14 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
143
141
|
CROSS JOIN LATERAL UNNEST(ix.indkey) WITH ORDINALITY AS k(attnum, n)
|
|
144
142
|
LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum AND k.attnum > 0
|
|
145
143
|
LEFT JOIN pg_opclass op ON op.oid = ix.indclass[k.n - 1]
|
|
146
|
-
WHERE t.relname = $
|
|
144
|
+
WHERE t.relname = ${tableName}
|
|
147
145
|
AND n.nspname = ${this.schemaExpr}
|
|
148
146
|
AND NOT ix.indisprimary
|
|
149
147
|
AND NOT EXISTS (
|
|
150
148
|
SELECT 1 FROM pg_constraint con WHERE con.conindid = ix.indexrelid AND con.contype = 'x'
|
|
151
149
|
)
|
|
152
150
|
ORDER BY i.relname, k.n
|
|
153
|
-
|
|
151
|
+
`);
|
|
154
152
|
// One row per index entry, ordered by position, so the rows of an index are its entries in order.
|
|
155
153
|
return [...Map.groupBy(rows, (row) => row.index_name)].map(([name, ofIndex]) => {
|
|
156
154
|
const [first] = ofIndex;
|
|
@@ -167,25 +165,24 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
167
165
|
});
|
|
168
166
|
}
|
|
169
167
|
/** Whether an entry sorts nulls first, which Postgres states on every entry. */
|
|
170
|
-
nullsFirstSql =
|
|
168
|
+
nullsFirstSql = raw `(ix.indoption[k.n - 1] & 2) <> 0`;
|
|
171
169
|
/** An index's access method, which is the type it declares. */
|
|
172
|
-
indexMethodSql =
|
|
170
|
+
indexMethodSql = raw `am.amname`;
|
|
173
171
|
/** An entry's operator class, where it is not the default for its type. */
|
|
174
|
-
opsClassSql =
|
|
172
|
+
opsClassSql = raw `CASE WHEN op.opcdefault THEN NULL ELSE op.opcname END`;
|
|
175
173
|
/** From `pg_constraint`, whose key arrays keep each column paired with the one it references. */
|
|
176
174
|
async getForeignKeys(read, tableName) {
|
|
177
|
-
const columnsOf = (keys, table) =>
|
|
175
|
+
const columnsOf = (keys, table) => raw `ARRAY_TO_JSON(ARRAY(
|
|
178
176
|
SELECT a.attname FROM UNNEST(${keys}) WITH ORDINALITY AS k(attnum, n)
|
|
179
177
|
JOIN pg_attribute a ON a.attrelid = ${table} AND a.attnum = k.attnum
|
|
180
178
|
ORDER BY k.n
|
|
181
179
|
))`;
|
|
182
|
-
const rows = await read(
|
|
183
|
-
/*sql*/ `
|
|
180
|
+
const rows = await read(raw `
|
|
184
181
|
SELECT
|
|
185
182
|
con.conname AS constraint_name,
|
|
186
|
-
${columnsOf(
|
|
183
|
+
${columnsOf(raw `con.conkey`, raw `con.conrelid`)} AS columns,
|
|
187
184
|
ref.relname AS referenced_table,
|
|
188
|
-
${columnsOf(
|
|
185
|
+
${columnsOf(raw `con.confkey`, raw `con.confrelid`)} AS referenced_columns,
|
|
189
186
|
con.confdeltype AS delete_rule,
|
|
190
187
|
con.confupdtype AS update_rule
|
|
191
188
|
FROM pg_constraint con
|
|
@@ -193,10 +190,10 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
193
190
|
JOIN pg_namespace n ON n.oid = t.relnamespace
|
|
194
191
|
JOIN pg_class ref ON ref.oid = con.confrelid
|
|
195
192
|
WHERE con.contype = 'f'
|
|
196
|
-
AND t.relname = $
|
|
193
|
+
AND t.relname = ${tableName}
|
|
197
194
|
AND n.nspname = ${this.schemaExpr}
|
|
198
195
|
ORDER BY con.conname
|
|
199
|
-
|
|
196
|
+
`);
|
|
200
197
|
return rows.map((row) => ({
|
|
201
198
|
name: row.constraint_name,
|
|
202
199
|
columns: row.columns,
|
|
@@ -206,8 +203,7 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
206
203
|
}));
|
|
207
204
|
}
|
|
208
205
|
getPrimaryKey(read, tableName) {
|
|
209
|
-
return this.readPrimaryKey(read,
|
|
210
|
-
/*sql*/ `
|
|
206
|
+
return this.readPrimaryKey(read, raw `
|
|
211
207
|
SELECT kcu.column_name, tc.constraint_name
|
|
212
208
|
FROM information_schema.table_constraints tc
|
|
213
209
|
JOIN information_schema.key_column_usage kcu
|
|
@@ -218,11 +214,11 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
218
214
|
AND c.table_name = kcu.table_name
|
|
219
215
|
AND c.column_name = kcu.column_name
|
|
220
216
|
WHERE tc.constraint_type = 'PRIMARY KEY'
|
|
221
|
-
AND tc.table_name = $
|
|
217
|
+
AND tc.table_name = ${tableName}
|
|
222
218
|
AND tc.table_schema = ${this.schemaExpr}
|
|
223
219
|
AND ${this.visibleColumnSql}
|
|
224
220
|
ORDER BY kcu.ordinal_position
|
|
225
|
-
|
|
221
|
+
`);
|
|
226
222
|
}
|
|
227
223
|
/**
|
|
228
224
|
* A vector index keeps its distance as its vector column's operator class, `{type}_{metric}_ops`, which
|
|
@@ -340,13 +336,13 @@ export class CockroachSchemaIntrospector extends PostgresSchemaIntrospector {
|
|
|
340
336
|
'distance',
|
|
341
337
|
]);
|
|
342
338
|
/** Not `rowid`, the `NOT VISIBLE` key it gives a table declared without one, which no entity can name. */
|
|
343
|
-
visibleColumnSql =
|
|
339
|
+
visibleColumnSql = raw `c.is_hidden = 'NO'`;
|
|
344
340
|
/** None: it rejects a stated nulls order, so reading one back gives an index it would refuse to rebuild. */
|
|
345
|
-
nullsFirstSql =
|
|
341
|
+
nullsFirstSql = raw `NULL::BOOL`;
|
|
346
342
|
/**
|
|
347
343
|
* Every index reports the access method `prefix` and no operator class, so a vector index is read off
|
|
348
344
|
* its definition, `USING cspann (vec vector_cosine_ops)`: its type, and its last key's class.
|
|
349
345
|
*/
|
|
350
|
-
indexMethodSql = `CASE WHEN pg_get_indexdef(ix.indexrelid) LIKE '% USING cspann %' THEN 'vector' ELSE am.amname END`;
|
|
351
|
-
opsClassSql = `CASE WHEN k.n = ix.indnkeyatts THEN substring(pg_get_indexdef(ix.indexrelid) from '(\\w+_ops)\\)') END`;
|
|
346
|
+
indexMethodSql = raw `CASE WHEN pg_get_indexdef(ix.indexrelid) LIKE '% USING cspann %' THEN 'vector' ELSE am.amname END`;
|
|
347
|
+
opsClassSql = raw `CASE WHEN k.n = ix.indnkeyatts THEN substring(pg_get_indexdef(ix.indexrelid) from '(\\w+_ops)\\)') END`;
|
|
352
348
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MongoSchemaIntrospector } from '../../
|
|
1
|
+
import { MongoSchemaIntrospector } from '../../mongodb/mongoIntrospector.js';
|
|
2
2
|
import { MsSqlSchemaIntrospector } from './mssqlIntrospector.js';
|
|
3
3
|
import { MariadbSchemaIntrospector, MysqlSchemaIntrospector } from './mysqlIntrospector.js';
|
|
4
4
|
import { CockroachSchemaIntrospector, PostgresSchemaIntrospector } from './postgresIntrospector.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IndexFacet } from '../../schema/indexDifferences.js';
|
|
2
2
|
import type { CheckSchema } from '../../schema/types.js';
|
|
3
|
-
import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema, StoredDefinition } from '../../type/index.js';
|
|
3
|
+
import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema, QueryRaw, StoredDefinition } from '../../type/index.js';
|
|
4
4
|
import { AbstractSqlSchemaIntrospector, type ReadColumn, type TableRowReader } from './abstractSqlSchemaIntrospector.js';
|
|
5
5
|
/**
|
|
6
6
|
* SQLite schema introspector
|
|
@@ -8,10 +8,10 @@ import { AbstractSqlSchemaIntrospector, type ReadColumn, type TableRowReader } f
|
|
|
8
8
|
export declare class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
9
9
|
/** Whether an index is libSQL's vector index, where the engine has one; elsewhere a declared one is built plain. */
|
|
10
10
|
protected readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
11
|
-
protected triggersQuery():
|
|
11
|
+
protected triggersQuery(tableName: string): QueryRaw;
|
|
12
12
|
/** User tables only: skips SQLite's own and libSQL's vector index tables (its metadata and `<index>_shadow`). */
|
|
13
|
-
protected getTableNamesQuery():
|
|
14
|
-
protected tableExistsQuery():
|
|
13
|
+
protected getTableNamesQuery(): QueryRaw;
|
|
14
|
+
protected tableExistsQuery(tableName: string): QueryRaw;
|
|
15
15
|
/**
|
|
16
16
|
* `table_xinfo`, not `table_info`: the latter omits generated columns entirely, so a table carrying
|
|
17
17
|
* one read back without it and every sync offered to add a column that was already there - which
|
|
@@ -33,6 +33,8 @@ export declare class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospec
|
|
|
33
33
|
/** libSQL's `libsql_vector_idx(col, 'metric=...')`, read back from the statement that created it. */
|
|
34
34
|
private vectorIndex;
|
|
35
35
|
private getIndexColumns;
|
|
36
|
+
/** A PRAGMA takes no parameters, so the name it reads is written into the statement, escaped. */
|
|
37
|
+
private pragma;
|
|
36
38
|
protected normalizeType(type: string): string;
|
|
37
39
|
protected extractLength(type: string): number | undefined;
|
|
38
40
|
protected parseDefaultValue(defaultValue: string | null): unknown;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { raw } from '../../util/raw.js';
|
|
1
2
|
import { AbstractSqlSchemaIntrospector, } from './abstractSqlSchemaIntrospector.js';
|
|
2
3
|
/**
|
|
3
4
|
* SQLite schema introspector
|
|
@@ -5,12 +6,12 @@ import { AbstractSqlSchemaIntrospector, } from './abstractSqlSchemaIntrospector.
|
|
|
5
6
|
export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
6
7
|
/** Whether an index is libSQL's vector index, where the engine has one; elsewhere a declared one is built plain. */
|
|
7
8
|
indexFacets = new Set(this.dialect.hasVectorIndex() ? ['vector', 'distance'] : []);
|
|
8
|
-
triggersQuery() {
|
|
9
|
-
return
|
|
9
|
+
triggersQuery(tableName) {
|
|
10
|
+
return raw `SELECT name, sql AS definition FROM sqlite_master WHERE type = 'trigger' AND tbl_name = ${tableName}`;
|
|
10
11
|
}
|
|
11
12
|
/** User tables only: skips SQLite's own and libSQL's vector index tables (its metadata and `<index>_shadow`). */
|
|
12
13
|
getTableNamesQuery() {
|
|
13
|
-
return
|
|
14
|
+
return raw `
|
|
14
15
|
SELECT name AS table_name
|
|
15
16
|
FROM sqlite_master
|
|
16
17
|
WHERE type = 'table'
|
|
@@ -20,8 +21,8 @@ export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
20
21
|
ORDER BY name
|
|
21
22
|
`;
|
|
22
23
|
}
|
|
23
|
-
tableExistsQuery() {
|
|
24
|
-
return
|
|
24
|
+
tableExistsQuery(tableName) {
|
|
25
|
+
return raw `SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ${tableName}`;
|
|
25
26
|
}
|
|
26
27
|
/**
|
|
27
28
|
* `table_xinfo`, not `table_info`: the latter omits generated columns entirely, so a table carrying
|
|
@@ -29,7 +30,7 @@ export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
29
30
|
* SQLite cannot do to an existing table anyway. Also the key, by each column's `pk`: one statement for both.
|
|
30
31
|
*/
|
|
31
32
|
tableInfo(read, tableName) {
|
|
32
|
-
return read(
|
|
33
|
+
return read(this.pragma('table_xinfo', tableName));
|
|
33
34
|
}
|
|
34
35
|
async getColumns(read, tableName) {
|
|
35
36
|
const rows = await this.tableInfo(read, tableName);
|
|
@@ -54,7 +55,7 @@ export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
54
55
|
return key.length ? { columns: key.map((row) => row.name) } : undefined;
|
|
55
56
|
}
|
|
56
57
|
async getIndexes(read, tableName) {
|
|
57
|
-
const list = await read(
|
|
58
|
+
const list = await read(this.pragma('index_list', tableName));
|
|
58
59
|
const statements = new Map((await this.getDefinition(read, tableName)).map(({ name, sql }) => [name, sql]));
|
|
59
60
|
const indexes = [];
|
|
60
61
|
// The key's own index ('pk') is left out; a unique constraint's ('u') is reported as every engine reports one.
|
|
@@ -77,8 +78,7 @@ export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
77
78
|
* AST derives one from the columns, as the entity side does.
|
|
78
79
|
*/
|
|
79
80
|
async getForeignKeys(read, tableName) {
|
|
80
|
-
const rows = await read(
|
|
81
|
-
/*sql*/ `PRAGMA foreign_key_list(${this.dialect.escapeId(tableName)})`);
|
|
81
|
+
const rows = await read(this.pragma('foreign_key_list', tableName));
|
|
82
82
|
return [...Map.groupBy(rows, (row) => row.id).values()].map((key) => {
|
|
83
83
|
const [first] = key;
|
|
84
84
|
return {
|
|
@@ -96,8 +96,7 @@ export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
96
96
|
}
|
|
97
97
|
/** Every statement `sqlite_master` keeps for the table, its `CREATE TABLE` first. An automatic index has none. */
|
|
98
98
|
getDefinition(read, tableName) {
|
|
99
|
-
return read(
|
|
100
|
-
/*sql*/ `SELECT type AS kind, name, sql FROM sqlite_master WHERE tbl_name = ? AND sql IS NOT NULL ORDER BY type <> 'table'`, [tableName]);
|
|
99
|
+
return read(raw `SELECT type AS kind, name, sql FROM sqlite_master WHERE tbl_name = ${tableName} AND sql IS NOT NULL ORDER BY type <> 'table'`);
|
|
101
100
|
}
|
|
102
101
|
/** libSQL's `libsql_vector_idx(col, 'metric=...')`, read back from the statement that created it. */
|
|
103
102
|
vectorIndex(name, sql) {
|
|
@@ -115,7 +114,11 @@ export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
115
114
|
};
|
|
116
115
|
}
|
|
117
116
|
getIndexColumns(read, indexName) {
|
|
118
|
-
return read(
|
|
117
|
+
return read(this.pragma('index_info', indexName));
|
|
118
|
+
}
|
|
119
|
+
/** A PRAGMA takes no parameters, so the name it reads is written into the statement, escaped. */
|
|
120
|
+
pragma(name, of) {
|
|
121
|
+
return raw.text(`PRAGMA ${name}(${this.dialect.escapeId(of)})`);
|
|
119
122
|
}
|
|
120
123
|
normalizeType(type) {
|
|
121
124
|
// Extract base type without length/precision
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { AbstractSqlDialect } from '../dialect/abstractSqlDialect.js';
|
|
2
|
+
import type { QuerierPool, QueryRaw, SqlQuerier } from '../type/index.js';
|
|
3
|
+
/** How long `up` and `down` wait for the lock another run holds, unless told otherwise. */
|
|
4
|
+
export declare const DEFAULT_LOCK_TIMEOUT: number;
|
|
5
|
+
/** The statements taking and releasing a named lock: `acquire` takes it without waiting, answering a row only when it did. */
|
|
6
|
+
type NamedLockSql = {
|
|
7
|
+
readonly acquire: QueryRaw;
|
|
8
|
+
readonly release: QueryRaw;
|
|
9
|
+
};
|
|
10
|
+
/** The statements of the lock named `name` on `dialect`'s engine, where it has one its sessions can hold. */
|
|
11
|
+
export declare function namedLockSql(dialect: AbstractSqlDialect, name: string): NamedLockSql | undefined;
|
|
12
|
+
/** The lock a run holds, named after its journal, and how long a second run waits for it. */
|
|
13
|
+
export type MigrationLockOptions = {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly timeout: number;
|
|
16
|
+
};
|
|
17
|
+
/** A lock as a run holds it. */
|
|
18
|
+
export type MigrationLock = {
|
|
19
|
+
/** Takes the lock if it is free, answering whether it did; never waits. */
|
|
20
|
+
take(): Promise<boolean>;
|
|
21
|
+
release(): Promise<unknown>;
|
|
22
|
+
/** What to do should it stay held, which the timeout says. */
|
|
23
|
+
readonly stuck: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Runs `work` holding `lock`, asking for it until `timeout` passes, and releasing it however `work` ends: a
|
|
27
|
+
* release failing after `work` did is a consequence of its error, and never replaces it.
|
|
28
|
+
*/
|
|
29
|
+
export declare function holdingLock<T>(lock: MigrationLock, { name, timeout }: MigrationLockOptions, work: () => Promise<T>): Promise<T>;
|
|
30
|
+
/**
|
|
31
|
+
* Runs `work` holding the lock `options` names, the journal created first: on `querier`'s session, in a
|
|
32
|
+
* transaction of its own kept open meanwhile, or, where the engine has no named lock, by a record in the
|
|
33
|
+
* journal, which then has to exist before the lock, created by whichever run gets there.
|
|
34
|
+
*/
|
|
35
|
+
export declare function withSqlMigrationLock<T>(pool: QuerierPool, querier: SqlQuerier, options: MigrationLockOptions, work: () => Promise<T>): Promise<T>;
|
|
36
|
+
/** What to do about a lock record no run released, which only a person can tell is stale. */
|
|
37
|
+
export declare const staleRecord: (what: string, journal: string) => string;
|
|
38
|
+
export {};
|