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.
Files changed (117) hide show
  1. package/dist/dialect/abstractSqlDialect.d.ts +1 -7
  2. package/dist/dialect/abstractSqlDialect.js +1 -9
  3. package/dist/dialect/mysqlLikeSqlDialect.d.ts +2 -0
  4. package/dist/dialect/mysqlLikeSqlDialect.js +4 -1
  5. package/dist/dialect/pgLikeSqlDialect.js +0 -1
  6. package/dist/migrate/acquireQuerierForMigrations.d.ts +4 -8
  7. package/dist/migrate/acquireQuerierForMigrations.js +10 -18
  8. package/dist/migrate/builder/columnBuilder.d.ts +1 -2
  9. package/dist/migrate/builder/columnBuilder.js +3 -10
  10. package/dist/migrate/builder/migrationBuilder.d.ts +6 -26
  11. package/dist/migrate/builder/migrationBuilder.js +21 -78
  12. package/dist/migrate/builder/tableBuilder.js +2 -3
  13. package/dist/migrate/builder/types.d.ts +59 -120
  14. package/dist/migrate/cli-config.d.ts +1 -0
  15. package/dist/migrate/cli-config.js +11 -26
  16. package/dist/migrate/cli.d.ts +0 -13
  17. package/dist/migrate/cli.js +55 -118
  18. package/dist/migrate/codegen/entityCodeGenerator.d.ts +7 -61
  19. package/dist/migrate/codegen/entityCodeGenerator.js +53 -174
  20. package/dist/migrate/codegen/fieldOptionsSource.d.ts +1 -3
  21. package/dist/migrate/codegen/fieldOptionsSource.js +3 -11
  22. package/dist/migrate/codegen/index.d.ts +2 -7
  23. package/dist/migrate/codegen/index.js +2 -8
  24. package/dist/migrate/codegen/migrationFile.d.ts +1 -8
  25. package/dist/migrate/codegen/migrationFile.js +9 -13
  26. package/dist/migrate/ddl/defaultSql.d.ts +2 -1
  27. package/dist/migrate/ddl/defaultSql.js +1 -3
  28. package/dist/migrate/ddl/indexCapabilities.d.ts +18 -0
  29. package/dist/migrate/ddl/indexCapabilities.js +60 -0
  30. package/dist/migrate/ddl/indexDdl.d.ts +11 -24
  31. package/dist/migrate/ddl/indexDdl.js +22 -28
  32. package/dist/migrate/ddl/mssqlIndexDdl.d.ts +1 -7
  33. package/dist/migrate/ddl/mssqlIndexDdl.js +1 -7
  34. package/dist/migrate/ddl/mssqlTableDdl.d.ts +6 -9
  35. package/dist/migrate/ddl/mssqlTableDdl.js +14 -17
  36. package/dist/migrate/ddl/mysqlIndexDdl.d.ts +0 -21
  37. package/dist/migrate/ddl/mysqlIndexDdl.js +4 -48
  38. package/dist/migrate/ddl/mysqlTableDdl.d.ts +6 -5
  39. package/dist/migrate/ddl/mysqlTableDdl.js +15 -10
  40. package/dist/migrate/ddl/pgIndexDdl.d.ts +0 -10
  41. package/dist/migrate/ddl/pgIndexDdl.js +5 -44
  42. package/dist/migrate/ddl/pgTableDdl.d.ts +6 -2
  43. package/dist/migrate/ddl/pgTableDdl.js +17 -1
  44. package/dist/migrate/ddl/sqliteIndexDdl.js +2 -7
  45. package/dist/migrate/ddl/tableDdl.d.ts +43 -24
  46. package/dist/migrate/ddl/tableDdl.js +56 -37
  47. package/dist/migrate/ddl/tableRebuild.d.ts +2 -1
  48. package/dist/migrate/drift/driftDetector.d.ts +29 -32
  49. package/dist/migrate/drift/driftDetector.js +155 -205
  50. package/dist/migrate/drift/index.d.ts +1 -6
  51. package/dist/migrate/drift/index.js +1 -6
  52. package/dist/migrate/generator/definitionToNode.d.ts +2 -2
  53. package/dist/migrate/generator/definitionToNode.js +6 -5
  54. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +56 -77
  55. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +75 -98
  56. package/dist/migrate/introspection/mssqlIntrospector.d.ts +10 -43
  57. package/dist/migrate/introspection/mssqlIntrospector.js +52 -77
  58. package/dist/migrate/introspection/mysqlIntrospector.d.ts +21 -37
  59. package/dist/migrate/introspection/mysqlIntrospector.js +75 -56
  60. package/dist/migrate/introspection/postgresIntrospector.d.ts +15 -60
  61. package/dist/migrate/introspection/postgresIntrospector.js +74 -77
  62. package/dist/migrate/introspection/sqliteIntrospector.d.ts +19 -53
  63. package/dist/migrate/introspection/sqliteIntrospector.js +73 -119
  64. package/dist/migrate/introspection/tableSchemaAST.d.ts +13 -0
  65. package/dist/migrate/introspection/tableSchemaAST.js +85 -0
  66. package/dist/migrate/migrationTarget.js +10 -2
  67. package/dist/migrate/migrator.d.ts +9 -44
  68. package/dist/migrate/migrator.js +72 -150
  69. package/dist/migrate/schemaChange.d.ts +8 -5
  70. package/dist/migrate/schemaChange.js +25 -8
  71. package/dist/migrate/schemaGenerator.d.ts +35 -109
  72. package/dist/migrate/schemaGenerator.js +179 -353
  73. package/dist/migrate/storage/databaseStorage.d.ts +2 -1
  74. package/dist/migrate/storage/databaseStorage.js +3 -2
  75. package/dist/migrate/storage/jsonStorage.d.ts +1 -2
  76. package/dist/migrate/storage/jsonStorage.js +11 -20
  77. package/dist/migrate/triggerSql.d.ts +7 -9
  78. package/dist/migrate/triggerSql.js +17 -18
  79. package/dist/mongo/index.d.ts +0 -1
  80. package/dist/mongo/index.js +0 -1
  81. package/dist/mongo/mongoDialect.js +0 -1
  82. package/dist/mongo/mongoIntrospector.d.ts +1 -6
  83. package/dist/mongo/mongoIntrospector.js +7 -18
  84. package/dist/mongo/mongoSchemaGenerator.d.ts +7 -26
  85. package/dist/mongo/mongoSchemaGenerator.js +51 -75
  86. package/dist/mssql/mssqlDialect.js +0 -1
  87. package/dist/schema/canonicalType.d.ts +15 -19
  88. package/dist/schema/canonicalType.js +20 -43
  89. package/dist/schema/dependencyGraph.d.ts +2 -3
  90. package/dist/schema/dependencyGraph.js +0 -4
  91. package/dist/schema/indexDifferences.js +2 -2
  92. package/dist/schema/schemaAST.d.ts +1 -1
  93. package/dist/schema/schemaAST.js +5 -12
  94. package/dist/schema/schemaASTBuilder.d.ts +5 -6
  95. package/dist/schema/schemaASTBuilder.js +17 -18
  96. package/dist/schema/schemaASTDiffer.d.ts +4 -5
  97. package/dist/schema/schemaASTDiffer.js +40 -73
  98. package/dist/schema/types.d.ts +24 -54
  99. package/dist/sqlite/localSqliteQuerierPool.d.ts +16 -2
  100. package/dist/sqlite/localSqliteQuerierPool.js +20 -4
  101. package/dist/sqlite/nodeSqliteQuerierPool.js +2 -2
  102. package/dist/sqlite/sqliteDialect.js +0 -1
  103. package/dist/sqlite/sqliteQuerierPool.js +3 -3
  104. package/dist/type/dialect.d.ts +0 -2
  105. package/dist/type/migration.d.ts +13 -73
  106. package/dist/util/ddlExpression.util.d.ts +13 -2
  107. package/dist/util/ddlExpression.util.js +27 -4
  108. package/dist/util/sql.util.d.ts +2 -12
  109. package/dist/util/sql.util.js +9 -18
  110. package/package.json +1 -1
  111. package/skills/uql-orm/SKILL.md +1 -1
  112. package/dist/migrate/introspection/baseSqlIntrospector.d.ts +0 -36
  113. package/dist/migrate/introspection/baseSqlIntrospector.js +0 -133
  114. package/dist/schema/index.d.ts +0 -2
  115. package/dist/schema/index.js +0 -1
  116. package/dist/sqlite/sqlitePragmas.d.ts +0 -3
  117. package/dist/sqlite/sqlitePragmas.js +0 -15
