@anthonyalayo/drizzle-kit 1.0.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.
Files changed (161) hide show
  1. package/.gitignore +25 -0
  2. package/README.md +79 -0
  3. package/build.dev.ts +51 -0
  4. package/build.ts +112 -0
  5. package/imports-checker/analyze.ts +78 -0
  6. package/imports-checker/checker.ts +296 -0
  7. package/imports-checker/grammar/grammar.ohm +121 -0
  8. package/imports-checker/grammar/grammar.ohm-bundle.d.ts +164 -0
  9. package/imports-checker/grammar/grammar.ohm-bundle.js +753 -0
  10. package/imports-checker/index.ts +48 -0
  11. package/package.json +142 -0
  12. package/patches/difflib@0.2.4.patch +31 -0
  13. package/src/@types/utils.ts +58 -0
  14. package/src/api.ts +504 -0
  15. package/src/cli/commands/_es5.ts +2 -0
  16. package/src/cli/commands/check.ts +52 -0
  17. package/src/cli/commands/drop.ts +60 -0
  18. package/src/cli/commands/introspect.ts +772 -0
  19. package/src/cli/commands/libSqlPushUtils.ts +355 -0
  20. package/src/cli/commands/migrate.ts +1526 -0
  21. package/src/cli/commands/mysqlIntrospect.ts +53 -0
  22. package/src/cli/commands/mysqlPushUtils.ts +352 -0
  23. package/src/cli/commands/mysqlUp.ts +102 -0
  24. package/src/cli/commands/pgIntrospect.ts +55 -0
  25. package/src/cli/commands/pgPushUtils.ts +269 -0
  26. package/src/cli/commands/pgUp.ts +217 -0
  27. package/src/cli/commands/push.ts +637 -0
  28. package/src/cli/commands/singlestoreIntrospect.ts +53 -0
  29. package/src/cli/commands/singlestorePushUtils.ts +456 -0
  30. package/src/cli/commands/singlestoreUp.ts +1 -0
  31. package/src/cli/commands/sqliteIntrospect.ts +96 -0
  32. package/src/cli/commands/sqlitePushUtils.ts +322 -0
  33. package/src/cli/commands/sqliteUp.ts +52 -0
  34. package/src/cli/commands/utils.ts +858 -0
  35. package/src/cli/connections.ts +914 -0
  36. package/src/cli/index.ts +48 -0
  37. package/src/cli/schema.ts +794 -0
  38. package/src/cli/selector-ui.ts +38 -0
  39. package/src/cli/utils.ts +112 -0
  40. package/src/cli/validations/cli.ts +79 -0
  41. package/src/cli/validations/common.ts +193 -0
  42. package/src/cli/validations/libsql.ts +27 -0
  43. package/src/cli/validations/mysql.ts +61 -0
  44. package/src/cli/validations/outputs.ts +91 -0
  45. package/src/cli/validations/postgres.ts +83 -0
  46. package/src/cli/validations/singlestore.ts +61 -0
  47. package/src/cli/validations/sqlite.ts +104 -0
  48. package/src/cli/validations/studio.ts +24 -0
  49. package/src/cli/views.ts +658 -0
  50. package/src/extensions/getTablesFilterByExtensions.ts +16 -0
  51. package/src/extensions/vector.ts +10 -0
  52. package/src/global.ts +61 -0
  53. package/src/index.ts +326 -0
  54. package/src/introspect-mysql.ts +1030 -0
  55. package/src/introspect-pg.ts +1435 -0
  56. package/src/introspect-singlestore.ts +913 -0
  57. package/src/introspect-sqlite.ts +533 -0
  58. package/src/jsonDiffer.js +882 -0
  59. package/src/jsonStatements.ts +3590 -0
  60. package/src/loader.mjs +57 -0
  61. package/src/migrationPreparator.ts +217 -0
  62. package/src/schemaValidator.ts +23 -0
  63. package/src/serializer/index.ts +142 -0
  64. package/src/serializer/mysqlImports.ts +38 -0
  65. package/src/serializer/mysqlSchema.ts +423 -0
  66. package/src/serializer/mysqlSerializer.ts +999 -0
  67. package/src/serializer/pgImports.ts +106 -0
  68. package/src/serializer/pgSchema.ts +937 -0
  69. package/src/serializer/pgSerializer.ts +2223 -0
  70. package/src/serializer/singlestoreImports.ts +38 -0
  71. package/src/serializer/singlestoreSchema.ts +257 -0
  72. package/src/serializer/singlestoreSerializer.ts +767 -0
  73. package/src/serializer/sqliteImports.ts +41 -0
  74. package/src/serializer/sqliteSchema.ts +352 -0
  75. package/src/serializer/sqliteSerializer.ts +931 -0
  76. package/src/serializer/studio.ts +694 -0
  77. package/src/serializer/utils.ts +45 -0
  78. package/src/simulator.ts +157 -0
  79. package/src/snapshotsDiffer.ts +4398 -0
  80. package/src/sqlgenerator.ts +4212 -0
  81. package/src/statementCombiner.ts +597 -0
  82. package/src/utils/certs.ts +44 -0
  83. package/src/utils/words.ts +1333 -0
  84. package/src/utils.ts +370 -0
  85. package/tests/bin.test.ts +58 -0
  86. package/tests/cli/d1http.config.ts +12 -0
  87. package/tests/cli/drizzle.config.ts +9 -0
  88. package/tests/cli/durable-sqlite.config.ts +7 -0
  89. package/tests/cli/expo.config.ts +7 -0
  90. package/tests/cli/postgres.config.ts +13 -0
  91. package/tests/cli/postgres2.config.ts +17 -0
  92. package/tests/cli/schema.ts +1 -0
  93. package/tests/cli/turso.config.ts +10 -0
  94. package/tests/cli-export.test.ts +79 -0
  95. package/tests/cli-generate.test.ts +259 -0
  96. package/tests/cli-migrate.test.ts +103 -0
  97. package/tests/cli-push.test.ts +126 -0
  98. package/tests/common.ts +15 -0
  99. package/tests/indexes/common.ts +21 -0
  100. package/tests/indexes/pg.test.ts +245 -0
  101. package/tests/introspect/libsql.test.ts +35 -0
  102. package/tests/introspect/mysql.test.ts +319 -0
  103. package/tests/introspect/pg.test.ts +894 -0
  104. package/tests/introspect/singlestore.test.ts +275 -0
  105. package/tests/introspect/sqlite.test.ts +122 -0
  106. package/tests/libsql-checks.test.ts +308 -0
  107. package/tests/libsql-statements.test.ts +989 -0
  108. package/tests/libsql-views.test.ts +218 -0
  109. package/tests/migrate/libsq-schema.ts +6 -0
  110. package/tests/migrate/libsql-migrate.test.ts +58 -0
  111. package/tests/migrate/migrations/0000_little_blizzard.sql +4 -0
  112. package/tests/migrate/migrations/0001_nebulous_storm.sql +10 -0
  113. package/tests/migrate/migrations/meta/0000_snapshot.json +40 -0
  114. package/tests/migrate/migrations/meta/0001_snapshot.json +40 -0
  115. package/tests/migrate/migrations/meta/_journal.json +20 -0
  116. package/tests/mysql-checks.test.ts +291 -0
  117. package/tests/mysql-generated.test.ts +1290 -0
  118. package/tests/mysql-schemas.test.ts +156 -0
  119. package/tests/mysql-views.test.ts +553 -0
  120. package/tests/mysql.test.ts +863 -0
  121. package/tests/pg-array.test.ts +368 -0
  122. package/tests/pg-checks.test.ts +329 -0
  123. package/tests/pg-columns.test.ts +484 -0
  124. package/tests/pg-domains.test.ts +306 -0
  125. package/tests/pg-enums.test.ts +751 -0
  126. package/tests/pg-generated.test.ts +529 -0
  127. package/tests/pg-identity.test.ts +447 -0
  128. package/tests/pg-schemas.test.ts +105 -0
  129. package/tests/pg-sequences.test.ts +298 -0
  130. package/tests/pg-tables.test.ts +1021 -0
  131. package/tests/pg-views.test.ts +1929 -0
  132. package/tests/push/common.ts +58 -0
  133. package/tests/push/libsql.test.ts +1400 -0
  134. package/tests/push/mysql-push.test.ts +345 -0
  135. package/tests/push/mysql.test.ts +788 -0
  136. package/tests/push/pg.test.ts +4341 -0
  137. package/tests/push/singlestore-push.test.ts +894 -0
  138. package/tests/push/singlestore.test.ts +444 -0
  139. package/tests/push/sqlite.test.ts +1613 -0
  140. package/tests/rls/pg-policy.test.ts +1656 -0
  141. package/tests/rls/pg-role.test.ts +234 -0
  142. package/tests/schemaDiffer.ts +2852 -0
  143. package/tests/singlestore-generated.test.ts +1290 -0
  144. package/tests/singlestore-schemas.test.ts +155 -0
  145. package/tests/singlestore.test.ts +978 -0
  146. package/tests/sqlite-checks.test.ts +308 -0
  147. package/tests/sqlite-columns.test.ts +1049 -0
  148. package/tests/sqlite-generated.test.ts +1726 -0
  149. package/tests/sqlite-tables.test.ts +637 -0
  150. package/tests/sqlite-views.test.ts +218 -0
  151. package/tests/statements-combiner/libsql-statements-combiner.test.ts +1812 -0
  152. package/tests/statements-combiner/singlestore-statements-combiner.test.ts +882 -0
  153. package/tests/statements-combiner/sqlite-statements-combiner.test.ts +1211 -0
  154. package/tests/test/sqlite.test.ts +39 -0
  155. package/tests/testsinglestore.ts +29 -0
  156. package/tests/validations.test.ts +869 -0
  157. package/tests/wrap-param.test.ts +16 -0
  158. package/tsconfig.build.json +4 -0
  159. package/tsconfig.cli-types.json +9 -0
  160. package/tsconfig.json +28 -0
  161. package/vitest.config.ts +34 -0
