@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,3590 @@
1
+ import chalk from 'chalk';
2
+ import { getNewTableName } from './cli/commands/sqlitePushUtils';
3
+ import { warning } from './cli/views';
4
+ import { CommonSquashedSchema } from './schemaValidator';
5
+ import { MySqlKitInternals, MySqlSchema, MySqlSquasher, View as MySqlView } from './serializer/mysqlSchema';
6
+ import {
7
+ CheckConstraint,
8
+ Index,
9
+ MatViewWithOption,
10
+ PgSchema,
11
+ PgSquasher,
12
+ Policy,
13
+ Role,
14
+ View as PgView,
15
+ ViewWithOption,
16
+ } from './serializer/pgSchema';
17
+ import { SingleStoreKitInternals, SingleStoreSchema, SingleStoreSquasher } from './serializer/singlestoreSchema';
18
+ import {
19
+ SQLiteKitInternals,
20
+ SQLiteSchemaInternal,
21
+ SQLiteSchemaSquashed,
22
+ SQLiteSquasher,
23
+ View as SqliteView,
24
+ } from './serializer/sqliteSchema';
25
+ import { AlteredColumn, Column, Domain, Sequence, Table } from './snapshotsDiffer';
26
+
27
+ export interface JsonSqliteCreateTableStatement {
28
+ type: 'sqlite_create_table';
29
+ tableName: string;
30
+ columns: Column[];
31
+ referenceData: {
32
+ name: string;
33
+ tableFrom: string;
34
+ columnsFrom: string[];
35
+ tableTo: string;
36
+ columnsTo: string[];
37
+ onUpdate?: string | undefined;
38
+ onDelete?: string | undefined;
39
+ }[];
40
+ compositePKs: string[][];
41
+ uniqueConstraints?: string[];
42
+ checkConstraints?: string[];
43
+ }
44
+
45
+ export interface JsonCreateTableStatement {
46
+ type: 'create_table';
47
+ tableName: string;
48
+ schema: string;
49
+ columns: Column[];
50
+ compositePKs: string[];
51
+ compositePkName?: string;
52
+ uniqueConstraints?: string[];
53
+ policies?: string[];
54
+ checkConstraints?: string[];
55
+ internals?: MySqlKitInternals | SingleStoreKitInternals;
56
+ isRLSEnabled?: boolean;
57
+ }
58
+
59
+ export interface JsonRecreateTableStatement {
60
+ type: 'recreate_table';
61
+ tableName: string;
62
+ columns: Column[];
63
+ referenceData: {
64
+ name: string;
65
+ tableFrom: string;
66
+ columnsFrom: string[];
67
+ tableTo: string;
68
+ columnsTo: string[];
69
+ onUpdate?: string | undefined;
70
+ onDelete?: string | undefined;
71
+ }[];
72
+ compositePKs: string[][];
73
+ uniqueConstraints?: string[];
74
+ checkConstraints: string[];
75
+ }
76
+
77
+ export interface JsonRecreateSingleStoreTableStatement {
78
+ type: 'singlestore_recreate_table';
79
+ tableName: string;
80
+ columns: Column[];
81
+ compositePKs: string[];
82
+ uniqueConstraints?: string[];
83
+ }
84
+
85
+ export interface JsonDropTableStatement {
86
+ type: 'drop_table';
87
+ tableName: string;
88
+ schema: string;
89
+ policies?: string[];
90
+ }
91
+
92
+ export interface JsonRenameTableStatement {
93
+ type: 'rename_table';
94
+ fromSchema: string;
95
+ toSchema: string;
96
+ tableNameFrom: string;
97
+ tableNameTo: string;
98
+ }
99
+
100
+ // Types for Domain-related JSON statements
101
+ export type JsonDomainStatement =
102
+ | JsonCreateDomainStatement
103
+ | JsonDropDomainStatement
104
+ | JsonAlterDomainStatement;
105
+
106
+ export interface JsonCreateDomainStatement {
107
+ type: 'create_domain';
108
+ name: string;
109
+ schema: string;
110
+ notNull?: boolean;
111
+ defaultValue?: string;
112
+ baseType: string;
113
+ checkConstraints?: string[];
114
+ }
115
+
116
+ export interface JsonDropDomainStatement {
117
+ type: 'drop_domain';
118
+ name: string;
119
+ schema: string;
120
+ }
121
+
122
+ export interface JsonAlterDomainStatement {
123
+ type: 'alter_domain';
124
+ action:
125
+ | 'add_constraint'
126
+ | 'drop_constraint'
127
+ | 'set_not_null'
128
+ | 'drop_not_null'
129
+ | 'set_default'
130
+ | 'drop_default';
131
+ name: string;
132
+ schema: string;
133
+ defaultValue?: string;
134
+ checkConstraints?: string[];
135
+ }
136
+
137
+ export interface JsonCreateEnumStatement {
138
+ type: 'create_type_enum';
139
+ name: string;
140
+ schema: string;
141
+ values: string[];
142
+ }
143
+
144
+ export interface JsonDropEnumStatement {
145
+ type: 'drop_type_enum';
146
+ name: string;
147
+ schema: string;
148
+ }
149
+
150
+ export interface JsonMoveEnumStatement {
151
+ type: 'move_type_enum';
152
+ name: string;
153
+ schemaFrom: string;
154
+ schemaTo: string;
155
+ }
156
+
157
+ export interface JsonRenameEnumStatement {
158
+ type: 'rename_type_enum';
159
+ nameFrom: string;
160
+ nameTo: string;
161
+ schema: string;
162
+ }
163
+
164
+ export interface JsonAddValueToEnumStatement {
165
+ type: 'alter_type_add_value';
166
+ name: string;
167
+ schema: string;
168
+ value: string;
169
+ before: string;
170
+ }
171
+
172
+ //////
173
+
174
+ export interface JsonCreateRoleStatement {
175
+ type: 'create_role';
176
+ name: string;
177
+ values: {
178
+ inherit?: boolean;
179
+ createDb?: boolean;
180
+ createRole?: boolean;
181
+ };
182
+ }
183
+
184
+ export interface JsonDropRoleStatement {
185
+ type: 'drop_role';
186
+ name: string;
187
+ }
188
+ export interface JsonRenameRoleStatement {
189
+ type: 'rename_role';
190
+ nameFrom: string;
191
+ nameTo: string;
192
+ }
193
+
194
+ export interface JsonAlterRoleStatement {
195
+ type: 'alter_role';
196
+ name: string;
197
+ values: {
198
+ inherit?: boolean;
199
+ createDb?: boolean;
200
+ createRole?: boolean;
201
+ };
202
+ }
203
+
204
+ //////
205
+
206
+ export interface JsonDropValueFromEnumStatement {
207
+ type: 'alter_type_drop_value';
208
+ name: string;
209
+ schema: string;
210
+ deletedValues: string[];
211
+ newValues: string[];
212
+ columnsWithEnum: { schema: string; table: string; column: string }[];
213
+ }
214
+
215
+ export interface JsonCreateSequenceStatement {
216
+ type: 'create_sequence';
217
+ name: string;
218
+ schema: string;
219
+ values: {
220
+ increment?: string | undefined;
221
+ minValue?: string | undefined;
222
+ maxValue?: string | undefined;
223
+ startWith?: string | undefined;
224
+ cache?: string | undefined;
225
+ cycle?: boolean | undefined;
226
+ };
227
+ }
228
+
229
+ export interface JsonDropSequenceStatement {
230
+ type: 'drop_sequence';
231
+ name: string;
232
+ schema: string;
233
+ }
234
+
235
+ export interface JsonMoveSequenceStatement {
236
+ type: 'move_sequence';
237
+ name: string;
238
+ schemaFrom: string;
239
+ schemaTo: string;
240
+ }
241
+
242
+ export interface JsonRenameSequenceStatement {
243
+ type: 'rename_sequence';
244
+ nameFrom: string;
245
+ nameTo: string;
246
+ schema: string;
247
+ }
248
+
249
+ export interface JsonAlterSequenceStatement {
250
+ type: 'alter_sequence';
251
+ name: string;
252
+ schema: string;
253
+ values: {
254
+ increment?: string | undefined;
255
+ minValue?: string | undefined;
256
+ maxValue?: string | undefined;
257
+ startWith?: string | undefined;
258
+ cache?: string | undefined;
259
+ cycle?: boolean | undefined;
260
+ };
261
+ }
262
+
263
+ export interface JsonDropColumnStatement {
264
+ type: 'alter_table_drop_column';
265
+ tableName: string;
266
+ columnName: string;
267
+ schema: string;
268
+ }
269
+
270
+ export interface JsonAddColumnStatement {
271
+ type: 'alter_table_add_column';
272
+ tableName: string;
273
+ column: Column;
274
+ schema: string;
275
+ }
276
+
277
+ export interface JsonSqliteAddColumnStatement {
278
+ type: 'sqlite_alter_table_add_column';
279
+ tableName: string;
280
+ column: Column;
281
+ referenceData?: string;
282
+ }
283
+
284
+ export interface JsonCreatePolicyStatement {
285
+ type: 'create_policy';
286
+ tableName: string;
287
+ data: Policy;
288
+ schema: string;
289
+ }
290
+
291
+ export interface JsonCreateIndPolicyStatement {
292
+ type: 'create_ind_policy';
293
+ tableName: string;
294
+ data: Policy;
295
+ }
296
+
297
+ export interface JsonDropPolicyStatement {
298
+ type: 'drop_policy';
299
+ tableName: string;
300
+ data: Policy;
301
+ schema: string;
302
+ }
303
+
304
+ export interface JsonDropIndPolicyStatement {
305
+ type: 'drop_ind_policy';
306
+ tableName: string;
307
+ data: Policy;
308
+ }
309
+
310
+ export interface JsonRenamePolicyStatement {
311
+ type: 'rename_policy';
312
+ tableName: string;
313
+ oldName: string;
314
+ newName: string;
315
+ schema: string;
316
+ }
317
+
318
+ export interface JsonIndRenamePolicyStatement {
319
+ type: 'rename_ind_policy';
320
+ tableKey: string;
321
+ oldName: string;
322
+ newName: string;
323
+ }
324
+
325
+ export interface JsonEnableRLSStatement {
326
+ type: 'enable_rls';
327
+ tableName: string;
328
+ schema: string;
329
+ }
330
+
331
+ export interface JsonDisableRLSStatement {
332
+ type: 'disable_rls';
333
+ tableName: string;
334
+ schema: string;
335
+ }
336
+
337
+ export interface JsonAlterPolicyStatement {
338
+ type: 'alter_policy';
339
+ tableName: string;
340
+ oldData: string;
341
+ newData: string;
342
+ schema: string;
343
+ }
344
+
345
+ export interface JsonAlterIndPolicyStatement {
346
+ type: 'alter_ind_policy';
347
+ oldData: Policy;
348
+ newData: Policy;
349
+ }
350
+
351
+ export interface JsonCreateIndexStatement {
352
+ type: 'create_index';
353
+ tableName: string;
354
+ data: string;
355
+ schema: string;
356
+ internal?: MySqlKitInternals | SQLiteKitInternals | SingleStoreKitInternals;
357
+ }
358
+
359
+ export interface JsonPgCreateIndexStatement {
360
+ type: 'create_index_pg';
361
+ tableName: string;
362
+ data: Index;
363
+ schema: string;
364
+ }
365
+
366
+ export interface JsonReferenceStatement {
367
+ type: 'create_reference' | 'alter_reference' | 'delete_reference';
368
+ data: string;
369
+ schema: string;
370
+ tableName: string;
371
+ isMulticolumn?: boolean;
372
+ columnNotNull?: boolean;
373
+ columnDefault?: string;
374
+ columnType?: string;
375
+ // fromTable: string;
376
+ // fromColumns: string[];
377
+ // toTable: string;
378
+ // toColumns: string[];
379
+ // foreignKeyName: string;
380
+ // onDelete?: string;
381
+ // onUpdate?: string;
382
+ }
383
+
384
+ export interface JsonCreateUniqueConstraint {
385
+ type: 'create_unique_constraint';
386
+ tableName: string;
387
+ data: string;
388
+ schema?: string;
389
+ constraintName?: string;
390
+ }
391
+
392
+ export interface JsonDeleteUniqueConstraint {
393
+ type: 'delete_unique_constraint';
394
+ tableName: string;
395
+ data: string;
396
+ schema?: string;
397
+ constraintName?: string;
398
+ }
399
+
400
+ export interface JsonAlterUniqueConstraint {
401
+ type: 'alter_unique_constraint';
402
+ tableName: string;
403
+ old: string;
404
+ new: string;
405
+ schema?: string;
406
+ oldConstraintName?: string;
407
+ newConstraintName?: string;
408
+ }
409
+
410
+ export interface JsonCreateCheckConstraint {
411
+ type: 'create_check_constraint';
412
+ tableName: string;
413
+ data: string;
414
+ schema?: string;
415
+ }
416
+
417
+ export interface JsonDeleteCheckConstraint {
418
+ type: 'delete_check_constraint';
419
+ tableName: string;
420
+ constraintName: string;
421
+ schema?: string;
422
+ }
423
+
424
+ export interface JsonCreateCompositePK {
425
+ type: 'create_composite_pk';
426
+ tableName: string;
427
+ data: string;
428
+ schema?: string;
429
+ constraintName?: string;
430
+ }
431
+
432
+ export interface JsonDeleteCompositePK {
433
+ type: 'delete_composite_pk';
434
+ tableName: string;
435
+ data: string;
436
+ schema?: string;
437
+ constraintName?: string;
438
+ }
439
+
440
+ export interface JsonAlterCompositePK {
441
+ type: 'alter_composite_pk';
442
+ tableName: string;
443
+ old: string;
444
+ new: string;
445
+ schema?: string;
446
+ oldConstraintName?: string;
447
+ newConstraintName?: string;
448
+ }
449
+
450
+ export interface JsonAlterTableSetSchema {
451
+ type: 'alter_table_set_schema';
452
+ tableName: string;
453
+ schemaFrom: string;
454
+ schemaTo: string;
455
+ }
456
+
457
+ export interface JsonAlterTableRemoveFromSchema {
458
+ type: 'alter_table_remove_from_schema';
459
+ tableName: string;
460
+ schema: string;
461
+ }
462
+
463
+ export interface JsonAlterTableSetNewSchema {
464
+ type: 'alter_table_set_new_schema';
465
+ tableName: string;
466
+ from: string;
467
+ to: string;
468
+ }
469
+
470
+ export interface JsonCreateReferenceStatement extends JsonReferenceStatement {
471
+ type: 'create_reference';
472
+ }
473
+
474
+ export interface JsonAlterReferenceStatement extends JsonReferenceStatement {
475
+ type: 'alter_reference';
476
+ oldFkey: string;
477
+ }
478
+
479
+ export interface JsonDeleteReferenceStatement extends JsonReferenceStatement {
480
+ type: 'delete_reference';
481
+ }
482
+
483
+ export interface JsonDropIndexStatement {
484
+ type: 'drop_index';
485
+ tableName: string;
486
+ data: string;
487
+ schema: string;
488
+ }
489
+
490
+ export interface JsonRenameColumnStatement {
491
+ type: 'alter_table_rename_column';
492
+ tableName: string;
493
+ oldColumnName: string;
494
+ newColumnName: string;
495
+ schema: string;
496
+ }
497
+
498
+ export interface JsonAlterColumnTypeStatement {
499
+ type: 'alter_table_alter_column_set_type';
500
+ tableName: string;
501
+ columnName: string;
502
+ newDataType: string;
503
+ oldDataType: string;
504
+ schema: string;
505
+ columnDefault: string;
506
+ columnOnUpdate: boolean;
507
+ columnNotNull: boolean;
508
+ columnAutoIncrement: boolean;
509
+ columnPk: boolean;
510
+ columnGenerated?: { as: string; type: 'stored' | 'virtual' };
511
+ }
512
+
513
+ export interface JsonAlterColumnSetPrimaryKeyStatement {
514
+ type: 'alter_table_alter_column_set_pk';
515
+ tableName: string;
516
+ schema: string;
517
+ columnName: string;
518
+ }
519
+
520
+ export interface JsonAlterColumnDropPrimaryKeyStatement {
521
+ type: 'alter_table_alter_column_drop_pk';
522
+ tableName: string;
523
+ columnName: string;
524
+ schema: string;
525
+ }
526
+
527
+ export interface JsonAlterColumnSetDefaultStatement {
528
+ type: 'alter_table_alter_column_set_default';
529
+ tableName: string;
530
+ columnName: string;
531
+ newDefaultValue: any;
532
+ oldDefaultValue?: any;
533
+ schema: string;
534
+ newDataType: string;
535
+ columnOnUpdate: boolean;
536
+ columnNotNull: boolean;
537
+ columnAutoIncrement: boolean;
538
+ columnPk: boolean;
539
+ }
540
+
541
+ export interface JsonAlterColumnDropDefaultStatement {
542
+ type: 'alter_table_alter_column_drop_default';
543
+ tableName: string;
544
+ columnName: string;
545
+ schema: string;
546
+ newDataType: string;
547
+ columnDefault: string;
548
+ columnOnUpdate: boolean;
549
+ columnNotNull: boolean;
550
+ columnAutoIncrement: boolean;
551
+ columnPk: boolean;
552
+ }
553
+
554
+ export interface JsonAlterColumnSetNotNullStatement {
555
+ type: 'alter_table_alter_column_set_notnull';
556
+ tableName: string;
557
+ columnName: string;
558
+ schema: string;
559
+ newDataType: string;
560
+ columnDefault: string;
561
+ columnOnUpdate: boolean;
562
+ columnNotNull: boolean;
563
+ columnAutoIncrement: boolean;
564
+ columnPk: boolean;
565
+ }
566
+
567
+ export interface JsonAlterColumnDropNotNullStatement {
568
+ type: 'alter_table_alter_column_drop_notnull';
569
+ tableName: string;
570
+ columnName: string;
571
+ schema: string;
572
+ newDataType: string;
573
+ columnDefault: string;
574
+ columnOnUpdate: boolean;
575
+ columnNotNull: boolean;
576
+ columnAutoIncrement: boolean;
577
+ columnPk: boolean;
578
+ }
579
+
580
+ export interface JsonAlterColumnSetGeneratedStatement {
581
+ type: 'alter_table_alter_column_set_generated';
582
+ tableName: string;
583
+ columnName: string;
584
+ schema: string;
585
+ newDataType: string;
586
+ columnDefault: string;
587
+ columnOnUpdate: boolean;
588
+ columnNotNull: boolean;
589
+ columnAutoIncrement: boolean;
590
+ columnPk: boolean;
591
+ columnGenerated?: { as: string; type: 'stored' | 'virtual' };
592
+ }
593
+ export interface JsonAlterColumnSetIdentityStatement {
594
+ type: 'alter_table_alter_column_set_identity';
595
+ tableName: string;
596
+ columnName: string;
597
+ schema: string;
598
+ identity: string;
599
+ }
600
+
601
+ export interface JsonAlterColumnDropIdentityStatement {
602
+ type: 'alter_table_alter_column_drop_identity';
603
+ tableName: string;
604
+ columnName: string;
605
+ schema: string;
606
+ }
607
+
608
+ export interface JsonAlterColumnAlterIdentityStatement {
609
+ type: 'alter_table_alter_column_change_identity';
610
+ tableName: string;
611
+ columnName: string;
612
+ schema: string;
613
+ identity: string;
614
+ oldIdentity: string;
615
+ }
616
+
617
+ export interface JsonAlterColumnDropGeneratedStatement {
618
+ type: 'alter_table_alter_column_drop_generated';
619
+ tableName: string;
620
+ columnName: string;
621
+ schema: string;
622
+ newDataType: string;
623
+ columnDefault: string;
624
+ columnOnUpdate: boolean;
625
+ columnNotNull: boolean;
626
+ columnAutoIncrement: boolean;
627
+ columnPk: boolean;
628
+ columnGenerated?: { as: string; type: 'stored' | 'virtual' };
629
+ oldColumn?: Column;
630
+ }
631
+
632
+ export interface JsonAlterColumnAlterGeneratedStatement {
633
+ type: 'alter_table_alter_column_alter_generated';
634
+ tableName: string;
635
+ columnName: string;
636
+ schema: string;
637
+ newDataType: string;
638
+ columnDefault: string;
639
+ columnOnUpdate: boolean;
640
+ columnNotNull: boolean;
641
+ columnAutoIncrement: boolean;
642
+ columnPk: boolean;
643
+ columnGenerated?: { as: string; type: 'stored' | 'virtual' };
644
+ }
645
+
646
+ export interface JsonAlterColumnSetOnUpdateStatement {
647
+ type: 'alter_table_alter_column_set_on_update';
648
+ tableName: string;
649
+ columnName: string;
650
+ schema: string;
651
+ newDataType: string;
652
+ columnDefault: string;
653
+ columnOnUpdate: boolean;
654
+ columnNotNull: boolean;
655
+ columnAutoIncrement: boolean;
656
+ columnPk: boolean;
657
+ }
658
+
659
+ export interface JsonAlterColumnDropOnUpdateStatement {
660
+ type: 'alter_table_alter_column_drop_on_update';
661
+ tableName: string;
662
+ columnName: string;
663
+ schema: string;
664
+ newDataType: string;
665
+ columnDefault: string;
666
+ columnOnUpdate: boolean;
667
+ columnNotNull: boolean;
668
+ columnAutoIncrement: boolean;
669
+ columnPk: boolean;
670
+ }
671
+
672
+ export interface JsonAlterColumnSetAutoincrementStatement {
673
+ type: 'alter_table_alter_column_set_autoincrement';
674
+ tableName: string;
675
+ columnName: string;
676
+ schema: string;
677
+ newDataType: string;
678
+ columnDefault: string;
679
+ columnOnUpdate: boolean;
680
+ columnNotNull: boolean;
681
+ columnAutoIncrement: boolean;
682
+ columnPk: boolean;
683
+ }
684
+
685
+ export interface JsonAlterColumnDropAutoincrementStatement {
686
+ type: 'alter_table_alter_column_drop_autoincrement';
687
+ tableName: string;
688
+ columnName: string;
689
+ schema: string;
690
+ newDataType: string;
691
+ columnDefault: string;
692
+ columnOnUpdate: boolean;
693
+ columnNotNull: boolean;
694
+ columnAutoIncrement: boolean;
695
+ columnPk: boolean;
696
+ }
697
+
698
+ export interface JsonCreateSchema {
699
+ type: 'create_schema';
700
+ name: string;
701
+ }
702
+
703
+ export interface JsonDropSchema {
704
+ type: 'drop_schema';
705
+ name: string;
706
+ }
707
+
708
+ export interface JsonRenameSchema {
709
+ type: 'rename_schema';
710
+ from: string;
711
+ to: string;
712
+ }
713
+
714
+ export type JsonCreatePgViewStatement = {
715
+ type: 'create_view';
716
+ } & Omit<PgView, 'columns' | 'isExisting'>;
717
+
718
+ export type JsonCreateMySqlViewStatement = {
719
+ type: 'mysql_create_view';
720
+ replace: boolean;
721
+ } & Omit<MySqlView, 'columns' | 'isExisting'>;
722
+
723
+ /* export type JsonCreateSingleStoreViewStatement = {
724
+ type: 'singlestore_create_view';
725
+ replace: boolean;
726
+ } & Omit<SingleStoreView, 'columns' | 'isExisting'>; */
727
+
728
+ export type JsonCreateSqliteViewStatement = {
729
+ type: 'sqlite_create_view';
730
+ } & Omit<SqliteView, 'columns' | 'isExisting'>;
731
+
732
+ export interface JsonDropViewStatement {
733
+ type: 'drop_view';
734
+ name: string;
735
+ schema?: string;
736
+ materialized?: boolean;
737
+ }
738
+
739
+ export interface JsonRenameViewStatement {
740
+ type: 'rename_view';
741
+ nameTo: string;
742
+ nameFrom: string;
743
+ schema: string;
744
+ materialized?: boolean;
745
+ }
746
+
747
+ export interface JsonRenameMySqlViewStatement {
748
+ type: 'rename_view';
749
+ nameTo: string;
750
+ nameFrom: string;
751
+ schema: string;
752
+ materialized?: boolean;
753
+ }
754
+
755
+ export interface JsonAlterViewAlterSchemaStatement {
756
+ type: 'alter_view_alter_schema';
757
+ fromSchema: string;
758
+ toSchema: string;
759
+ name: string;
760
+ materialized?: boolean;
761
+ }
762
+
763
+ export type JsonAlterViewAddWithOptionStatement =
764
+ & {
765
+ type: 'alter_view_add_with_option';
766
+ schema: string;
767
+ name: string;
768
+ }
769
+ & ({
770
+ materialized: true;
771
+ with: MatViewWithOption;
772
+ } | {
773
+ materialized: false;
774
+ with: ViewWithOption;
775
+ });
776
+
777
+ export type JsonAlterViewDropWithOptionStatement =
778
+ & {
779
+ type: 'alter_view_drop_with_option';
780
+ schema: string;
781
+ name: string;
782
+ }
783
+ & ({
784
+ materialized: true;
785
+ with: MatViewWithOption;
786
+ } | {
787
+ materialized: false;
788
+ with: ViewWithOption;
789
+ });
790
+
791
+ export interface JsonAlterViewAlterTablespaceStatement {
792
+ type: 'alter_view_alter_tablespace';
793
+ toTablespace: string;
794
+ name: string;
795
+ schema: string;
796
+ materialized: true;
797
+ }
798
+
799
+ export interface JsonAlterViewAlterUsingStatement {
800
+ type: 'alter_view_alter_using';
801
+ toUsing: string;
802
+ name: string;
803
+ schema: string;
804
+ materialized: true;
805
+ }
806
+
807
+ export type JsonAlterMySqlViewStatement = {
808
+ type: 'alter_mysql_view';
809
+ } & Omit<MySqlView, 'isExisting'>;
810
+
811
+ /* export type JsonAlterSingleStoreViewStatement = {
812
+ type: 'alter_singlestore_view';
813
+ } & Omit<SingleStoreView, 'isExisting'>; */
814
+
815
+ export type JsonAlterViewStatement =
816
+ | JsonAlterViewAlterSchemaStatement
817
+ | JsonAlterViewAddWithOptionStatement
818
+ | JsonAlterViewDropWithOptionStatement
819
+ | JsonAlterViewAlterTablespaceStatement
820
+ | JsonAlterViewAlterUsingStatement;
821
+
822
+ export type JsonAlterColumnStatement =
823
+ | JsonRenameColumnStatement
824
+ | JsonAlterColumnTypeStatement
825
+ | JsonAlterColumnSetDefaultStatement
826
+ | JsonAlterColumnDropDefaultStatement
827
+ | JsonAlterColumnSetNotNullStatement
828
+ | JsonAlterColumnDropNotNullStatement
829
+ | JsonAlterColumnDropOnUpdateStatement
830
+ | JsonAlterColumnSetOnUpdateStatement
831
+ | JsonAlterColumnDropAutoincrementStatement
832
+ | JsonAlterColumnSetAutoincrementStatement
833
+ | JsonAlterColumnSetPrimaryKeyStatement
834
+ | JsonAlterColumnDropPrimaryKeyStatement
835
+ | JsonAlterColumnSetGeneratedStatement
836
+ | JsonAlterColumnDropGeneratedStatement
837
+ | JsonAlterColumnAlterGeneratedStatement
838
+ | JsonAlterColumnSetIdentityStatement
839
+ | JsonAlterColumnAlterIdentityStatement
840
+ | JsonAlterColumnDropIdentityStatement;
841
+
842
+ export type JsonStatement =
843
+ | JsonRecreateSingleStoreTableStatement
844
+ | JsonRecreateTableStatement
845
+ | JsonAlterColumnStatement
846
+ | JsonCreateTableStatement
847
+ | JsonDropTableStatement
848
+ | JsonRenameTableStatement
849
+ | JsonCreateEnumStatement
850
+ | JsonDropEnumStatement
851
+ | JsonMoveEnumStatement
852
+ | JsonRenameEnumStatement
853
+ | JsonAddValueToEnumStatement
854
+ | JsonDropColumnStatement
855
+ | JsonAddColumnStatement
856
+ | JsonCreateIndexStatement
857
+ | JsonCreateReferenceStatement
858
+ | JsonAlterReferenceStatement
859
+ | JsonDeleteReferenceStatement
860
+ | JsonDropIndexStatement
861
+ | JsonReferenceStatement
862
+ | JsonSqliteCreateTableStatement
863
+ | JsonSqliteAddColumnStatement
864
+ | JsonCreateCompositePK
865
+ | JsonDeleteCompositePK
866
+ | JsonAlterCompositePK
867
+ | JsonCreateUniqueConstraint
868
+ | JsonDeleteUniqueConstraint
869
+ | JsonAlterUniqueConstraint
870
+ | JsonCreateSchema
871
+ | JsonDropSchema
872
+ | JsonRenameSchema
873
+ | JsonAlterTableSetSchema
874
+ | JsonAlterTableRemoveFromSchema
875
+ | JsonAlterTableSetNewSchema
876
+ | JsonPgCreateIndexStatement
877
+ | JsonAlterSequenceStatement
878
+ | JsonDropSequenceStatement
879
+ | JsonCreateSequenceStatement
880
+ | JsonMoveSequenceStatement
881
+ | JsonRenameSequenceStatement
882
+ | JsonDropPolicyStatement
883
+ | JsonCreatePolicyStatement
884
+ | JsonAlterPolicyStatement
885
+ | JsonRenamePolicyStatement
886
+ | JsonEnableRLSStatement
887
+ | JsonDisableRLSStatement
888
+ | JsonRenameRoleStatement
889
+ | JsonCreateRoleStatement
890
+ | JsonDropRoleStatement
891
+ | JsonAlterRoleStatement
892
+ | JsonCreatePgViewStatement
893
+ | JsonDropViewStatement
894
+ | JsonRenameViewStatement
895
+ | JsonAlterViewStatement
896
+ | JsonCreateMySqlViewStatement
897
+ | JsonAlterMySqlViewStatement
898
+ /* | JsonCreateSingleStoreViewStatement
899
+ | JsonAlterSingleStoreViewStatement */
900
+ | JsonCreateSqliteViewStatement
901
+ | JsonCreateCheckConstraint
902
+ | JsonDeleteCheckConstraint
903
+ | JsonDropValueFromEnumStatement
904
+ | JsonIndRenamePolicyStatement
905
+ | JsonDropIndPolicyStatement
906
+ | JsonCreateIndPolicyStatement
907
+ | JsonAlterIndPolicyStatement
908
+ | JsonCreateDomainStatement
909
+ | JsonDropDomainStatement
910
+ | JsonAlterDomainStatement;
911
+
912
+ export const preparePgCreateTableJson = (
913
+ table: Table,
914
+ // TODO: remove?
915
+ json2: PgSchema,
916
+ ): JsonCreateTableStatement => {
917
+ const { name, schema, columns, compositePrimaryKeys, uniqueConstraints, checkConstraints, policies, isRLSEnabled } =
918
+ table;
919
+ const tableKey = `${schema || 'public'}.${name}`;
920
+
921
+ // TODO: @AndriiSherman. We need this, will add test cases
922
+ const compositePkName = Object.values(compositePrimaryKeys).length > 0
923
+ ? json2.tables[tableKey].compositePrimaryKeys[
924
+ `${PgSquasher.unsquashPK(Object.values(compositePrimaryKeys)[0]).name}`
925
+ ].name
926
+ : '';
927
+
928
+ return {
929
+ type: 'create_table',
930
+ tableName: name,
931
+ schema,
932
+ columns: Object.values(columns),
933
+ compositePKs: Object.values(compositePrimaryKeys),
934
+ compositePkName: compositePkName,
935
+ uniqueConstraints: Object.values(uniqueConstraints),
936
+ policies: Object.values(policies),
937
+ checkConstraints: Object.values(checkConstraints),
938
+ isRLSEnabled: isRLSEnabled ?? false,
939
+ };
940
+ };
941
+
942
+ export const prepareMySqlCreateTableJson = (
943
+ table: Table,
944
+ // TODO: remove?
945
+ json2: MySqlSchema,
946
+ // we need it to know if some of the indexes(and in future other parts) are expressions or columns
947
+ // didn't change mysqlserialaizer, because it will break snapshots and diffs and it's hard to detect
948
+ // if previously it was an expression or column
949
+ internals: MySqlKitInternals,
950
+ ): JsonCreateTableStatement => {
951
+ const { name, schema, columns, compositePrimaryKeys, uniqueConstraints, checkConstraints } = table;
952
+
953
+ return {
954
+ type: 'create_table',
955
+ tableName: name,
956
+ schema,
957
+ columns: Object.values(columns),
958
+ compositePKs: Object.values(compositePrimaryKeys),
959
+ compositePkName: Object.values(compositePrimaryKeys).length > 0
960
+ ? json2.tables[name].compositePrimaryKeys[
961
+ MySqlSquasher.unsquashPK(Object.values(compositePrimaryKeys)[0])
962
+ .name
963
+ ].name
964
+ : '',
965
+ uniqueConstraints: Object.values(uniqueConstraints),
966
+ internals,
967
+ checkConstraints: Object.values(checkConstraints),
968
+ };
969
+ };
970
+
971
+ export const prepareSingleStoreCreateTableJson = (
972
+ table: Table,
973
+ // TODO: remove?
974
+ json2: SingleStoreSchema,
975
+ // we need it to know if some of the indexes(and in future other parts) are expressions or columns
976
+ // didn't change singlestoreserialaizer, because it will break snapshots and diffs and it's hard to detect
977
+ // if previously it was an expression or column
978
+ internals: SingleStoreKitInternals,
979
+ ): JsonCreateTableStatement => {
980
+ const { name, schema, columns, compositePrimaryKeys, uniqueConstraints } = table;
981
+
982
+ return {
983
+ type: 'create_table',
984
+ tableName: name,
985
+ schema,
986
+ columns: Object.values(columns),
987
+ compositePKs: Object.values(compositePrimaryKeys),
988
+ compositePkName: Object.values(compositePrimaryKeys).length > 0
989
+ ? json2.tables[name].compositePrimaryKeys[
990
+ SingleStoreSquasher.unsquashPK(Object.values(compositePrimaryKeys)[0])
991
+ .name
992
+ ].name
993
+ : '',
994
+ uniqueConstraints: Object.values(uniqueConstraints),
995
+ internals,
996
+ };
997
+ };
998
+
999
+ export const prepareSQLiteCreateTable = (
1000
+ table: Table,
1001
+ action?: 'push' | undefined,
1002
+ ): JsonSqliteCreateTableStatement => {
1003
+ const { name, columns, uniqueConstraints, checkConstraints } = table;
1004
+
1005
+ const references: string[] = Object.values(table.foreignKeys);
1006
+
1007
+ const composites: string[][] = Object.values(table.compositePrimaryKeys).map(
1008
+ (it) => SQLiteSquasher.unsquashPK(it),
1009
+ );
1010
+
1011
+ const fks = references.map((it) =>
1012
+ action === 'push'
1013
+ ? SQLiteSquasher.unsquashPushFK(it)
1014
+ : SQLiteSquasher.unsquashFK(it)
1015
+ );
1016
+
1017
+ return {
1018
+ type: 'sqlite_create_table',
1019
+ tableName: name,
1020
+ columns: Object.values(columns),
1021
+ referenceData: fks,
1022
+ compositePKs: composites,
1023
+ uniqueConstraints: Object.values(uniqueConstraints),
1024
+ checkConstraints: Object.values(checkConstraints),
1025
+ };
1026
+ };
1027
+
1028
+ export const prepareDropTableJson = (table: Table): JsonDropTableStatement => {
1029
+ return {
1030
+ type: 'drop_table',
1031
+ tableName: table.name,
1032
+ schema: table.schema,
1033
+ policies: table.policies ? Object.values(table.policies) : [],
1034
+ };
1035
+ };
1036
+
1037
+ export const prepareRenameTableJson = (
1038
+ tableFrom: Table,
1039
+ tableTo: Table,
1040
+ ): JsonRenameTableStatement => {
1041
+ return {
1042
+ type: 'rename_table',
1043
+ fromSchema: tableTo.schema,
1044
+ toSchema: tableTo.schema,
1045
+ tableNameFrom: tableFrom.name,
1046
+ tableNameTo: tableTo.name,
1047
+ };
1048
+ };
1049
+
1050
+ // Helper function to create domain statements
1051
+ export const prepareDomainJson = (
1052
+ domain: Domain,
1053
+ type: 'create' | 'alter' | 'drop',
1054
+ action?: JsonAlterDomainStatement['action'],
1055
+ ): JsonDomainStatement => {
1056
+ let checkConstraints;
1057
+ if (domain.checkConstraints) {
1058
+ checkConstraints = Object.values(domain.checkConstraints);
1059
+ }
1060
+
1061
+ if (type === 'create') {
1062
+ return {
1063
+ type: 'create_domain',
1064
+ name: domain.name,
1065
+ schema: domain.schema,
1066
+ notNull: domain.notNull,
1067
+ defaultValue: domain.defaultValue,
1068
+ baseType: domain.baseType,
1069
+ checkConstraints,
1070
+ };
1071
+ } else if (type === 'drop') {
1072
+ return {
1073
+ type: 'drop_domain',
1074
+ name: domain.name,
1075
+ schema: domain.schema,
1076
+ };
1077
+ }
1078
+
1079
+ // For alter actions
1080
+ return {
1081
+ type: 'alter_domain',
1082
+ action: action!,
1083
+ name: domain.name,
1084
+ schema: domain.schema,
1085
+ defaultValue: domain.defaultValue,
1086
+ checkConstraints,
1087
+ };
1088
+ };
1089
+
1090
+ export const prepareCreateEnumJson = (
1091
+ name: string,
1092
+ schema: string,
1093
+ values: string[],
1094
+ ): JsonCreateEnumStatement => {
1095
+ return {
1096
+ type: 'create_type_enum',
1097
+ name: name,
1098
+ schema: schema,
1099
+ values,
1100
+ };
1101
+ };
1102
+
1103
+ // https://blog.yo1.dog/updating-enum-values-in-postgresql-the-safe-and-easy-way/
1104
+ export const prepareAddValuesToEnumJson = (
1105
+ name: string,
1106
+ schema: string,
1107
+ values: { value: string; before: string }[],
1108
+ ): JsonAddValueToEnumStatement[] => {
1109
+ return values.map((it) => {
1110
+ return {
1111
+ type: 'alter_type_add_value',
1112
+ name: name,
1113
+ schema: schema,
1114
+ value: it.value,
1115
+ before: it.before,
1116
+ };
1117
+ });
1118
+ };
1119
+
1120
+ export const prepareDropEnumValues = (
1121
+ name: string,
1122
+ schema: string,
1123
+ removedValues: string[],
1124
+ json2: PgSchema,
1125
+ ): JsonDropValueFromEnumStatement[] => {
1126
+ if (!removedValues.length) return [];
1127
+
1128
+ const affectedColumns: { schema: string; table: string; column: string }[] = [];
1129
+
1130
+ for (const tableKey in json2.tables) {
1131
+ const table = json2.tables[tableKey];
1132
+ for (const columnKey in table.columns) {
1133
+ const column = table.columns[columnKey];
1134
+ if (column.type === name && column.typeSchema === schema) {
1135
+ affectedColumns.push({ schema: table.schema || 'public', table: table.name, column: column.name });
1136
+ }
1137
+ }
1138
+ }
1139
+
1140
+ return [{
1141
+ type: 'alter_type_drop_value',
1142
+ name: name,
1143
+ schema: schema,
1144
+ deletedValues: removedValues,
1145
+ newValues: json2.enums[`${schema}.${name}`].values,
1146
+ columnsWithEnum: affectedColumns,
1147
+ }];
1148
+ };
1149
+
1150
+ export const prepareDropEnumJson = (
1151
+ name: string,
1152
+ schema: string,
1153
+ ): JsonDropEnumStatement => {
1154
+ return {
1155
+ type: 'drop_type_enum',
1156
+ name: name,
1157
+ schema: schema,
1158
+ };
1159
+ };
1160
+
1161
+ export const prepareMoveEnumJson = (
1162
+ name: string,
1163
+ schemaFrom: string,
1164
+ schemaTo: string,
1165
+ ): JsonMoveEnumStatement => {
1166
+ return {
1167
+ type: 'move_type_enum',
1168
+ name: name,
1169
+ schemaFrom,
1170
+ schemaTo,
1171
+ };
1172
+ };
1173
+
1174
+ export const prepareRenameEnumJson = (
1175
+ nameFrom: string,
1176
+ nameTo: string,
1177
+ schema: string,
1178
+ ): JsonRenameEnumStatement => {
1179
+ return {
1180
+ type: 'rename_type_enum',
1181
+ nameFrom,
1182
+ nameTo,
1183
+ schema,
1184
+ };
1185
+ };
1186
+
1187
+ ////////////
1188
+
1189
+ export const prepareCreateSequenceJson = (
1190
+ seq: Sequence,
1191
+ ): JsonCreateSequenceStatement => {
1192
+ const values = PgSquasher.unsquashSequence(seq.values);
1193
+ return {
1194
+ type: 'create_sequence',
1195
+ name: seq.name,
1196
+ schema: seq.schema,
1197
+ values,
1198
+ };
1199
+ };
1200
+
1201
+ export const prepareAlterSequenceJson = (
1202
+ seq: Sequence,
1203
+ ): JsonAlterSequenceStatement[] => {
1204
+ const values = PgSquasher.unsquashSequence(seq.values);
1205
+ return [
1206
+ {
1207
+ type: 'alter_sequence',
1208
+ schema: seq.schema,
1209
+ name: seq.name,
1210
+ values,
1211
+ },
1212
+ ];
1213
+ };
1214
+
1215
+ export const prepareDropSequenceJson = (
1216
+ name: string,
1217
+ schema: string,
1218
+ ): JsonDropSequenceStatement => {
1219
+ return {
1220
+ type: 'drop_sequence',
1221
+ name: name,
1222
+ schema: schema,
1223
+ };
1224
+ };
1225
+
1226
+ export const prepareMoveSequenceJson = (
1227
+ name: string,
1228
+ schemaFrom: string,
1229
+ schemaTo: string,
1230
+ ): JsonMoveSequenceStatement => {
1231
+ return {
1232
+ type: 'move_sequence',
1233
+ name: name,
1234
+ schemaFrom,
1235
+ schemaTo,
1236
+ };
1237
+ };
1238
+
1239
+ export const prepareRenameSequenceJson = (
1240
+ nameFrom: string,
1241
+ nameTo: string,
1242
+ schema: string,
1243
+ ): JsonRenameSequenceStatement => {
1244
+ return {
1245
+ type: 'rename_sequence',
1246
+ nameFrom,
1247
+ nameTo,
1248
+ schema,
1249
+ };
1250
+ };
1251
+
1252
+ ////////////
1253
+
1254
+ export const prepareCreateRoleJson = (
1255
+ role: Role,
1256
+ ): JsonCreateRoleStatement => {
1257
+ return {
1258
+ type: 'create_role',
1259
+ name: role.name,
1260
+ values: {
1261
+ createDb: role.createDb,
1262
+ createRole: role.createRole,
1263
+ inherit: role.inherit,
1264
+ },
1265
+ };
1266
+ };
1267
+
1268
+ export const prepareAlterRoleJson = (
1269
+ role: Role,
1270
+ ): JsonAlterRoleStatement => {
1271
+ return {
1272
+ type: 'alter_role',
1273
+ name: role.name,
1274
+ values: {
1275
+ createDb: role.createDb,
1276
+ createRole: role.createRole,
1277
+ inherit: role.inherit,
1278
+ },
1279
+ };
1280
+ };
1281
+
1282
+ export const prepareDropRoleJson = (
1283
+ name: string,
1284
+ ): JsonDropRoleStatement => {
1285
+ return {
1286
+ type: 'drop_role',
1287
+ name: name,
1288
+ };
1289
+ };
1290
+
1291
+ export const prepareRenameRoleJson = (
1292
+ nameFrom: string,
1293
+ nameTo: string,
1294
+ ): JsonRenameRoleStatement => {
1295
+ return {
1296
+ type: 'rename_role',
1297
+ nameFrom,
1298
+ nameTo,
1299
+ };
1300
+ };
1301
+
1302
+ //////////
1303
+
1304
+ export const prepareCreateSchemasJson = (
1305
+ values: string[],
1306
+ ): JsonCreateSchema[] => {
1307
+ return values.map((it) => {
1308
+ return {
1309
+ type: 'create_schema',
1310
+ name: it,
1311
+ } as JsonCreateSchema;
1312
+ });
1313
+ };
1314
+
1315
+ export const prepareRenameSchemasJson = (
1316
+ values: { from: string; to: string }[],
1317
+ ): JsonRenameSchema[] => {
1318
+ return values.map((it) => {
1319
+ return {
1320
+ type: 'rename_schema',
1321
+ from: it.from,
1322
+ to: it.to,
1323
+ } as JsonRenameSchema;
1324
+ });
1325
+ };
1326
+
1327
+ export const prepareDeleteSchemasJson = (
1328
+ values: string[],
1329
+ ): JsonDropSchema[] => {
1330
+ return values.map((it) => {
1331
+ return {
1332
+ type: 'drop_schema',
1333
+ name: it,
1334
+ } as JsonDropSchema;
1335
+ });
1336
+ };
1337
+
1338
+ export const prepareRenameColumns = (
1339
+ tableName: string,
1340
+ // TODO: split for pg and mysql+sqlite and singlestore without schema
1341
+ schema: string,
1342
+ pairs: { from: Column; to: Column }[],
1343
+ ): JsonRenameColumnStatement[] => {
1344
+ return pairs.map((it) => {
1345
+ return {
1346
+ type: 'alter_table_rename_column',
1347
+ tableName: tableName,
1348
+ oldColumnName: it.from.name,
1349
+ newColumnName: it.to.name,
1350
+ schema,
1351
+ };
1352
+ });
1353
+ };
1354
+
1355
+ export const _prepareDropColumns = (
1356
+ taleName: string,
1357
+ schema: string,
1358
+ columns: Column[],
1359
+ ): JsonDropColumnStatement[] => {
1360
+ return columns.map((it) => {
1361
+ return {
1362
+ type: 'alter_table_drop_column',
1363
+ tableName: taleName,
1364
+ columnName: it.name,
1365
+ schema,
1366
+ };
1367
+ });
1368
+ };
1369
+
1370
+ export const _prepareAddColumns = (
1371
+ tableName: string,
1372
+ schema: string,
1373
+ columns: Column[],
1374
+ ): JsonAddColumnStatement[] => {
1375
+ return columns.map((it) => {
1376
+ return {
1377
+ type: 'alter_table_add_column',
1378
+ tableName: tableName,
1379
+ column: it,
1380
+ schema,
1381
+ };
1382
+ });
1383
+ };
1384
+
1385
+ export const _prepareSqliteAddColumns = (
1386
+ tableName: string,
1387
+ columns: Column[],
1388
+ referenceData: string[],
1389
+ ): JsonSqliteAddColumnStatement[] => {
1390
+ const unsquashed = referenceData.map((addedFkValue) => SQLiteSquasher.unsquashFK(addedFkValue));
1391
+
1392
+ return columns
1393
+ .map((it) => {
1394
+ const columnsWithReference = unsquashed.find((t) => t.columnsFrom.includes(it.name));
1395
+
1396
+ if (it.generated?.type === 'stored') {
1397
+ warning(
1398
+ `As SQLite docs mention: "It is not possible to ALTER TABLE ADD COLUMN a STORED column. One can add a VIRTUAL column, however", source: "https://www.sqlite.org/gencol.html"`,
1399
+ );
1400
+ return undefined;
1401
+ }
1402
+
1403
+ return {
1404
+ type: 'sqlite_alter_table_add_column',
1405
+ tableName: tableName,
1406
+ column: it,
1407
+ referenceData: columnsWithReference
1408
+ ? SQLiteSquasher.squashFK(columnsWithReference)
1409
+ : undefined,
1410
+ };
1411
+ })
1412
+ .filter(Boolean) as JsonSqliteAddColumnStatement[];
1413
+ };
1414
+
1415
+ export const prepareAlterColumnsMysql = (
1416
+ tableName: string,
1417
+ schema: string,
1418
+ columns: AlteredColumn[],
1419
+ // TODO: remove?
1420
+ json1: CommonSquashedSchema,
1421
+ json2: CommonSquashedSchema,
1422
+ action?: 'push' | undefined,
1423
+ ): JsonAlterColumnStatement[] => {
1424
+ let statements: JsonAlterColumnStatement[] = [];
1425
+ let dropPkStatements: JsonAlterColumnDropPrimaryKeyStatement[] = [];
1426
+ let setPkStatements: JsonAlterColumnSetPrimaryKeyStatement[] = [];
1427
+
1428
+ for (const column of columns) {
1429
+ const columnName = typeof column.name !== 'string' ? column.name.new : column.name;
1430
+
1431
+ const table = json2.tables[tableName];
1432
+ const snapshotColumn = table.columns[columnName];
1433
+
1434
+ const columnType = snapshotColumn.type;
1435
+ const columnDefault = snapshotColumn.default;
1436
+ const columnOnUpdate = 'onUpdate' in snapshotColumn ? snapshotColumn.onUpdate : undefined;
1437
+ const columnNotNull = table.columns[columnName].notNull;
1438
+
1439
+ const columnAutoIncrement = 'autoincrement' in snapshotColumn
1440
+ ? snapshotColumn.autoincrement ?? false
1441
+ : false;
1442
+
1443
+ const columnPk = table.columns[columnName].primaryKey;
1444
+
1445
+ if (column.autoincrement?.type === 'added') {
1446
+ statements.push({
1447
+ type: 'alter_table_alter_column_set_autoincrement',
1448
+ tableName,
1449
+ columnName,
1450
+ schema,
1451
+ newDataType: columnType,
1452
+ columnDefault,
1453
+ columnOnUpdate,
1454
+ columnNotNull,
1455
+ columnAutoIncrement,
1456
+ columnPk,
1457
+ });
1458
+ }
1459
+
1460
+ if (column.autoincrement?.type === 'changed') {
1461
+ const type = column.autoincrement.new
1462
+ ? 'alter_table_alter_column_set_autoincrement'
1463
+ : 'alter_table_alter_column_drop_autoincrement';
1464
+
1465
+ statements.push({
1466
+ type,
1467
+ tableName,
1468
+ columnName,
1469
+ schema,
1470
+ newDataType: columnType,
1471
+ columnDefault,
1472
+ columnOnUpdate,
1473
+ columnNotNull,
1474
+ columnAutoIncrement,
1475
+ columnPk,
1476
+ });
1477
+ }
1478
+
1479
+ if (column.autoincrement?.type === 'deleted') {
1480
+ statements.push({
1481
+ type: 'alter_table_alter_column_drop_autoincrement',
1482
+ tableName,
1483
+ columnName,
1484
+ schema,
1485
+ newDataType: columnType,
1486
+ columnDefault,
1487
+ columnOnUpdate,
1488
+ columnNotNull,
1489
+ columnAutoIncrement,
1490
+ columnPk,
1491
+ });
1492
+ }
1493
+ }
1494
+
1495
+ for (const column of columns) {
1496
+ const columnName = typeof column.name !== 'string' ? column.name.new : column.name;
1497
+
1498
+ // I used any, because those fields are available only for mysql dialect
1499
+ // For other dialects it will become undefined, that is fine for json statements
1500
+ const columnType = json2.tables[tableName].columns[columnName].type;
1501
+ const columnDefault = json2.tables[tableName].columns[columnName].default;
1502
+ const columnGenerated = json2.tables[tableName].columns[columnName].generated;
1503
+ const columnOnUpdate = (json2.tables[tableName].columns[columnName] as any)
1504
+ .onUpdate;
1505
+ const columnNotNull = json2.tables[tableName].columns[columnName].notNull;
1506
+ const columnAutoIncrement = (
1507
+ json2.tables[tableName].columns[columnName] as any
1508
+ ).autoincrement;
1509
+ const columnPk = (json2.tables[tableName].columns[columnName] as any)
1510
+ .primaryKey;
1511
+
1512
+ const compositePk = json2.tables[tableName].compositePrimaryKeys[
1513
+ `${tableName}_${columnName}`
1514
+ ];
1515
+
1516
+ if (typeof column.name !== 'string') {
1517
+ statements.push({
1518
+ type: 'alter_table_rename_column',
1519
+ tableName,
1520
+ oldColumnName: column.name.old,
1521
+ newColumnName: column.name.new,
1522
+ schema,
1523
+ });
1524
+ }
1525
+
1526
+ if (column.type?.type === 'changed') {
1527
+ statements.push({
1528
+ type: 'alter_table_alter_column_set_type',
1529
+ tableName,
1530
+ columnName,
1531
+ newDataType: column.type.new,
1532
+ oldDataType: column.type.old,
1533
+ schema,
1534
+ columnDefault,
1535
+ columnOnUpdate,
1536
+ columnNotNull,
1537
+ columnAutoIncrement,
1538
+ columnPk,
1539
+ columnGenerated,
1540
+ });
1541
+ }
1542
+
1543
+ if (
1544
+ column.primaryKey?.type === 'deleted'
1545
+ || (column.primaryKey?.type === 'changed'
1546
+ && !column.primaryKey.new
1547
+ && typeof compositePk === 'undefined')
1548
+ ) {
1549
+ dropPkStatements.push({
1550
+ ////
1551
+ type: 'alter_table_alter_column_drop_pk',
1552
+ tableName,
1553
+ columnName,
1554
+ schema,
1555
+ });
1556
+ }
1557
+
1558
+ if (column.default?.type === 'added') {
1559
+ statements.push({
1560
+ type: 'alter_table_alter_column_set_default',
1561
+ tableName,
1562
+ columnName,
1563
+ newDefaultValue: column.default.value,
1564
+ schema,
1565
+ columnOnUpdate,
1566
+ columnNotNull,
1567
+ columnAutoIncrement,
1568
+ newDataType: columnType,
1569
+ columnPk,
1570
+ });
1571
+ }
1572
+
1573
+ if (column.default?.type === 'changed') {
1574
+ statements.push({
1575
+ type: 'alter_table_alter_column_set_default',
1576
+ tableName,
1577
+ columnName,
1578
+ newDefaultValue: column.default.new,
1579
+ oldDefaultValue: column.default.old,
1580
+ schema,
1581
+ columnOnUpdate,
1582
+ columnNotNull,
1583
+ columnAutoIncrement,
1584
+ newDataType: columnType,
1585
+ columnPk,
1586
+ });
1587
+ }
1588
+
1589
+ if (column.default?.type === 'deleted') {
1590
+ statements.push({
1591
+ type: 'alter_table_alter_column_drop_default',
1592
+ tableName,
1593
+ columnName,
1594
+ schema,
1595
+ columnDefault,
1596
+ columnOnUpdate,
1597
+ columnNotNull,
1598
+ columnAutoIncrement,
1599
+ newDataType: columnType,
1600
+ columnPk,
1601
+ });
1602
+ }
1603
+
1604
+ if (column.notNull?.type === 'added') {
1605
+ statements.push({
1606
+ type: 'alter_table_alter_column_set_notnull',
1607
+ tableName,
1608
+ columnName,
1609
+ schema,
1610
+ newDataType: columnType,
1611
+ columnDefault,
1612
+ columnOnUpdate,
1613
+ columnNotNull,
1614
+ columnAutoIncrement,
1615
+ columnPk,
1616
+ });
1617
+ }
1618
+
1619
+ if (column.notNull?.type === 'changed') {
1620
+ const type = column.notNull.new
1621
+ ? 'alter_table_alter_column_set_notnull'
1622
+ : 'alter_table_alter_column_drop_notnull';
1623
+ statements.push({
1624
+ type: type,
1625
+ tableName,
1626
+ columnName,
1627
+ schema,
1628
+ newDataType: columnType,
1629
+ columnDefault,
1630
+ columnOnUpdate,
1631
+ columnNotNull,
1632
+ columnAutoIncrement,
1633
+ columnPk,
1634
+ });
1635
+ }
1636
+
1637
+ if (column.notNull?.type === 'deleted') {
1638
+ statements.push({
1639
+ type: 'alter_table_alter_column_drop_notnull',
1640
+ tableName,
1641
+ columnName,
1642
+ schema,
1643
+ newDataType: columnType,
1644
+ columnDefault,
1645
+ columnOnUpdate,
1646
+ columnNotNull,
1647
+ columnAutoIncrement,
1648
+ columnPk,
1649
+ });
1650
+ }
1651
+
1652
+ if (column.generated?.type === 'added') {
1653
+ if (columnGenerated?.type === 'virtual') {
1654
+ warning(
1655
+ `You are trying to add virtual generated constraint to ${
1656
+ chalk.blue(
1657
+ columnName,
1658
+ )
1659
+ } column. As MySQL docs mention: "Nongenerated columns can be altered to stored but not virtual generated columns". We will drop an existing column and add it with a virtual generated statement. This means that the data previously stored in this column will be wiped, and new data will be generated on each read for this column\n`,
1660
+ );
1661
+ }
1662
+ statements.push({
1663
+ type: 'alter_table_alter_column_set_generated',
1664
+ tableName,
1665
+ columnName,
1666
+ schema,
1667
+ newDataType: columnType,
1668
+ columnDefault,
1669
+ columnOnUpdate,
1670
+ columnNotNull,
1671
+ columnAutoIncrement,
1672
+ columnPk,
1673
+ columnGenerated,
1674
+ });
1675
+ }
1676
+
1677
+ if (column.generated?.type === 'changed' && action !== 'push') {
1678
+ statements.push({
1679
+ type: 'alter_table_alter_column_alter_generated',
1680
+ tableName,
1681
+ columnName,
1682
+ schema,
1683
+ newDataType: columnType,
1684
+ columnDefault,
1685
+ columnOnUpdate,
1686
+ columnNotNull,
1687
+ columnAutoIncrement,
1688
+ columnPk,
1689
+ columnGenerated,
1690
+ });
1691
+ }
1692
+
1693
+ if (column.generated?.type === 'deleted') {
1694
+ if (columnGenerated?.type === 'virtual') {
1695
+ warning(
1696
+ `You are trying to remove virtual generated constraint from ${
1697
+ chalk.blue(
1698
+ columnName,
1699
+ )
1700
+ } column. As MySQL docs mention: "Stored but not virtual generated columns can be altered to nongenerated columns. The stored generated values become the values of the nongenerated column". We will drop an existing column and add it without a virtual generated statement. This means that this column will have no data after migration\n`,
1701
+ );
1702
+ }
1703
+ statements.push({
1704
+ type: 'alter_table_alter_column_drop_generated',
1705
+ tableName,
1706
+ columnName,
1707
+ schema,
1708
+ newDataType: columnType,
1709
+ columnDefault,
1710
+ columnOnUpdate,
1711
+ columnNotNull,
1712
+ columnAutoIncrement,
1713
+ columnPk,
1714
+ columnGenerated,
1715
+ oldColumn: json1.tables[tableName].columns[columnName],
1716
+ });
1717
+ }
1718
+
1719
+ if (
1720
+ column.primaryKey?.type === 'added'
1721
+ || (column.primaryKey?.type === 'changed' && column.primaryKey.new)
1722
+ ) {
1723
+ const wasAutoincrement = statements.filter(
1724
+ (it) => it.type === 'alter_table_alter_column_set_autoincrement',
1725
+ );
1726
+ if (wasAutoincrement.length === 0) {
1727
+ setPkStatements.push({
1728
+ type: 'alter_table_alter_column_set_pk',
1729
+ tableName,
1730
+ schema,
1731
+ columnName,
1732
+ });
1733
+ }
1734
+ }
1735
+
1736
+ if (column.onUpdate?.type === 'added') {
1737
+ statements.push({
1738
+ type: 'alter_table_alter_column_set_on_update',
1739
+ tableName,
1740
+ columnName,
1741
+ schema,
1742
+ newDataType: columnType,
1743
+ columnDefault,
1744
+ columnOnUpdate,
1745
+ columnNotNull,
1746
+ columnAutoIncrement,
1747
+ columnPk,
1748
+ });
1749
+ }
1750
+
1751
+ if (column.onUpdate?.type === 'deleted') {
1752
+ statements.push({
1753
+ type: 'alter_table_alter_column_drop_on_update',
1754
+ tableName,
1755
+ columnName,
1756
+ schema,
1757
+ newDataType: columnType,
1758
+ columnDefault,
1759
+ columnOnUpdate,
1760
+ columnNotNull,
1761
+ columnAutoIncrement,
1762
+ columnPk,
1763
+ });
1764
+ }
1765
+ }
1766
+
1767
+ return [...dropPkStatements, ...setPkStatements, ...statements];
1768
+ };
1769
+
1770
+ export const prepareAlterColumnsSingleStore = (
1771
+ tableName: string,
1772
+ schema: string,
1773
+ columns: AlteredColumn[],
1774
+ // TODO: remove?
1775
+ json1: CommonSquashedSchema,
1776
+ json2: CommonSquashedSchema,
1777
+ action?: 'push' | undefined,
1778
+ ): JsonAlterColumnStatement[] => {
1779
+ let statements: JsonAlterColumnStatement[] = [];
1780
+ let dropPkStatements: JsonAlterColumnDropPrimaryKeyStatement[] = [];
1781
+ let setPkStatements: JsonAlterColumnSetPrimaryKeyStatement[] = [];
1782
+
1783
+ for (const column of columns) {
1784
+ const columnName = typeof column.name !== 'string' ? column.name.new : column.name;
1785
+
1786
+ const table = json2.tables[tableName];
1787
+ const snapshotColumn = table.columns[columnName];
1788
+
1789
+ const columnType = snapshotColumn.type;
1790
+ const columnDefault = snapshotColumn.default;
1791
+ const columnOnUpdate = 'onUpdate' in snapshotColumn ? snapshotColumn.onUpdate : undefined;
1792
+ const columnNotNull = table.columns[columnName].notNull;
1793
+
1794
+ const columnAutoIncrement = 'autoincrement' in snapshotColumn
1795
+ ? snapshotColumn.autoincrement ?? false
1796
+ : false;
1797
+
1798
+ const columnPk = table.columns[columnName].primaryKey;
1799
+
1800
+ if (column.autoincrement?.type === 'added') {
1801
+ statements.push({
1802
+ type: 'alter_table_alter_column_set_autoincrement',
1803
+ tableName,
1804
+ columnName,
1805
+ schema,
1806
+ newDataType: columnType,
1807
+ columnDefault,
1808
+ columnOnUpdate,
1809
+ columnNotNull,
1810
+ columnAutoIncrement,
1811
+ columnPk,
1812
+ });
1813
+ }
1814
+
1815
+ if (column.autoincrement?.type === 'changed') {
1816
+ const type = column.autoincrement.new
1817
+ ? 'alter_table_alter_column_set_autoincrement'
1818
+ : 'alter_table_alter_column_drop_autoincrement';
1819
+
1820
+ statements.push({
1821
+ type,
1822
+ tableName,
1823
+ columnName,
1824
+ schema,
1825
+ newDataType: columnType,
1826
+ columnDefault,
1827
+ columnOnUpdate,
1828
+ columnNotNull,
1829
+ columnAutoIncrement,
1830
+ columnPk,
1831
+ });
1832
+ }
1833
+
1834
+ if (column.autoincrement?.type === 'deleted') {
1835
+ statements.push({
1836
+ type: 'alter_table_alter_column_drop_autoincrement',
1837
+ tableName,
1838
+ columnName,
1839
+ schema,
1840
+ newDataType: columnType,
1841
+ columnDefault,
1842
+ columnOnUpdate,
1843
+ columnNotNull,
1844
+ columnAutoIncrement,
1845
+ columnPk,
1846
+ });
1847
+ }
1848
+ }
1849
+
1850
+ for (const column of columns) {
1851
+ const columnName = typeof column.name !== 'string' ? column.name.new : column.name;
1852
+
1853
+ // I used any, because those fields are available only for mysql and singlestore dialect
1854
+ // For other dialects it will become undefined, that is fine for json statements
1855
+ const columnType = json2.tables[tableName].columns[columnName].type;
1856
+ const columnDefault = json2.tables[tableName].columns[columnName].default;
1857
+ const columnGenerated = json2.tables[tableName].columns[columnName].generated;
1858
+ const columnOnUpdate = (json2.tables[tableName].columns[columnName] as any)
1859
+ .onUpdate;
1860
+ const columnNotNull = json2.tables[tableName].columns[columnName].notNull;
1861
+ const columnAutoIncrement = (
1862
+ json2.tables[tableName].columns[columnName] as any
1863
+ ).autoincrement;
1864
+ const columnPk = (json2.tables[tableName].columns[columnName] as any)
1865
+ .primaryKey;
1866
+
1867
+ const compositePk = json2.tables[tableName].compositePrimaryKeys[
1868
+ `${tableName}_${columnName}`
1869
+ ];
1870
+
1871
+ if (typeof column.name !== 'string') {
1872
+ statements.push({
1873
+ type: 'alter_table_rename_column',
1874
+ tableName,
1875
+ oldColumnName: column.name.old,
1876
+ newColumnName: column.name.new,
1877
+ schema,
1878
+ });
1879
+ }
1880
+
1881
+ if (column.type?.type === 'changed') {
1882
+ statements.push({
1883
+ type: 'alter_table_alter_column_set_type',
1884
+ tableName,
1885
+ columnName,
1886
+ newDataType: column.type.new,
1887
+ oldDataType: column.type.old,
1888
+ schema,
1889
+ columnDefault,
1890
+ columnOnUpdate,
1891
+ columnNotNull,
1892
+ columnAutoIncrement,
1893
+ columnPk,
1894
+ columnGenerated,
1895
+ });
1896
+ }
1897
+
1898
+ if (
1899
+ column.primaryKey?.type === 'deleted'
1900
+ || (column.primaryKey?.type === 'changed'
1901
+ && !column.primaryKey.new
1902
+ && typeof compositePk === 'undefined')
1903
+ ) {
1904
+ dropPkStatements.push({
1905
+ ////
1906
+ type: 'alter_table_alter_column_drop_pk',
1907
+ tableName,
1908
+ columnName,
1909
+ schema,
1910
+ });
1911
+ }
1912
+
1913
+ if (column.default?.type === 'added') {
1914
+ statements.push({
1915
+ type: 'alter_table_alter_column_set_default',
1916
+ tableName,
1917
+ columnName,
1918
+ newDefaultValue: column.default.value,
1919
+ schema,
1920
+ columnOnUpdate,
1921
+ columnNotNull,
1922
+ columnAutoIncrement,
1923
+ newDataType: columnType,
1924
+ columnPk,
1925
+ });
1926
+ }
1927
+
1928
+ if (column.default?.type === 'changed') {
1929
+ statements.push({
1930
+ type: 'alter_table_alter_column_set_default',
1931
+ tableName,
1932
+ columnName,
1933
+ newDefaultValue: column.default.new,
1934
+ oldDefaultValue: column.default.old,
1935
+ schema,
1936
+ columnOnUpdate,
1937
+ columnNotNull,
1938
+ columnAutoIncrement,
1939
+ newDataType: columnType,
1940
+ columnPk,
1941
+ });
1942
+ }
1943
+
1944
+ if (column.default?.type === 'deleted') {
1945
+ statements.push({
1946
+ type: 'alter_table_alter_column_drop_default',
1947
+ tableName,
1948
+ columnName,
1949
+ schema,
1950
+ columnDefault,
1951
+ columnOnUpdate,
1952
+ columnNotNull,
1953
+ columnAutoIncrement,
1954
+ newDataType: columnType,
1955
+ columnPk,
1956
+ });
1957
+ }
1958
+
1959
+ if (column.notNull?.type === 'added') {
1960
+ statements.push({
1961
+ type: 'alter_table_alter_column_set_notnull',
1962
+ tableName,
1963
+ columnName,
1964
+ schema,
1965
+ newDataType: columnType,
1966
+ columnDefault,
1967
+ columnOnUpdate,
1968
+ columnNotNull,
1969
+ columnAutoIncrement,
1970
+ columnPk,
1971
+ });
1972
+ }
1973
+
1974
+ if (column.notNull?.type === 'changed') {
1975
+ const type = column.notNull.new
1976
+ ? 'alter_table_alter_column_set_notnull'
1977
+ : 'alter_table_alter_column_drop_notnull';
1978
+ statements.push({
1979
+ type: type,
1980
+ tableName,
1981
+ columnName,
1982
+ schema,
1983
+ newDataType: columnType,
1984
+ columnDefault,
1985
+ columnOnUpdate,
1986
+ columnNotNull,
1987
+ columnAutoIncrement,
1988
+ columnPk,
1989
+ });
1990
+ }
1991
+
1992
+ if (column.notNull?.type === 'deleted') {
1993
+ statements.push({
1994
+ type: 'alter_table_alter_column_drop_notnull',
1995
+ tableName,
1996
+ columnName,
1997
+ schema,
1998
+ newDataType: columnType,
1999
+ columnDefault,
2000
+ columnOnUpdate,
2001
+ columnNotNull,
2002
+ columnAutoIncrement,
2003
+ columnPk,
2004
+ });
2005
+ }
2006
+
2007
+ if (column.generated?.type === 'added') {
2008
+ if (columnGenerated?.type === 'virtual') {
2009
+ // TODO: Change warning message according to SingleStore docs
2010
+ warning(
2011
+ `You are trying to add virtual generated constraint to ${
2012
+ chalk.blue(
2013
+ columnName,
2014
+ )
2015
+ } column. As MySQL docs mention: "Nongenerated columns can be altered to stored but not virtual generated columns". We will drop an existing column and add it with a virtual generated statement. This means that the data previously stored in this column will be wiped, and new data will be generated on each read for this column\n`,
2016
+ );
2017
+ }
2018
+ statements.push({
2019
+ type: 'alter_table_alter_column_set_generated',
2020
+ tableName,
2021
+ columnName,
2022
+ schema,
2023
+ newDataType: columnType,
2024
+ columnDefault,
2025
+ columnOnUpdate,
2026
+ columnNotNull,
2027
+ columnAutoIncrement,
2028
+ columnPk,
2029
+ columnGenerated,
2030
+ });
2031
+ }
2032
+
2033
+ if (column.generated?.type === 'changed' && action !== 'push') {
2034
+ statements.push({
2035
+ type: 'alter_table_alter_column_alter_generated',
2036
+ tableName,
2037
+ columnName,
2038
+ schema,
2039
+ newDataType: columnType,
2040
+ columnDefault,
2041
+ columnOnUpdate,
2042
+ columnNotNull,
2043
+ columnAutoIncrement,
2044
+ columnPk,
2045
+ columnGenerated,
2046
+ });
2047
+ }
2048
+
2049
+ if (column.generated?.type === 'deleted') {
2050
+ if (columnGenerated?.type === 'virtual') {
2051
+ // TODO: Change warning message according to SingleStore docs
2052
+ warning(
2053
+ `You are trying to remove virtual generated constraint from ${
2054
+ chalk.blue(
2055
+ columnName,
2056
+ )
2057
+ } column. As MySQL docs mention: "Stored but not virtual generated columns can be altered to nongenerated columns. The stored generated values become the values of the nongenerated column". We will drop an existing column and add it without a virtual generated statement. This means that this column will have no data after migration\n`,
2058
+ );
2059
+ }
2060
+ statements.push({
2061
+ type: 'alter_table_alter_column_drop_generated',
2062
+ tableName,
2063
+ columnName,
2064
+ schema,
2065
+ newDataType: columnType,
2066
+ columnDefault,
2067
+ columnOnUpdate,
2068
+ columnNotNull,
2069
+ columnAutoIncrement,
2070
+ columnPk,
2071
+ columnGenerated,
2072
+ oldColumn: json1.tables[tableName].columns[columnName],
2073
+ });
2074
+ }
2075
+
2076
+ if (
2077
+ column.primaryKey?.type === 'added'
2078
+ || (column.primaryKey?.type === 'changed' && column.primaryKey.new)
2079
+ ) {
2080
+ const wasAutoincrement = statements.filter(
2081
+ (it) => it.type === 'alter_table_alter_column_set_autoincrement',
2082
+ );
2083
+ if (wasAutoincrement.length === 0) {
2084
+ setPkStatements.push({
2085
+ type: 'alter_table_alter_column_set_pk',
2086
+ tableName,
2087
+ schema,
2088
+ columnName,
2089
+ });
2090
+ }
2091
+ }
2092
+
2093
+ if (column.onUpdate?.type === 'added') {
2094
+ statements.push({
2095
+ type: 'alter_table_alter_column_set_on_update',
2096
+ tableName,
2097
+ columnName,
2098
+ schema,
2099
+ newDataType: columnType,
2100
+ columnDefault,
2101
+ columnOnUpdate,
2102
+ columnNotNull,
2103
+ columnAutoIncrement,
2104
+ columnPk,
2105
+ });
2106
+ }
2107
+
2108
+ if (column.onUpdate?.type === 'deleted') {
2109
+ statements.push({
2110
+ type: 'alter_table_alter_column_drop_on_update',
2111
+ tableName,
2112
+ columnName,
2113
+ schema,
2114
+ newDataType: columnType,
2115
+ columnDefault,
2116
+ columnOnUpdate,
2117
+ columnNotNull,
2118
+ columnAutoIncrement,
2119
+ columnPk,
2120
+ });
2121
+ }
2122
+ }
2123
+
2124
+ return [...dropPkStatements, ...setPkStatements, ...statements];
2125
+ };
2126
+
2127
+ export const preparePgAlterColumns = (
2128
+ _tableName: string,
2129
+ schema: string,
2130
+ columns: AlteredColumn[],
2131
+ // TODO: remove?
2132
+ json2: CommonSquashedSchema,
2133
+ action?: 'push' | undefined,
2134
+ ): JsonAlterColumnStatement[] => {
2135
+ const tableKey = `${schema || 'public'}.${_tableName}`;
2136
+ let statements: JsonAlterColumnStatement[] = [];
2137
+ let dropPkStatements: JsonAlterColumnDropPrimaryKeyStatement[] = [];
2138
+ let setPkStatements: JsonAlterColumnSetPrimaryKeyStatement[] = [];
2139
+
2140
+ for (const column of columns) {
2141
+ const columnName = typeof column.name !== 'string' ? column.name.new : column.name;
2142
+
2143
+ const tableName = json2.tables[tableKey].name;
2144
+
2145
+ // I used any, because those fields are available only for mysql dialect
2146
+ // For other dialects it will become undefined, that is fine for json statements
2147
+ const columnType = json2.tables[tableKey].columns[columnName].type;
2148
+ const columnDefault = json2.tables[tableKey].columns[columnName].default;
2149
+ const columnGenerated = json2.tables[tableKey].columns[columnName].generated;
2150
+ const columnOnUpdate = (json2.tables[tableKey].columns[columnName] as any)
2151
+ .onUpdate;
2152
+ const columnNotNull = json2.tables[tableKey].columns[columnName].notNull;
2153
+ const columnAutoIncrement = (
2154
+ json2.tables[tableKey].columns[columnName] as any
2155
+ ).autoincrement;
2156
+ const columnPk = (json2.tables[tableKey].columns[columnName] as any)
2157
+ .primaryKey;
2158
+
2159
+ const compositePk = json2.tables[tableKey].compositePrimaryKeys[`${tableName}_${columnName}`];
2160
+
2161
+ if (typeof column.name !== 'string') {
2162
+ statements.push({
2163
+ type: 'alter_table_rename_column',
2164
+ tableName,
2165
+ oldColumnName: column.name.old,
2166
+ newColumnName: column.name.new,
2167
+ schema,
2168
+ });
2169
+ }
2170
+
2171
+ if (column.type?.type === 'changed') {
2172
+ statements.push({
2173
+ type: 'alter_table_alter_column_set_type',
2174
+ tableName,
2175
+ columnName,
2176
+ newDataType: column.type.new,
2177
+ oldDataType: column.type.old,
2178
+ schema,
2179
+ columnDefault,
2180
+ columnOnUpdate,
2181
+ columnNotNull,
2182
+ columnAutoIncrement,
2183
+ columnPk,
2184
+ });
2185
+ }
2186
+
2187
+ if (
2188
+ column.primaryKey?.type === 'deleted'
2189
+ || (column.primaryKey?.type === 'changed'
2190
+ && !column.primaryKey.new
2191
+ && typeof compositePk === 'undefined')
2192
+ ) {
2193
+ dropPkStatements.push({
2194
+ ////
2195
+ type: 'alter_table_alter_column_drop_pk',
2196
+ tableName,
2197
+ columnName,
2198
+ schema,
2199
+ });
2200
+ }
2201
+
2202
+ if (column.default?.type === 'added') {
2203
+ statements.push({
2204
+ type: 'alter_table_alter_column_set_default',
2205
+ tableName,
2206
+ columnName,
2207
+ newDefaultValue: column.default.value,
2208
+ schema,
2209
+ columnOnUpdate,
2210
+ columnNotNull,
2211
+ columnAutoIncrement,
2212
+ newDataType: columnType,
2213
+ columnPk,
2214
+ });
2215
+ }
2216
+
2217
+ if (column.default?.type === 'changed') {
2218
+ statements.push({
2219
+ type: 'alter_table_alter_column_set_default',
2220
+ tableName,
2221
+ columnName,
2222
+ newDefaultValue: column.default.new,
2223
+ oldDefaultValue: column.default.old,
2224
+ schema,
2225
+ columnOnUpdate,
2226
+ columnNotNull,
2227
+ columnAutoIncrement,
2228
+ newDataType: columnType,
2229
+ columnPk,
2230
+ });
2231
+ }
2232
+
2233
+ if (column.default?.type === 'deleted') {
2234
+ statements.push({
2235
+ type: 'alter_table_alter_column_drop_default',
2236
+ tableName,
2237
+ columnName,
2238
+ schema,
2239
+ columnDefault,
2240
+ columnOnUpdate,
2241
+ columnNotNull,
2242
+ columnAutoIncrement,
2243
+ newDataType: columnType,
2244
+ columnPk,
2245
+ });
2246
+ }
2247
+
2248
+ if (column.notNull?.type === 'added') {
2249
+ statements.push({
2250
+ type: 'alter_table_alter_column_set_notnull',
2251
+ tableName,
2252
+ columnName,
2253
+ schema,
2254
+ newDataType: columnType,
2255
+ columnDefault,
2256
+ columnOnUpdate,
2257
+ columnNotNull,
2258
+ columnAutoIncrement,
2259
+ columnPk,
2260
+ });
2261
+ }
2262
+
2263
+ if (column.notNull?.type === 'changed') {
2264
+ const type = column.notNull.new
2265
+ ? 'alter_table_alter_column_set_notnull'
2266
+ : 'alter_table_alter_column_drop_notnull';
2267
+ statements.push({
2268
+ type: type,
2269
+ tableName,
2270
+ columnName,
2271
+ schema,
2272
+ newDataType: columnType,
2273
+ columnDefault,
2274
+ columnOnUpdate,
2275
+ columnNotNull,
2276
+ columnAutoIncrement,
2277
+ columnPk,
2278
+ });
2279
+ }
2280
+
2281
+ if (column.notNull?.type === 'deleted') {
2282
+ statements.push({
2283
+ type: 'alter_table_alter_column_drop_notnull',
2284
+ tableName,
2285
+ columnName,
2286
+ schema,
2287
+ newDataType: columnType,
2288
+ columnDefault,
2289
+ columnOnUpdate,
2290
+ columnNotNull,
2291
+ columnAutoIncrement,
2292
+ columnPk,
2293
+ });
2294
+ }
2295
+
2296
+ if (column.identity?.type === 'added') {
2297
+ statements.push({
2298
+ type: 'alter_table_alter_column_set_identity',
2299
+ tableName,
2300
+ columnName,
2301
+ schema,
2302
+ identity: column.identity.value,
2303
+ });
2304
+ }
2305
+
2306
+ if (column.identity?.type === 'changed') {
2307
+ statements.push({
2308
+ type: 'alter_table_alter_column_change_identity',
2309
+ tableName,
2310
+ columnName,
2311
+ schema,
2312
+ identity: column.identity.new,
2313
+ oldIdentity: column.identity.old,
2314
+ });
2315
+ }
2316
+
2317
+ if (column.identity?.type === 'deleted') {
2318
+ statements.push({
2319
+ type: 'alter_table_alter_column_drop_identity',
2320
+ tableName,
2321
+ columnName,
2322
+ schema,
2323
+ });
2324
+ }
2325
+
2326
+ if (column.generated?.type === 'added') {
2327
+ statements.push({
2328
+ type: 'alter_table_alter_column_set_generated',
2329
+ tableName,
2330
+ columnName,
2331
+ schema,
2332
+ newDataType: columnType,
2333
+ columnDefault,
2334
+ columnOnUpdate,
2335
+ columnNotNull,
2336
+ columnAutoIncrement,
2337
+ columnPk,
2338
+ columnGenerated,
2339
+ });
2340
+ }
2341
+
2342
+ if (column.generated?.type === 'changed' && action !== 'push') {
2343
+ statements.push({
2344
+ type: 'alter_table_alter_column_alter_generated',
2345
+ tableName,
2346
+ columnName,
2347
+ schema,
2348
+ newDataType: columnType,
2349
+ columnDefault,
2350
+ columnOnUpdate,
2351
+ columnNotNull,
2352
+ columnAutoIncrement,
2353
+ columnPk,
2354
+ columnGenerated,
2355
+ });
2356
+ }
2357
+
2358
+ if (column.generated?.type === 'deleted') {
2359
+ statements.push({
2360
+ type: 'alter_table_alter_column_drop_generated',
2361
+ tableName,
2362
+ columnName,
2363
+ schema,
2364
+ newDataType: columnType,
2365
+ columnDefault,
2366
+ columnOnUpdate,
2367
+ columnNotNull,
2368
+ columnAutoIncrement,
2369
+ columnPk,
2370
+ columnGenerated,
2371
+ });
2372
+ }
2373
+
2374
+ if (
2375
+ column.primaryKey?.type === 'added'
2376
+ || (column.primaryKey?.type === 'changed' && column.primaryKey.new)
2377
+ ) {
2378
+ const wasAutoincrement = statements.filter(
2379
+ (it) => it.type === 'alter_table_alter_column_set_autoincrement',
2380
+ );
2381
+ if (wasAutoincrement.length === 0) {
2382
+ setPkStatements.push({
2383
+ type: 'alter_table_alter_column_set_pk',
2384
+ tableName,
2385
+ schema,
2386
+ columnName,
2387
+ });
2388
+ }
2389
+ }
2390
+
2391
+ // if (column.primaryKey?.type === "added") {
2392
+ // statements.push({
2393
+ // type: "alter_table_alter_column_set_primarykey",
2394
+ // tableName,
2395
+ // columnName,
2396
+ // schema,
2397
+ // newDataType: columnType,
2398
+ // columnDefault,
2399
+ // columnOnUpdate,
2400
+ // columnNotNull,
2401
+ // columnAutoIncrement,
2402
+ // });
2403
+ // }
2404
+
2405
+ // if (column.primaryKey?.type === "changed") {
2406
+ // const type = column.primaryKey.new
2407
+ // ? "alter_table_alter_column_set_primarykey"
2408
+ // : "alter_table_alter_column_drop_primarykey";
2409
+
2410
+ // statements.push({
2411
+ // type,
2412
+ // tableName,
2413
+ // columnName,
2414
+ // schema,
2415
+ // newDataType: columnType,
2416
+ // columnDefault,
2417
+ // columnOnUpdate,
2418
+ // columnNotNull,
2419
+ // columnAutoIncrement,
2420
+ // });
2421
+ // }
2422
+
2423
+ // if (column.primaryKey?.type === "deleted") {
2424
+ // statements.push({
2425
+ // type: "alter_table_alter_column_drop_primarykey",
2426
+ // tableName,
2427
+ // columnName,
2428
+ // schema,
2429
+ // newDataType: columnType,
2430
+ // columnDefault,
2431
+ // columnOnUpdate,
2432
+ // columnNotNull,
2433
+ // columnAutoIncrement,
2434
+ // });
2435
+ // }
2436
+
2437
+ if (column.onUpdate?.type === 'added') {
2438
+ statements.push({
2439
+ type: 'alter_table_alter_column_set_on_update',
2440
+ tableName,
2441
+ columnName,
2442
+ schema,
2443
+ newDataType: columnType,
2444
+ columnDefault,
2445
+ columnOnUpdate,
2446
+ columnNotNull,
2447
+ columnAutoIncrement,
2448
+ columnPk,
2449
+ });
2450
+ }
2451
+
2452
+ if (column.onUpdate?.type === 'deleted') {
2453
+ statements.push({
2454
+ type: 'alter_table_alter_column_drop_on_update',
2455
+ tableName,
2456
+ columnName,
2457
+ schema,
2458
+ newDataType: columnType,
2459
+ columnDefault,
2460
+ columnOnUpdate,
2461
+ columnNotNull,
2462
+ columnAutoIncrement,
2463
+ columnPk,
2464
+ });
2465
+ }
2466
+ }
2467
+
2468
+ return [...dropPkStatements, ...setPkStatements, ...statements];
2469
+ };
2470
+
2471
+ export const prepareSqliteAlterColumns = (
2472
+ tableName: string,
2473
+ schema: string,
2474
+ columns: AlteredColumn[],
2475
+ // TODO: remove?
2476
+ json2: CommonSquashedSchema,
2477
+ ): JsonAlterColumnStatement[] => {
2478
+ let statements: JsonAlterColumnStatement[] = [];
2479
+ let dropPkStatements: JsonAlterColumnDropPrimaryKeyStatement[] = [];
2480
+ let setPkStatements: JsonAlterColumnSetPrimaryKeyStatement[] = [];
2481
+
2482
+ for (const column of columns) {
2483
+ const columnName = typeof column.name !== 'string' ? column.name.new : column.name;
2484
+
2485
+ // I used any, because those fields are available only for mysql dialect
2486
+ // For other dialects it will become undefined, that is fine for json statements
2487
+ const columnType = json2.tables[tableName].columns[columnName].type;
2488
+ const columnDefault = json2.tables[tableName].columns[columnName].default;
2489
+ const columnOnUpdate = (json2.tables[tableName].columns[columnName] as any)
2490
+ .onUpdate;
2491
+ const columnNotNull = json2.tables[tableName].columns[columnName].notNull;
2492
+ const columnAutoIncrement = (
2493
+ json2.tables[tableName].columns[columnName] as any
2494
+ ).autoincrement;
2495
+ const columnPk = (json2.tables[tableName].columns[columnName] as any)
2496
+ .primaryKey;
2497
+
2498
+ const columnGenerated = json2.tables[tableName].columns[columnName].generated;
2499
+
2500
+ const compositePk = json2.tables[tableName].compositePrimaryKeys[
2501
+ `${tableName}_${columnName}`
2502
+ ];
2503
+
2504
+ if (column.autoincrement?.type === 'added') {
2505
+ statements.push({
2506
+ type: 'alter_table_alter_column_set_autoincrement',
2507
+ tableName,
2508
+ columnName,
2509
+ schema,
2510
+ newDataType: columnType,
2511
+ columnDefault,
2512
+ columnOnUpdate,
2513
+ columnNotNull,
2514
+ columnAutoIncrement,
2515
+ columnPk,
2516
+ });
2517
+ }
2518
+
2519
+ if (column.autoincrement?.type === 'changed') {
2520
+ const type = column.autoincrement.new
2521
+ ? 'alter_table_alter_column_set_autoincrement'
2522
+ : 'alter_table_alter_column_drop_autoincrement';
2523
+
2524
+ statements.push({
2525
+ type,
2526
+ tableName,
2527
+ columnName,
2528
+ schema,
2529
+ newDataType: columnType,
2530
+ columnDefault,
2531
+ columnOnUpdate,
2532
+ columnNotNull,
2533
+ columnAutoIncrement,
2534
+ columnPk,
2535
+ });
2536
+ }
2537
+
2538
+ if (column.autoincrement?.type === 'deleted') {
2539
+ statements.push({
2540
+ type: 'alter_table_alter_column_drop_autoincrement',
2541
+ tableName,
2542
+ columnName,
2543
+ schema,
2544
+ newDataType: columnType,
2545
+ columnDefault,
2546
+ columnOnUpdate,
2547
+ columnNotNull,
2548
+ columnAutoIncrement,
2549
+ columnPk,
2550
+ });
2551
+ }
2552
+
2553
+ if (typeof column.name !== 'string') {
2554
+ statements.push({
2555
+ type: 'alter_table_rename_column',
2556
+ tableName,
2557
+ oldColumnName: column.name.old,
2558
+ newColumnName: column.name.new,
2559
+ schema,
2560
+ });
2561
+ }
2562
+
2563
+ if (column.type?.type === 'changed') {
2564
+ statements.push({
2565
+ type: 'alter_table_alter_column_set_type',
2566
+ tableName,
2567
+ columnName,
2568
+ newDataType: column.type.new,
2569
+ oldDataType: column.type.old,
2570
+ schema,
2571
+ columnDefault,
2572
+ columnOnUpdate,
2573
+ columnNotNull,
2574
+ columnAutoIncrement,
2575
+ columnPk,
2576
+ });
2577
+ }
2578
+
2579
+ if (
2580
+ column.primaryKey?.type === 'deleted'
2581
+ || (column.primaryKey?.type === 'changed'
2582
+ && !column.primaryKey.new
2583
+ && typeof compositePk === 'undefined')
2584
+ ) {
2585
+ dropPkStatements.push({
2586
+ ////
2587
+ type: 'alter_table_alter_column_drop_pk',
2588
+ tableName,
2589
+ columnName,
2590
+ schema,
2591
+ });
2592
+ }
2593
+
2594
+ if (column.default?.type === 'added') {
2595
+ statements.push({
2596
+ type: 'alter_table_alter_column_set_default',
2597
+ tableName,
2598
+ columnName,
2599
+ newDefaultValue: column.default.value,
2600
+ schema,
2601
+ columnOnUpdate,
2602
+ columnNotNull,
2603
+ columnAutoIncrement,
2604
+ newDataType: columnType,
2605
+ columnPk,
2606
+ });
2607
+ }
2608
+
2609
+ if (column.default?.type === 'changed') {
2610
+ statements.push({
2611
+ type: 'alter_table_alter_column_set_default',
2612
+ tableName,
2613
+ columnName,
2614
+ newDefaultValue: column.default.new,
2615
+ oldDefaultValue: column.default.old,
2616
+ schema,
2617
+ columnOnUpdate,
2618
+ columnNotNull,
2619
+ columnAutoIncrement,
2620
+ newDataType: columnType,
2621
+ columnPk,
2622
+ });
2623
+ }
2624
+
2625
+ if (column.default?.type === 'deleted') {
2626
+ statements.push({
2627
+ type: 'alter_table_alter_column_drop_default',
2628
+ tableName,
2629
+ columnName,
2630
+ schema,
2631
+ columnDefault,
2632
+ columnOnUpdate,
2633
+ columnNotNull,
2634
+ columnAutoIncrement,
2635
+ newDataType: columnType,
2636
+ columnPk,
2637
+ });
2638
+ }
2639
+
2640
+ if (column.notNull?.type === 'added') {
2641
+ statements.push({
2642
+ type: 'alter_table_alter_column_set_notnull',
2643
+ tableName,
2644
+ columnName,
2645
+ schema,
2646
+ newDataType: columnType,
2647
+ columnDefault,
2648
+ columnOnUpdate,
2649
+ columnNotNull,
2650
+ columnAutoIncrement,
2651
+ columnPk,
2652
+ });
2653
+ }
2654
+
2655
+ if (column.notNull?.type === 'changed') {
2656
+ const type = column.notNull.new
2657
+ ? 'alter_table_alter_column_set_notnull'
2658
+ : 'alter_table_alter_column_drop_notnull';
2659
+ statements.push({
2660
+ type: type,
2661
+ tableName,
2662
+ columnName,
2663
+ schema,
2664
+ newDataType: columnType,
2665
+ columnDefault,
2666
+ columnOnUpdate,
2667
+ columnNotNull,
2668
+ columnAutoIncrement,
2669
+ columnPk,
2670
+ });
2671
+ }
2672
+
2673
+ if (column.notNull?.type === 'deleted') {
2674
+ statements.push({
2675
+ type: 'alter_table_alter_column_drop_notnull',
2676
+ tableName,
2677
+ columnName,
2678
+ schema,
2679
+ newDataType: columnType,
2680
+ columnDefault,
2681
+ columnOnUpdate,
2682
+ columnNotNull,
2683
+ columnAutoIncrement,
2684
+ columnPk,
2685
+ });
2686
+ }
2687
+
2688
+ if (column.generated?.type === 'added') {
2689
+ if (columnGenerated?.type === 'virtual') {
2690
+ statements.push({
2691
+ type: 'alter_table_alter_column_set_generated',
2692
+ tableName,
2693
+ columnName,
2694
+ schema,
2695
+ newDataType: columnType,
2696
+ columnDefault,
2697
+ columnOnUpdate,
2698
+ columnNotNull,
2699
+ columnAutoIncrement,
2700
+ columnPk,
2701
+ columnGenerated,
2702
+ });
2703
+ } else {
2704
+ warning(
2705
+ `As SQLite docs mention: "It is not possible to ALTER TABLE ADD COLUMN a STORED column. One can add a VIRTUAL column, however", source: "https://www.sqlite.org/gencol.html"`,
2706
+ );
2707
+ }
2708
+ }
2709
+
2710
+ if (column.generated?.type === 'changed') {
2711
+ if (columnGenerated?.type === 'virtual') {
2712
+ statements.push({
2713
+ type: 'alter_table_alter_column_alter_generated',
2714
+ tableName,
2715
+ columnName,
2716
+ schema,
2717
+ newDataType: columnType,
2718
+ columnDefault,
2719
+ columnOnUpdate,
2720
+ columnNotNull,
2721
+ columnAutoIncrement,
2722
+ columnPk,
2723
+ columnGenerated,
2724
+ });
2725
+ } else {
2726
+ warning(
2727
+ `As SQLite docs mention: "It is not possible to ALTER TABLE ADD COLUMN a STORED column. One can add a VIRTUAL column, however", source: "https://www.sqlite.org/gencol.html"`,
2728
+ );
2729
+ }
2730
+ }
2731
+
2732
+ if (column.generated?.type === 'deleted') {
2733
+ statements.push({
2734
+ type: 'alter_table_alter_column_drop_generated',
2735
+ tableName,
2736
+ columnName,
2737
+ schema,
2738
+ newDataType: columnType,
2739
+ columnDefault,
2740
+ columnOnUpdate,
2741
+ columnNotNull,
2742
+ columnAutoIncrement,
2743
+ columnPk,
2744
+ columnGenerated,
2745
+ });
2746
+ }
2747
+
2748
+ if (
2749
+ column.primaryKey?.type === 'added'
2750
+ || (column.primaryKey?.type === 'changed' && column.primaryKey.new)
2751
+ ) {
2752
+ const wasAutoincrement = statements.filter(
2753
+ (it) => it.type === 'alter_table_alter_column_set_autoincrement',
2754
+ );
2755
+ if (wasAutoincrement.length === 0) {
2756
+ setPkStatements.push({
2757
+ type: 'alter_table_alter_column_set_pk',
2758
+ tableName,
2759
+ schema,
2760
+ columnName,
2761
+ });
2762
+ }
2763
+ }
2764
+
2765
+ if (column.onUpdate?.type === 'added') {
2766
+ statements.push({
2767
+ type: 'alter_table_alter_column_set_on_update',
2768
+ tableName,
2769
+ columnName,
2770
+ schema,
2771
+ newDataType: columnType,
2772
+ columnDefault,
2773
+ columnOnUpdate,
2774
+ columnNotNull,
2775
+ columnAutoIncrement,
2776
+ columnPk,
2777
+ });
2778
+ }
2779
+
2780
+ if (column.onUpdate?.type === 'deleted') {
2781
+ statements.push({
2782
+ type: 'alter_table_alter_column_drop_on_update',
2783
+ tableName,
2784
+ columnName,
2785
+ schema,
2786
+ newDataType: columnType,
2787
+ columnDefault,
2788
+ columnOnUpdate,
2789
+ columnNotNull,
2790
+ columnAutoIncrement,
2791
+ columnPk,
2792
+ });
2793
+ }
2794
+ }
2795
+
2796
+ return [...dropPkStatements, ...setPkStatements, ...statements];
2797
+ };
2798
+
2799
+ export const prepareRenamePolicyJsons = (
2800
+ tableName: string,
2801
+ schema: string,
2802
+ renames: {
2803
+ from: Policy;
2804
+ to: Policy;
2805
+ }[],
2806
+ ): JsonRenamePolicyStatement[] => {
2807
+ return renames.map((it) => {
2808
+ return {
2809
+ type: 'rename_policy',
2810
+ tableName: tableName,
2811
+ oldName: it.from.name,
2812
+ newName: it.to.name,
2813
+ schema,
2814
+ };
2815
+ });
2816
+ };
2817
+
2818
+ export const prepareRenameIndPolicyJsons = (
2819
+ renames: {
2820
+ from: Policy;
2821
+ to: Policy;
2822
+ }[],
2823
+ ): JsonIndRenamePolicyStatement[] => {
2824
+ return renames.map((it) => {
2825
+ return {
2826
+ type: 'rename_ind_policy',
2827
+ tableKey: it.from.on!,
2828
+ oldName: it.from.name,
2829
+ newName: it.to.name,
2830
+ };
2831
+ });
2832
+ };
2833
+
2834
+ export const prepareCreatePolicyJsons = (
2835
+ tableName: string,
2836
+ schema: string,
2837
+ policies: Policy[],
2838
+ ): JsonCreatePolicyStatement[] => {
2839
+ return policies.map((it) => {
2840
+ return {
2841
+ type: 'create_policy',
2842
+ tableName,
2843
+ data: it,
2844
+ schema,
2845
+ };
2846
+ });
2847
+ };
2848
+
2849
+ export const prepareCreateIndPolicyJsons = (
2850
+ policies: Policy[],
2851
+ ): JsonCreateIndPolicyStatement[] => {
2852
+ return policies.map((it) => {
2853
+ return {
2854
+ type: 'create_ind_policy',
2855
+ tableName: it.on!,
2856
+ data: it,
2857
+ };
2858
+ });
2859
+ };
2860
+
2861
+ export const prepareDropPolicyJsons = (
2862
+ tableName: string,
2863
+ schema: string,
2864
+ policies: Policy[],
2865
+ ): JsonDropPolicyStatement[] => {
2866
+ return policies.map((it) => {
2867
+ return {
2868
+ type: 'drop_policy',
2869
+ tableName,
2870
+ data: it,
2871
+ schema,
2872
+ };
2873
+ });
2874
+ };
2875
+
2876
+ export const prepareDropIndPolicyJsons = (
2877
+ policies: Policy[],
2878
+ ): JsonDropIndPolicyStatement[] => {
2879
+ return policies.map((it) => {
2880
+ return {
2881
+ type: 'drop_ind_policy',
2882
+ tableName: it.on!,
2883
+ data: it,
2884
+ };
2885
+ });
2886
+ };
2887
+
2888
+ export const prepareAlterPolicyJson = (
2889
+ tableName: string,
2890
+ schema: string,
2891
+ oldPolicy: string,
2892
+ newPolicy: string,
2893
+ ): JsonAlterPolicyStatement => {
2894
+ return {
2895
+ type: 'alter_policy',
2896
+ tableName,
2897
+ oldData: oldPolicy,
2898
+ newData: newPolicy,
2899
+ schema,
2900
+ };
2901
+ };
2902
+
2903
+ export const prepareAlterIndPolicyJson = (
2904
+ oldPolicy: Policy,
2905
+ newPolicy: Policy,
2906
+ ): JsonAlterIndPolicyStatement => {
2907
+ return {
2908
+ type: 'alter_ind_policy',
2909
+ oldData: oldPolicy,
2910
+ newData: newPolicy,
2911
+ };
2912
+ };
2913
+
2914
+ export const preparePgCreateIndexesJson = (
2915
+ tableName: string,
2916
+ schema: string,
2917
+ indexes: Record<string, string>,
2918
+ fullSchema: PgSchema,
2919
+ action?: 'push' | undefined,
2920
+ ): JsonPgCreateIndexStatement[] => {
2921
+ if (action === 'push') {
2922
+ return Object.values(indexes).map((indexData) => {
2923
+ const unsquashedIndex = PgSquasher.unsquashIdxPush(indexData);
2924
+ const data = fullSchema.tables[`${schema === '' ? 'public' : schema}.${tableName}`]
2925
+ .indexes[unsquashedIndex.name];
2926
+ return {
2927
+ type: 'create_index_pg',
2928
+ tableName,
2929
+ data,
2930
+ schema,
2931
+ };
2932
+ });
2933
+ }
2934
+ return Object.values(indexes).map((indexData) => {
2935
+ return {
2936
+ type: 'create_index_pg',
2937
+ tableName,
2938
+ data: PgSquasher.unsquashIdx(indexData),
2939
+ schema,
2940
+ };
2941
+ });
2942
+ };
2943
+
2944
+ export const prepareCreateIndexesJson = (
2945
+ tableName: string,
2946
+ schema: string,
2947
+ indexes: Record<string, string>,
2948
+ internal?: MySqlKitInternals | SQLiteKitInternals,
2949
+ ): JsonCreateIndexStatement[] => {
2950
+ return Object.values(indexes).map((indexData) => {
2951
+ return {
2952
+ type: 'create_index',
2953
+ tableName,
2954
+ data: indexData,
2955
+ schema,
2956
+ internal,
2957
+ };
2958
+ });
2959
+ };
2960
+
2961
+ export const prepareCreateReferencesJson = (
2962
+ tableName: string,
2963
+ schema: string,
2964
+ foreignKeys: Record<string, string>,
2965
+ ): JsonCreateReferenceStatement[] => {
2966
+ return Object.values(foreignKeys).map((fkData) => {
2967
+ return {
2968
+ type: 'create_reference',
2969
+ tableName,
2970
+ data: fkData,
2971
+ schema,
2972
+ };
2973
+ });
2974
+ };
2975
+ export const prepareLibSQLCreateReferencesJson = (
2976
+ tableName: string,
2977
+ schema: string,
2978
+ foreignKeys: Record<string, string>,
2979
+ json2: SQLiteSchemaSquashed,
2980
+ action?: 'push',
2981
+ ): JsonCreateReferenceStatement[] => {
2982
+ return Object.values(foreignKeys).map((fkData) => {
2983
+ const { columnsFrom, tableFrom, columnsTo } = action === 'push'
2984
+ ? SQLiteSquasher.unsquashPushFK(fkData)
2985
+ : SQLiteSquasher.unsquashFK(fkData);
2986
+
2987
+ // When trying to alter table in lib sql it is necessary to pass all config for column like "NOT NULL", "DEFAULT", etc.
2988
+ // If it is multicolumn reference it is not possible to pass this data for all columns
2989
+ // Pass multicolumn flag for sql statements to not generate migration
2990
+ let isMulticolumn = false;
2991
+
2992
+ if (columnsFrom.length > 1 || columnsTo.length > 1) {
2993
+ isMulticolumn = true;
2994
+
2995
+ return {
2996
+ type: 'create_reference',
2997
+ tableName,
2998
+ data: fkData,
2999
+ schema,
3000
+ isMulticolumn,
3001
+ };
3002
+ }
3003
+
3004
+ const columnFrom = columnsFrom[0];
3005
+
3006
+ const {
3007
+ notNull: columnNotNull,
3008
+ default: columnDefault,
3009
+ type: columnType,
3010
+ } = json2.tables[tableFrom].columns[columnFrom];
3011
+
3012
+ return {
3013
+ type: 'create_reference',
3014
+ tableName,
3015
+ data: fkData,
3016
+ schema,
3017
+ columnNotNull,
3018
+ columnDefault,
3019
+ columnType,
3020
+ };
3021
+ });
3022
+ };
3023
+
3024
+ export const prepareDropReferencesJson = (
3025
+ tableName: string,
3026
+ schema: string,
3027
+ foreignKeys: Record<string, string>,
3028
+ ): JsonDeleteReferenceStatement[] => {
3029
+ return Object.values(foreignKeys).map((fkData) => {
3030
+ return {
3031
+ type: 'delete_reference',
3032
+ tableName,
3033
+ data: fkData,
3034
+ schema,
3035
+ };
3036
+ });
3037
+ };
3038
+ export const prepareLibSQLDropReferencesJson = (
3039
+ tableName: string,
3040
+ schema: string,
3041
+ foreignKeys: Record<string, string>,
3042
+ json2: SQLiteSchemaSquashed,
3043
+ meta: SQLiteSchemaInternal['_meta'],
3044
+ action?: 'push',
3045
+ ): JsonDeleteReferenceStatement[] => {
3046
+ const statements = Object.values(foreignKeys).map((fkData) => {
3047
+ const { columnsFrom, tableFrom, columnsTo, name, tableTo, onDelete, onUpdate } = action === 'push'
3048
+ ? SQLiteSquasher.unsquashPushFK(fkData)
3049
+ : SQLiteSquasher.unsquashFK(fkData);
3050
+
3051
+ // If all columns from where were references were deleted -> skip this logic
3052
+ // Drop columns will cover this scenario
3053
+ const keys = Object.keys(json2.tables[tableName].columns);
3054
+ const filtered = columnsFrom.filter((it) => keys.includes(it));
3055
+ const fullDrop = filtered.length === 0;
3056
+ if (fullDrop) return;
3057
+
3058
+ // When trying to alter table in lib sql it is necessary to pass all config for column like "NOT NULL", "DEFAULT", etc.
3059
+ // If it is multicolumn reference it is not possible to pass this data for all columns
3060
+ // Pass multicolumn flag for sql statements to not generate migration
3061
+ let isMulticolumn = false;
3062
+
3063
+ if (columnsFrom.length > 1 || columnsTo.length > 1) {
3064
+ isMulticolumn = true;
3065
+
3066
+ return {
3067
+ type: 'delete_reference',
3068
+ tableName,
3069
+ data: fkData,
3070
+ schema,
3071
+ isMulticolumn,
3072
+ };
3073
+ }
3074
+
3075
+ const columnFrom = columnsFrom[0];
3076
+ const newTableName = getNewTableName(tableFrom, meta);
3077
+
3078
+ const {
3079
+ notNull: columnNotNull,
3080
+ default: columnDefault,
3081
+ type: columnType,
3082
+ } = json2.tables[newTableName].columns[columnFrom];
3083
+
3084
+ const fkToSquash = {
3085
+ columnsFrom,
3086
+ columnsTo,
3087
+ name,
3088
+ tableFrom: newTableName,
3089
+ tableTo,
3090
+ onDelete,
3091
+ onUpdate,
3092
+ };
3093
+ const foreignKey = action === 'push'
3094
+ ? SQLiteSquasher.squashPushFK(fkToSquash)
3095
+ : SQLiteSquasher.squashFK(fkToSquash);
3096
+ return {
3097
+ type: 'delete_reference',
3098
+ tableName,
3099
+ data: foreignKey,
3100
+ schema,
3101
+ columnNotNull,
3102
+ columnDefault,
3103
+ columnType,
3104
+ };
3105
+ });
3106
+
3107
+ return statements.filter((it) => it) as JsonDeleteReferenceStatement[];
3108
+ };
3109
+
3110
+ // alter should create 2 statements. It's important to make only 1 sql per statement(for breakpoints)
3111
+ export const prepareAlterReferencesJson = (
3112
+ tableName: string,
3113
+ schema: string,
3114
+ foreignKeys: Record<string, { __old: string; __new: string }>,
3115
+ ): JsonReferenceStatement[] => {
3116
+ const stmts: JsonReferenceStatement[] = [];
3117
+ Object.values(foreignKeys).map((val) => {
3118
+ stmts.push({
3119
+ type: 'delete_reference',
3120
+ tableName,
3121
+ schema,
3122
+ data: val.__old,
3123
+ });
3124
+
3125
+ stmts.push({
3126
+ type: 'create_reference',
3127
+ tableName,
3128
+ schema,
3129
+ data: val.__new,
3130
+ });
3131
+ });
3132
+ return stmts;
3133
+ };
3134
+
3135
+ export const prepareDropIndexesJson = (
3136
+ tableName: string,
3137
+ schema: string,
3138
+ indexes: Record<string, string>,
3139
+ ): JsonDropIndexStatement[] => {
3140
+ return Object.values(indexes).map((indexData) => {
3141
+ return {
3142
+ type: 'drop_index',
3143
+ tableName,
3144
+ data: indexData,
3145
+ schema,
3146
+ };
3147
+ });
3148
+ };
3149
+
3150
+ export const prepareAddCompositePrimaryKeySqlite = (
3151
+ tableName: string,
3152
+ pks: Record<string, string>,
3153
+ ): JsonCreateCompositePK[] => {
3154
+ return Object.values(pks).map((it) => {
3155
+ return {
3156
+ type: 'create_composite_pk',
3157
+ tableName,
3158
+ data: it,
3159
+ } as JsonCreateCompositePK;
3160
+ });
3161
+ };
3162
+
3163
+ export const prepareDeleteCompositePrimaryKeySqlite = (
3164
+ tableName: string,
3165
+ pks: Record<string, string>,
3166
+ ): JsonDeleteCompositePK[] => {
3167
+ return Object.values(pks).map((it) => {
3168
+ return {
3169
+ type: 'delete_composite_pk',
3170
+ tableName,
3171
+ data: it,
3172
+ } as JsonDeleteCompositePK;
3173
+ });
3174
+ };
3175
+
3176
+ export const prepareAlterCompositePrimaryKeySqlite = (
3177
+ tableName: string,
3178
+ pks: Record<string, { __old: string; __new: string }>,
3179
+ ): JsonAlterCompositePK[] => {
3180
+ return Object.values(pks).map((it) => {
3181
+ return {
3182
+ type: 'alter_composite_pk',
3183
+ tableName,
3184
+ old: it.__old,
3185
+ new: it.__new,
3186
+ } as JsonAlterCompositePK;
3187
+ });
3188
+ };
3189
+
3190
+ export const prepareAddCompositePrimaryKeyPg = (
3191
+ tableName: string,
3192
+ schema: string,
3193
+ pks: Record<string, string>,
3194
+ // TODO: remove?
3195
+ json2: PgSchema,
3196
+ ): JsonCreateCompositePK[] => {
3197
+ return Object.values(pks).map((it) => {
3198
+ const unsquashed = PgSquasher.unsquashPK(it);
3199
+ return {
3200
+ type: 'create_composite_pk',
3201
+ tableName,
3202
+ data: it,
3203
+ schema,
3204
+ constraintName: PgSquasher.unsquashPK(it).name,
3205
+ } as JsonCreateCompositePK;
3206
+ });
3207
+ };
3208
+
3209
+ export const prepareDeleteCompositePrimaryKeyPg = (
3210
+ tableName: string,
3211
+ schema: string,
3212
+ pks: Record<string, string>,
3213
+ // TODO: remove?
3214
+ json1: PgSchema,
3215
+ ): JsonDeleteCompositePK[] => {
3216
+ return Object.values(pks).map((it) => {
3217
+ return {
3218
+ type: 'delete_composite_pk',
3219
+ tableName,
3220
+ data: it,
3221
+ schema,
3222
+ constraintName: PgSquasher.unsquashPK(it).name,
3223
+ } as JsonDeleteCompositePK;
3224
+ });
3225
+ };
3226
+
3227
+ export const prepareAlterCompositePrimaryKeyPg = (
3228
+ tableName: string,
3229
+ schema: string,
3230
+ pks: Record<string, { __old: string; __new: string }>,
3231
+ // TODO: remove?
3232
+ json1: PgSchema,
3233
+ json2: PgSchema,
3234
+ ): JsonAlterCompositePK[] => {
3235
+ return Object.values(pks).map((it) => {
3236
+ return {
3237
+ type: 'alter_composite_pk',
3238
+ tableName,
3239
+ old: it.__old,
3240
+ new: it.__new,
3241
+ schema,
3242
+ oldConstraintName: PgSquasher.unsquashPK(it.__old).name,
3243
+ newConstraintName: PgSquasher.unsquashPK(it.__new).name,
3244
+ } as JsonAlterCompositePK;
3245
+ });
3246
+ };
3247
+
3248
+ export const prepareAddUniqueConstraintPg = (
3249
+ tableName: string,
3250
+ schema: string,
3251
+ unqs: Record<string, string>,
3252
+ ): JsonCreateUniqueConstraint[] => {
3253
+ return Object.values(unqs).map((it) => {
3254
+ return {
3255
+ type: 'create_unique_constraint',
3256
+ tableName,
3257
+ data: it,
3258
+ schema,
3259
+ } as JsonCreateUniqueConstraint;
3260
+ });
3261
+ };
3262
+
3263
+ export const prepareDeleteUniqueConstraintPg = (
3264
+ tableName: string,
3265
+ schema: string,
3266
+ unqs: Record<string, string>,
3267
+ ): JsonDeleteUniqueConstraint[] => {
3268
+ return Object.values(unqs).map((it) => {
3269
+ return {
3270
+ type: 'delete_unique_constraint',
3271
+ tableName,
3272
+ data: it,
3273
+ schema,
3274
+ } as JsonDeleteUniqueConstraint;
3275
+ });
3276
+ };
3277
+
3278
+ export const prepareAddCheckConstraint = (
3279
+ tableName: string,
3280
+ schema: string,
3281
+ check: Record<string, string>,
3282
+ ): JsonCreateCheckConstraint[] => {
3283
+ return Object.values(check).map((it) => {
3284
+ return {
3285
+ type: 'create_check_constraint',
3286
+ tableName,
3287
+ data: it,
3288
+ schema,
3289
+ } as JsonCreateCheckConstraint;
3290
+ });
3291
+ };
3292
+
3293
+ export const prepareDeleteCheckConstraint = (
3294
+ tableName: string,
3295
+ schema: string,
3296
+ check: Record<string, string>,
3297
+ ): JsonDeleteCheckConstraint[] => {
3298
+ return Object.values(check).map((it) => {
3299
+ return {
3300
+ type: 'delete_check_constraint',
3301
+ tableName,
3302
+ constraintName: PgSquasher.unsquashCheck(it).name,
3303
+ schema,
3304
+ } as JsonDeleteCheckConstraint;
3305
+ });
3306
+ };
3307
+
3308
+ // add create table changes
3309
+ // add handler to make drop and add and not alter(looking at __old and __new)
3310
+ // add serializer for mysql and sqlite + types
3311
+ // add introspect serializer for pg+sqlite+mysql
3312
+ // add introspect actual code
3313
+ // add push sqlite handler
3314
+ // add push mysql warning if data exists and may have unique conflict
3315
+ // add release notes
3316
+ // add docs changes
3317
+
3318
+ export const prepareAlterUniqueConstraintPg = (
3319
+ tableName: string,
3320
+ schema: string,
3321
+ unqs: Record<string, { __old: string; __new: string }>,
3322
+ ): JsonAlterUniqueConstraint[] => {
3323
+ return Object.values(unqs).map((it) => {
3324
+ return {
3325
+ type: 'alter_unique_constraint',
3326
+ tableName,
3327
+ old: it.__old,
3328
+ new: it.__new,
3329
+ schema,
3330
+ } as JsonAlterUniqueConstraint;
3331
+ });
3332
+ };
3333
+
3334
+ export const prepareAddCompositePrimaryKeyMySql = (
3335
+ tableName: string,
3336
+ pks: Record<string, string>,
3337
+ // TODO: remove?
3338
+ json1: MySqlSchema,
3339
+ json2: MySqlSchema,
3340
+ ): JsonCreateCompositePK[] => {
3341
+ const res: JsonCreateCompositePK[] = [];
3342
+ for (const it of Object.values(pks)) {
3343
+ const unsquashed = MySqlSquasher.unsquashPK(it);
3344
+
3345
+ if (
3346
+ unsquashed.columns.length === 1
3347
+ && json1.tables[tableName]?.columns[unsquashed.columns[0]]?.primaryKey
3348
+ ) {
3349
+ continue;
3350
+ }
3351
+
3352
+ res.push({
3353
+ type: 'create_composite_pk',
3354
+ tableName,
3355
+ data: it,
3356
+ constraintName: unsquashed.name,
3357
+ } as JsonCreateCompositePK);
3358
+ }
3359
+ return res;
3360
+ };
3361
+
3362
+ export const prepareDeleteCompositePrimaryKeyMySql = (
3363
+ tableName: string,
3364
+ pks: Record<string, string>,
3365
+ // TODO: remove?
3366
+ json1: MySqlSchema,
3367
+ ): JsonDeleteCompositePK[] => {
3368
+ return Object.values(pks).map((it) => {
3369
+ const unsquashed = MySqlSquasher.unsquashPK(it);
3370
+ return {
3371
+ type: 'delete_composite_pk',
3372
+ tableName,
3373
+ data: it,
3374
+ } as JsonDeleteCompositePK;
3375
+ });
3376
+ };
3377
+
3378
+ export const prepareAlterCompositePrimaryKeyMySql = (
3379
+ tableName: string,
3380
+ pks: Record<string, { __old: string; __new: string }>,
3381
+ // TODO: remove?
3382
+ json1: MySqlSchema,
3383
+ json2: MySqlSchema,
3384
+ ): JsonAlterCompositePK[] => {
3385
+ return Object.values(pks).map((it) => {
3386
+ return {
3387
+ type: 'alter_composite_pk',
3388
+ tableName,
3389
+ old: it.__old,
3390
+ new: it.__new,
3391
+ oldConstraintName: json1.tables[tableName].compositePrimaryKeys[
3392
+ MySqlSquasher.unsquashPK(it.__old).name
3393
+ ].name,
3394
+ newConstraintName: json2.tables[tableName].compositePrimaryKeys[
3395
+ MySqlSquasher.unsquashPK(it.__new).name
3396
+ ].name,
3397
+ } as JsonAlterCompositePK;
3398
+ });
3399
+ };
3400
+
3401
+ export const preparePgCreateViewJson = (
3402
+ name: string,
3403
+ schema: string,
3404
+ definition: string,
3405
+ materialized: boolean,
3406
+ withNoData: boolean = false,
3407
+ withOption?: any,
3408
+ using?: string,
3409
+ tablespace?: string,
3410
+ ): JsonCreatePgViewStatement => {
3411
+ return {
3412
+ type: 'create_view',
3413
+ name: name,
3414
+ schema: schema,
3415
+ definition: definition,
3416
+ with: withOption,
3417
+ materialized: materialized,
3418
+ withNoData,
3419
+ using,
3420
+ tablespace,
3421
+ };
3422
+ };
3423
+
3424
+ export const prepareMySqlCreateViewJson = (
3425
+ name: string,
3426
+ definition: string,
3427
+ meta: string,
3428
+ replace: boolean = false,
3429
+ ): JsonCreateMySqlViewStatement => {
3430
+ const { algorithm, sqlSecurity, withCheckOption } = MySqlSquasher.unsquashView(meta);
3431
+ return {
3432
+ type: 'mysql_create_view',
3433
+ name: name,
3434
+ definition: definition,
3435
+ algorithm,
3436
+ sqlSecurity,
3437
+ withCheckOption,
3438
+ replace,
3439
+ };
3440
+ };
3441
+
3442
+ /* export const prepareSingleStoreCreateViewJson = (
3443
+ name: string,
3444
+ definition: string,
3445
+ meta: string,
3446
+ replace: boolean = false,
3447
+ ): JsonCreateSingleStoreViewStatement => {
3448
+ const { algorithm, sqlSecurity, withCheckOption } = SingleStoreSquasher.unsquashView(meta);
3449
+ return {
3450
+ type: 'singlestore_create_view',
3451
+ name: name,
3452
+ definition: definition,
3453
+ algorithm,
3454
+ sqlSecurity,
3455
+ withCheckOption,
3456
+ replace,
3457
+ };
3458
+ }; */
3459
+
3460
+ export const prepareSqliteCreateViewJson = (
3461
+ name: string,
3462
+ definition: string,
3463
+ ): JsonCreateSqliteViewStatement => {
3464
+ return {
3465
+ type: 'sqlite_create_view',
3466
+ name: name,
3467
+ definition: definition,
3468
+ };
3469
+ };
3470
+
3471
+ export const prepareDropViewJson = (
3472
+ name: string,
3473
+ schema?: string,
3474
+ materialized?: boolean,
3475
+ ): JsonDropViewStatement => {
3476
+ const resObject: JsonDropViewStatement = <JsonDropViewStatement> { name, type: 'drop_view' };
3477
+
3478
+ if (schema) resObject['schema'] = schema;
3479
+
3480
+ if (materialized) resObject['materialized'] = materialized;
3481
+
3482
+ return resObject;
3483
+ };
3484
+
3485
+ export const prepareRenameViewJson = (
3486
+ to: string,
3487
+ from: string,
3488
+ schema?: string,
3489
+ materialized?: boolean,
3490
+ ): JsonRenameViewStatement => {
3491
+ const resObject: JsonRenameViewStatement = <JsonRenameViewStatement> {
3492
+ type: 'rename_view',
3493
+ nameTo: to,
3494
+ nameFrom: from,
3495
+ };
3496
+
3497
+ if (schema) resObject['schema'] = schema;
3498
+ if (materialized) resObject['materialized'] = materialized;
3499
+
3500
+ return resObject;
3501
+ };
3502
+
3503
+ export const preparePgAlterViewAlterSchemaJson = (
3504
+ to: string,
3505
+ from: string,
3506
+ name: string,
3507
+ materialized?: boolean,
3508
+ ): JsonAlterViewAlterSchemaStatement => {
3509
+ const returnObject: JsonAlterViewAlterSchemaStatement = {
3510
+ type: 'alter_view_alter_schema',
3511
+ fromSchema: from,
3512
+ toSchema: to,
3513
+ name,
3514
+ };
3515
+
3516
+ if (materialized) returnObject['materialized'] = materialized;
3517
+ return returnObject;
3518
+ };
3519
+
3520
+ export const preparePgAlterViewAddWithOptionJson = (
3521
+ name: string,
3522
+ schema: string,
3523
+ materialized: boolean,
3524
+ withOption: MatViewWithOption | ViewWithOption,
3525
+ ): JsonAlterViewAddWithOptionStatement => {
3526
+ return {
3527
+ type: 'alter_view_add_with_option',
3528
+ name,
3529
+ schema,
3530
+ materialized: materialized,
3531
+ with: withOption,
3532
+ } as JsonAlterViewAddWithOptionStatement;
3533
+ };
3534
+
3535
+ export const preparePgAlterViewDropWithOptionJson = (
3536
+ name: string,
3537
+ schema: string,
3538
+ materialized: boolean,
3539
+ withOption: MatViewWithOption | ViewWithOption,
3540
+ ): JsonAlterViewDropWithOptionStatement => {
3541
+ return {
3542
+ type: 'alter_view_drop_with_option',
3543
+ name,
3544
+ schema,
3545
+ materialized: materialized,
3546
+ with: withOption,
3547
+ } as JsonAlterViewDropWithOptionStatement;
3548
+ };
3549
+
3550
+ export const preparePgAlterViewAlterTablespaceJson = (
3551
+ name: string,
3552
+ schema: string,
3553
+ materialized: boolean,
3554
+ to: string,
3555
+ ): JsonAlterViewAlterTablespaceStatement => {
3556
+ return {
3557
+ type: 'alter_view_alter_tablespace',
3558
+ name,
3559
+ schema,
3560
+ materialized: materialized,
3561
+ toTablespace: to,
3562
+ } as JsonAlterViewAlterTablespaceStatement;
3563
+ };
3564
+
3565
+ export const preparePgAlterViewAlterUsingJson = (
3566
+ name: string,
3567
+ schema: string,
3568
+ materialized: boolean,
3569
+ to: string,
3570
+ ): JsonAlterViewAlterUsingStatement => {
3571
+ return {
3572
+ type: 'alter_view_alter_using',
3573
+ name,
3574
+ schema,
3575
+ materialized: materialized,
3576
+ toUsing: to,
3577
+ } as JsonAlterViewAlterUsingStatement;
3578
+ };
3579
+
3580
+ export const prepareMySqlAlterView = (
3581
+ view: Omit<MySqlView, 'isExisting'>,
3582
+ ): JsonAlterMySqlViewStatement => {
3583
+ return { type: 'alter_mysql_view', ...view };
3584
+ };
3585
+
3586
+ /* export const prepareSingleStoreAlterView = (
3587
+ view: Omit<SingleStoreView, 'isExisting'>,
3588
+ ): JsonAlterSingleStoreViewStatement => {
3589
+ return { type: 'alter_singlestore_view', ...view };
3590
+ }; */