@@ -1,6 +1,6 @@
1
1
  import { INDEX_TYPES } from '../../schema/types.js';
2
2
  import { isVectorIndexType } from '../../type/vector.js';
3
- import { AbstractSqlSchemaIntrospector } from './abstractSqlSchemaIntrospector.js';
3
+ import { AbstractSqlSchemaIntrospector, } from './abstractSqlSchemaIntrospector.js';
4
4
  /**
5
5
  * PostgreSQL schema introspector
6
6
  */
@@ -27,6 +27,18 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
27
27
  WHERE NOT t.tgisinternal AND n.nspname = ${this.schemaExpr} AND c.relname = ${this.dialect.placeholder(1)}
28
28
  `;
29
29
  }
30
+ /** `pg_get_expr` answers NULL for a table dropped mid-read, where `pg_get_constraintdef` raises. */
31
+ async getChecks(read, tableName) {
32
+ const rows = await read(
33
+ /*sql*/ `
34
+ SELECT con.conname AS name, pg_get_expr(con.conbin, con.conrelid) AS expression
35
+ FROM pg_constraint con
36
+ JOIN pg_class t ON t.oid = con.conrelid
37
+ JOIN pg_namespace n ON n.oid = t.relnamespace
38
+ WHERE con.contype = 'c' AND t.relname = $1 AND n.nspname = ${this.schemaExpr}
39
+ `, [tableName]);
40
+ return rows.flatMap(({ name, expression }) => (expression === null ? [] : [{ name, expression }]));
41
+ }
30
42
  getTableNamesQuery() {
31
43
  return /*sql*/ `
32
44
  SELECT table_name
@@ -38,16 +50,10 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
38
50
  }
39
51
  tableExistsQuery() {
40
52
  return /*sql*/ `
41
- SELECT EXISTS (
42
- SELECT FROM information_schema.tables
43
- WHERE table_schema = ${this.schemaExpr}
44
- AND table_name = $1
45
- ) AS exists
53
+ SELECT 1 FROM information_schema.tables
54
+ WHERE table_schema = ${this.schemaExpr} AND table_name = $1 AND table_type = 'BASE TABLE'
46
55
  `;
47
56
  }