@@ -0,0 +1,931 @@
1
+ import chalk from 'chalk';
2
+ import { getTableName, is, SQL } from 'drizzle-orm';
3
+ import {
4
+ AnySQLiteTable,
5
+ getTableConfig,
6
+ getViewConfig,
7
+ SQLiteBaseInteger,
8
+ SQLiteColumn,
9
+ SQLiteSyncDialect,
10
+ SQLiteView,
11
+ uniqueKeyName,
12
+ } from 'drizzle-orm/sqlite-core';
13
+ import { CasingType } from 'src/cli/validations/common';
14
+ import { withStyle } from '../cli/validations/outputs';
15
+ import type { IntrospectStage, IntrospectStatus } from '../cli/views';
16
+ import type {
17
+ CheckConstraint,
18
+ Column,
19
+ ForeignKey,
20
+ Index,
21
+ PrimaryKey,
22
+ SQLiteKitInternals,
23
+ SQLiteSchemaInternal,
24
+ Table,
25
+ UniqueConstraint,
26
+ View,
27
+ } from '../serializer/sqliteSchema';
28
+ import { escapeSingleQuotes, type SQLiteDB } from '../utils';
29
+ import { getColumnCasing, sqlToStr } from './utils';
30
+
31
+ export const generateSqliteSnapshot = (
32
+ tables: AnySQLiteTable[],
33
+ views: SQLiteView[],
34
+ casing: CasingType | undefined,
35
+ ): SQLiteSchemaInternal => {
36
+ const dialect = new SQLiteSyncDialect({ casing });
37
+ const result: Record<string, Table> = {};
38
+ const resultViews: Record<string, View> = {};
39
+
40
+ const internal: SQLiteKitInternals = { indexes: {} };
41
+ for (const table of tables) {
42
+ // const tableName = getTableName(table);
43
+ const columnsObject: Record<string, Column> = {};
44
+ const indexesObject: Record<string, Index> = {};
45
+ const foreignKeysObject: Record<string, ForeignKey> = {};
46
+ const primaryKeysObject: Record<string, PrimaryKey> = {};
47
+ const uniqueConstraintObject: Record<string, UniqueConstraint> = {};
48
+ const checkConstraintObject: Record<string, CheckConstraint> = {};
49
+
50
+ const checksInTable: Record<string, string[]> = {};
51
+
52
+ const {
53
+ name: tableName,
54
+ columns,
55
+ indexes,
56
+ checks,
57
+ foreignKeys: tableForeignKeys,
58
+ primaryKeys,
59
+ uniqueConstraints,
60
+ } = getTableConfig(table);
61
+
62
+ columns.forEach((column) => {
63
+ const name = getColumnCasing(column, casing);
64
+ const notNull: boolean = column.notNull;
65
+ const primaryKey: boolean = column.primary;
66
+ const generated = column.generated;
67
+
68
+ const columnToSet: Column = {
69
+ name,
70
+ type: column.getSQLType(),
71
+ primaryKey,
72
+ notNull,
73
+ autoincrement: is(column, SQLiteBaseInteger)
74
+ ? column.autoIncrement
75
+ : false,
76
+ generated: generated
77
+ ? {
78
+ as: is(generated.as, SQL)
79
+ ? `(${dialect.sqlToQuery(generated.as as SQL, 'indexes').sql})`
80
+ : typeof generated.as === 'function'
81
+ ? `(${dialect.sqlToQuery(generated.as() as SQL, 'indexes').sql})`
82
+ : `(${generated.as as any})`,
83
+ type: generated.mode ?? 'virtual',
84
+ }
85
+ : undefined,
86
+ };
87
+
88
+ if (column.default !== undefined) {
89
+ if (is(column.default, SQL)) {
90
+ columnToSet.default = sqlToStr(column.default, casing);
91
+ } else {
92
+ columnToSet.default = typeof column.default === 'string'
93
+ ? `'${escapeSingleQuotes(column.default)}'`
94
+ : typeof column.default === 'object'
95
+ || Array.isArray(column.default)
96
+ ? `'${JSON.stringify(column.default)}'`
97
+ : column.default;
98
+ }
99
+ }
100
+ columnsObject[name] = columnToSet;
101
+
102
+ if (column.isUnique) {
103
+ const existingUnique = indexesObject[column.uniqueName!];
104
+ if (typeof existingUnique !== 'undefined') {
105
+ console.log(
106
+ `\n${
107
+ withStyle.errorWarning(`We\'ve found duplicated unique constraint names in ${
108
+ chalk.underline.blue(
109
+ tableName,
110
+ )
111
+ } table.
112
+ The unique constraint ${
113
+ chalk.underline.blue(
114
+ column.uniqueName,
115
+ )
116
+ } on the ${
117
+ chalk.underline.blue(
118
+ name,
119
+ )
120
+ } column is confilcting with a unique constraint name already defined for ${
121
+ chalk.underline.blue(
122
+ existingUnique.columns.join(','),
123
+ )
124
+ } columns\n`)
125
+ }`,
126
+ );
127
+ process.exit(1);
128
+ }
129
+ indexesObject[column.uniqueName!] = {
130
+ name: column.uniqueName!,
131
+ columns: [columnToSet.name],
132
+ isUnique: true,
133
+ };
134
+ }
135
+ });
136
+
137
+ const foreignKeys: ForeignKey[] = tableForeignKeys.map((fk) => {
138
+ const tableFrom = tableName;
139
+ const onDelete = fk.onDelete ?? 'no action';
140
+ const onUpdate = fk.onUpdate ?? 'no action';
141
+ const reference = fk.reference();
142
+
143
+ const referenceFT = reference.foreignTable;
144
+
145
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
146
+ const tableTo = getTableName(referenceFT);
147
+
148
+ const originalColumnsFrom = reference.columns.map((it) => it.name);
149
+ const columnsFrom = reference.columns.map((it) => getColumnCasing(it, casing));
150
+ const originalColumnsTo = reference.foreignColumns.map((it) => it.name);
151
+ const columnsTo = reference.foreignColumns.map((it) => getColumnCasing(it, casing));
152
+
153
+ let name = fk.getName();
154
+ if (casing !== undefined) {
155
+ for (let i = 0; i < originalColumnsFrom.length; i++) {
156
+ name = name.replace(originalColumnsFrom[i], columnsFrom[i]);
157
+ }
158
+ for (let i = 0; i < originalColumnsTo.length; i++) {
159
+ name = name.replace(originalColumnsTo[i], columnsTo[i]);
160
+ }
161
+ }
162
+
163
+ return {
164
+ name,
165
+ tableFrom,
166
+ tableTo,
167
+ columnsFrom,
168
+ columnsTo,
169
+ onDelete,
170
+ onUpdate,
171
+ } as ForeignKey;
172
+ });
173
+
174
+ foreignKeys.forEach((it) => {
175
+ foreignKeysObject[it.name] = it;
176
+ });
177
+
178
+ indexes.forEach((value) => {
179
+ const columns = value.config.columns;
180
+ const name = value.config.name;
181
+
182
+ let indexColumns = columns.map((it) => {
183
+ if (is(it, SQL)) {
184
+ const sql = dialect.sqlToQuery(it, 'indexes').sql;
185
+ if (typeof internal!.indexes![name] === 'undefined') {
186
+ internal!.indexes![name] = {
187
+ columns: {
188
+ [sql]: {
189
+ isExpression: true,
190
+ },
191
+ },
192
+ };
193
+ } else {
194
+ if (typeof internal!.indexes![name]?.columns[sql] === 'undefined') {
195
+ internal!.indexes![name]!.columns[sql] = {
196
+ isExpression: true,
197
+ };
198
+ } else {
199
+ internal!.indexes![name]!.columns[sql]!.isExpression = true;
200
+ }
201
+ }
202
+ return sql;
203
+ } else {
204
+ return getColumnCasing(it, casing);
205
+ }
206
+ });
207
+
208
+ let where: string | undefined = undefined;
209
+ if (value.config.where !== undefined) {
210
+ if (is(value.config.where, SQL)) {
211
+ where = dialect.sqlToQuery(value.config.where).sql;
212
+ }
213
+ }
214
+
215
+ indexesObject[name] = {
216
+ name,
217
+ columns: indexColumns,
218
+ isUnique: value.config.unique ?? false,
219
+ where,
220
+ };
221
+ });
222
+
223
+ uniqueConstraints?.map((unq) => {
224
+ const columnNames = unq.columns.map((c) => getColumnCasing(c, casing));
225
+
226
+ const name = unq.name ?? uniqueKeyName(table, columnNames);
227
+
228
+ const existingUnique = indexesObject[name];
229
+ if (typeof existingUnique !== 'undefined') {
230
+ console.log(
231
+ `\n${
232
+ withStyle.errorWarning(
233
+ `We\'ve found duplicated unique constraint names in ${
234
+ chalk.underline.blue(
235
+ tableName,
236
+ )
237
+ } table. \nThe unique constraint ${
238
+ chalk.underline.blue(
239
+ name,
240
+ )
241
+ } on the ${
242
+ chalk.underline.blue(
243
+ columnNames.join(','),
244
+ )
245
+ } columns is confilcting with a unique constraint name already defined for ${
246
+ chalk.underline.blue(
247
+ existingUnique.columns.join(','),
248
+ )
249
+ } columns\n`,
250
+ )
251
+ }`,
252
+ );
253
+ process.exit(1);
254
+ }
255
+
256
+ indexesObject[name] = {
257
+ name: unq.name!,
258
+ columns: columnNames,
259
+ isUnique: true,
260
+ };
261
+ });
262
+
263
+ primaryKeys.forEach((it) => {
264
+ if (it.columns.length > 1) {
265
+ const originalColumnNames = it.columns.map((c) => c.name);
266
+ const columnNames = it.columns.map((c) => getColumnCasing(c, casing));
267
+
268
+ let name = it.getName();
269
+ if (casing !== undefined) {
270
+ for (let i = 0; i < originalColumnNames.length; i++) {
271
+ name = name.replace(originalColumnNames[i], columnNames[i]);
272
+ }
273
+ }
274
+
275
+ primaryKeysObject[name] = {
276
+ columns: columnNames,
277
+ name,
278
+ };
279
+ } else {
280
+ columnsObject[getColumnCasing(it.columns[0], casing)].primaryKey = true;
281
+ }
282
+ });
283
+
284
+ checks.forEach((check) => {
285
+ const checkName = check.name;
286
+ if (typeof checksInTable[tableName] !== 'undefined') {
287
+ if (checksInTable[tableName].includes(check.name)) {
288
+ console.log(
289
+ `\n${
290
+ withStyle.errorWarning(
291
+ `We\'ve found duplicated check constraint name in ${
292
+ chalk.underline.blue(
293
+ tableName,
294
+ )
295
+ }. Please rename your check constraint in the ${
296
+ chalk.underline.blue(
297
+ tableName,
298
+ )
299
+ } table`,
300
+ )
301
+ }`,
302
+ );
303
+ process.exit(1);
304
+ }
305
+ checksInTable[tableName].push(checkName);
306
+ } else {
307
+ checksInTable[tableName] = [check.name];
308
+ }
309
+
310
+ checkConstraintObject[checkName] = {
311
+ name: checkName,
312
+ value: dialect.sqlToQuery(check.value).sql,
313
+ };
314
+ });
315
+
316
+ result[tableName] = {
317
+ name: tableName,
318
+ columns: columnsObject,
319
+ indexes: indexesObject,
320
+ foreignKeys: foreignKeysObject,
321
+ compositePrimaryKeys: primaryKeysObject,
322
+ uniqueConstraints: uniqueConstraintObject,
323
+ checkConstraints: checkConstraintObject,
324
+ };
325
+ }
326
+
327
+ for (const view of views) {
328
+ const { name, isExisting, selectedFields, query, schema } = getViewConfig(view);
329
+
330
+ const columnsObject: Record<string, Column> = {};
331
+
332
+ const existingView = resultViews[name];
333
+ if (typeof existingView !== 'undefined') {
334
+ console.log(
335
+ `\n${
336
+ withStyle.errorWarning(
337
+ `We\'ve found duplicated view name across ${
338
+ chalk.underline.blue(
339
+ schema ?? 'public',
340
+ )
341
+ } schema. Please rename your view`,
342
+ )
343
+ }`,
344
+ );
345
+ process.exit(1);
346
+ }
347
+
348
+ for (const key in selectedFields) {
349
+ if (is(selectedFields[key], SQLiteColumn)) {
350
+ const column = selectedFields[key];
351
+ const notNull: boolean = column.notNull;
352
+ const primaryKey: boolean = column.primary;
353
+ const generated = column.generated;
354
+
355
+ const columnToSet: Column = {
356
+ name: column.name,
357
+ type: column.getSQLType(),
358
+ primaryKey,
359
+ notNull,
360
+ autoincrement: is(column, SQLiteBaseInteger)
361
+ ? column.autoIncrement
362
+ : false,
363
+ generated: generated
364
+ ? {
365
+ as: is(generated.as, SQL)
366
+ ? `(${dialect.sqlToQuery(generated.as as SQL, 'indexes').sql})`
367
+ : typeof generated.as === 'function'
368
+ ? `(${dialect.sqlToQuery(generated.as() as SQL, 'indexes').sql})`
369
+ : `(${generated.as as any})`,
370
+ type: generated.mode ?? 'virtual',
371
+ }
372
+ : undefined,
373
+ };
374
+
375
+ if (column.default !== undefined) {
376
+ if (is(column.default, SQL)) {
377
+ columnToSet.default = sqlToStr(column.default, casing);
378
+ } else {
379
+ columnToSet.default = typeof column.default === 'string'
380
+ ? `'${column.default}'`
381
+ : typeof column.default === 'object'
382
+ || Array.isArray(column.default)
383
+ ? `'${JSON.stringify(column.default)}'`
384
+ : column.default;
385
+ }
386
+ }
387
+ columnsObject[column.name] = columnToSet;
388
+ }
389
+ }
390
+
391
+ resultViews[name] = {
392
+ columns: columnsObject,
393
+ name,
394
+ isExisting,
395
+ definition: isExisting ? undefined : dialect.sqlToQuery(query!).sql,
396
+ };
397
+ }
398
+
399
+ return {
400
+ version: '6',
401
+ dialect: 'sqlite',
402
+ tables: result,
403
+ views: resultViews,
404
+ enums: {},
405
+ _meta: {
406
+ tables: {},
407
+ columns: {},
408
+ },
409
+ internal,
410
+ };
411
+ };
412
+
413
+ function mapSqlToSqliteType(sqlType: string): string {
414
+ const lowered = sqlType.toLowerCase();
415
+ if (
416
+ [
417
+ 'int',
418
+ 'integer',
419
+ 'integer auto_increment',
420
+ 'tinyint',
421
+ 'smallint',
422
+ 'mediumint',
423
+ 'bigint',
424
+ 'unsigned big int',
425
+ 'int2',
426
+ 'int8',
427
+ ].some((it) => lowered.startsWith(it))
428
+ ) {
429
+ return 'integer';
430
+ } else if (
431
+ [
432
+ 'character',
433
+ 'varchar',
434
+ 'varying character',
435
+ 'national varying character',
436
+ 'nchar',
437
+ 'native character',
438
+ 'nvarchar',
439
+ 'text',
440
+ 'clob',
441
+ ].some((it) => lowered.startsWith(it))
442
+ ) {
443
+ const match = lowered.match(/\d+/);
444
+
445
+ if (match) {
446
+ return `text(${match[0]})`;
447
+ }
448
+
449
+ return 'text';
450
+ } else if (lowered.startsWith('blob')) {
451
+ return 'blob';
452
+ } else if (
453
+ ['real', 'double', 'double precision', 'float'].some((it) => lowered.startsWith(it))
454
+ ) {
455
+ return 'real';
456
+ } else {
457
+ return 'numeric';
458
+ }
459
+ }
460
+
461
+ interface ColumnInfo {
462
+ columnName: string;
463
+ expression: string;
464
+ type: 'stored' | 'virtual';
465
+ }
466
+
467
+ function extractGeneratedColumns(input: string): Record<string, ColumnInfo> {
468
+ const columns: Record<string, ColumnInfo> = {};
469
+ const lines = input.split(/,\s*(?![^()]*\))/); // Split by commas outside parentheses
470
+
471
+ for (const line of lines) {
472
+ if (line.includes('GENERATED ALWAYS AS')) {
473
+ const parts = line.trim().split(/\s+/);
474
+ const columnName = parts[0].replace(/[`'"]/g, ''); // Remove quotes around the column name
475
+ const expression = line
476
+ .substring(line.indexOf('('), line.indexOf(')') + 1)
477
+ .trim();
478
+
479
+ // Extract type ensuring to remove any trailing characters like ')'
480
+ const typeIndex = parts.findIndex((part) => part.match(/(stored|virtual)/i));
481
+ let type: ColumnInfo['type'] = 'virtual';
482
+ if (typeIndex !== -1) {
483
+ type = parts[typeIndex]
484
+ .replace(/[^a-z]/gi, '')
485
+ .toLowerCase() as ColumnInfo['type'];
486
+ }
487
+
488
+ columns[columnName] = {
489
+ columnName: columnName,
490
+ expression: expression,
491
+ type,
492
+ };
493
+ }
494
+ }
495
+ return columns;
496
+ }
497
+
498
+ export const fromDatabase = async (
499
+ db: SQLiteDB,
500
+ tablesFilter: (table: string) => boolean = (table) => true,
501
+ progressCallback?: (
502
+ stage: IntrospectStage,
503
+ count: number,
504
+ status: IntrospectStatus,
505
+ ) => void,
506
+ ): Promise<SQLiteSchemaInternal> => {
507
+ const result: Record<string, Table> = {};
508
+ const resultViews: Record<string, View> = {};
509
+
510
+ const columns = await db.query<{
511
+ tableName: string;
512
+ columnName: string;
513
+ columnType: string;
514
+ notNull: number;
515
+ defaultValue: string;
516
+ pk: number;
517
+ seq: number;
518
+ hidden: number;
519
+ sql: string;
520
+ type: 'view' | 'table';
521
+ }>(
522
+ `SELECT
523
+ m.name as "tableName", p.name as "columnName", p.type as "columnType", p."notnull" as "notNull", p.dflt_value as "defaultValue", p.pk as pk, p.hidden as hidden, m.sql, m.type as type
524
+ FROM sqlite_master AS m JOIN pragma_table_xinfo(m.name) AS p
525
+ WHERE (m.type = 'table' OR m.type = 'view')
526
+ and m.tbl_name != 'sqlite_sequence'
527
+ and m.tbl_name != 'sqlite_stat1'
528
+ and m.tbl_name != '_litestream_seq'
529
+ and m.tbl_name != '_litestream_lock'
530
+ and m.tbl_name != 'libsql_wasm_func_table'
531
+ and m.tbl_name != '__drizzle_migrations'
532
+ and m.tbl_name != '_cf_KV';
533
+ `,
534
+ );
535
+
536
+ const tablesWithSeq: string[] = [];
537
+
538
+ const seq = await db.query<{
539
+ name: string;
540
+ }>(
541
+ `SELECT * FROM sqlite_master WHERE name != 'sqlite_sequence'
542
+ and name != 'sqlite_stat1'
543
+ and name != '_litestream_seq'
544
+ and name != '_litestream_lock'
545
+ and tbl_name != '_cf_KV'
546
+ and sql GLOB '*[ *' || CHAR(9) || CHAR(10) || CHAR(13) || ']AUTOINCREMENT[^'']*';`,
547
+ );
548
+
549
+ for (const s of seq) {
550
+ tablesWithSeq.push(s.name);
551
+ }
552
+
553
+ let columnsCount = 0;
554
+ let tablesCount = new Set();
555
+ let indexesCount = 0;
556
+ let foreignKeysCount = 0;
557
+ let checksCount = 0;
558
+ let viewsCount = 0;
559
+
560
+ // append primaryKeys by table
561
+ const tableToPk: { [tname: string]: string[] } = {};
562
+
563
+ let tableToGeneratedColumnsInfo: Record<
564
+ string,
565
+ Record<string, ColumnInfo>
566
+ > = {};
567
+
568
+ for (const column of columns) {
569
+ if (!tablesFilter(column.tableName)) continue;
570
+
571
+ // TODO
572
+ if (column.type !== 'view') {
573
+ columnsCount += 1;
574
+ }
575
+ if (progressCallback) {
576
+ progressCallback('columns', columnsCount, 'fetching');
577
+ }
578
+ const tableName = column.tableName;
579
+
580
+ tablesCount.add(tableName);
581
+ if (progressCallback) {
582
+ progressCallback('tables', tablesCount.size, 'fetching');
583
+ }
584
+ const columnName = column.columnName;
585
+ const isNotNull = column.notNull === 1; // 'YES', 'NO'
586
+ const columnType = column.columnType; // varchar(256)
587
+ const isPrimary = column.pk !== 0; // 'PRI', ''
588
+ const columnDefault: string = column.defaultValue;
589
+
590
+ const isAutoincrement = isPrimary && tablesWithSeq.includes(tableName);
591
+
592
+ if (isPrimary) {
593
+ if (typeof tableToPk[tableName] === 'undefined') {
594
+ tableToPk[tableName] = [columnName];
595
+ } else {
596
+ tableToPk[tableName].push(columnName);
597
+ }
598
+ }
599
+
600
+ const table = result[tableName];
601
+
602
+ if (column.hidden === 2 || column.hidden === 3) {
603
+ if (
604
+ typeof tableToGeneratedColumnsInfo[column.tableName] === 'undefined'
605
+ ) {
606
+ tableToGeneratedColumnsInfo[column.tableName] = extractGeneratedColumns(
607
+ column.sql,
608
+ );
609
+ }
610
+ }
611
+
612
+ const newColumn: Column = {
613
+ default: columnDefault === null
614
+ ? undefined
615
+ : /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault)
616
+ ? Number(columnDefault)
617
+ : ['CURRENT_TIME', 'CURRENT_DATE', 'CURRENT_TIMESTAMP'].includes(
618
+ columnDefault,
619
+ )
620
+ ? `(${columnDefault})`
621
+ : columnDefault === 'false'
622
+ ? false
623
+ : columnDefault === 'true'
624
+ ? true
625
+ : columnDefault.startsWith("'") && columnDefault.endsWith("'")
626
+ ? columnDefault
627
+ // ? columnDefault.substring(1, columnDefault.length - 1)
628
+ : `(${columnDefault})`,
629
+ autoincrement: isAutoincrement,
630
+ name: columnName,
631
+ type: mapSqlToSqliteType(columnType),
632
+ primaryKey: false,
633
+ notNull: isNotNull,
634
+ generated: tableToGeneratedColumnsInfo[tableName]
635
+ && tableToGeneratedColumnsInfo[tableName][columnName]
636
+ ? {
637
+ type: tableToGeneratedColumnsInfo[tableName][columnName].type,
638
+ as: tableToGeneratedColumnsInfo[tableName][columnName].expression,
639
+ }
640
+ : undefined,
641
+ };
642
+
643
+ if (!table) {
644
+ result[tableName] = {
645
+ name: tableName,
646
+ columns: {
647
+ [columnName]: newColumn,
648
+ },
649
+ compositePrimaryKeys: {},
650
+ indexes: {},
651
+ foreignKeys: {},
652
+ uniqueConstraints: {},
653
+ checkConstraints: {},
654
+ };
655
+ } else {
656
+ result[tableName]!.columns[columnName] = newColumn;
657
+ }
658
+ }
659
+
660
+ for (const [key, value] of Object.entries(tableToPk)) {
661
+ if (value.length > 1) {
662
+ result[key].compositePrimaryKeys = {
663
+ [`${key}_${value.join('_')}_pk`]: {
664
+ columns: value,
665
+ name: `${key}_${value.join('_')}_pk`,
666
+ },
667
+ };
668
+ } else if (value.length === 1) {
669
+ result[key].columns[value[0]].primaryKey = true;
670
+ } else {
671
+ }
672
+ }
673
+
674
+ if (progressCallback) {
675
+ progressCallback('columns', columnsCount, 'done');
676
+ progressCallback('tables', tablesCount.size, 'done');
677
+ }
678
+ try {
679
+ const fks = await db.query<{
680
+ tableFrom: string;
681
+ tableTo: string;
682
+ from: string;
683
+ to: string;
684
+ onUpdate: string;
685
+ onDelete: string;
686
+ seq: number;
687
+ id: number;
688
+ }>(
689
+ `SELECT m.name as "tableFrom", f.id as "id", f."table" as "tableTo", f."from", f."to", f."on_update" as "onUpdate", f."on_delete" as "onDelete", f.seq as "seq"
690
+ FROM sqlite_master m, pragma_foreign_key_list(m.name) as f
691
+ where m.tbl_name != '_cf_KV';`,
692
+ );
693
+
694
+ const fkByTableName: Record<string, ForeignKey> = {};
695
+
696
+ for (const fkRow of fks) {
697
+ foreignKeysCount += 1;
698
+ if (progressCallback) {
699
+ progressCallback('fks', foreignKeysCount, 'fetching');
700
+ }
701
+ const tableName: string = fkRow.tableFrom;
702
+ const columnName: string = fkRow.from;
703
+ const refTableName = fkRow.tableTo;
704
+ const refColumnName: string = fkRow.to;
705
+ const updateRule: string = fkRow.onUpdate;
706
+ const deleteRule = fkRow.onDelete;
707
+ const sequence = fkRow.seq;
708
+ const id = fkRow.id;
709
+
710
+ const tableInResult = result[tableName];
711
+ if (typeof tableInResult === 'undefined') continue;
712
+
713
+ if (typeof fkByTableName[`${tableName}_${id}`] !== 'undefined') {
714
+ fkByTableName[`${tableName}_${id}`]!.columnsFrom.push(columnName);
715
+ fkByTableName[`${tableName}_${id}`]!.columnsTo.push(refColumnName);
716
+ } else {
717
+ fkByTableName[`${tableName}_${id}`] = {
718
+ name: '',
719
+ tableFrom: tableName,
720
+ tableTo: refTableName,
721
+ columnsFrom: [columnName],
722
+ columnsTo: [refColumnName],
723
+ onDelete: deleteRule?.toLowerCase(),
724
+ onUpdate: updateRule?.toLowerCase(),
725
+ };
726
+ }
727
+
728
+ const columnsFrom = fkByTableName[`${tableName}_${id}`].columnsFrom;
729
+ const columnsTo = fkByTableName[`${tableName}_${id}`].columnsTo;
730
+ fkByTableName[
731
+ `${tableName}_${id}`
732
+ ].name = `${tableName}_${
733
+ columnsFrom.join(
734
+ '_',
735
+ )
736
+ }_${refTableName}_${columnsTo.join('_')}_fk`;
737
+ }
738
+
739
+ for (const idx of Object.keys(fkByTableName)) {
740
+ const value = fkByTableName[idx];
741
+ result[value.tableFrom].foreignKeys[value.name] = value;
742
+ }
743
+ } catch (e) {
744
+ // console.log(`Can't proccess foreign keys`);
745
+ }
746
+ if (progressCallback) {
747
+ progressCallback('fks', foreignKeysCount, 'done');
748
+ }
749
+ const idxs = await db.query<{
750
+ tableName: string;
751
+ indexName: string;
752
+ columnName: string;
753
+ isUnique: number;
754
+ seq: string;
755
+ }>(
756
+ `SELECT
757
+ m.tbl_name as tableName,
758
+ il.name as indexName,
759
+ ii.name as columnName,
760
+ il.[unique] as isUnique,
761
+ il.seq as seq
762
+ FROM sqlite_master AS m,
763
+ pragma_index_list(m.name) AS il,
764
+ pragma_index_info(il.name) AS ii
765
+ WHERE
766
+ m.type = 'table'
767
+ and il.name NOT LIKE 'sqlite_autoindex_%'
768
+ and m.tbl_name != '_cf_KV';`,
769
+ );
770
+
771
+ for (const idxRow of idxs) {
772
+ const tableName = idxRow.tableName;
773
+ const constraintName = idxRow.indexName;
774
+ const columnName: string = idxRow.columnName;
775
+ const isUnique = idxRow.isUnique === 1;
776
+
777
+ const tableInResult = result[tableName];
778
+ if (typeof tableInResult === 'undefined') continue;
779
+
780
+ indexesCount += 1;
781
+ if (progressCallback) {
782
+ progressCallback('indexes', indexesCount, 'fetching');
783
+ }
784
+
785
+ if (
786
+ typeof tableInResult.indexes[constraintName] !== 'undefined'
787
+ && columnName
788
+ ) {
789
+ tableInResult.indexes[constraintName]!.columns.push(columnName);
790
+ } else {
791
+ tableInResult.indexes[constraintName] = {
792
+ name: constraintName,
793
+ columns: columnName ? [columnName] : [],
794
+ isUnique: isUnique,
795
+ };
796
+ }
797
+ // if (isUnique) {
798
+ // if (typeof tableInResult.uniqueConstraints[constraintName] !== "undefined") {
799
+ // tableInResult.uniqueConstraints[constraintName]!.columns.push(columnName);
800
+ // } else {
801
+ // tableInResult.uniqueConstraints[constraintName] = {
802
+ // name: constraintName,
803
+ // columns: [columnName],
804
+ // };
805
+ // }
806
+ // } else {
807
+ // if (typeof tableInResult.indexes[constraintName] !== "undefined") {
808
+ // tableInResult.indexes[constraintName]!.columns.push(columnName);
809
+ // } else {
810
+ // tableInResult.indexes[constraintName] = {
811
+ // name: constraintName,
812
+ // columns: [columnName],
813
+ // isUnique: isUnique,
814
+ // };
815
+ // }
816
+ // }
817
+ }
818
+ if (progressCallback) {
819
+ progressCallback('indexes', indexesCount, 'done');
820
+ // progressCallback("enums", 0, "fetching");
821
+ progressCallback('enums', 0, 'done');
822
+ }
823
+
824
+ const views = await db.query(
825
+ `SELECT name AS view_name, sql AS sql FROM sqlite_master WHERE type = 'view';`,
826
+ );
827
+
828
+ viewsCount = views.length;
829
+
830
+ if (progressCallback) {
831
+ progressCallback('views', viewsCount, 'fetching');
832
+ }
833
+ for (const view of views) {
834
+ const viewName = view['view_name'];
835
+ const sql = view['sql'];
836
+
837
+ const regex = new RegExp(`\\bAS\\b\\s+(SELECT.+)$`, 'i');
838
+ const match = sql.match(regex);
839
+
840
+ if (!match) {
841
+ console.log('Could not process view');
842
+ process.exit(1);
843
+ }
844
+
845
+ const viewDefinition = match[1] as string;
846
+
847
+ const columns = result[viewName].columns;
848
+ delete result[viewName];
849
+
850
+ resultViews[viewName] = {
851
+ columns: columns,
852
+ isExisting: false,
853
+ name: viewName,
854
+ definition: viewDefinition,
855
+ };
856
+ }
857
+ if (progressCallback) {
858
+ progressCallback('views', viewsCount, 'done');
859
+ }
860
+
861
+ const namedCheckPattern = /CONSTRAINT\s*["']?(\w+)["']?\s*CHECK\s*\((.*?)\)/gi;
862
+ const unnamedCheckPattern = /CHECK\s*\((.*?)\)/gi;
863
+ let checkCounter = 0;
864
+ const checkConstraints: Record<string, CheckConstraint> = {};
865
+ const checks = await db.query<{ tableName: string; sql: string }>(`SELECT name as "tableName", sql as "sql"
866
+ FROM sqlite_master
867
+ WHERE type = 'table' AND name != 'sqlite_sequence';`);
868
+ for (const check of checks) {
869
+ if (!tablesFilter(check.tableName)) continue;
870
+
871
+ const { tableName, sql } = check;
872
+
873
+ // Find named CHECK constraints
874
+ let namedChecks = [...sql.matchAll(namedCheckPattern)];
875
+ if (namedChecks.length > 0) {
876
+ namedChecks.forEach(([_, checkName, checkValue]) => {
877
+ checkConstraints[checkName] = {
878
+ name: checkName,
879
+ value: checkValue.trim(),
880
+ };
881
+ });
882
+ } else {
883
+ // If no named constraints, find unnamed CHECK constraints and assign names
884
+ let unnamedChecks = [...sql.matchAll(unnamedCheckPattern)];
885
+ unnamedChecks.forEach(([_, checkValue]) => {
886
+ let checkName = `${tableName}_check_${++checkCounter}`;
887
+ checkConstraints[checkName] = {
888
+ name: checkName,
889
+ value: checkValue.trim(),
890
+ };
891
+ });
892
+ }
893
+
894
+ checksCount += Object.values(checkConstraints).length;
895
+ if (progressCallback) {
896
+ progressCallback('checks', checksCount, 'fetching');
897
+ }
898
+
899
+ const table = result[tableName];
900
+
901
+ if (!table) {
902
+ result[tableName] = {
903
+ name: tableName,
904
+ columns: {},
905
+ compositePrimaryKeys: {},
906
+ indexes: {},
907
+ foreignKeys: {},
908
+ uniqueConstraints: {},
909
+ checkConstraints: checkConstraints,
910
+ };
911
+ } else {
912
+ result[tableName]!.checkConstraints = checkConstraints;
913
+ }
914
+ }
915
+
916
+ if (progressCallback) {
917
+ progressCallback('checks', checksCount, 'done');
918
+ }
919
+
920
+ return {
921
+ version: '6',
922
+ dialect: 'sqlite',
923
+ tables: result,
924
+ views: resultViews,
925
+ enums: {},
926
+ _meta: {
927
+ tables: {},
928
+ columns: {},
929
+ },
930
+ };
931
+ };