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