48
- parseTableExistsResult([row]) {
49
- return row['exists'] === true;
50
- }
51
57
  /**
52
58
  * The comment reads through `to_regclass` rather than a `::regclass` cast: a name resolves against
53
59
  * the live catalogue while `information_schema` answers from this statement's snapshot, so a table
@@ -58,11 +64,10 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
58
64
  * virtual one Postgres 18 added and uql never declares. CockroachDB states it too. `format_type` for an
59
65
  * extension type's modifier, which `information_schema` drops: a `vector(256)` read back as `vector`.
60
66
  * CockroachDB names that type `vector` where Postgres says `USER-DEFINED`.
61
- *
62
- * A column is unique by a unique index over it alone, a constraint's or its own, as every engine reads it.
63
67
  */
64
- getColumnsQuery(_tableName) {
65
- return /*sql*/ `
68
+ async getColumns(read, tableName) {
69
+ const rows = await read(
70
+ /*sql*/ `
66
71
  SELECT
67
72
  c.column_name,
68
73
  c.data_type,
@@ -77,20 +82,6 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
77
82
  CASE WHEN a.attgenerated = 's' THEN c.generation_expression END AS generated_as,
78
83
  CASE WHEN (c.data_type IN ('USER-DEFINED', 'vector') OR c.data_type LIKE 'timestamp%') AND a.atttypmod > -1
79
84
  THEN format_type(a.atttypid, a.atttypmod) END AS formatted_type,
80
- EXISTS (
81
- SELECT 1 FROM information_schema.table_constraints tc
82
- JOIN information_schema.key_column_usage kcu USING (constraint_schema, constraint_name)
83
- WHERE tc.table_schema = c.table_schema
84
- AND tc.table_name = c.table_name
85
- AND tc.constraint_type = 'PRIMARY KEY'
86
- AND kcu.column_name = c.column_name
87
- ) AS is_primary_key,
88
- EXISTS (
89
- SELECT 1 FROM pg_catalog.pg_index ix
90
- WHERE ix.indrelid = a.attrelid AND ix.indisunique AND NOT ix.indisprimary
91
- AND ix.indnkeyatts = 1 AND ix.indkey[0] = a.attnum
92
- AND ix.indpred IS NULL AND ix.indexprs IS NULL
93
- ) AS is_unique,
94
85
  pg_catalog.col_description(
95
86
  to_regclass(quote_ident(c.table_schema) || '.' || quote_ident(c.table_name)),
96
87
  c.ordinal_position
@@ -101,9 +92,24 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
101
92
  AND a.attname = c.column_name
102
93
  WHERE c.table_schema = ${this.schemaExpr}
103
94
  AND c.table_name = $1
95
+ AND ${this.visibleColumnSql}
104
96
  ORDER BY c.ordinal_position
105
- `;
97
+ `, [tableName]);
98
+ return rows.map((row) => ({
99
+ name: row.column_name,
100
+ type: row.formatted_type?.toUpperCase() ?? this.normalizeType(row.data_type, row.udt_name),
101
+ nullable: row.is_nullable === 'YES',
102
+ defaultValue: this.parseDefaultValue(row.column_default),
103
+ isAutoIncrement: this.isAutoIncrement(row.column_default, row.is_identity),
104
+ length: row.character_maximum_length ?? undefined,
105
+ precision: row.numeric_precision ?? undefined,
106
+ scale: row.numeric_scale ?? undefined,
107
+ comment: row.column_comment ?? undefined,
108
+ generatedAs: row.generated_as ?? undefined,
109
+ }));
106
110
  }
111
+ /** Whether `information_schema.columns c` lists a column the table shows: every one, on Postgres. */
112
+ visibleColumnSql = 'TRUE';
107
113
  /**
108
114
  * `attname` where the entry is a column, `pg_get_indexdef` for that one position where it is an
109
115
  * expression. Neither alone will do: an expression entry has `attnum = 0`, so joining `pg_attribute`
@@ -115,8 +121,9 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
115
121
  * and the MySQL family report theirs: the diff reads one over a single column as that column's
116
122
  * uniqueness, and one over several as the unique `@Index` it is.
117
123
  */
118
- getIndexesQuery(_tableName) {
119
- return /*sql*/ `
124
+ async getIndexes(read, tableName) {
125
+ const rows = await read(
126
+ /*sql*/ `
120
127
  SELECT
121
128
  i.relname AS index_name,
122
129
  ix.indisunique AS is_unique,
@@ -143,7 +150,21 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
143
150
  SELECT 1 FROM pg_constraint con WHERE con.conindid = ix.indexrelid AND con.contype = 'x'
144
151
  )
145
152
  ORDER BY i.relname, k.n
146
- `;
153
+ `, [tableName]);
154
+ // One row per index entry, ordered by position, so the rows of an index are its entries in order.
155
+ return [...Map.groupBy(rows, (row) => row.index_name)].map(([name, ofIndex]) => {
156
+ const [first] = ofIndex;
157
+ const include = ofIndex.filter((row) => !row.is_key).map((row) => row.entry);
158
+ return this.withVectorDistance({
159
+ name,
160
+ entries: ofIndex.filter((row) => row.is_key).map(mapIndexEntry),
161
+ unique: first.is_unique,
162
+ type: INDEX_TYPES.find((type) => type === first.method),
163
+ where: first.predicate ?? undefined,
164
+ include: include.length > 0 ? include : undefined,
165
+ ...fulltextIndex(ofIndex),
166
+ });
167
+ });
147
168
  }
