-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathsql_to_database.ts
More file actions
722 lines (550 loc) · 27.4 KB
/
sql_to_database.ts
File metadata and controls
722 lines (550 loc) · 27.4 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
import { DataTypes, ForeignKeyActions, Modifiers, TimeDefaultValues } from "@/lib/field";
import { DataType } from "@/lib/schemas/data-type-schema";
import { FieldInsertType, FieldType } from "@/lib/schemas/field-schema";
import { TableInsertType } from "@/lib/schemas/table-schema";
import { Parser } from "node-sql-parser";
import { v4 } from "uuid";
import { parse } from 'pgsql-ast-parser';
import { DatabaseDialect, getDatabaseByDialect } from "@/lib/database";
import { randomColor } from "@/lib/colors";
import { Cardinality, RelationshipInsertType } from "@/lib/schemas/relationship-schema";
import { IndexInsertType } from "@/lib/schemas/index-schema";
export const SqlToDatabase = (sql: string, data_types: DataType[], dialect: DatabaseDialect) => {
const parser = new Parser();
let errors: Error[] = [];
const createTableStatements: string[] = [];
const alterTableStatements: string[] = [];
const createIndexStatements: string[] = [];
const createPostgresTypesStatements: string[] = [];
const tables: TableInsertType[] = [];
let relationships: RelationshipInsertType[] = [];
const indices: IndexInsertType[] = []
const postgresTypes: any[] = [];
// Clean up SQL: remove comments and normalize
const cleanedSql = sql
.replace(/--.*$/gm, '') // remove single-line comments
.replace(/\/\*[\s\S]*?\*\//g, '') // remove multi-line comments
.replace(/\s+/g, ' ') // normalize whitespace
.replace(/;\s*/g, ';\n'); // separate statements
// Split into individual statements
const statements = cleanedSql
.split('\n')
.map(s => s.trim())
.filter(Boolean);
for (const stmt of statements) {
const upper = stmt.toUpperCase();
if (upper.startsWith('CREATE TABLE')) {
createTableStatements.push(stmt);
} else if (upper.startsWith('ALTER TABLE')) {
alterTableStatements.push(stmt);
} else if (upper.startsWith('CREATE INDEX') || upper.startsWith('CREATE UNIQUE INDEX')) {
createIndexStatements.push(stmt);
} else if (upper.startsWith('CREATE TYPE')) {
createPostgresTypesStatements.push(stmt);
}
}
if (dialect == DatabaseDialect.POSTGRES) {
for (const postgresType of createPostgresTypesStatements) {
try {
const instructionAst = parse(postgresType);
if (Array.isArray(instructionAst) && instructionAst.length > 0) {
postgresTypes.push(instructionAst[0]);
}
} catch (error) {
errors.push(error as Error);
}
}
for (const createTable of createTableStatements) {
try {
const instructionAst = parse(createTable);
if (Array.isArray(instructionAst) && instructionAst.length > 0) {
const table: TableInsertType = postgresAstToTable(instructionAst[0], data_types, postgresTypes);
tables.push(table);
for (const column of (instructionAst[0] as any).columns)
if (column.constraints?.length > 0) {
let referenceColumn: any | undefined = column.constraints.find((contraint: any) => contraint.type == "reference")
if (!referenceColumn)
continue
referenceColumn = {
...referenceColumn, localColumns: [
{ name: column.name.name }
]
}
const relationshipAst: any = foreignKeyConstraintToAlterTableAst([referenceColumn], table);
try {
const newRelationships = postgresAstToRelationship(relationshipAst, tables);
relationships = relationships.concat(newRelationships);
} catch (error) {
errors.push(error as Error);
if ((error as any).relationships && (error as any).relationships.length > 0)
relationships = relationships.concat((error as any).relationships);
}
}
if ((instructionAst[0] as any).constraints && (instructionAst[0] as any).constraints.length > 0) {
const relationshipAst: any = foreignKeyConstraintToAlterTableAst((instructionAst[0] as any).constraints, table)
try {
const newRelationships = postgresAstToRelationship(relationshipAst, tables);
relationships = relationships.concat(newRelationships);
} catch (error) {
errors.push(error as Error);
if ((error as any).relationships && (error as any).relationships.length > 0)
relationships = relationships.concat((error as any).relationships);
}
}
}
} catch (error) {
errors.push(error as Error);
}
}
for (const createIndex of createIndexStatements) {
try {
const instructionAst = parse(createIndex);
if (Array.isArray(instructionAst) && instructionAst.length > 0) {
indices.push(postgresAstToIndex(instructionAst[0], tables));
}
} catch (error) {
errors.push(error as Error);
}
}
for (const alterTable of alterTableStatements) {
try {
const instructionAst = parse(alterTable);
if (Array.isArray(instructionAst) && instructionAst.length > 0) {
const extractedRelationships: RelationshipInsertType[] = postgresAstToRelationship((instructionAst[0] as any), tables);
relationships = relationships.concat(extractedRelationships);
}
} catch (error) {
if ((error as any).relationships && (error as any).relationships.length > 0)
relationships = relationships.concat((error as any).relationships);
}
}
} else {
let foreignKeyConstraints: any[] = [];
let referenceDefinitions: any[] = [];
for (let createTable of createTableStatements) {
try {
if (dialect == DatabaseDialect.SQLITE)
createTable = createTable.replace(/\btext\s*\(\s*\d+\s*\)/gi, 'TEXT');
let instructionAst = parser.astify(createTable, {
database: dialect == DatabaseDialect.MARIADB ? getDatabaseByDialect(DatabaseDialect.MYSQL).name : getDatabaseByDialect(dialect).name
});
if (Array.isArray(instructionAst) && instructionAst.length > 0) {
instructionAst = instructionAst[0];
}
if (instructionAst) {
const table: TableInsertType = astToTable(instructionAst, data_types);
tables.push(table);
const tableForeignKeyConstraints = (instructionAst as any).create_definitions.filter((definition: any) => definition.constraint_type == "FOREIGN KEY");
const tableReferenceDefinitions = (instructionAst as any).create_definitions.filter((definition: any) => definition.resource == "column" && definition.reference_definition);
foreignKeyConstraints = foreignKeyConstraints.concat(
tableForeignKeyConstraints.map((constraint: any) => ({ ...constraint, table: (instructionAst as any).table }))
)
referenceDefinitions = referenceDefinitions.concat(
tableReferenceDefinitions.map((constraint: any) => ({ ...constraint, table: (instructionAst as any).table }))
)
}
} catch (error) {
errors.push(error as Error);
continue;
}
}
for (const foreignKeyConstraint of foreignKeyConstraints) {
try {
relationships.push(astToRelationship(tables, foreignKeyConstraint) as RelationshipInsertType);
} catch (error) {
errors.push(error as Error);
continue;
}
}
for (const referenceDefinition of referenceDefinitions) {
try {
relationships.push(astToRelationship(tables, undefined, referenceDefinition) as RelationshipInsertType);
} catch (error) {
errors.push(error as Error);
continue;
}
}
for (const alterTable of alterTableStatements) {
try {
let instructionAst: any = parser.astify(alterTable, {
database: getDatabaseByDialect(dialect).name
});
if (instructionAst) {
const extractedRelationships: RelationshipInsertType[] = astToRelationship(tables, undefined, undefined, {
...instructionAst[0],
table: instructionAst[0].table?.[0].table
}) as RelationshipInsertType[];
relationships = relationships.concat(extractedRelationships)
}
} catch (error) {
if ((error as any).relationships && (error as any).relationships.length > 0)
relationships = relationships.concat((error as any).relationships);
}
}
for (const createIndex of createIndexStatements) {
try {
let instructionAst = parser.astify(createIndex, {
database: getDatabaseByDialect(dialect).name
});
if (Array.isArray(instructionAst) && instructionAst.length > 0) {
instructionAst = instructionAst[0];
}
indices.push(astToIndex(instructionAst, tables));
} catch (error) {
errors.push(error as Error);
continue;
}
}
}
if (tables.length == 0)
throw Error("Error while Parsing")
return { tables, relationships, indices, errors };
}
const astToForiegnKeyAction = (ast: string): ForeignKeyActions => {
switch (ast) {
case "cascade":
return ForeignKeyActions.CASCADE;
case "set null":
return ForeignKeyActions.SET_NULL;
case 'restrict':
return ForeignKeyActions.RESTRICT;
case "set default":
return ForeignKeyActions.SET_DEFAULT;
}
return ForeignKeyActions.NO_ACTION
}
export const astToRelationship = (tables: TableInsertType[], constraintAst?: any, columnAst?: any, alterTableAst?: any): RelationshipInsertType | RelationshipInsertType[] => {
let targetField: FieldInsertType | undefined;
let sourceTable: TableInsertType | undefined;
let sourceField: FieldInsertType | undefined;
let targetTable: TableInsertType | undefined;
let relationships: RelationshipInsertType[] = [];
let onDelete: ForeignKeyActions | undefined;
let onUpdate: ForeignKeyActions | undefined;
let on_action: any | undefined;
if (constraintAst) {
targetTable = tables.find((table: TableInsertType) => table.name == constraintAst.table?.[0].table);
targetField = targetTable?.fields?.find((field: FieldInsertType) => field.name == constraintAst.definition?.[0].column);
sourceTable = tables.find((table: TableInsertType) => table.name == constraintAst.reference_definition?.table?.[0].table);
sourceField = sourceTable?.fields?.find((field: FieldInsertType) => field.name == constraintAst.reference_definition?.definition?.[0].column);
on_action = constraintAst.reference_definition.on_action;
}
if (columnAst) {
targetTable = tables.find((table: TableInsertType) => table.name == columnAst.table?.[0].table);
targetField = targetTable?.fields?.find((field: FieldInsertType) => field.name == columnAst.column?.column);
sourceTable = tables.find((table: TableInsertType) => table.name == columnAst.reference_definition?.table?.[0].table);
sourceField = sourceTable?.fields?.find((field: FieldInsertType) => field.name == columnAst.reference_definition?.definition?.[0].column);
on_action = columnAst.reference_definition.on_action;
}
if (on_action) {
const onDeleteAst: any | undefined = on_action.find((action: any) => action.type == "on delete");
const onUpdateAst: any | undefined = on_action.find((action: any) => action.type == "on update");
if (onDeleteAst)
onDelete = astToForiegnKeyAction(onDeleteAst.value.value);
if (onUpdateAst)
onUpdate = astToForiegnKeyAction(onUpdateAst.value.value);
}
if (alterTableAst) {
const expressions = alterTableAst.expr;
const foreignKeyExpressions = expressions.filter((expression: any) => expression.resource == "constraint" && expression.create_definitions?.constraint_type == "FOREIGN KEY")
targetTable = tables.find((table: TableInsertType) => table.name == alterTableAst.table);
for (const expression of foreignKeyExpressions) {
const targetField: FieldInsertType | undefined = targetTable?.fields?.find((field: FieldInsertType) => field.name == expression.create_definitions?.definition?.[0].column);
const sourceTable: TableInsertType | undefined = tables.find((table: TableInsertType) => table.name == expression.create_definitions?.reference_definition?.table?.[0].table)
const sourceField: FieldInsertType | undefined = sourceTable?.fields?.find((field: FieldInsertType) => field.name == expression.create_definitions?.reference_definition?.definition?.[0].column);
const on_action: any | undefined = expression.create_definitions?.reference_definition?.on_action;
let onDelete: ForeignKeyActions | undefined;
let onUpdate: ForeignKeyActions | undefined;
if (on_action) {
const onDeleteAst: any | undefined = on_action.find((action: any) => action.type == "on delete");
const onUpdateAst: any | undefined = on_action.find((action: any) => action.type == "on update");
if (onDeleteAst)
onDelete = astToForiegnKeyAction(onDeleteAst.value.value);
if (onUpdateAst)
onUpdate = astToForiegnKeyAction(onUpdateAst.value.value);
}
if (!targetField || !sourceField || !sourceTable)
continue;
relationships.push({
id: v4(),
targetTableId: targetTable?.id,
targetFieldId: targetField.id,
sourceTableId: sourceTable.id,
sourceFieldId: sourceField.id,
cardinality: targetField.unique ? Cardinality.one_to_one : Cardinality.one_to_many,
onDelete,
onUpdate
} as RelationshipInsertType)
}
if (relationships.length == foreignKeyExpressions.length)
return relationships;
else
throw Error({
success: false,
message: "Failed to Extract all relationships",
relationships
} as any)
}
if (!targetField || !sourceField || !sourceTable)
throw Error("Failed to extract relationship");
return {
id: v4(),
targetTableId: targetTable?.id,
targetFieldId: targetField.id,
sourceTableId: sourceTable.id,
sourceFieldId: sourceField.id,
cardinality: targetField.unique ? Cardinality.one_to_one : Cardinality.one_to_many,
onDelete,
onUpdate
} as RelationshipInsertType;
}
const astToTable = (ast: any, data_types: DataType[]): TableInsertType => {
return {
id: v4(),
name: ast.table[0]?.table,
fields: ast.create_definitions.filter((column: any) => column.resource == "column")
.map((fieldAst: any, index: number) => astToField(fieldAst, data_types, index)),
color: randomColor()
} as TableInsertType;
}
export const astToField = (ast: any, data_types: DataType[], sequence: number): FieldInsertType => {
const dataType: DataType | undefined = data_types.find((dataType: DataType) => {
const synonyms: string[] = dataType.synonyms ? JSON.parse(dataType.synonyms) : [];
return dataType.name == ast.definition.dataType?.toLowerCase() || synonyms.includes(ast.definition.dataType?.toLowerCase())
});
let values: string | undefined = undefined;
const modifiers: string[] = dataType?.modifiers ? JSON.parse(dataType.modifiers) : [];
const { length, scale } = ast.definition;
const { character_set, collate: collation } = ast;
let charset: string | undefined;
let collate: string | undefined;
let defaultValue: string | undefined = ast.default_val?.value?.value ? ast.default_val?.value?.value : undefined;
let maxLength: number | null = null;
let precision: number | null = null;
if (modifiers.includes(Modifiers.LENGTH) && length)
maxLength = length;
if (modifiers.includes(Modifiers.PRECISION) && length)
precision = length;
if (modifiers.includes(Modifiers.CHARSET) && character_set)
charset = character_set.value?.value;
if (modifiers.includes(Modifiers.COLLATE) && collation)
collate = collation.collate?.name;
if (modifiers.includes(Modifiers.VALUES)) {
const value = ast.definition?.expr?.value.map((value: any) => value.value);
if (value)
values = JSON.stringify(value);
}
if (dataType?.type == DataTypes.TIME &&
ast.default_val?.value?.type == "function" &&
ast.default_val?.value?.name?.name?.length > 0 &&
ast.default_val?.value?.name?.name[0].value == "CURRENT_TIMESTAMP")
defaultValue = TimeDefaultValues.NOW;
const isPrimary: boolean = ast.primary_key == "primary key";
const nullable: boolean = ast.nullable?.value ? ast.nullable?.value != "not null" : !isPrimary;
return {
id: v4(),
name: ast.column.column,
defaultValue,
typeId: dataType?.id,
nullable,
unique: ast.unique == "unique",
maxLength,
precision,
scale,
sequence,
autoIncrement: ast.auto_increment == "auto_increment",
isPrimary,
values,
charset,
collate,
} as FieldInsertType;
}
export const postgresAstToTable = (ast: any, data_types: DataType[], postgresTypes: any[]): TableInsertType => {
return {
id: v4(),
name: ast.name.name,
fields: ast.columns.filter((column: any) => column.kind == "column")
.map((fieldAst: any, index: number) => postgresAstToField(fieldAst, data_types, index, postgresTypes)),
color: randomColor(),
} as TableInsertType;
}
export const postgresAstToField = (ast: any, data_types: DataType[], sequence: number, postgresTypes: any[]): FieldInsertType => {
let dataType: DataType | undefined = data_types.find((dataType: DataType) => {
const synonyms: string[] = dataType.synonyms ? JSON.parse(dataType.synonyms) : [];
return dataType.name == ast.dataType.name?.toLowerCase() || synonyms.includes(ast.dataType.name?.toLowerCase())
});
let values: string | undefined;
let autoIncrement: boolean = false;
if (!dataType && postgresTypes && postgresTypes.length > 0) {
const postgresType: any = postgresTypes.find((type: any) => type.name.name == ast.dataType.name);
if (postgresType) {
dataType = data_types.find((dataType: DataType) => dataType.name == "enum") as DataType;
values = JSON.stringify(postgresType.values.map((value: any) => value.value));
};
}
if (ast.dataType.name?.toLowerCase().includes("serial")) {
let baseType: string = ast.dataType.name?.toLowerCase() == "serial" ? "integer" : ast.dataType.name?.toLowerCase().replace("serial", "int");
dataType = data_types.find((dataType: DataType) => {
return dataType.name == baseType;
});
autoIncrement = true;
}
const modifiers: string[] = dataType?.modifiers ? JSON.parse(dataType.modifiers) : [];
let length: number | undefined;
let scale: number | undefined;
let unique: boolean = false;
let primaryKey: boolean = false;
if (ast.dataType.config && ast.dataType.config.length > 0) {
if (ast.dataType.config.length >= 1)
length = ast.dataType.config[0];
if (ast.dataType.config.length == 2)
scale = ast.dataType.config[1];
}
let maxLength: number | null = null;
let precision: number | null = null;
if (modifiers.includes(Modifiers.LENGTH) && length)
maxLength = length;
if (modifiers.includes(Modifiers.PRECISION) && length)
precision = length;
let defaultValue: string | undefined;
let nullable: boolean = true;
const constraints: any[] | undefined = ast.constraints;
if (constraints && constraints.length > 0) {
const nullableConstraints: any | undefined = constraints.find((c: any) => c.type == "not null");
const defaultValueConstraints: any | undefined = constraints.find((c: any) => c.type == "default");
if (defaultValueConstraints) {
if (defaultValueConstraints.default.type == "keyword" && defaultValueConstraints.default.keyword == "current_timestamp")
defaultValue = TimeDefaultValues.NOW;
else if (defaultValueConstraints.default.type == "call" && defaultValueConstraints.default.function?.name == "now")
defaultValue = TimeDefaultValues.NOW;
else if (defaultValueConstraints.default.type == "cast" && defaultValueConstraints.default.operand)
defaultValue = String(defaultValueConstraints.default.operand.value)
else
defaultValue = String(defaultValueConstraints.default.value);
}
const uniqueConstraints: any | undefined = constraints.find((c: any) => c.type == "unique");
const primryKeyConstraints: any | undefined = constraints.find((c: any) => c.type == "primary key");
if (uniqueConstraints)
unique = true;
if (primryKeyConstraints)
primaryKey = true;
if (nullableConstraints || primaryKey)
nullable = false;
}
return {
id: v4(),
name: ast.name.name,
defaultValue,
typeId: dataType?.id,
nullable,
unique,
maxLength,
precision,
scale,
isPrimary: primaryKey,
sequence,
values,
autoIncrement
} as FieldInsertType;
}
export const postgresAstToRelationship = (ast: any, tables: TableInsertType[]): RelationshipInsertType[] => {
const relationships: RelationshipInsertType[] = [];
const changes = ast.changes;
const targetTable: TableInsertType | undefined = tables.find((table: TableInsertType) => table.name == ast.table.name);
if (!targetTable)
throw Error("source table not found");
const foreignKeyConstraints = changes.filter((change: any) => change.type == 'add constraint' && change.constraint && change.constraint.type == "foreign key").map((change: any) => change.constraint);
for (const foreignKeyConstraint of foreignKeyConstraints) {
const sourceTable: TableInsertType | undefined = tables.find((table: TableInsertType) => table.name == foreignKeyConstraint.foreignTable.name);
const targetField: FieldInsertType | undefined = targetTable.fields?.find((field: FieldInsertType) => field.name == foreignKeyConstraint.localColumns[0]?.name)
const sourceField: FieldInsertType | undefined = sourceTable?.fields?.find((field: FieldInsertType) => field.name == foreignKeyConstraint.foreignColumns[0]?.name)
const onDelete = foreignKeyConstraint.onDelete ? astToForiegnKeyAction(foreignKeyConstraint.onDelete) : undefined;
const onUpdate = foreignKeyConstraint.onUpdate ? astToForiegnKeyAction(foreignKeyConstraint.onUpdate) : undefined;
if (!sourceField || !targetField || !sourceTable)
continue;
relationships.push({
id: v4(),
sourceTableId: sourceTable.id,
targetTableId: targetTable.id,
sourceFieldId: sourceField.id,
targetFieldId: targetField.id,
cardinality: targetField.unique ? Cardinality.one_to_one : Cardinality.one_to_many,
onDelete, onUpdate
} as RelationshipInsertType)
}
if (relationships.length == foreignKeyConstraints.length)
return relationships;
else
throw Error({
success: false,
message: "Failed to Extract all relationships",
relationships
} as any)
}
const foreignKeyConstraintToAlterTableAst = (constraints: any[], table: TableInsertType) => {
const changes: any[] = constraints.filter((constraint: any) => constraint.type == "foreign key" || constraint.type == "reference").map((constraint: any) => ({
type: "add constraint",
constraint: {
type: "foreign key",
localColumns: [
{
name: constraint.localColumns?.[0].name
}
],
foreignTable: {
name: constraint.foreignTable.name,
},
foreignColumns: [
{
name: constraint.foreignColumns?.[0].name
}
],
onDelete: constraint.onDelete,
onUpdate: constraint.onUpdate,
}
}))
return {
type: "alter table",
only: true,
table: {
name: table.name
},
changes
}
}
export const astToIndex = (ast: any, tables: TableInsertType[]): IndexInsertType => {
const table: TableInsertType | undefined = tables.find((table: TableInsertType) => table.name == ast.table.table);
if (!table)
throw Error("table not found");
const fieldNames: string[] = ast.index_columns.map((column: any) => column.column);
const fieldIds: string[] | undefined = table.fields?.filter((field: FieldInsertType) => fieldNames.includes(field.name))
.map((field: FieldInsertType) => field.id);
return {
id: v4(),
name: ast.index,
tableId: table.id,
unique: ast.index_type == "unique",
fieldIndices: fieldIds?.map((id: string) => ({
id: v4(),
fieldId: id
}))
} as IndexInsertType
}
export const postgresAstToIndex = (ast: any, tables: TableInsertType[]): IndexInsertType => {
const table: TableInsertType | undefined = tables.find((table: TableInsertType) => table.name == ast.table.name);
if (!table)
throw Error("table not found");
const fieldNames: string[] = ast.expressions.map((expression: any) => expression.expression.name);
const fieldIds: string[] | undefined = table.fields?.filter((field: FieldInsertType) => fieldNames.includes(field.name))
.map((field: FieldInsertType) => field.id);
return {
id: v4(),
name: ast.indexName.name,
tableId: table.id,
unique: ast.unique,
fieldIndices: fieldIds?.map((id: string) => ({
id: v4(),
fieldId: id
}))
} as IndexInsertType
}