@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,999 @@
1
+ import chalk from 'chalk';
2
+ import { getTableName, is, SQL } from 'drizzle-orm';
3
+ import {
4
+ AnyMySqlTable,
5
+ getTableConfig,
6
+ getViewConfig,
7
+ MySqlColumn,
8
+ MySqlDialect,
9
+ MySqlView,
10
+ type PrimaryKey as PrimaryKeyORM,
11
+ uniqueKeyName,
12
+ } from 'drizzle-orm/mysql-core';
13
+ import { RowDataPacket } from 'mysql2/promise';
14
+ import { CasingType } from 'src/cli/validations/common';
15
+ import { withStyle } from '../cli/validations/outputs';
16
+ import { IntrospectStage, IntrospectStatus } from '../cli/views';
17
+ import {
18
+ CheckConstraint,
19
+ Column,
20
+ ForeignKey,
21
+ Index,
22
+ MySqlKitInternals,
23
+ MySqlSchemaInternal,
24
+ PrimaryKey,
25
+ Table,
26
+ UniqueConstraint,
27
+ View,
28
+ } from '../serializer/mysqlSchema';
29
+ import { type DB, escapeSingleQuotes } from '../utils';
30
+ import { getColumnCasing, sqlToStr } from './utils';
31
+
32
+ export const indexName = (tableName: string, columns: string[]) => {
33
+ return `${tableName}_${columns.join('_')}_index`;
34
+ };
35
+
36
+ const handleEnumType = (type: string) => {
37
+ let str = type.split('(')[1];
38
+ str = str.substring(0, str.length - 1);
39
+ const values = str.split(',').map((v) => `'${escapeSingleQuotes(v.substring(1, v.length - 1))}'`);
40
+ return `enum(${values.join(',')})`;
41
+ };
42
+
43
+ export const generateMySqlSnapshot = (
44
+ tables: AnyMySqlTable[],
45
+ views: MySqlView[],
46
+ casing: CasingType | undefined,
47
+ ): MySqlSchemaInternal => {
48
+ const dialect = new MySqlDialect({ casing });
49
+ const result: Record<string, Table> = {};
50
+ const resultViews: Record<string, View> = {};
51
+ const internal: MySqlKitInternals = { tables: {}, indexes: {} };
52
+
53
+ for (const table of tables) {
54
+ const {
55
+ name: tableName,
56
+ columns,
57
+ indexes,
58
+ foreignKeys,
59
+ schema,
60
+ checks,
61
+ primaryKeys,
62
+ uniqueConstraints,
63
+ } = getTableConfig(table);
64
+
65
+ const columnsObject: Record<string, Column> = {};
66
+ const indexesObject: Record<string, Index> = {};
67
+ const foreignKeysObject: Record<string, ForeignKey> = {};
68
+ const primaryKeysObject: Record<string, PrimaryKey> = {};
69
+ const uniqueConstraintObject: Record<string, UniqueConstraint> = {};
70
+ const checkConstraintObject: Record<string, CheckConstraint> = {};
71
+
72
+ // this object will help to identify same check names
73
+ let checksInTable: Record<string, string[]> = {};
74
+
75
+ columns.forEach((column) => {
76
+ const name = getColumnCasing(column, casing);
77
+ const notNull: boolean = column.notNull;
78
+ const sqlType = column.getSQLType();
79
+ const sqlTypeLowered = sqlType.toLowerCase();
80
+ const autoIncrement = typeof (column as any).autoIncrement === 'undefined'
81
+ ? false
82
+ : (column as any).autoIncrement;
83
+
84
+ const generated = column.generated;
85
+
86
+ const columnToSet: Column = {
87
+ name,
88
+ type: sqlType.startsWith('enum') ? handleEnumType(sqlType) : sqlType,
89
+ primaryKey: false,
90
+ // If field is autoincrement it's notNull by default
91
+ // notNull: autoIncrement ? true : notNull,
92
+ notNull,
93
+ autoincrement: autoIncrement,
94
+ onUpdate: (column as any).hasOnUpdateNow,
95
+ generated: generated
96
+ ? {
97
+ as: is(generated.as, SQL)
98
+ ? dialect.sqlToQuery(generated.as as SQL).sql
99
+ : typeof generated.as === 'function'
100
+ ? dialect.sqlToQuery(generated.as() as SQL).sql
101
+ : (generated.as as any),
102
+ type: generated.mode ?? 'stored',
103
+ }
104
+ : undefined,
105
+ };
106
+
107
+ if (column.primary) {
108
+ primaryKeysObject[`${tableName}_${name}`] = {
109
+ name: `${tableName}_${name}`,
110
+ columns: [name],
111
+ };
112
+ }
113
+
114
+ if (column.isUnique) {
115
+ const existingUnique = uniqueConstraintObject[column.uniqueName!];
116
+ if (typeof existingUnique !== 'undefined') {
117
+ console.log(
118
+ `\n${
119
+ withStyle.errorWarning(`We\'ve found duplicated unique constraint names in ${
120
+ chalk.underline.blue(
121
+ tableName,
122
+ )
123
+ } table.
124
+ The unique constraint ${
125
+ chalk.underline.blue(
126
+ column.uniqueName,
127
+ )
128
+ } on the ${
129
+ chalk.underline.blue(
130
+ name,
131
+ )
132
+ } column is confilcting with a unique constraint name already defined for ${
133
+ chalk.underline.blue(
134
+ existingUnique.columns.join(','),
135
+ )
136
+ } columns\n`)
137
+ }`,
138
+ );
139
+ process.exit(1);
140
+ }
141
+ uniqueConstraintObject[column.uniqueName!] = {
142
+ name: column.uniqueName!,
143
+ columns: [columnToSet.name],
144
+ };
145
+ }
146
+
147
+ if (column.default !== undefined) {
148
+ if (is(column.default, SQL)) {
149
+ columnToSet.default = sqlToStr(column.default, casing);
150
+ } else {
151
+ if (typeof column.default === 'string') {
152
+ columnToSet.default = `'${escapeSingleQuotes(column.default)}'`;
153
+ } else {
154
+ if (sqlTypeLowered === 'json') {
155
+ columnToSet.default = `'${JSON.stringify(column.default)}'`;
156
+ } else if (column.default instanceof Date) {
157
+ if (sqlTypeLowered === 'date') {
158
+ columnToSet.default = `'${column.default.toISOString().split('T')[0]}'`;
159
+ } else if (
160
+ sqlTypeLowered.startsWith('datetime')
161
+ || sqlTypeLowered.startsWith('timestamp')
162
+ ) {
163
+ columnToSet.default = `'${
164
+ column.default
165
+ .toISOString()
166
+ .replace('T', ' ')
167
+ .slice(0, 23)
168
+ }'`;
169
+ }
170
+ } else {
171
+ columnToSet.default = column.default;
172
+ }
173
+ }
174
+ if (['blob', 'text', 'json'].includes(column.getSQLType())) {
175
+ columnToSet.default = `(${columnToSet.default})`;
176
+ }
177
+ }
178
+ }
179
+ columnsObject[name] = columnToSet;
180
+ });
181
+
182
+ primaryKeys.map((pk: PrimaryKeyORM) => {
183
+ const originalColumnNames = pk.columns.map((c) => c.name);
184
+ const columnNames = pk.columns.map((c: any) => getColumnCasing(c, casing));
185
+
186
+ let name = pk.getName();
187
+ if (casing !== undefined) {
188
+ for (let i = 0; i < originalColumnNames.length; i++) {
189
+ name = name.replace(originalColumnNames[i], columnNames[i]);
190
+ }
191
+ }
192
+
193
+ primaryKeysObject[name] = {
194
+ name,
195
+ columns: columnNames,
196
+ };
197
+
198
+ // all composite pk's should be treated as notNull
199
+ for (const column of pk.columns) {
200
+ columnsObject[getColumnCasing(column, casing)].notNull = true;
201
+ }
202
+ });
203
+
204
+ uniqueConstraints?.map((unq) => {
205
+ const columnNames = unq.columns.map((c) => getColumnCasing(c, casing));
206
+
207
+ const name = unq.name ?? uniqueKeyName(table, columnNames);
208
+
209
+ const existingUnique = uniqueConstraintObject[name];
210
+ if (typeof existingUnique !== 'undefined') {
211
+ console.log(
212
+ `\n${
213
+ withStyle.errorWarning(
214
+ `We\'ve found duplicated unique constraint names in ${
215
+ chalk.underline.blue(
216
+ tableName,
217
+ )
218
+ } table. \nThe unique constraint ${
219
+ chalk.underline.blue(
220
+ name,
221
+ )
222
+ } on the ${
223
+ chalk.underline.blue(
224
+ columnNames.join(','),
225
+ )
226
+ } columns is confilcting with a unique constraint name already defined for ${
227
+ chalk.underline.blue(
228
+ existingUnique.columns.join(','),
229
+ )
230
+ } columns\n`,
231
+ )
232
+ }`,
233
+ );
234
+ process.exit(1);
235
+ }
236
+
237
+ uniqueConstraintObject[name] = {
238
+ name: unq.name!,
239
+ columns: columnNames,
240
+ };
241
+ });
242
+
243
+ const fks: ForeignKey[] = foreignKeys.map((fk) => {
244
+ const tableFrom = tableName;
245
+ const onDelete = fk.onDelete ?? 'no action';
246
+ const onUpdate = fk.onUpdate ?? 'no action';
247
+ const reference = fk.reference();
248
+
249
+ const referenceFT = reference.foreignTable;
250
+
251
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
252
+ const tableTo = getTableName(referenceFT);
253
+
254
+ const originalColumnsFrom = reference.columns.map((it) => it.name);
255
+ const columnsFrom = reference.columns.map((it) => getColumnCasing(it, casing));
256
+ const originalColumnsTo = reference.foreignColumns.map((it) => it.name);
257
+ const columnsTo = reference.foreignColumns.map((it) => getColumnCasing(it, casing));
258
+
259
+ let name = fk.getName();
260
+ if (casing !== undefined) {
261
+ for (let i = 0; i < originalColumnsFrom.length; i++) {
262
+ name = name.replace(originalColumnsFrom[i], columnsFrom[i]);
263
+ }
264
+ for (let i = 0; i < originalColumnsTo.length; i++) {
265
+ name = name.replace(originalColumnsTo[i], columnsTo[i]);
266
+ }
267
+ }
268
+
269
+ return {
270
+ name,
271
+ tableFrom,
272
+ tableTo,
273
+ columnsFrom,
274
+ columnsTo,
275
+ onDelete,
276
+ onUpdate,
277
+ } as ForeignKey;
278
+ });
279
+
280
+ fks.forEach((it) => {
281
+ foreignKeysObject[it.name] = it;
282
+ });
283
+
284
+ indexes.forEach((value) => {
285
+ const columns = value.config.columns;
286
+ const name = value.config.name;
287
+
288
+ let indexColumns = columns.map((it) => {
289
+ if (is(it, SQL)) {
290
+ const sql = dialect.sqlToQuery(it, 'indexes').sql;
291
+ if (typeof internal!.indexes![name] === 'undefined') {
292
+ internal!.indexes![name] = {
293
+ columns: {
294
+ [sql]: {
295
+ isExpression: true,
296
+ },
297
+ },
298
+ };
299
+ } else {
300
+ if (typeof internal!.indexes![name]?.columns[sql] === 'undefined') {
301
+ internal!.indexes![name]!.columns[sql] = {
302
+ isExpression: true,
303
+ };
304
+ } else {
305
+ internal!.indexes![name]!.columns[sql]!.isExpression = true;
306
+ }
307
+ }
308
+ return sql;
309
+ } else {
310
+ return `${getColumnCasing(it, casing)}`;
311
+ }
312
+ });
313
+
314
+ if (value.config.unique) {
315
+ if (typeof uniqueConstraintObject[name] !== 'undefined') {
316
+ console.log(
317
+ `\n${
318
+ withStyle.errorWarning(
319
+ `We\'ve found duplicated unique constraint names in ${
320
+ chalk.underline.blue(
321
+ tableName,
322
+ )
323
+ } table. \nThe unique index ${
324
+ chalk.underline.blue(
325
+ name,
326
+ )
327
+ } on the ${
328
+ chalk.underline.blue(
329
+ indexColumns.join(','),
330
+ )
331
+ } columns is confilcting with a unique constraint name already defined for ${
332
+ chalk.underline.blue(
333
+ uniqueConstraintObject[name].columns.join(','),
334
+ )
335
+ } columns\n`,
336
+ )
337
+ }`,
338
+ );
339
+ process.exit(1);
340
+ }
341
+ } else {
342
+ if (typeof foreignKeysObject[name] !== 'undefined') {
343
+ console.log(
344
+ `\n${
345
+ withStyle.errorWarning(
346
+ `In MySQL, when creating a foreign key, an index is automatically generated with the same name as the foreign key constraint.\n\nWe have encountered a collision between the index name on columns ${
347
+ chalk.underline.blue(
348
+ indexColumns.join(','),
349
+ )
350
+ } and the foreign key on columns ${
351
+ chalk.underline.blue(
352
+ foreignKeysObject[name].columnsFrom.join(','),
353
+ )
354
+ }. Please change either the index name or the foreign key name. For more information, please refer to https://dev.mysql.com/doc/refman/8.0/en/constraint-foreign-key.html\n
355
+ `,
356
+ )
357
+ }`,
358
+ );
359
+ process.exit(1);
360
+ }
361
+ }
362
+
363
+ indexesObject[name] = {
364
+ name,
365
+ columns: indexColumns,
366
+ isUnique: value.config.unique ?? false,
367
+ using: value.config.using,
368
+ algorithm: value.config.algorythm,
369
+ lock: value.config.lock,
370
+ };
371
+ });
372
+
373
+ checks.forEach((check) => {
374
+ check;
375
+ const checkName = check.name;
376
+ if (typeof checksInTable[tableName] !== 'undefined') {
377
+ if (checksInTable[tableName].includes(check.name)) {
378
+ console.log(
379
+ `\n${
380
+ withStyle.errorWarning(
381
+ `We\'ve found duplicated check constraint name in ${
382
+ chalk.underline.blue(
383
+ tableName,
384
+ )
385
+ }. Please rename your check constraint in the ${
386
+ chalk.underline.blue(
387
+ tableName,
388
+ )
389
+ } table`,
390
+ )
391
+ }`,
392
+ );
393
+ process.exit(1);
394
+ }
395
+ checksInTable[tableName].push(checkName);
396
+ } else {
397
+ checksInTable[tableName] = [check.name];
398
+ }
399
+
400
+ checkConstraintObject[checkName] = {
401
+ name: checkName,
402
+ value: dialect.sqlToQuery(check.value).sql,
403
+ };
404
+ });
405
+
406
+ // only handle tables without schemas
407
+ if (!schema) {
408
+ result[tableName] = {
409
+ name: tableName,
410
+ columns: columnsObject,
411
+ indexes: indexesObject,
412
+ foreignKeys: foreignKeysObject,
413
+ compositePrimaryKeys: primaryKeysObject,
414
+ uniqueConstraints: uniqueConstraintObject,
415
+ checkConstraint: checkConstraintObject,
416
+ };
417
+ }
418
+ }
419
+
420
+ for (const view of views) {
421
+ const {
422
+ isExisting,
423
+ name,
424
+ query,
425
+ schema,
426
+ selectedFields,
427
+ algorithm,
428
+ sqlSecurity,
429
+ withCheckOption,
430
+ } = getViewConfig(view);
431
+
432
+ const columnsObject: Record<string, Column> = {};
433
+
434
+ const existingView = resultViews[name];
435
+ if (typeof existingView !== 'undefined') {
436
+ console.log(
437
+ `\n${
438
+ withStyle.errorWarning(
439
+ `We\'ve found duplicated view name across ${
440
+ chalk.underline.blue(
441
+ schema ?? 'public',
442
+ )
443
+ } schema. Please rename your view`,
444
+ )
445
+ }`,
446
+ );
447
+ process.exit(1);
448
+ }
449
+
450
+ for (const key in selectedFields) {
451
+ if (is(selectedFields[key], MySqlColumn)) {
452
+ const column = selectedFields[key];
453
+
454
+ const notNull: boolean = column.notNull;
455
+ const sqlTypeLowered = column.getSQLType().toLowerCase();
456
+ const autoIncrement = typeof (column as any).autoIncrement === 'undefined'
457
+ ? false
458
+ : (column as any).autoIncrement;
459
+
460
+ const generated = column.generated;
461
+
462
+ const columnToSet: Column = {
463
+ name: column.name,
464
+ type: column.getSQLType(),
465
+ primaryKey: false,
466
+ // If field is autoincrement it's notNull by default
467
+ // notNull: autoIncrement ? true : notNull,
468
+ notNull,
469
+ autoincrement: autoIncrement,
470
+ onUpdate: (column as any).hasOnUpdateNow,
471
+ generated: generated
472
+ ? {
473
+ as: is(generated.as, SQL)
474
+ ? dialect.sqlToQuery(generated.as as SQL).sql
475
+ : typeof generated.as === 'function'
476
+ ? dialect.sqlToQuery(generated.as() as SQL).sql
477
+ : (generated.as as any),
478
+ type: generated.mode ?? 'stored',
479
+ }
480
+ : undefined,
481
+ };
482
+
483
+ if (column.default !== undefined) {
484
+ if (is(column.default, SQL)) {
485
+ columnToSet.default = sqlToStr(column.default, casing);
486
+ } else {
487
+ if (typeof column.default === 'string') {
488
+ columnToSet.default = `'${column.default}'`;
489
+ } else {
490
+ if (sqlTypeLowered === 'json') {
491
+ columnToSet.default = `'${JSON.stringify(column.default)}'`;
492
+ } else if (column.default instanceof Date) {
493
+ if (sqlTypeLowered === 'date') {
494
+ columnToSet.default = `'${column.default.toISOString().split('T')[0]}'`;
495
+ } else if (
496
+ sqlTypeLowered.startsWith('datetime')
497
+ || sqlTypeLowered.startsWith('timestamp')
498
+ ) {
499
+ columnToSet.default = `'${
500
+ column.default
501
+ .toISOString()
502
+ .replace('T', ' ')
503
+ .slice(0, 23)
504
+ }'`;
505
+ }
506
+ } else {
507
+ columnToSet.default = column.default;
508
+ }
509
+ }
510
+ if (['blob', 'text', 'json'].includes(column.getSQLType())) {
511
+ columnToSet.default = `(${columnToSet.default})`;
512
+ }
513
+ }
514
+ }
515
+ columnsObject[column.name] = columnToSet;
516
+ }
517
+ }
518
+
519
+ resultViews[name] = {
520
+ columns: columnsObject,
521
+ name,
522
+ isExisting,
523
+ definition: isExisting ? undefined : dialect.sqlToQuery(query!).sql,
524
+ withCheckOption,
525
+ algorithm: algorithm ?? 'undefined', // set default values
526
+ sqlSecurity: sqlSecurity ?? 'definer', // set default values
527
+ };
528
+ }
529
+
530
+ return {
531
+ version: '5',
532
+ dialect: 'mysql',
533
+ tables: result,
534
+ views: resultViews,
535
+ _meta: {
536
+ tables: {},
537
+ columns: {},
538
+ },
539
+ internal,
540
+ };
541
+ };
542
+
543
+ function clearDefaults(defaultValue: any, collate: string) {
544
+ if (typeof collate === 'undefined' || collate === null) {
545
+ collate = `utf8mb4`;
546
+ }
547
+
548
+ let resultDefault = defaultValue;
549
+ collate = `_${collate}`;
550
+ if (defaultValue.startsWith(collate)) {
551
+ resultDefault = resultDefault
552
+ .substring(collate.length, defaultValue.length)
553
+ .replace(/\\/g, '');
554
+ if (resultDefault.startsWith("'") && resultDefault.endsWith("'")) {
555
+ return `('${escapeSingleQuotes(resultDefault.substring(1, resultDefault.length - 1))}')`;
556
+ } else {
557
+ return `'${escapeSingleQuotes(resultDefault.substring(1, resultDefault.length - 1))}'`;
558
+ }
559
+ } else {
560
+ return `(${resultDefault})`;
561
+ }
562
+ }
563
+
564
+ export const fromDatabase = async (
565
+ db: DB,
566
+ inputSchema: string,
567
+ tablesFilter: (table: string) => boolean = (table) => true,
568
+ progressCallback?: (
569
+ stage: IntrospectStage,
570
+ count: number,
571
+ status: IntrospectStatus,
572
+ ) => void,
573
+ ): Promise<MySqlSchemaInternal> => {
574
+ const result: Record<string, Table> = {};
575
+ const internals: MySqlKitInternals = { tables: {}, indexes: {} };
576
+
577
+ const columns = await db.query(`select * from information_schema.columns
578
+ where table_schema = '${inputSchema}' and table_name != '__drizzle_migrations'
579
+ order by table_name, ordinal_position;`);
580
+
581
+ const response = columns as RowDataPacket[];
582
+
583
+ const schemas: string[] = [];
584
+
585
+ let columnsCount = 0;
586
+ let tablesCount = new Set();
587
+ let indexesCount = 0;
588
+ let foreignKeysCount = 0;
589
+ let checksCount = 0;
590
+ let viewsCount = 0;
591
+
592
+ const idxs = await db.query(
593
+ `select * from INFORMATION_SCHEMA.STATISTICS
594
+ WHERE INFORMATION_SCHEMA.STATISTICS.TABLE_SCHEMA = '${inputSchema}' and INFORMATION_SCHEMA.STATISTICS.INDEX_NAME != 'PRIMARY';`,
595
+ );
596
+
597
+ const idxRows = idxs as RowDataPacket[];
598
+
599
+ for (const column of response) {
600
+ if (!tablesFilter(column['TABLE_NAME'] as string)) continue;
601
+
602
+ columnsCount += 1;
603
+ if (progressCallback) {
604
+ progressCallback('columns', columnsCount, 'fetching');
605
+ }
606
+ const schema: string = column['TABLE_SCHEMA'];
607
+ const tableName = column['TABLE_NAME'];
608
+
609
+ tablesCount.add(`${schema}.${tableName}`);
610
+ if (progressCallback) {
611
+ progressCallback('columns', tablesCount.size, 'fetching');
612
+ }
613
+ const columnName: string = column['COLUMN_NAME'];
614
+ const isNullable = column['IS_NULLABLE'] === 'YES'; // 'YES', 'NO'
615
+ const dataType = column['DATA_TYPE']; // varchar
616
+ const columnType = column['COLUMN_TYPE']; // varchar(256)
617
+ const isPrimary = column['COLUMN_KEY'] === 'PRI'; // 'PRI', ''
618
+ const columnDefault: string = column['COLUMN_DEFAULT'];
619
+ const collation: string = column['CHARACTER_SET_NAME'];
620
+ const geenratedExpression: string = column['GENERATION_EXPRESSION'];
621
+
622
+ let columnExtra = column['EXTRA'];
623
+ let isAutoincrement = false; // 'auto_increment', ''
624
+ let isDefaultAnExpression = false; // 'auto_increment', ''
625
+
626
+ if (typeof column['EXTRA'] !== 'undefined') {
627
+ columnExtra = column['EXTRA'];
628
+ isAutoincrement = column['EXTRA'] === 'auto_increment'; // 'auto_increment', ''
629
+ isDefaultAnExpression = column['EXTRA'].includes('DEFAULT_GENERATED'); // 'auto_increment', ''
630
+ }
631
+
632
+ // if (isPrimary) {
633
+ // if (typeof tableToPk[tableName] === "undefined") {
634
+ // tableToPk[tableName] = [columnName];
635
+ // } else {
636
+ // tableToPk[tableName].push(columnName);
637
+ // }
638
+ // }
639
+
640
+ if (schema !== inputSchema) {
641
+ schemas.push(schema);
642
+ }
643
+
644
+ const table = result[tableName];
645
+
646
+ // let changedType = columnType.replace("bigint unsigned", "serial")
647
+ let changedType = columnType;
648
+
649
+ if (columnType === 'bigint unsigned' && !isNullable && isAutoincrement) {
650
+ // check unique here
651
+ const uniqueIdx = idxRows.filter(
652
+ (it) =>
653
+ it['COLUMN_NAME'] === columnName
654
+ && it['TABLE_NAME'] === tableName
655
+ && it['NON_UNIQUE'] === 0,
656
+ );
657
+ if (uniqueIdx && uniqueIdx.length === 1) {
658
+ changedType = columnType.replace('bigint unsigned', 'serial');
659
+ }
660
+ }
661
+
662
+ if (columnType.includes('decimal(10,0)')) {
663
+ changedType = columnType.replace('decimal(10,0)', 'decimal');
664
+ }
665
+
666
+ let onUpdate: boolean | undefined = undefined;
667
+ if (
668
+ columnType.startsWith('timestamp')
669
+ && typeof columnExtra !== 'undefined'
670
+ && columnExtra.includes('on update CURRENT_TIMESTAMP')
671
+ ) {
672
+ onUpdate = true;
673
+ }
674
+
675
+ const newColumn: Column = {
676
+ default: columnDefault === null || columnDefault === undefined
677
+ ? undefined
678
+ : /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault)
679
+ && !['decimal', 'char', 'varchar'].some((type) => columnType.startsWith(type))
680
+ ? Number(columnDefault)
681
+ : isDefaultAnExpression
682
+ ? clearDefaults(columnDefault, collation)
683
+ : `'${escapeSingleQuotes(columnDefault)}'`,
684
+ autoincrement: isAutoincrement,
685
+ name: columnName,
686
+ type: changedType,
687
+ primaryKey: false,
688
+ notNull: !isNullable,
689
+ onUpdate,
690
+ generated: geenratedExpression
691
+ ? {
692
+ as: geenratedExpression,
693
+ type: columnExtra === 'VIRTUAL GENERATED' ? 'virtual' : 'stored',
694
+ }
695
+ : undefined,
696
+ };
697
+
698
+ // Set default to internal object
699
+ if (isDefaultAnExpression) {
700
+ if (typeof internals!.tables![tableName] === 'undefined') {
701
+ internals!.tables![tableName] = {
702
+ columns: {
703
+ [columnName]: {
704
+ isDefaultAnExpression: true,
705
+ },
706
+ },
707
+ };
708
+ } else {
709
+ if (
710
+ typeof internals!.tables![tableName]!.columns[columnName]
711
+ === 'undefined'
712
+ ) {
713
+ internals!.tables![tableName]!.columns[columnName] = {
714
+ isDefaultAnExpression: true,
715
+ };
716
+ } else {
717
+ internals!.tables![tableName]!.columns[
718
+ columnName
719
+ ]!.isDefaultAnExpression = true;
720
+ }
721
+ }
722
+ }
723
+
724
+ if (!table) {
725
+ result[tableName] = {
726
+ name: tableName,
727
+ columns: {
728
+ [columnName]: newColumn,
729
+ },
730
+ compositePrimaryKeys: {},
731
+ indexes: {},
732
+ foreignKeys: {},
733
+ uniqueConstraints: {},
734
+ checkConstraint: {},
735
+ };
736
+ } else {
737
+ result[tableName]!.columns[columnName] = newColumn;
738
+ }
739
+ }
740
+
741
+ const tablePks = await db.query(
742
+ `SELECT table_name, column_name, ordinal_position
743
+ FROM information_schema.table_constraints t
744
+ LEFT JOIN information_schema.key_column_usage k
745
+ USING(constraint_name,table_schema,table_name)
746
+ WHERE t.constraint_type='PRIMARY KEY'
747
+ and table_name != '__drizzle_migrations'
748
+ AND t.table_schema = '${inputSchema}'
749
+ ORDER BY ordinal_position`,
750
+ );
751
+
752
+ const tableToPk: { [tname: string]: string[] } = {};
753
+
754
+ const tableToPkRows = tablePks as RowDataPacket[];
755
+ for (const tableToPkRow of tableToPkRows) {
756
+ const tableName: string = tableToPkRow['TABLE_NAME'];
757
+ const columnName: string = tableToPkRow['COLUMN_NAME'];
758
+ const position: string = tableToPkRow['ordinal_position'];
759
+
760
+ if (typeof result[tableName] === 'undefined') {
761
+ continue;
762
+ }
763
+
764
+ if (typeof tableToPk[tableName] === 'undefined') {
765
+ tableToPk[tableName] = [columnName];
766
+ } else {
767
+ tableToPk[tableName].push(columnName);
768
+ }
769
+ }
770
+
771
+ for (const [key, value] of Object.entries(tableToPk)) {
772
+ // if (value.length > 1) {
773
+ result[key].compositePrimaryKeys = {
774
+ [`${key}_${value.join('_')}`]: {
775
+ name: `${key}_${value.join('_')}`,
776
+ columns: value,
777
+ },
778
+ };
779
+ // } else if (value.length === 1) {
780
+ // result[key].columns[value[0]].primaryKey = true;
781
+ // } else {
782
+ // }
783
+ }
784
+ if (progressCallback) {
785
+ progressCallback('columns', columnsCount, 'done');
786
+ progressCallback('tables', tablesCount.size, 'done');
787
+ }
788
+ try {
789
+ const fks = await db.query(
790
+ `SELECT
791
+ kcu.TABLE_SCHEMA,
792
+ kcu.TABLE_NAME,
793
+ kcu.CONSTRAINT_NAME,
794
+ kcu.COLUMN_NAME,
795
+ kcu.REFERENCED_TABLE_SCHEMA,
796
+ kcu.REFERENCED_TABLE_NAME,
797
+ kcu.REFERENCED_COLUMN_NAME,
798
+ rc.UPDATE_RULE,
799
+ rc.DELETE_RULE
800
+ FROM
801
+ INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
802
+ LEFT JOIN
803
+ information_schema.referential_constraints rc
804
+ ON kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
805
+ WHERE kcu.TABLE_SCHEMA = '${inputSchema}' AND kcu.CONSTRAINT_NAME != 'PRIMARY'
806
+ AND kcu.REFERENCED_TABLE_NAME IS NOT NULL;`,
807
+ );
808
+
809
+ const fkRows = fks as RowDataPacket[];
810
+
811
+ for (const fkRow of fkRows) {
812
+ foreignKeysCount += 1;
813
+ if (progressCallback) {
814
+ progressCallback('fks', foreignKeysCount, 'fetching');
815
+ }
816
+ const tableSchema = fkRow['TABLE_SCHEMA'];
817
+ const tableName: string = fkRow['TABLE_NAME'];
818
+ const constraintName = fkRow['CONSTRAINT_NAME'];
819
+ const columnName: string = fkRow['COLUMN_NAME'];
820
+ const refTableSchema = fkRow['REFERENCED_TABLE_SCHEMA'];
821
+ const refTableName = fkRow['REFERENCED_TABLE_NAME'];
822
+ const refColumnName: string = fkRow['REFERENCED_COLUMN_NAME'];
823
+ const updateRule: string = fkRow['UPDATE_RULE'];
824
+ const deleteRule = fkRow['DELETE_RULE'];
825
+
826
+ const tableInResult = result[tableName];
827
+ if (typeof tableInResult === 'undefined') continue;
828
+
829
+ if (typeof tableInResult.foreignKeys[constraintName] !== 'undefined') {
830
+ tableInResult.foreignKeys[constraintName]!.columnsFrom.push(columnName);
831
+ tableInResult.foreignKeys[constraintName]!.columnsTo.push(
832
+ refColumnName,
833
+ );
834
+ } else {
835
+ tableInResult.foreignKeys[constraintName] = {
836
+ name: constraintName,
837
+ tableFrom: tableName,
838
+ tableTo: refTableName,
839
+ columnsFrom: [columnName],
840
+ columnsTo: [refColumnName],
841
+ onDelete: deleteRule?.toLowerCase(),
842
+ onUpdate: updateRule?.toLowerCase(),
843
+ };
844
+ }
845
+
846
+ tableInResult.foreignKeys[constraintName]!.columnsFrom = [
847
+ ...new Set(tableInResult.foreignKeys[constraintName]!.columnsFrom),
848
+ ];
849
+
850
+ tableInResult.foreignKeys[constraintName]!.columnsTo = [
851
+ ...new Set(tableInResult.foreignKeys[constraintName]!.columnsTo),
852
+ ];
853
+ }
854
+ } catch (e) {
855
+ // console.log(`Can't proccess foreign keys`);
856
+ }
857
+ if (progressCallback) {
858
+ progressCallback('fks', foreignKeysCount, 'done');
859
+ }
860
+
861
+ for (const idxRow of idxRows) {
862
+ const tableSchema = idxRow['TABLE_SCHEMA'];
863
+ const tableName = idxRow['TABLE_NAME'];
864
+ const constraintName = idxRow['INDEX_NAME'];
865
+ const columnName: string = idxRow['COLUMN_NAME'];
866
+ const isUnique = idxRow['NON_UNIQUE'] === 0;
867
+
868
+ const tableInResult = result[tableName];
869
+ if (typeof tableInResult === 'undefined') continue;
870
+
871
+ // if (tableInResult.columns[columnName].type === "serial") continue;
872
+
873
+ indexesCount += 1;
874
+ if (progressCallback) {
875
+ progressCallback('indexes', indexesCount, 'fetching');
876
+ }
877
+
878
+ if (isUnique) {
879
+ if (
880
+ typeof tableInResult.uniqueConstraints[constraintName] !== 'undefined'
881
+ ) {
882
+ tableInResult.uniqueConstraints[constraintName]!.columns.push(
883
+ columnName,
884
+ );
885
+ } else {
886
+ tableInResult.uniqueConstraints[constraintName] = {
887
+ name: constraintName,
888
+ columns: [columnName],
889
+ };
890
+ }
891
+ } else {
892
+ // in MySQL FK creates index by default. Name of index is the same as fk constraint name
893
+ // so for introspect we will just skip it
894
+ if (typeof tableInResult.foreignKeys[constraintName] === 'undefined') {
895
+ if (typeof tableInResult.indexes[constraintName] !== 'undefined') {
896
+ tableInResult.indexes[constraintName]!.columns.push(columnName);
897
+ } else {
898
+ tableInResult.indexes[constraintName] = {
899
+ name: constraintName,
900
+ columns: [columnName],
901
+ isUnique: isUnique,
902
+ };
903
+ }
904
+ }
905
+ }
906
+ }
907
+
908
+ const views = await db.query(
909
+ `select * from INFORMATION_SCHEMA.VIEWS WHERE table_schema = '${inputSchema}';`,
910
+ );
911
+
912
+ const resultViews: Record<string, View> = {};
913
+
914
+ viewsCount = views.length;
915
+ if (progressCallback) {
916
+ progressCallback('views', viewsCount, 'fetching');
917
+ }
918
+ for await (const view of views) {
919
+ const viewName = view['TABLE_NAME'];
920
+ const definition = view['VIEW_DEFINITION'];
921
+
922
+ const withCheckOption = view['CHECK_OPTION'] === 'NONE' ? undefined : view['CHECK_OPTION'].toLowerCase();
923
+ const sqlSecurity = view['SECURITY_TYPE'].toLowerCase();
924
+
925
+ const [createSqlStatement] = await db.query(`SHOW CREATE VIEW \`${viewName}\`;`);
926
+ const algorithmMatch = createSqlStatement['Create View'].match(/ALGORITHM=([^ ]+)/);
927
+ const algorithm = algorithmMatch ? algorithmMatch[1].toLowerCase() : undefined;
928
+
929
+ const columns = result[viewName].columns;
930
+ delete result[viewName];
931
+
932
+ resultViews[viewName] = {
933
+ columns: columns,
934
+ isExisting: false,
935
+ name: viewName,
936
+ algorithm,
937
+ definition,
938
+ sqlSecurity,
939
+ withCheckOption,
940
+ };
941
+ }
942
+
943
+ if (progressCallback) {
944
+ progressCallback('indexes', indexesCount, 'done');
945
+ // progressCallback("enums", 0, "fetching");
946
+ progressCallback('enums', 0, 'done');
947
+ progressCallback('views', viewsCount, 'done');
948
+ }
949
+
950
+ const checkConstraints = await db.query(
951
+ `SELECT
952
+ tc.table_name,
953
+ tc.constraint_name,
954
+ cc.check_clause
955
+ FROM
956
+ information_schema.table_constraints tc
957
+ JOIN
958
+ information_schema.check_constraints cc
959
+ ON tc.constraint_name = cc.constraint_name
960
+ WHERE
961
+ tc.constraint_schema = '${inputSchema}'
962
+ AND
963
+ tc.constraint_type = 'CHECK';`,
964
+ );
965
+
966
+ checksCount += checkConstraints.length;
967
+ if (progressCallback) {
968
+ progressCallback('checks', checksCount, 'fetching');
969
+ }
970
+ for (const checkConstraintRow of checkConstraints) {
971
+ const constraintName = checkConstraintRow['CONSTRAINT_NAME'];
972
+ const constraintValue = checkConstraintRow['CHECK_CLAUSE'];
973
+ const tableName = checkConstraintRow['TABLE_NAME'];
974
+
975
+ const tableInResult = result[tableName];
976
+ // if (typeof tableInResult === 'undefined') continue;
977
+
978
+ tableInResult.checkConstraint[constraintName] = {
979
+ name: constraintName,
980
+ value: constraintValue,
981
+ };
982
+ }
983
+
984
+ if (progressCallback) {
985
+ progressCallback('checks', checksCount, 'done');
986
+ }
987
+
988
+ return {
989
+ version: '5',
990
+ dialect: 'mysql',
991
+ tables: result,
992
+ views: resultViews,
993
+ _meta: {
994
+ tables: {},
995
+ columns: {},
996
+ },
997
+ internal: internals,
998
+ };
999
+ };