148
169
  /** Whether an entry sorts nulls first, which Postgres states on every entry. */
149
170
  nullsFirstSql = '(ix.indoption[k.n - 1] & 2) <> 0';
@@ -152,13 +173,14 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
152
173
  /** An entry's operator class, where it is not the default for its type. */
153
174
  opsClassSql = 'CASE WHEN op.opcdefault THEN NULL ELSE op.opcname END';
154
175
  /** From `pg_constraint`, whose key arrays keep each column paired with the one it references. */
155
- getForeignKeysQuery(_tableName) {
176
+ async getForeignKeys(read, tableName) {
156
177
  const columnsOf = (keys, table) => /*sql*/ `ARRAY_TO_JSON(ARRAY(
157
178
  SELECT a.attname FROM UNNEST(${keys}) WITH ORDINALITY AS k(attnum, n)
158
179
  JOIN pg_attribute a ON a.attrelid = ${table} AND a.attnum = k.attnum
159
180
  ORDER BY k.n
160
181
  ))`;
161
- return /*sql*/ `
182
+ const rows = await read(
183
+ /*sql*/ `
162
184
  SELECT
163
185
  con.conname AS constraint_name,
164
186
  ${columnsOf('con.conkey', 'con.conrelid')} AS columns,
@@ -174,36 +196,33 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
174
196
  AND t.relname = $1
175
197
  AND n.nspname = ${this.schemaExpr}
176
198
  ORDER BY con.conname
177
- `;
199
+ `, [tableName]);
200
+ return rows.map((row) => ({
201
+ name: row.constraint_name,
202
+ columns: row.columns,
203
+ references: { table: row.referenced_table, columns: row.referenced_columns },
204
+ onDelete: FOREIGN_KEY_ACTION_CODES[row.delete_rule],
205
+ onUpdate: FOREIGN_KEY_ACTION_CODES[row.update_rule],
206
+ }));
178
207
  }
179
- getPrimaryKeyQuery(_tableName) {
180
- return /*sql*/ `
208
+ getPrimaryKey(read, tableName) {
209
+ return this.readPrimaryKey(read,
210
+ /*sql*/ `
181
211
  SELECT kcu.column_name, tc.constraint_name
182
212
  FROM information_schema.table_constraints tc
183
213
  JOIN information_schema.key_column_usage kcu
184
214
  ON tc.constraint_name = kcu.constraint_name
185
215
  AND tc.table_schema = kcu.table_schema
216
+ JOIN information_schema.columns c
217
+ ON c.table_schema = kcu.table_schema
218
+ AND c.table_name = kcu.table_name
219
+ AND c.column_name = kcu.column_name
186
220
  WHERE tc.constraint_type = 'PRIMARY KEY'
187
221
  AND tc.table_name = $1
188
222
  AND tc.table_schema = ${this.schemaExpr}
223
+ AND ${this.visibleColumnSql}
189
224
  ORDER BY kcu.ordinal_position
190
- `;
191
- }
192
- async mapColumnsResult(_read, _tableName, results) {
193
- return results.map((row) => ({
194
- name: row.column_name,
195
- type: row.formatted_type?.toUpperCase() ?? this.normalizeType(row.data_type, row.udt_name),
196
- nullable: row.is_nullable === 'YES',
197
- defaultValue: this.parseDefaultValue(row.column_default),
198
- isPrimaryKey: row.is_primary_key,
199
- isAutoIncrement: this.isAutoIncrement(row.column_default, row.is_identity),
200
- isUnique: row.is_unique,
201
- length: row.character_maximum_length ?? undefined,
202
- precision: row.numeric_precision ?? undefined,
203
- scale: row.numeric_scale ?? undefined,
204
- comment: row.column_comment ?? undefined,
205
- generatedAs: row.generated_as ?? undefined,
206
- }));
225
+ `, tableName);
207
226
  }
208
227
  /**
209
228
  * A vector index keeps its distance as its vector column's operator class, `{type}_{metric}_ops`, which
@@ -219,30 +238,6 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
219
238
  const entries = index.entries.map(({ opsClass: _opsClass, ...entry }) => entry);
220
239
  return distance ? { ...index, distance, entries } : index;
221
240
  }
222
- async mapIndexesResult(_read, _tableName, results) {
223
- // One row per index entry, ordered by position, so the rows of an index are its entries in order.
224
- return [...Map.groupBy(results, (row) => row.index_name)].map(([name, rows]) => {
225
- const include = rows.filter((row) => !row.is_key).map((row) => row.entry);
226
- return this.withVectorDistance({
227
- name,
228
- entries: rows.filter((row) => row.is_key).map(mapIndexEntry),
229
- unique: rows[0].is_unique,
230
- type: INDEX_TYPES.find((type) => type === rows[0].method),
231
- where: rows[0].predicate ?? undefined,
232
- include: include.length > 0 ? include : undefined,
233
- ...fulltextIndex(rows),
234
- });
235
- });
236
- }
237
- async mapForeignKeysResult(_read, _tableName, results) {
238
- return results.map((row) => ({
239
- name: row.constraint_name,
240
- columns: row.columns,
241
- references: { table: row.referenced_table, columns: row.referenced_columns },
242
- onDelete: FOREIGN_KEY_ACTION_CODES[row.delete_rule],
243
- onUpdate: FOREIGN_KEY_ACTION_CODES[row.update_rule],
244
- }));
245
- }
246
241
  normalizeType(dataType, udtName) {
247
242
  // Handle user-defined types and arrays
248
243
  if (dataType === 'USER-DEFINED') {
@@ -344,6 +339,8 @@ export class CockroachSchemaIntrospector extends PostgresSchemaIntrospector {
344
339
  'vector',
345
340
  'distance',
346
341
  ]);
342
+ /** Not `rowid`, the `NOT VISIBLE` key it gives a table declared without one, which no entity can name. */
343
+ visibleColumnSql = "c.is_hidden = 'NO'";
347
344
  /** None: it rejects a stated nulls order, so reading one back gives an index it would refuse to rebuild. */
348
345
  nullsFirstSql = 'NULL::BOOL';
349
346
  /**
@@ -1,43 +1,37 @@
1
1
  import type { IndexFacet } from '../../schema/indexDifferences.js';
2
- import type { ColumnSchema, ForeignKeySchema, IndexSchema, StoredDefinition } from '../../type/index.js';
3
- import { AbstractSqlSchemaIntrospector, type TableRowReader } from './abstractSqlSchemaIntrospector.js';
2
+ import type { CheckSchema } from '../../schema/types.js';
3
+ import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema, StoredDefinition } from '../../type/index.js';
4
+ import { AbstractSqlSchemaIntrospector, type ReadColumn, type TableRowReader } from './abstractSqlSchemaIntrospector.js';
4
5
  /**
5
6
  * SQLite schema introspector
6
7
  */
7
8
  export declare class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
8
9
  /** Whether an index is libSQL's vector index, where the engine has one; elsewhere a declared one is built plain. */
9
- readonly indexFacets: ReadonlySet<IndexFacet>;
10
+ protected readonly indexFacets: ReadonlySet<IndexFacet>;
10
11
  protected triggersQuery(): string;
11
12
  /** User tables only: skips SQLite's own and libSQL's vector index tables (its metadata and `<index>_shadow`). */
12
13
  protected getTableNamesQuery(): string;
13
14
  protected tableExistsQuery(): string;
14
- protected parseTableExistsResult([row]: SqliteCountRow[]): boolean;
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
18
- * SQLite cannot do to an existing table anyway. PRAGMA takes no bound parameters, hence the splice.
18
+ * SQLite cannot do to an existing table anyway. Also the key, by each column's `pk`: one statement for both.
19
19
  */
20
- protected getColumnsQuery(tableName: string): string;
21
- protected getIndexesQuery(tableName: string): string;
22
- protected getForeignKeysQuery(tableName: string): string;
23
- protected getPrimaryKeyQuery(tableName: string): string;
24
- protected getColumnsParams(_tableName: string): unknown[];
25
- protected getIndexesParams(_tableName: string): unknown[];
26
- protected getForeignKeysParams(_tableName: string): unknown[];
27
- protected getPrimaryKeyParams(_tableName: string): unknown[];
28
- /** `sqlite_master`, not `information_schema`, so the column is `name`. */
29
- protected mapTableNameRow(row: {
30
- name: string;
31
- }): string;
32
- protected mapColumnsResult(read: TableRowReader, tableName: string, results: SqliteColumnRow[]): Promise<ColumnSchema[]>;
33
- protected mapIndexesResult(read: TableRowReader, _tableName: string, results: SqliteIndexRow[]): Promise<IndexSchema[]>;
34
- protected mapForeignKeysResult(_read: TableRowReader, _tableName: string, results: SqliteForeignKeyRow[]): Promise<ForeignKeySchema[]>;
20
+ private tableInfo;
21
+ protected getColumns(read: TableRowReader, tableName: string): Promise<ReadColumn[]>;
22
+ protected getPrimaryKey(read: TableRowReader, tableName: string): Promise<PrimaryKeySchema | undefined>;
23
+ protected getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
24
+ /**
25
+ * A row per column of each key, grouped by its id. Unnamed: `PRAGMA foreign_key_list` reports none, so the
26
+ * AST derives one from the columns, as the entity side does.
27
+ */
28
+ protected getForeignKeys(read: TableRowReader, tableName: string): Promise<ForeignKeySchema[]>;
29
+ /** The named checks in the table's `CREATE TABLE`, the only place SQLite keeps one. */
30
+ protected getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
35
31
  /** Every statement `sqlite_master` keeps for the table, its `CREATE TABLE` first. An automatic index has none. */
36
32
  protected getDefinition(read: TableRowReader, tableName: string): Promise<StoredDefinition[]>;
37
- protected mapPrimaryKeyResult(results: SqliteColumnRow[]): string[] | undefined;
38
- private getUniqueColumns;
39
33
  /** libSQL's `libsql_vector_idx(col, 'metric=...')`, read back from the statement that created it. */
40
- private getVectorIndex;
34
+ private vectorIndex;
41
35
  private getIndexColumns;
42
36
  protected normalizeType(type: string): string;
43
37
  protected extractLength(type: string): number | undefined;
@@ -48,33 +42,5 @@ export declare class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospec
48
42
  * reports one, and SQLite keeps the statement's text exactly as it was given.
49
43
  */
50
44
  export declare function generatedExpression(ddl: string, column: string): string | undefined;
51
- type SqliteCountRow = {
52
- count: number | bigint;
53
- };
54
- type SqliteColumnRow = {
55
- name: string;
56
- type: string;
57
- notnull: number;
58
- dflt_value: string | null;
59
- pk: number;
60
- /** `PRAGMA table_xinfo`'s flag: 0 ordinary, 1 a hidden `VIRTUAL` table column, 2 virtual, 3 stored. Absent from `table_info`. */
61
- hidden?: number;
62
- };
63
- type SqliteIndexRow = {
64
- seq: number;
65
- name: string;
66
- unique: number;
67
- origin: string;
68
- partial: number;
69
- };
70
- type SqliteForeignKeyRow = {
71
- id: number;
72
- seq: number;
73
- table: string;
74
- from: string;
75
- to: string;
76
- on_update: string;
77
- on_delete: string;
78
- match: string;
79
- };
80
- export {};
45
+ /** Each `CONSTRAINT <name> CHECK (...)` in a `CREATE TABLE`, the only place SQLite keeps one. */
46
+ export declare function namedChecks(ddl: string): CheckSchema[];
@@ -1,4 +1,4 @@
1
- import { AbstractSqlSchemaIntrospector } from './abstractSqlSchemaIntrospector.js';
1
+ import { AbstractSqlSchemaIntrospector, } from './abstractSqlSchemaIntrospector.js';
2
2
  /**
3
3
  * SQLite schema introspector
4
4
  */
@@ -11,7 +11,7 @@ export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
11
11
  /** User tables only: skips SQLite's own and libSQL's vector index tables (its metadata and `<index>_shadow`). */
12
12
  getTableNamesQuery() {
13
13
  return /*sql*/ `
14
- SELECT name
14
+ SELECT name AS table_name
15
15
  FROM sqlite_master
16
16
  WHERE type = 'table'
17
17
  AND name NOT LIKE 'sqlite_%'
@@ -21,160 +21,93 @@ export class SqliteSchemaIntrospector extends AbstractSqlSchemaIntrospector {
21
21
  `;
22
22
  }
23
23
  tableExistsQuery() {
24
- return /*sql*/ `
25
- SELECT COUNT(*) as count
26
- FROM sqlite_master
27
- WHERE type = 'table'
28
- AND name = ?
29
- `;
30
- }
31
- parseTableExistsResult([row]) {
32
- return Number(row.count) > 0;
24
+ return /*sql*/ `SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?`;
33
25
  }
34
26
  /**
35
27
  * `table_xinfo`, not `table_info`: the latter omits generated columns entirely, so a table carrying
36
28
  * one read back without it and every sync offered to add a column that was already there - which
37
- * SQLite cannot do to an existing table anyway. PRAGMA takes no bound parameters, hence the splice.
29
+ * SQLite cannot do to an existing table anyway. Also the key, by each column's `pk`: one statement for both.
38
30
  */
39
- getColumnsQuery(tableName) {
40
- return /*sql*/ `PRAGMA table_xinfo(${this.dialect.escapeId(tableName)})`;
41
- }
42
- getIndexesQuery(tableName) {
43
- return /*sql*/ `PRAGMA index_list(${this.dialect.escapeId(tableName)})`;
44
- }
45
- getForeignKeysQuery(tableName) {
46
- return /*sql*/ `PRAGMA foreign_key_list(${this.dialect.escapeId(tableName)})`;
47
- }
48
- getPrimaryKeyQuery(tableName) {
49
- return /*sql*/ `PRAGMA table_info(${this.dialect.escapeId(tableName)})`;
31
+ tableInfo(read, tableName) {
32
+ return read(/*sql*/ `PRAGMA table_xinfo(${this.dialect.escapeId(tableName)})`);
50
33
  }
51
- getColumnsParams(_tableName) {
52
- return [];
53
- }
54
- getIndexesParams(_tableName) {
55
- return [];
56
- }
57
- getForeignKeysParams(_tableName) {
58
- return [];
59
- }
60
- getPrimaryKeyParams(_tableName) {
61
- return [];
62
- }
63
- /** `sqlite_master`, not `information_schema`, so the column is `name`. */
64
- mapTableNameRow(row) {
65
- return row.name;
66
- }
67
- async mapColumnsResult(read, tableName, results) {
68
- const uniqueColumns = await this.getUniqueColumns(read, tableName);
34
+ async getColumns(read, tableName) {
35
+ const rows = await this.tableInfo(read, tableName);
69
36
  // Only a sole `INTEGER PRIMARY KEY` is the rowid, which is what numbers itself.
70
- const soleKey = results.filter((row) => row.pk > 0).length === 1;
71
- const [table] = results.some((row) => row.hidden === STORED_GENERATED)
37
+ const soleKey = rows.filter((row) => row.pk > 0).length === 1;
38
+ const [table] = rows.some((row) => row.hidden === STORED_GENERATED)
72
39
  ? await this.getDefinition(read, tableName)
73
40
  : [];
74
41
  const ddl = table?.sql ?? '';
75
- return results.map((row) => ({
42
+ return rows.map((row) => ({
76
43
  name: row.name,
77
44
  type: this.normalizeType(row.type),
78
45
  nullable: row.notnull === 0,
79
46
  defaultValue: this.parseDefaultValue(row.dflt_value),
80
- isPrimaryKey: row.pk > 0,
81
47
  isAutoIncrement: soleKey && row.pk > 0 && row.type.toUpperCase() === 'INTEGER',
82
- isUnique: uniqueColumns.has(row.name),
83
48
  length: this.extractLength(row.type),
84
- precision: undefined,
85
- scale: undefined,
86
- comment: undefined, // SQLite doesn't support column comments
87
49
  generatedAs: row.hidden === STORED_GENERATED ? generatedExpression(ddl, row.name) : undefined,
88
50
  }));
89
51
  }
90
- async mapIndexesResult(read, _tableName, results) {
91
- const indexSchemas = [];
92
- for (const index of results) {
93
- const columns = await this.getIndexColumns(read, index.name);
94
- // A unique constraint's index ('u') is reported as every engine reports it, and only the key's ('pk') left out.
95
- // `PRAGMA index_info` names an expression entry `null` (its `cid` is -2), and the expression text
96
- // lives only in `sqlite_master.sql`. Reporting `{ column: null }` put a column literally named
97
- // `null` into the diff, so an index UQL cannot describe is left out, libSQL's vector index aside.
98
- const named = columns.filter((column) => column.name !== null);
99
- if (index.origin === 'pk') {
100
- continue;
101
- }
102
- if (named.length === columns.length) {
103
- indexSchemas.push({
104
- name: index.name,
105
- entries: named.map((column) => ({ column: column.name })),
106
- unique: Boolean(index.unique),
107
- });
108
- }
109
- else {
110
- const vectorIndex = await this.getVectorIndex(read, index.name);
111
- if (vectorIndex) {
112
- indexSchemas.push(vectorIndex);
113
- }
114
- }
52
+ async getPrimaryKey(read, tableName) {
53
+ const key = (await this.tableInfo(read, tableName)).filter((row) => row.pk > 0).sort((a, b) => a.pk - b.pk);
54
+ return key.length ? { columns: key.map((row) => row.name) } : undefined;
55
+ }
56
+ async getIndexes(read, tableName) {
57
+ const list = await read(/*sql*/ `PRAGMA index_list(${this.dialect.escapeId(tableName)})`);
58
+ const statements = new Map((await this.getDefinition(read, tableName)).map(({ name, sql }) => [name, sql]));
59
+ const indexes = [];
60
+ // The key's own index ('pk') is left out; a unique constraint's ('u') is reported as every engine reports one.
61
+ for (const index of list.filter((it) => it.origin !== 'pk')) {
62
+ const sql = statements.get(index.name) ?? '';
63
+ // `PRAGMA index_info` names an expression entry `null`, its text kept only in `sqlite_master.sql`: it is
64
+ // reported as the expression it is, as MySQL reports one, and libSQL's vector index is read off that text.
65
+ const entries = (await this.getIndexColumns(read, index.name)).map(({ name }) => name === null ? { column: '', expression: true } : { column: name });
66
+ indexes.push(this.vectorIndex(index.name, sql) ?? {
67
+ name: index.name,
68
+ entries,
69
+ unique: Boolean(index.unique),
70
+ where: index.partial ? indexPredicate(sql) : undefined,
71
+ });
115
72
  }
116
- return indexSchemas;
73
+ return indexes;
117
74
  }
118
- async mapForeignKeysResult(_read, _tableName, results) {
119
- // Group by id to handle composite foreign keys
120
- const grouped = new Map();
121
- for (const row of results) {
122
- const id = row.id;
123
- const existing = grouped.get(id) ?? [];
124
- existing.push(row);
125
- grouped.set(id, existing);
126
- }
127
- return Array.from(grouped.entries()).map(([, rows]) => {
128
- const first = rows[0];
129
- const columns = rows.map((r) => r.from);
130
- // Unnamed: `PRAGMA foreign_key_list` reports none, so the AST derives one from the columns, as the entity side does.
75
+ /**
76
+ * A row per column of each key, grouped by its id. Unnamed: `PRAGMA foreign_key_list` reports none, so the
77
+ * AST derives one from the columns, as the entity side does.
78
+ */
79
+ async getForeignKeys(read, tableName) {
80
+ const rows = await read(
81
+ /*sql*/ `PRAGMA foreign_key_list(${this.dialect.escapeId(tableName)})`);
82
+ return [...Map.groupBy(rows, (row) => row.id).values()].map((key) => {
83
+ const [first] = key;
131
84
  return {
132
- columns,
133
- references: { table: first.table, columns: rows.map((r) => r.to) },
85
+ columns: key.map((row) => row.from),
86
+ references: { table: first.table, columns: key.map((row) => row.to) },
134
87
  onDelete: this.normalizeReferentialAction(first.on_delete),
135
88
  onUpdate: this.normalizeReferentialAction(first.on_update),
136
89
  };
137
90
  });
138
91
  }
92
+ /** The named checks in the table's `CREATE TABLE`, the only place SQLite keeps one. */
93
+ async getChecks(read, tableName) {
94
+ const [table] = await this.getDefinition(read, tableName);
95
+ return table ? namedChecks(table.sql) : [];
96
+ }
139
97
  /** Every statement `sqlite_master` keeps for the table, its `CREATE TABLE` first. An automatic index has none. */
140
98
  getDefinition(read, tableName) {
141
99
  return read(
142
100
  /*sql*/ `SELECT type AS kind, name, sql FROM sqlite_master WHERE tbl_name = ? AND sql IS NOT NULL ORDER BY type <> 'table'`, [tableName]);
143
101
  }
144
- mapPrimaryKeyResult(results) {
145
- const pkColumns = results.filter((r) => r.pk > 0).sort((a, b) => a.pk - b.pk);
146
- if (pkColumns.length === 0) {
147
- return undefined;
148
- }
149
- return pkColumns.map((r) => r.name);
150
- }
151
- async getUniqueColumns(read, tableName) {
152
- const indexes = await read(this.getIndexesQuery(tableName));
153
- const uniqueColumns = new Set();
154
- // The key's own index is left out: a key column is unique already, which the entity side never states.
155
- for (const index of indexes) {
156
- if (index.unique && index.origin !== 'pk') {
157
- const columns = await this.getIndexColumns(read, index.name);
158
- // Only single-column unique constraints, and only over a real column (not an expression)
159
- const [column] = columns;
160
- if (columns.length === 1 && column.name !== null) {
161
- uniqueColumns.add(column.name);
162
- }
163
- }
164
- }
165
- return uniqueColumns;
166
- }
167
102
  /** libSQL's `libsql_vector_idx(col, 'metric=...')`, read back from the statement that created it. */
168
- async getVectorIndex(read, indexName) {
169
- const [row] = await read(
170
- /*sql*/ `SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ?`, [indexName]);
171
- const column = row?.sql?.match(/libsql_vector_idx\s*\(\s*[`"[]?([^`"\],\s)]+)/i)?.[1];
103
+ vectorIndex(name, sql) {
104
+ const column = sql.match(/libsql_vector_idx\s*\(\s*[`"[]?([^`"\],\s)]+)/i)?.[1];
172
105
  if (!column) {
173
106
  return undefined;
174
107
  }
175
- const metric = row.sql?.match(/'metric=(\w+)'/i)?.[1]?.toLowerCase();
108
+ const metric = sql.match(/'metric=(\w+)'/i)?.[1]?.toLowerCase();
176
109
  return {
177
- name: indexName,
110
+ name,
178
111
  entries: [{ column }],
179
112
  unique: false,
180
113
  type: 'vector',
@@ -245,6 +178,18 @@ function tableEntries(ddl) {
245
178
  });
246
179
  return [...entries, body.slice(start)];
247
180
  }
181
+ /** A partial index's predicate: what follows `WHERE` after its column list, as the statement gave it. */
182
+ function indexPredicate(sql) {
183
+ let rest = '';
184
+ scan(sql, (char, index, depth) => {
185
+ const closes = char === ')' && depth === 0;
186
+ if (closes) {
187
+ rest = sql.slice(index + 1);
188
+ }
189
+ return closes;
190
+ });
191
+ return /^\s*WHERE\s+([\s\S]+)$/i.exec(rest)?.[1].trim();
192
+ }
248
193
  /** What a leading `(` encloses, its own nesting and quoting respected. */
249
194
  function parenthesized(text) {
250
195
  let end = text.length;
@@ -286,3 +231,12 @@ function leadingIdentifier(entry) {
286
231
  const token = entry.trimStart().replace(/^("[^"]*"|`[^`]*`|\[[^\]]*\]|[^\s(]*)[\s\S]*$/, '$1');
287
232
  return token.replace(/^["`[]|["`\]]$/g, '');
288
233
  }
234
+ /** Where a named check starts, its name quoted any way SQLite takes, up to its opening parenthesis. */
235
+ const NAMED_CHECK = /CONSTRAINT\s+("[^"]*"|`[^`]*`|\[[^\]]*\]|\w+)\s+CHECK\s*\(/gi;
236
+ /** Each `CONSTRAINT <name> CHECK (...)` in a `CREATE TABLE`, the only place SQLite keeps one. */
237
+ export function namedChecks(ddl) {
238
+ return [...ddl.matchAll(NAMED_CHECK)].map((match) => ({
239
+ name: leadingIdentifier(match[1]),
240
+ expression: parenthesized(ddl.slice(match.index + match[0].length - 1)),
241
+ }));
242
+ }
@@ -0,0 +1,13 @@
1
+ import type { IndexFacet } from '../../schema/indexDifferences.js';
2
+ import { SchemaAST } from '../../schema/schemaAST.js';
3
+ import type { ColumnRenames, DialectName, TableSchema } from '../../type/index.js';
4
+ /** Where the tables were read: the engine, the one schema read, and what its catalogue reports about an index. */
5
+ export type ReadFrom = {
6
+ readonly dialectName: DialectName;
7
+ readonly schema?: string;
8
+ readonly indexFacets: ReadonlySet<IndexFacet>;
9
+ };
10
+ /** The tables an introspector read, as a {@link SchemaAST}: every table first, so a foreign key reaches one read after it. */
11
+ export declare function tableSchemasToAST(schemas: readonly TableSchema[], from: ReadFrom): SchemaAST;
12
+ /** `table` with each column `renames` names under its new name, wherever the table names it. */
13
+ export declare function renamedTable(table: TableSchema, renames: ColumnRenames, schema?: string): TableSchema;