@anthonyalayo/drizzle-kit 1.0.2 → 1.0.4

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 (172) hide show
  1. package/api.d.mts +2891 -0
  2. package/api.d.ts +2891 -0
  3. package/api.js +47534 -0
  4. package/api.mjs +47521 -0
  5. package/bin.cjs +80043 -0
  6. package/{src/index.ts → index-Z-1TKnbX.d.mts} +123 -141
  7. package/index-Z-1TKnbX.d.ts +308 -0
  8. package/index.d.mts +2 -0
  9. package/index.d.ts +2 -0
  10. package/index.js +32 -0
  11. package/index.mjs +7 -0
  12. package/package.json +1 -1
  13. package/utils.js +6056 -0
  14. package/utils.mjs +6032 -0
  15. package/.gitignore +0 -25
  16. package/build.dev.ts +0 -51
  17. package/build.ts +0 -112
  18. package/imports-checker/analyze.ts +0 -78
  19. package/imports-checker/checker.ts +0 -296
  20. package/imports-checker/grammar/grammar.ohm +0 -121
  21. package/imports-checker/grammar/grammar.ohm-bundle.d.ts +0 -164
  22. package/imports-checker/grammar/grammar.ohm-bundle.js +0 -753
  23. package/imports-checker/index.ts +0 -48
  24. package/patches/difflib@0.2.4.patch +0 -31
  25. package/src/@types/utils.ts +0 -58
  26. package/src/api.ts +0 -504
  27. package/src/cli/commands/_es5.ts +0 -2
  28. package/src/cli/commands/check.ts +0 -52
  29. package/src/cli/commands/drop.ts +0 -60
  30. package/src/cli/commands/introspect.ts +0 -772
  31. package/src/cli/commands/libSqlPushUtils.ts +0 -355
  32. package/src/cli/commands/migrate.ts +0 -1526
  33. package/src/cli/commands/mysqlIntrospect.ts +0 -53
  34. package/src/cli/commands/mysqlPushUtils.ts +0 -352
  35. package/src/cli/commands/mysqlUp.ts +0 -102
  36. package/src/cli/commands/pgIntrospect.ts +0 -55
  37. package/src/cli/commands/pgPushUtils.ts +0 -269
  38. package/src/cli/commands/pgUp.ts +0 -217
  39. package/src/cli/commands/push.ts +0 -637
  40. package/src/cli/commands/singlestoreIntrospect.ts +0 -53
  41. package/src/cli/commands/singlestorePushUtils.ts +0 -456
  42. package/src/cli/commands/singlestoreUp.ts +0 -1
  43. package/src/cli/commands/sqliteIntrospect.ts +0 -96
  44. package/src/cli/commands/sqlitePushUtils.ts +0 -322
  45. package/src/cli/commands/sqliteUp.ts +0 -52
  46. package/src/cli/commands/utils.ts +0 -858
  47. package/src/cli/connections.ts +0 -914
  48. package/src/cli/index.ts +0 -48
  49. package/src/cli/schema.ts +0 -794
  50. package/src/cli/selector-ui.ts +0 -38
  51. package/src/cli/utils.ts +0 -112
  52. package/src/cli/validations/cli.ts +0 -79
  53. package/src/cli/validations/common.ts +0 -193
  54. package/src/cli/validations/libsql.ts +0 -27
  55. package/src/cli/validations/mysql.ts +0 -61
  56. package/src/cli/validations/outputs.ts +0 -91
  57. package/src/cli/validations/postgres.ts +0 -83
  58. package/src/cli/validations/singlestore.ts +0 -61
  59. package/src/cli/validations/sqlite.ts +0 -104
  60. package/src/cli/validations/studio.ts +0 -24
  61. package/src/cli/views.ts +0 -658
  62. package/src/extensions/getTablesFilterByExtensions.ts +0 -16
  63. package/src/extensions/vector.ts +0 -10
  64. package/src/global.ts +0 -61
  65. package/src/introspect-mysql.ts +0 -1030
  66. package/src/introspect-pg.ts +0 -1435
  67. package/src/introspect-singlestore.ts +0 -913
  68. package/src/introspect-sqlite.ts +0 -533
  69. package/src/jsonDiffer.js +0 -882
  70. package/src/jsonStatements.ts +0 -3590
  71. package/src/loader.mjs +0 -57
  72. package/src/migrationPreparator.ts +0 -217
  73. package/src/schemaValidator.ts +0 -23
  74. package/src/serializer/index.ts +0 -142
  75. package/src/serializer/mysqlImports.ts +0 -38
  76. package/src/serializer/mysqlSchema.ts +0 -423
  77. package/src/serializer/mysqlSerializer.ts +0 -999
  78. package/src/serializer/pgImports.ts +0 -106
  79. package/src/serializer/pgSchema.ts +0 -937
  80. package/src/serializer/pgSerializer.ts +0 -2223
  81. package/src/serializer/singlestoreImports.ts +0 -38
  82. package/src/serializer/singlestoreSchema.ts +0 -257
  83. package/src/serializer/singlestoreSerializer.ts +0 -767
  84. package/src/serializer/sqliteImports.ts +0 -41
  85. package/src/serializer/sqliteSchema.ts +0 -352
  86. package/src/serializer/sqliteSerializer.ts +0 -931
  87. package/src/serializer/studio.ts +0 -694
  88. package/src/serializer/utils.ts +0 -45
  89. package/src/simulator.ts +0 -157
  90. package/src/snapshotsDiffer.ts +0 -4398
  91. package/src/sqlgenerator.ts +0 -4212
  92. package/src/statementCombiner.ts +0 -597
  93. package/src/utils/certs.ts +0 -44
  94. package/src/utils/words.ts +0 -1333
  95. package/src/utils.ts +0 -370
  96. package/tests/bin.test.ts +0 -58
  97. package/tests/cli/d1http.config.ts +0 -12
  98. package/tests/cli/drizzle.config.ts +0 -9
  99. package/tests/cli/durable-sqlite.config.ts +0 -7
  100. package/tests/cli/expo.config.ts +0 -7
  101. package/tests/cli/postgres.config.ts +0 -13
  102. package/tests/cli/postgres2.config.ts +0 -17
  103. package/tests/cli/schema.ts +0 -1
  104. package/tests/cli/turso.config.ts +0 -10
  105. package/tests/cli-export.test.ts +0 -79
  106. package/tests/cli-generate.test.ts +0 -259
  107. package/tests/cli-migrate.test.ts +0 -103
  108. package/tests/cli-push.test.ts +0 -126
  109. package/tests/common.ts +0 -15
  110. package/tests/indexes/common.ts +0 -21
  111. package/tests/indexes/pg.test.ts +0 -245
  112. package/tests/introspect/libsql.test.ts +0 -35
  113. package/tests/introspect/mysql.test.ts +0 -319
  114. package/tests/introspect/pg.test.ts +0 -894
  115. package/tests/introspect/singlestore.test.ts +0 -275
  116. package/tests/introspect/sqlite.test.ts +0 -122
  117. package/tests/libsql-checks.test.ts +0 -308
  118. package/tests/libsql-statements.test.ts +0 -989
  119. package/tests/libsql-views.test.ts +0 -218
  120. package/tests/migrate/libsq-schema.ts +0 -6
  121. package/tests/migrate/libsql-migrate.test.ts +0 -58
  122. package/tests/migrate/migrations/0000_little_blizzard.sql +0 -4
  123. package/tests/migrate/migrations/0001_nebulous_storm.sql +0 -10
  124. package/tests/migrate/migrations/meta/0000_snapshot.json +0 -40
  125. package/tests/migrate/migrations/meta/0001_snapshot.json +0 -40
  126. package/tests/migrate/migrations/meta/_journal.json +0 -20
  127. package/tests/mysql-checks.test.ts +0 -291
  128. package/tests/mysql-generated.test.ts +0 -1290
  129. package/tests/mysql-schemas.test.ts +0 -156
  130. package/tests/mysql-views.test.ts +0 -553
  131. package/tests/mysql.test.ts +0 -863
  132. package/tests/pg-array.test.ts +0 -368
  133. package/tests/pg-checks.test.ts +0 -329
  134. package/tests/pg-columns.test.ts +0 -484
  135. package/tests/pg-domains.test.ts +0 -306
  136. package/tests/pg-enums.test.ts +0 -751
  137. package/tests/pg-generated.test.ts +0 -529
  138. package/tests/pg-identity.test.ts +0 -447
  139. package/tests/pg-schemas.test.ts +0 -105
  140. package/tests/pg-sequences.test.ts +0 -298
  141. package/tests/pg-tables.test.ts +0 -1021
  142. package/tests/pg-views.test.ts +0 -1929
  143. package/tests/push/common.ts +0 -58
  144. package/tests/push/libsql.test.ts +0 -1400
  145. package/tests/push/mysql-push.test.ts +0 -345
  146. package/tests/push/mysql.test.ts +0 -788
  147. package/tests/push/pg.test.ts +0 -4341
  148. package/tests/push/singlestore-push.test.ts +0 -894
  149. package/tests/push/singlestore.test.ts +0 -444
  150. package/tests/push/sqlite.test.ts +0 -1613
  151. package/tests/rls/pg-policy.test.ts +0 -1656
  152. package/tests/rls/pg-role.test.ts +0 -234
  153. package/tests/schemaDiffer.ts +0 -2852
  154. package/tests/singlestore-generated.test.ts +0 -1290
  155. package/tests/singlestore-schemas.test.ts +0 -155
  156. package/tests/singlestore.test.ts +0 -978
  157. package/tests/sqlite-checks.test.ts +0 -308
  158. package/tests/sqlite-columns.test.ts +0 -1049
  159. package/tests/sqlite-generated.test.ts +0 -1726
  160. package/tests/sqlite-tables.test.ts +0 -637
  161. package/tests/sqlite-views.test.ts +0 -218
  162. package/tests/statements-combiner/libsql-statements-combiner.test.ts +0 -1812
  163. package/tests/statements-combiner/singlestore-statements-combiner.test.ts +0 -882
  164. package/tests/statements-combiner/sqlite-statements-combiner.test.ts +0 -1211
  165. package/tests/test/sqlite.test.ts +0 -39
  166. package/tests/testsinglestore.ts +0 -29
  167. package/tests/validations.test.ts +0 -869
  168. package/tests/wrap-param.test.ts +0 -16
  169. package/tsconfig.build.json +0 -4
  170. package/tsconfig.cli-types.json +0 -9
  171. package/tsconfig.json +0 -28
  172. package/vitest.config.ts +0 -34
@@ -1,4398 +0,0 @@
1
- import {
2
- any,
3
- array,
4
- boolean,
5
- enum as enumType,
6
- literal,
7
- never,
8
- object,
9
- record,
10
- string,
11
- TypeOf,
12
- union,
13
- ZodTypeAny,
14
- } from 'zod';
15
- import { applyJsonDiff, diffColumns, diffIndPolicies, diffPolicies, diffSchemasOrTables } from './jsonDiffer';
16
- import { fromJson } from './sqlgenerator';
17
-
18
- import {
19
- _prepareAddColumns,
20
- _prepareDropColumns,
21
- _prepareSqliteAddColumns,
22
- JsonAddColumnStatement,
23
- JsonAlterCompositePK,
24
- JsonAlterIndPolicyStatement,
25
- JsonAlterMySqlViewStatement,
26
- JsonAlterPolicyStatement,
27
- JsonAlterTableSetSchema,
28
- JsonAlterUniqueConstraint,
29
- JsonAlterViewStatement,
30
- JsonCreateCheckConstraint,
31
- JsonCreateCompositePK,
32
- JsonCreateIndPolicyStatement,
33
- JsonCreateMySqlViewStatement,
34
- JsonCreatePgViewStatement,
35
- JsonCreatePolicyStatement,
36
- JsonCreateReferenceStatement,
37
- JsonCreateSqliteViewStatement,
38
- JsonCreateUniqueConstraint,
39
- JsonDeleteCheckConstraint,
40
- JsonDeleteCompositePK,
41
- JsonDeleteUniqueConstraint,
42
- JsonDisableRLSStatement,
43
- JsonDomainStatement,
44
- JsonDropColumnStatement,
45
- JsonDropIndPolicyStatement,
46
- JsonDropPolicyStatement,
47
- JsonDropViewStatement,
48
- JsonEnableRLSStatement,
49
- JsonIndRenamePolicyStatement,
50
- JsonReferenceStatement,
51
- JsonRenameColumnStatement,
52
- JsonRenamePolicyStatement,
53
- JsonRenameRoleStatement,
54
- JsonRenameViewStatement,
55
- JsonSqliteAddColumnStatement,
56
- JsonStatement,
57
- prepareAddCheckConstraint,
58
- prepareAddCompositePrimaryKeyMySql,
59
- prepareAddCompositePrimaryKeyPg,
60
- prepareAddCompositePrimaryKeySqlite,
61
- prepareAddUniqueConstraintPg as prepareAddUniqueConstraint,
62
- prepareAddValuesToEnumJson,
63
- prepareAlterColumnsMysql,
64
- prepareAlterCompositePrimaryKeyMySql,
65
- prepareAlterCompositePrimaryKeyPg,
66
- prepareAlterCompositePrimaryKeySqlite,
67
- prepareAlterIndPolicyJson,
68
- prepareAlterPolicyJson,
69
- prepareAlterReferencesJson,
70
- prepareAlterRoleJson,
71
- prepareAlterSequenceJson,
72
- prepareCreateEnumJson,
73
- prepareCreateIndexesJson,
74
- prepareCreateIndPolicyJsons,
75
- prepareCreatePolicyJsons,
76
- prepareCreateReferencesJson,
77
- prepareCreateRoleJson,
78
- prepareCreateSchemasJson,
79
- prepareCreateSequenceJson,
80
- prepareDeleteCheckConstraint,
81
- prepareDeleteCompositePrimaryKeyMySql,
82
- prepareDeleteCompositePrimaryKeyPg,
83
- prepareDeleteCompositePrimaryKeySqlite,
84
- prepareDeleteSchemasJson as prepareDropSchemasJson,
85
- prepareDeleteUniqueConstraintPg as prepareDeleteUniqueConstraint,
86
- prepareDomainJson,
87
- prepareDropEnumJson,
88
- prepareDropEnumValues,
89
- prepareDropIndexesJson,
90
- prepareDropIndPolicyJsons,
91
- prepareDropPolicyJsons,
92
- prepareDropReferencesJson,
93
- prepareDropRoleJson,
94
- prepareDropSequenceJson,
95
- prepareDropTableJson,
96
- prepareDropViewJson,
97
- prepareLibSQLCreateReferencesJson,
98
- prepareLibSQLDropReferencesJson,
99
- prepareMoveEnumJson,
100
- prepareMoveSequenceJson,
101
- prepareMySqlAlterView,
102
- prepareMySqlCreateTableJson,
103
- prepareMySqlCreateViewJson,
104
- preparePgAlterColumns,
105
- preparePgAlterViewAddWithOptionJson,
106
- preparePgAlterViewAlterSchemaJson,
107
- preparePgAlterViewAlterTablespaceJson,
108
- preparePgAlterViewAlterUsingJson,
109
- preparePgAlterViewDropWithOptionJson,
110
- preparePgCreateIndexesJson,
111
- preparePgCreateTableJson,
112
- preparePgCreateViewJson,
113
- prepareRenameColumns,
114
- prepareRenameEnumJson,
115
- prepareRenameIndPolicyJsons,
116
- prepareRenamePolicyJsons,
117
- prepareRenameRoleJson,
118
- prepareRenameSchemasJson,
119
- prepareRenameSequenceJson,
120
- prepareRenameTableJson,
121
- prepareRenameViewJson,
122
- prepareSingleStoreCreateTableJson,
123
- prepareSqliteAlterColumns,
124
- prepareSQLiteCreateTable,
125
- prepareSqliteCreateViewJson,
126
- } from './jsonStatements';
127
-
128
- import { Named, NamedWithSchema } from './cli/commands/migrate';
129
- import { mapEntries, mapKeys, mapValues } from './global';
130
- import { MySqlSchema, MySqlSchemaSquashed, MySqlSquasher, ViewSquashed } from './serializer/mysqlSchema';
131
- import {
132
- mergedViewWithOption,
133
- PgSchema,
134
- PgSchemaSquashed,
135
- PgSquasher,
136
- Policy,
137
- policy,
138
- policySquashed,
139
- Role,
140
- roleSchema,
141
- sequenceSquashed,
142
- View,
143
- } from './serializer/pgSchema';
144
- import { SingleStoreSchema, SingleStoreSchemaSquashed, SingleStoreSquasher } from './serializer/singlestoreSchema';
145
- import { SQLiteSchema, SQLiteSchemaSquashed, SQLiteSquasher, View as SqliteView } from './serializer/sqliteSchema';
146
- import { libSQLCombineStatements, singleStoreCombineStatements, sqliteCombineStatements } from './statementCombiner';
147
- import { copy, prepareMigrationMeta } from './utils';
148
-
149
- const makeChanged = <T extends ZodTypeAny>(schema: T) => {
150
- return object({
151
- type: enumType(['changed']),
152
- old: schema,
153
- new: schema,
154
- });
155
- };
156
-
157
- const makeSelfOrChanged = <T extends ZodTypeAny>(schema: T) => {
158
- return union([
159
- schema,
160
- object({
161
- type: enumType(['changed']),
162
- old: schema,
163
- new: schema,
164
- }),
165
- ]);
166
- };
167
-
168
- export const makePatched = <T extends ZodTypeAny>(schema: T) => {
169
- return union([
170
- object({
171
- type: literal('added'),
172
- value: schema,
173
- }),
174
- object({
175
- type: literal('deleted'),
176
- value: schema,
177
- }),
178
- object({
179
- type: literal('changed'),
180
- old: schema,
181
- new: schema,
182
- }),
183
- ]);
184
- };
185
-
186
- export const makeSelfOrPatched = <T extends ZodTypeAny>(schema: T) => {
187
- return union([
188
- object({
189
- type: literal('none'),
190
- value: schema,
191
- }),
192
- object({
193
- type: literal('added'),
194
- value: schema,
195
- }),
196
- object({
197
- type: literal('deleted'),
198
- value: schema,
199
- }),
200
- object({
201
- type: literal('changed'),
202
- old: schema,
203
- new: schema,
204
- }),
205
- ]);
206
- };
207
-
208
- const columnSchema = object({
209
- name: string(),
210
- type: string(),
211
- typeSchema: string().optional(),
212
- primaryKey: boolean().optional(),
213
- default: any().optional(),
214
- notNull: boolean().optional(),
215
- // should it be optional? should if be here?
216
- autoincrement: boolean().optional(),
217
- onUpdate: boolean().optional(),
218
- isUnique: any().optional(),
219
- uniqueName: string().optional(),
220
- nullsNotDistinct: boolean().optional(),
221
- generated: object({
222
- as: string(),
223
- type: enumType(['stored', 'virtual']).default('stored'),
224
- }).optional(),
225
- identity: string().optional(),
226
- }).strict();
227
-
228
- const alteredColumnSchema = object({
229
- name: makeSelfOrChanged(string()),
230
- type: makeChanged(string()).optional(),
231
- default: makePatched(any()).optional(),
232
- primaryKey: makePatched(boolean()).optional(),
233
- notNull: makePatched(boolean()).optional(),
234
- typeSchema: makePatched(string()).optional(),
235
- onUpdate: makePatched(boolean()).optional(),
236
- autoincrement: makePatched(boolean()).optional(),
237
- generated: makePatched(
238
- object({
239
- as: string(),
240
- type: enumType(['stored', 'virtual']).default('stored'),
241
- }),
242
- ).optional(),
243
-
244
- identity: makePatched(string()).optional(),
245
- }).strict();
246
-
247
- const domainSchema = object({
248
- name: string(),
249
- schema: string(),
250
- baseType: string(),
251
- notNull: boolean().optional(),
252
- defaultValue: string().optional(),
253
- checkConstraints: record(string(), string()).optional(),
254
- }).strict();
255
-
256
- const changedDomainSchema = object({
257
- name: string(),
258
- schema: string(),
259
- baseType: string(),
260
- notNull: boolean().optional(),
261
- defaultValue: string().optional(),
262
- checkConstraints: record(string(), string()).optional(),
263
- }).strict();
264
-
265
- const enumSchema = object({
266
- name: string(),
267
- schema: string(),
268
- values: array(string()),
269
- }).strict();
270
-
271
- const changedEnumSchema = object({
272
- name: string(),
273
- schema: string(),
274
- addedValues: object({
275
- before: string(),
276
- value: string(),
277
- }).array(),
278
- deletedValues: array(string()),
279
- }).strict();
280
-
281
- const tableScheme = object({
282
- name: string(),
283
- schema: string().default(''),
284
- columns: record(string(), columnSchema),
285
- indexes: record(string(), string()),
286
- foreignKeys: record(string(), string()),
287
- compositePrimaryKeys: record(string(), string()).default({}),
288
- uniqueConstraints: record(string(), string()).default({}),
289
- policies: record(string(), string()).default({}),
290
- checkConstraints: record(string(), string()).default({}),
291
- isRLSEnabled: boolean().default(false),
292
- }).strict();
293
-
294
- export const alteredTableScheme = object({
295
- name: string(),
296
- schema: string(),
297
- altered: alteredColumnSchema.array(),
298
- addedIndexes: record(string(), string()),
299
- deletedIndexes: record(string(), string()),
300
- alteredIndexes: record(
301
- string(),
302
- object({
303
- __new: string(),
304
- __old: string(),
305
- }).strict(),
306
- ),
307
- addedForeignKeys: record(string(), string()),
308
- deletedForeignKeys: record(string(), string()),
309
- alteredForeignKeys: record(
310
- string(),
311
- object({
312
- __new: string(),
313
- __old: string(),
314
- }).strict(),
315
- ),
316
- addedCompositePKs: record(string(), string()),
317
- deletedCompositePKs: record(string(), string()),
318
- alteredCompositePKs: record(
319
- string(),
320
- object({
321
- __new: string(),
322
- __old: string(),
323
- }),
324
- ),
325
- addedUniqueConstraints: record(string(), string()),
326
- deletedUniqueConstraints: record(string(), string()),
327
- alteredUniqueConstraints: record(
328
- string(),
329
- object({
330
- __new: string(),
331
- __old: string(),
332
- }),
333
- ),
334
- addedPolicies: record(string(), string()),
335
- deletedPolicies: record(string(), string()),
336
- alteredPolicies: record(
337
- string(),
338
- object({
339
- __new: string(),
340
- __old: string(),
341
- }),
342
- ),
343
- addedCheckConstraints: record(
344
- string(),
345
- string(),
346
- ),
347
- deletedCheckConstraints: record(
348
- string(),
349
- string(),
350
- ),
351
- alteredCheckConstraints: record(
352
- string(),
353
- object({
354
- __new: string(),
355
- __old: string(),
356
- }),
357
- ),
358
- }).strict();
359
-
360
- const alteredViewCommon = object({
361
- name: string(),
362
- alteredDefinition: object({
363
- __old: string(),
364
- __new: string(),
365
- }).strict().optional(),
366
- alteredExisting: object({
367
- __old: boolean(),
368
- __new: boolean(),
369
- }).strict().optional(),
370
- });
371
-
372
- export const alteredPgViewSchema = alteredViewCommon.merge(
373
- object({
374
- schema: string(),
375
- deletedWithOption: mergedViewWithOption.optional(),
376
- addedWithOption: mergedViewWithOption.optional(),
377
- addedWith: mergedViewWithOption.optional(),
378
- deletedWith: mergedViewWithOption.optional(),
379
- alteredWith: mergedViewWithOption.optional(),
380
- alteredSchema: object({
381
- __old: string(),
382
- __new: string(),
383
- }).strict().optional(),
384
- alteredTablespace: object({
385
- __old: string(),
386
- __new: string(),
387
- }).strict().optional(),
388
- alteredUsing: object({
389
- __old: string(),
390
- __new: string(),
391
- }).strict().optional(),
392
- }).strict(),
393
- );
394
-
395
- const alteredMySqlViewSchema = alteredViewCommon.merge(
396
- object({
397
- alteredMeta: object({
398
- __old: string(),
399
- __new: string(),
400
- }).strict().optional(),
401
- }).strict(),
402
- );
403
-
404
- export const diffResultScheme = object({
405
- alteredTablesWithColumns: alteredTableScheme.array(),
406
- alteredDomains: changedDomainSchema.array(),
407
- alteredEnums: changedEnumSchema.array(),
408
- alteredSequences: sequenceSquashed.array(),
409
- alteredRoles: roleSchema.array(),
410
- alteredPolicies: policySquashed.array(),
411
- alteredViews: alteredPgViewSchema.array(),
412
- }).strict();
413
-
414
- export const diffResultSchemeMysql = object({
415
- alteredTablesWithColumns: alteredTableScheme.array(),
416
- alteredEnums: never().array(),
417
- alteredViews: alteredMySqlViewSchema.array(),
418
- });
419
-
420
- export const diffResultSchemeSingleStore = object({
421
- alteredTablesWithColumns: alteredTableScheme.array(),
422
- alteredEnums: never().array(),
423
- });
424
-
425
- export const diffResultSchemeSQLite = object({
426
- alteredTablesWithColumns: alteredTableScheme.array(),
427
- alteredEnums: never().array(),
428
- alteredViews: alteredViewCommon.array(),
429
- });
430
-
431
- export type Column = TypeOf<typeof columnSchema>;
432
- export type AlteredColumn = TypeOf<typeof alteredColumnSchema>;
433
- export type Domain = TypeOf<typeof domainSchema>;
434
- export type Enum = TypeOf<typeof enumSchema>;
435
- export type Sequence = TypeOf<typeof sequenceSquashed>;
436
- export type Table = TypeOf<typeof tableScheme>;
437
- export type AlteredTable = TypeOf<typeof alteredTableScheme>;
438
- export type DiffResult = TypeOf<typeof diffResultScheme>;
439
- export type DiffResultMysql = TypeOf<typeof diffResultSchemeMysql>;
440
- export type DiffResultSingleStore = TypeOf<typeof diffResultSchemeSingleStore>;
441
- export type DiffResultSQLite = TypeOf<typeof diffResultSchemeSQLite>;
442
-
443
- export interface ResolverInput<T extends { name: string }> {
444
- created: T[];
445
- deleted: T[];
446
- }
447
-
448
- export interface ResolverOutput<T extends { name: string }> {
449
- created: T[];
450
- renamed: { from: T; to: T }[];
451
- deleted: T[];
452
- }
453
-
454
- export interface ResolverOutputWithMoved<T extends { name: string }> {
455
- created: T[];
456
- moved: { name: string; schemaFrom: string; schemaTo: string }[];
457
- renamed: { from: T; to: T }[];
458
- deleted: T[];
459
- }
460
-
461
- export interface ColumnsResolverInput<T extends { name: string }> {
462
- tableName: string;
463
- schema: string;
464
- created: T[];
465
- deleted: T[];
466
- }
467
-
468
- export interface TablePolicyResolverInput<T extends { name: string }> {
469
- tableName: string;
470
- schema: string;
471
- created: T[];
472
- deleted: T[];
473
- }
474
-
475
- export interface TablePolicyResolverOutput<T extends { name: string }> {
476
- tableName: string;
477
- schema: string;
478
- created: T[];
479
- renamed: { from: T; to: T }[];
480
- deleted: T[];
481
- }
482
-
483
- export interface PolicyResolverInput<T extends { name: string }> {
484
- created: T[];
485
- deleted: T[];
486
- }
487
-
488
- export interface PolicyResolverOutput<T extends { name: string }> {
489
- created: T[];
490
- renamed: { from: T; to: T }[];
491
- deleted: T[];
492
- }
493
-
494
- export interface RolesResolverInput<T extends { name: string }> {
495
- created: T[];
496
- deleted: T[];
497
- }
498
-
499
- export interface RolesResolverOutput<T extends { name: string }> {
500
- created: T[];
501
- renamed: { from: T; to: T }[];
502
- deleted: T[];
503
- }
504
-
505
- export interface ColumnsResolverOutput<T extends { name: string }> {
506
- tableName: string;
507
- schema: string;
508
- created: T[];
509
- renamed: { from: T; to: T }[];
510
- deleted: T[];
511
- }
512
-
513
- const schemaChangeFor = (
514
- table: NamedWithSchema,
515
- renamedSchemas: { from: Named; to: Named }[],
516
- ) => {
517
- for (let ren of renamedSchemas) {
518
- if (table.schema === ren.from.name) {
519
- return { key: `${ren.to.name}.${table.name}`, schema: ren.to.name };
520
- }
521
- }
522
-
523
- return {
524
- key: `${table.schema || 'public'}.${table.name}`,
525
- schema: table.schema,
526
- };
527
- };
528
-
529
- const nameChangeFor = (table: Named, renamed: { from: Named; to: Named }[]) => {
530
- for (let ren of renamed) {
531
- if (table.name === ren.from.name) {
532
- return { name: ren.to.name };
533
- }
534
- }
535
-
536
- return {
537
- name: table.name,
538
- };
539
- };
540
-
541
- const nameSchemaChangeFor = (
542
- table: NamedWithSchema,
543
- renamedTables: { from: NamedWithSchema; to: NamedWithSchema }[],
544
- ) => {
545
- for (let ren of renamedTables) {
546
- if (table.name === ren.from.name && table.schema === ren.from.schema) {
547
- return {
548
- key: `${ren.to.schema || 'public'}.${ren.to.name}`,
549
- name: ren.to.name,
550
- schema: ren.to.schema,
551
- };
552
- }
553
- }
554
-
555
- return {
556
- key: `${table.schema || 'public'}.${table.name}`,
557
- name: table.name,
558
- schema: table.schema,
559
- };
560
- };
561
-
562
- const columnChangeFor = (
563
- column: string,
564
- renamedColumns: { from: Named; to: Named }[],
565
- ) => {
566
- for (let ren of renamedColumns) {
567
- if (column === ren.from.name) {
568
- return ren.to.name;
569
- }
570
- }
571
-
572
- return column;
573
- };
574
-
575
- // resolve roles same as enums
576
- // create new json statements
577
- // sql generators
578
-
579
- // tests everything!
580
-
581
- export const applyPgSnapshotsDiff = async (
582
- json1: PgSchemaSquashed,
583
- json2: PgSchemaSquashed,
584
- schemasResolver: (
585
- input: ResolverInput<Named>,
586
- ) => Promise<ResolverOutput<Named>>,
587
- domainsResolver: (
588
- input: ResolverInput<Domain>,
589
- ) => Promise<ResolverOutputWithMoved<Domain>>,
590
- enumsResolver: (
591
- input: ResolverInput<Enum>,
592
- ) => Promise<ResolverOutputWithMoved<Enum>>,
593
- sequencesResolver: (
594
- input: ResolverInput<Sequence>,
595
- ) => Promise<ResolverOutputWithMoved<Sequence>>,
596
- policyResolver: (
597
- input: TablePolicyResolverInput<Policy>,
598
- ) => Promise<TablePolicyResolverOutput<Policy>>,
599
- indPolicyResolver: (
600
- input: PolicyResolverInput<Policy>,
601
- ) => Promise<PolicyResolverOutput<Policy>>,
602
- roleResolver: (
603
- input: RolesResolverInput<Role>,
604
- ) => Promise<RolesResolverOutput<Role>>,
605
- tablesResolver: (
606
- input: ResolverInput<Table>,
607
- ) => Promise<ResolverOutputWithMoved<Table>>,
608
- columnsResolver: (
609
- input: ColumnsResolverInput<Column>,
610
- ) => Promise<ColumnsResolverOutput<Column>>,
611
- viewsResolver: (
612
- input: ResolverInput<View>,
613
- ) => Promise<ResolverOutputWithMoved<View>>,
614
- prevFull: PgSchema,
615
- curFull: PgSchema,
616
- action?: 'push' | undefined,
617
- ): Promise<{
618
- statements: JsonStatement[];
619
- sqlStatements: string[];
620
- _meta:
621
- | {
622
- schemas: {};
623
- tables: {};
624
- columns: {};
625
- }
626
- | undefined;
627
- }> => {
628
- const schemasDiff = diffSchemasOrTables(json1.schemas, json2.schemas);
629
-
630
- const {
631
- created: createdSchemas,
632
- deleted: deletedSchemas,
633
- renamed: renamedSchemas,
634
- } = await schemasResolver({
635
- created: schemasDiff.added.map((it) => ({ name: it })),
636
- deleted: schemasDiff.deleted.map((it) => ({ name: it })),
637
- });
638
-
639
- const schemasPatchedSnap1 = copy(json1);
640
- schemasPatchedSnap1.tables = mapEntries(
641
- schemasPatchedSnap1.tables,
642
- (_, it) => {
643
- const { key, schema } = schemaChangeFor(it, renamedSchemas);
644
- it.schema = schema;
645
- return [key, it];
646
- },
647
- );
648
-
649
- schemasPatchedSnap1.enums = mapEntries(schemasPatchedSnap1.enums, (_, it) => {
650
- const { key, schema } = schemaChangeFor(it, renamedSchemas);
651
- it.schema = schema;
652
- return [key, it];
653
- });
654
-
655
- const domainsDiff = diffSchemasOrTables(json1.domains, json2.domains);
656
-
657
- const {
658
- created: createdDomains,
659
- deleted: deletedDomains,
660
- renamed: renamedDomains,
661
- moved: movedDomains,
662
- } = await domainsResolver({
663
- created: domainsDiff.added,
664
- deleted: domainsDiff.deleted,
665
- });
666
-
667
- const enumsDiff = diffSchemasOrTables(schemasPatchedSnap1.enums, json2.enums);
668
-
669
- const {
670
- created: createdEnums,
671
- deleted: deletedEnums,
672
- renamed: renamedEnums,
673
- moved: movedEnums,
674
- } = await enumsResolver({
675
- created: enumsDiff.added,
676
- deleted: enumsDiff.deleted,
677
- });
678
-
679
- schemasPatchedSnap1.enums = mapEntries(schemasPatchedSnap1.enums, (_, it) => {
680
- const { key, name, schema } = nameSchemaChangeFor(it, renamedEnums);
681
- it.name = name;
682
- it.schema = schema;
683
- return [key, it];
684
- });
685
-
686
- const columnTypesChangeMap = renamedEnums.reduce(
687
- (acc, it) => {
688
- acc[`${it.from.schema}.${it.from.name}`] = {
689
- nameFrom: it.from.name,
690
- nameTo: it.to.name,
691
- schemaFrom: it.from.schema,
692
- schemaTo: it.to.schema,
693
- };
694
- return acc;
695
- },
696
- {} as Record<
697
- string,
698
- {
699
- nameFrom: string;
700
- nameTo: string;
701
- schemaFrom: string;
702
- schemaTo: string;
703
- }
704
- >,
705
- );
706
-
707
- const columnTypesMovesMap = movedEnums.reduce(
708
- (acc, it) => {
709
- acc[`${it.schemaFrom}.${it.name}`] = {
710
- nameFrom: it.name,
711
- nameTo: it.name,
712
- schemaFrom: it.schemaFrom,
713
- schemaTo: it.schemaTo,
714
- };
715
- return acc;
716
- },
717
- {} as Record<
718
- string,
719
- {
720
- nameFrom: string;
721
- nameTo: string;
722
- schemaFrom: string;
723
- schemaTo: string;
724
- }
725
- >,
726
- );
727
-
728
- schemasPatchedSnap1.tables = mapEntries(
729
- schemasPatchedSnap1.tables,
730
- (tableKey, tableValue) => {
731
- const patchedColumns = mapValues(tableValue.columns, (column) => {
732
- const key = `${column.typeSchema || 'public'}.${column.type}`;
733
- const change = columnTypesChangeMap[key] || columnTypesMovesMap[key];
734
-
735
- if (change) {
736
- column.type = change.nameTo;
737
- column.typeSchema = change.schemaTo;
738
- }
739
-
740
- return column;
741
- });
742
-
743
- tableValue.columns = patchedColumns;
744
- return [tableKey, tableValue];
745
- },
746
- );
747
-
748
- schemasPatchedSnap1.sequences = mapEntries(
749
- schemasPatchedSnap1.sequences,
750
- (_, it) => {
751
- const { key, schema } = schemaChangeFor(it, renamedSchemas);
752
- it.schema = schema;
753
- return [key, it];
754
- },
755
- );
756
-
757
- const sequencesDiff = diffSchemasOrTables(
758
- schemasPatchedSnap1.sequences,
759
- json2.sequences,
760
- );
761
-
762
- const {
763
- created: createdSequences,
764
- deleted: deletedSequences,
765
- renamed: renamedSequences,
766
- moved: movedSequences,
767
- } = await sequencesResolver({
768
- created: sequencesDiff.added,
769
- deleted: sequencesDiff.deleted,
770
- });
771
-
772
- schemasPatchedSnap1.sequences = mapEntries(
773
- schemasPatchedSnap1.sequences,
774
- (_, it) => {
775
- const { key, name, schema } = nameSchemaChangeFor(it, renamedSequences);
776
- it.name = name;
777
- it.schema = schema;
778
- return [key, it];
779
- },
780
- );
781
-
782
- const sequencesChangeMap = renamedSequences.reduce(
783
- (acc, it) => {
784
- acc[`${it.from.schema}.${it.from.name}`] = {
785
- nameFrom: it.from.name,
786
- nameTo: it.to.name,
787
- schemaFrom: it.from.schema,
788
- schemaTo: it.to.schema,
789
- };
790
- return acc;
791
- },
792
- {} as Record<
793
- string,
794
- {
795
- nameFrom: string;
796
- nameTo: string;
797
- schemaFrom: string;
798
- schemaTo: string;
799
- }
800
- >,
801
- );
802
-
803
- const sequencesMovesMap = movedSequences.reduce(
804
- (acc, it) => {
805
- acc[`${it.schemaFrom}.${it.name}`] = {
806
- nameFrom: it.name,
807
- nameTo: it.name,
808
- schemaFrom: it.schemaFrom,
809
- schemaTo: it.schemaTo,
810
- };
811
- return acc;
812
- },
813
- {} as Record<
814
- string,
815
- {
816
- nameFrom: string;
817
- nameTo: string;
818
- schemaFrom: string;
819
- schemaTo: string;
820
- }
821
- >,
822
- );
823
-
824
- schemasPatchedSnap1.tables = mapEntries(
825
- schemasPatchedSnap1.tables,
826
- (tableKey, tableValue) => {
827
- const patchedColumns = mapValues(tableValue.columns, (column) => {
828
- const key = `${column.typeSchema || 'public'}.${column.type}`;
829
- const change = sequencesChangeMap[key] || sequencesMovesMap[key];
830
-
831
- if (change) {
832
- column.type = change.nameTo;
833
- column.typeSchema = change.schemaTo;
834
- }
835
-
836
- return column;
837
- });
838
-
839
- tableValue.columns = patchedColumns;
840
- return [tableKey, tableValue];
841
- },
842
- );
843
-
844
- const rolesDiff = diffSchemasOrTables(
845
- schemasPatchedSnap1.roles,
846
- json2.roles,
847
- );
848
-
849
- const {
850
- created: createdRoles,
851
- deleted: deletedRoles,
852
- renamed: renamedRoles,
853
- } = await roleResolver({
854
- created: rolesDiff.added,
855
- deleted: rolesDiff.deleted,
856
- });
857
-
858
- schemasPatchedSnap1.roles = mapEntries(
859
- schemasPatchedSnap1.roles,
860
- (_, it) => {
861
- const { name } = nameChangeFor(it, renamedRoles);
862
- it.name = name;
863
- return [name, it];
864
- },
865
- );
866
-
867
- const rolesChangeMap = renamedRoles.reduce(
868
- (acc, it) => {
869
- acc[it.from.name] = {
870
- nameFrom: it.from.name,
871
- nameTo: it.to.name,
872
- };
873
- return acc;
874
- },
875
- {} as Record<
876
- string,
877
- {
878
- nameFrom: string;
879
- nameTo: string;
880
- }
881
- >,
882
- );
883
-
884
- schemasPatchedSnap1.roles = mapEntries(
885
- schemasPatchedSnap1.roles,
886
- (roleKey, roleValue) => {
887
- const key = roleKey;
888
- const change = rolesChangeMap[key];
889
-
890
- if (change) {
891
- roleValue.name = change.nameTo;
892
- }
893
-
894
- return [roleKey, roleValue];
895
- },
896
- );
897
-
898
- const tablesDiff = diffSchemasOrTables(
899
- schemasPatchedSnap1.tables as Record<string, any>,
900
- json2.tables,
901
- );
902
-
903
- const {
904
- created: createdTables,
905
- deleted: deletedTables,
906
- moved: movedTables,
907
- renamed: renamedTables, // renamed or moved
908
- } = await tablesResolver({
909
- created: tablesDiff.added,
910
- deleted: tablesDiff.deleted,
911
- });
912
-
913
- const tablesPatchedSnap1 = copy(schemasPatchedSnap1);
914
- tablesPatchedSnap1.tables = mapEntries(tablesPatchedSnap1.tables, (_, it) => {
915
- const { key, name, schema } = nameSchemaChangeFor(it, renamedTables);
916
- it.name = name;
917
- it.schema = schema;
918
- return [key, it];
919
- });
920
-
921
- const res = diffColumns(tablesPatchedSnap1.tables, json2.tables);
922
-
923
- const columnRenames = [] as {
924
- table: string;
925
- schema: string;
926
- renames: { from: Column; to: Column }[];
927
- }[];
928
-
929
- const columnCreates = [] as {
930
- table: string;
931
- schema: string;
932
- columns: Column[];
933
- }[];
934
-
935
- const columnDeletes = [] as {
936
- table: string;
937
- schema: string;
938
- columns: Column[];
939
- }[];
940
-
941
- for (let entry of Object.values(res)) {
942
- const { renamed, created, deleted } = await columnsResolver({
943
- tableName: entry.name,
944
- schema: entry.schema,
945
- deleted: entry.columns.deleted,
946
- created: entry.columns.added,
947
- });
948
-
949
- if (created.length > 0) {
950
- columnCreates.push({
951
- table: entry.name,
952
- schema: entry.schema,
953
- columns: created,
954
- });
955
- }
956
-
957
- if (deleted.length > 0) {
958
- columnDeletes.push({
959
- table: entry.name,
960
- schema: entry.schema,
961
- columns: deleted,
962
- });
963
- }
964
-
965
- if (renamed.length > 0) {
966
- columnRenames.push({
967
- table: entry.name,
968
- schema: entry.schema,
969
- renames: renamed,
970
- });
971
- }
972
- }
973
-
974
- const columnRenamesDict = columnRenames.reduce(
975
- (acc, it) => {
976
- acc[`${it.schema || 'public'}.${it.table}`] = it.renames;
977
- return acc;
978
- },
979
- {} as Record<
980
- string,
981
- {
982
- from: Named;
983
- to: Named;
984
- }[]
985
- >,
986
- );
987
-
988
- const columnsPatchedSnap1 = copy(tablesPatchedSnap1);
989
- columnsPatchedSnap1.tables = mapEntries(
990
- columnsPatchedSnap1.tables,
991
- (tableKey, tableValue) => {
992
- const patchedColumns = mapKeys(
993
- tableValue.columns,
994
- (columnKey, column) => {
995
- const rens = columnRenamesDict[
996
- `${tableValue.schema || 'public'}.${tableValue.name}`
997
- ] || [];
998
-
999
- const newName = columnChangeFor(columnKey, rens);
1000
- column.name = newName;
1001
- return newName;
1002
- },
1003
- );
1004
-
1005
- tableValue.columns = patchedColumns;
1006
- return [tableKey, tableValue];
1007
- },
1008
- );
1009
-
1010
- //// Policies
1011
-
1012
- const policyRes = diffPolicies(tablesPatchedSnap1.tables, json2.tables);
1013
-
1014
- const policyRenames = [] as {
1015
- table: string;
1016
- schema: string;
1017
- renames: { from: Policy; to: Policy }[];
1018
- }[];
1019
-
1020
- const policyCreates = [] as {
1021
- table: string;
1022
- schema: string;
1023
- columns: Policy[];
1024
- }[];
1025
-
1026
- const policyDeletes = [] as {
1027
- table: string;
1028
- schema: string;
1029
- columns: Policy[];
1030
- }[];
1031
-
1032
- for (let entry of Object.values(policyRes)) {
1033
- const { renamed, created, deleted } = await policyResolver({
1034
- tableName: entry.name,
1035
- schema: entry.schema,
1036
- deleted: entry.policies.deleted.map(
1037
- action === 'push' ? PgSquasher.unsquashPolicyPush : PgSquasher.unsquashPolicy,
1038
- ),
1039
- created: entry.policies.added.map(action === 'push' ? PgSquasher.unsquashPolicyPush : PgSquasher.unsquashPolicy),
1040
- });
1041
-
1042
- if (created.length > 0) {
1043
- policyCreates.push({
1044
- table: entry.name,
1045
- schema: entry.schema,
1046
- columns: created,
1047
- });
1048
- }
1049
-
1050
- if (deleted.length > 0) {
1051
- policyDeletes.push({
1052
- table: entry.name,
1053
- schema: entry.schema,
1054
- columns: deleted,
1055
- });
1056
- }
1057
-
1058
- if (renamed.length > 0) {
1059
- policyRenames.push({
1060
- table: entry.name,
1061
- schema: entry.schema,
1062
- renames: renamed,
1063
- });
1064
- }
1065
- }
1066
-
1067
- const policyRenamesDict = columnRenames.reduce(
1068
- (acc, it) => {
1069
- acc[`${it.schema || 'public'}.${it.table}`] = it.renames;
1070
- return acc;
1071
- },
1072
- {} as Record<
1073
- string,
1074
- {
1075
- from: Named;
1076
- to: Named;
1077
- }[]
1078
- >,
1079
- );
1080
-
1081
- const policyPatchedSnap1 = copy(tablesPatchedSnap1);
1082
- policyPatchedSnap1.tables = mapEntries(
1083
- policyPatchedSnap1.tables,
1084
- (tableKey, tableValue) => {
1085
- const patchedPolicies = mapKeys(
1086
- tableValue.policies,
1087
- (policyKey, policy) => {
1088
- const rens = policyRenamesDict[
1089
- `${tableValue.schema || 'public'}.${tableValue.name}`
1090
- ] || [];
1091
-
1092
- const newName = columnChangeFor(policyKey, rens);
1093
- const unsquashedPolicy = action === 'push'
1094
- ? PgSquasher.unsquashPolicyPush(policy)
1095
- : PgSquasher.unsquashPolicy(policy);
1096
- unsquashedPolicy.name = newName;
1097
- policy = PgSquasher.squashPolicy(unsquashedPolicy);
1098
- return newName;
1099
- },
1100
- );
1101
-
1102
- tableValue.policies = patchedPolicies;
1103
- return [tableKey, tableValue];
1104
- },
1105
- );
1106
-
1107
- //// Individual policies
1108
-
1109
- const indPolicyRes = diffIndPolicies(policyPatchedSnap1.policies, json2.policies);
1110
-
1111
- const indPolicyCreates = [] as {
1112
- policies: Policy[];
1113
- }[];
1114
-
1115
- const indPolicyDeletes = [] as {
1116
- policies: Policy[];
1117
- }[];
1118
-
1119
- const { renamed: indPolicyRenames, created, deleted } = await indPolicyResolver({
1120
- deleted: indPolicyRes.deleted.map((t) =>
1121
- action === 'push' ? PgSquasher.unsquashPolicyPush(t.values) : PgSquasher.unsquashPolicy(t.values)
1122
- ),
1123
- created: indPolicyRes.added.map((t) =>
1124
- action === 'push' ? PgSquasher.unsquashPolicyPush(t.values) : PgSquasher.unsquashPolicy(t.values)
1125
- ),
1126
- });
1127
-
1128
- if (created.length > 0) {
1129
- indPolicyCreates.push({
1130
- policies: created,
1131
- });
1132
- }
1133
-
1134
- if (deleted.length > 0) {
1135
- indPolicyDeletes.push({
1136
- policies: deleted,
1137
- });
1138
- }
1139
-
1140
- const indPolicyRenamesDict = indPolicyRenames.reduce(
1141
- (acc, it) => {
1142
- acc[it.from.name] = {
1143
- nameFrom: it.from.name,
1144
- nameTo: it.to.name,
1145
- };
1146
- return acc;
1147
- },
1148
- {} as Record<
1149
- string,
1150
- {
1151
- nameFrom: string;
1152
- nameTo: string;
1153
- }
1154
- >,
1155
- );
1156
-
1157
- const indPolicyPatchedSnap1 = copy(policyPatchedSnap1);
1158
- indPolicyPatchedSnap1.policies = mapEntries(
1159
- indPolicyPatchedSnap1.policies,
1160
- (policyKey, policyValue) => {
1161
- const key = policyKey;
1162
- const change = indPolicyRenamesDict[key];
1163
-
1164
- if (change) {
1165
- policyValue.name = change.nameTo;
1166
- }
1167
-
1168
- return [policyKey, policyValue];
1169
- },
1170
- );
1171
-
1172
- ////
1173
- const viewsDiff = diffSchemasOrTables(indPolicyPatchedSnap1.views, json2.views);
1174
-
1175
- const {
1176
- created: createdViews,
1177
- deleted: deletedViews,
1178
- renamed: renamedViews,
1179
- moved: movedViews,
1180
- } = await viewsResolver({
1181
- created: viewsDiff.added,
1182
- deleted: viewsDiff.deleted,
1183
- });
1184
-
1185
- const renamesViewDic: Record<string, { to: string; from: string }> = {};
1186
- renamedViews.forEach((it) => {
1187
- renamesViewDic[`${it.from.schema}.${it.from.name}`] = { to: it.to.name, from: it.from.name };
1188
- });
1189
-
1190
- const movedViewDic: Record<string, { to: string; from: string }> = {};
1191
- movedViews.forEach((it) => {
1192
- movedViewDic[`${it.schemaFrom}.${it.name}`] = { to: it.schemaTo, from: it.schemaFrom };
1193
- });
1194
-
1195
- const viewsPatchedSnap1 = copy(policyPatchedSnap1);
1196
- viewsPatchedSnap1.views = mapEntries(
1197
- viewsPatchedSnap1.views,
1198
- (viewKey, viewValue) => {
1199
- const rename = renamesViewDic[`${viewValue.schema}.${viewValue.name}`];
1200
- const moved = movedViewDic[`${viewValue.schema}.${viewValue.name}`];
1201
-
1202
- if (rename) {
1203
- viewValue.name = rename.to;
1204
- viewKey = `${viewValue.schema}.${viewValue.name}`;
1205
- }
1206
-
1207
- if (moved) viewKey = `${moved.to}.${viewValue.name}`;
1208
-
1209
- return [viewKey, viewValue];
1210
- },
1211
- );
1212
-
1213
- const diffResult = applyJsonDiff(viewsPatchedSnap1, json2);
1214
-
1215
- const typedResult: DiffResult = diffResultScheme.parse(diffResult);
1216
-
1217
- const jsonStatements: JsonStatement[] = [];
1218
-
1219
- const jsonCreateIndexesForCreatedTables = createdTables
1220
- .map((it) => {
1221
- return preparePgCreateIndexesJson(
1222
- it.name,
1223
- it.schema,
1224
- it.indexes,
1225
- curFull,
1226
- action,
1227
- );
1228
- })
1229
- .flat();
1230
-
1231
- const jsonDropTables = deletedTables.map((it) => {
1232
- return prepareDropTableJson(it);
1233
- });
1234
-
1235
- const jsonRenameTables = renamedTables.map((it) => {
1236
- return prepareRenameTableJson(it.from, it.to);
1237
- });
1238
-
1239
- const alteredTables = typedResult.alteredTablesWithColumns;
1240
-
1241
- const jsonRenameColumnsStatements: JsonRenameColumnStatement[] = [];
1242
- const jsonDropColumnsStatemets: JsonDropColumnStatement[] = [];
1243
- const jsonAddColumnsStatemets: JsonAddColumnStatement[] = [];
1244
-
1245
- for (let it of columnRenames) {
1246
- jsonRenameColumnsStatements.push(
1247
- ...prepareRenameColumns(it.table, it.schema, it.renames),
1248
- );
1249
- }
1250
-
1251
- for (let it of columnDeletes) {
1252
- jsonDropColumnsStatemets.push(
1253
- ..._prepareDropColumns(it.table, it.schema, it.columns),
1254
- );
1255
- }
1256
-
1257
- for (let it of columnCreates) {
1258
- jsonAddColumnsStatemets.push(
1259
- ..._prepareAddColumns(it.table, it.schema, it.columns),
1260
- );
1261
- }
1262
-
1263
- const jsonAddedCompositePKs: JsonCreateCompositePK[] = [];
1264
- const jsonDeletedCompositePKs: JsonDeleteCompositePK[] = [];
1265
- const jsonAlteredCompositePKs: JsonAlterCompositePK[] = [];
1266
-
1267
- const jsonAddedUniqueConstraints: JsonCreateUniqueConstraint[] = [];
1268
- const jsonDeletedUniqueConstraints: JsonDeleteUniqueConstraint[] = [];
1269
- const jsonAlteredUniqueConstraints: JsonAlterUniqueConstraint[] = [];
1270
-
1271
- const jsonSetTableSchemas: JsonAlterTableSetSchema[] = [];
1272
-
1273
- if (movedTables) {
1274
- for (let it of movedTables) {
1275
- jsonSetTableSchemas.push({
1276
- type: 'alter_table_set_schema',
1277
- tableName: it.name,
1278
- schemaFrom: it.schemaFrom || 'public',
1279
- schemaTo: it.schemaTo || 'public',
1280
- });
1281
- }
1282
- }
1283
-
1284
- const jsonDeletedCheckConstraints: JsonDeleteCheckConstraint[] = [];
1285
- const jsonCreatedCheckConstraints: JsonCreateCheckConstraint[] = [];
1286
-
1287
- for (let it of alteredTables) {
1288
- // This part is needed to make sure that same columns in a table are not triggered for change
1289
- // there is a case where orm and kit are responsible for pk name generation and one of them is not sorting name
1290
- // We double-check that pk with same set of columns are both in added and deleted diffs
1291
- let addedColumns: { name: string; columns: string[] } | undefined;
1292
- for (const addedPkName of Object.keys(it.addedCompositePKs)) {
1293
- const addedPkColumns = it.addedCompositePKs[addedPkName];
1294
- addedColumns = PgSquasher.unsquashPK(addedPkColumns);
1295
- }
1296
-
1297
- let deletedColumns: { name: string; columns: string[] } | undefined;
1298
- for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
1299
- const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
1300
- deletedColumns = PgSquasher.unsquashPK(deletedPkColumns);
1301
- }
1302
-
1303
- // Don't need to sort, but need to add tests for it
1304
- // addedColumns.sort();
1305
- // deletedColumns.sort();
1306
- const doPerformDeleteAndCreate = JSON.stringify(addedColumns ?? {}) !== JSON.stringify(deletedColumns ?? {});
1307
-
1308
- let addedCompositePKs: JsonCreateCompositePK[] = [];
1309
- let deletedCompositePKs: JsonDeleteCompositePK[] = [];
1310
- let alteredCompositePKs: JsonAlterCompositePK[] = [];
1311
- if (doPerformDeleteAndCreate) {
1312
- addedCompositePKs = prepareAddCompositePrimaryKeyPg(
1313
- it.name,
1314
- it.schema,
1315
- it.addedCompositePKs,
1316
- curFull as PgSchema,
1317
- );
1318
- deletedCompositePKs = prepareDeleteCompositePrimaryKeyPg(
1319
- it.name,
1320
- it.schema,
1321
- it.deletedCompositePKs,
1322
- prevFull as PgSchema,
1323
- );
1324
- }
1325
- alteredCompositePKs = prepareAlterCompositePrimaryKeyPg(
1326
- it.name,
1327
- it.schema,
1328
- it.alteredCompositePKs,
1329
- prevFull as PgSchema,
1330
- curFull as PgSchema,
1331
- );
1332
-
1333
- // add logic for unique constraints
1334
- let addedUniqueConstraints: JsonCreateUniqueConstraint[] = [];
1335
- let deletedUniqueConstraints: JsonDeleteUniqueConstraint[] = [];
1336
- let alteredUniqueConstraints: JsonAlterUniqueConstraint[] = [];
1337
- let createCheckConstraints: JsonCreateCheckConstraint[] = [];
1338
- let deleteCheckConstraints: JsonDeleteCheckConstraint[] = [];
1339
-
1340
- addedUniqueConstraints = prepareAddUniqueConstraint(
1341
- it.name,
1342
- it.schema,
1343
- it.addedUniqueConstraints,
1344
- );
1345
- deletedUniqueConstraints = prepareDeleteUniqueConstraint(
1346
- it.name,
1347
- it.schema,
1348
- it.deletedUniqueConstraints,
1349
- );
1350
- if (it.alteredUniqueConstraints) {
1351
- const added: Record<string, string> = {};
1352
- const deleted: Record<string, string> = {};
1353
- for (const k of Object.keys(it.alteredUniqueConstraints)) {
1354
- added[k] = it.alteredUniqueConstraints[k].__new;
1355
- deleted[k] = it.alteredUniqueConstraints[k].__old;
1356
- }
1357
- addedUniqueConstraints.push(
1358
- ...prepareAddUniqueConstraint(it.name, it.schema, added),
1359
- );
1360
- deletedUniqueConstraints.push(
1361
- ...prepareDeleteUniqueConstraint(it.name, it.schema, deleted),
1362
- );
1363
- }
1364
-
1365
- createCheckConstraints = prepareAddCheckConstraint(it.name, it.schema, it.addedCheckConstraints);
1366
- deleteCheckConstraints = prepareDeleteCheckConstraint(
1367
- it.name,
1368
- it.schema,
1369
- it.deletedCheckConstraints,
1370
- );
1371
-
1372
- if (it.alteredCheckConstraints && action !== 'push') {
1373
- const added: Record<string, string> = {};
1374
- const deleted: Record<string, string> = {};
1375
-
1376
- for (const k of Object.keys(it.alteredCheckConstraints)) {
1377
- added[k] = it.alteredCheckConstraints[k].__new;
1378
- deleted[k] = it.alteredCheckConstraints[k].__old;
1379
- }
1380
- createCheckConstraints.push(...prepareAddCheckConstraint(it.name, it.schema, added));
1381
- deleteCheckConstraints.push(...prepareDeleteCheckConstraint(it.name, it.schema, deleted));
1382
- }
1383
-
1384
- jsonCreatedCheckConstraints.push(...createCheckConstraints);
1385
- jsonDeletedCheckConstraints.push(...deleteCheckConstraints);
1386
-
1387
- jsonAddedCompositePKs.push(...addedCompositePKs);
1388
- jsonDeletedCompositePKs.push(...deletedCompositePKs);
1389
- jsonAlteredCompositePKs.push(...alteredCompositePKs);
1390
-
1391
- jsonAddedUniqueConstraints.push(...addedUniqueConstraints);
1392
- jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
1393
- jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
1394
- }
1395
-
1396
- const rColumns = jsonRenameColumnsStatements.map((it) => {
1397
- const tableName = it.tableName;
1398
- const schema = it.schema;
1399
- return {
1400
- from: { schema, table: tableName, column: it.oldColumnName },
1401
- to: { schema, table: tableName, column: it.newColumnName },
1402
- };
1403
- });
1404
-
1405
- const jsonTableAlternations = alteredTables
1406
- .map((it) => {
1407
- return preparePgAlterColumns(
1408
- it.name,
1409
- it.schema,
1410
- it.altered,
1411
- json2,
1412
- action,
1413
- );
1414
- })
1415
- .flat();
1416
-
1417
- const jsonCreateIndexesFoAlteredTables = alteredTables
1418
- .map((it) => {
1419
- return preparePgCreateIndexesJson(
1420
- it.name,
1421
- it.schema,
1422
- it.addedIndexes || {},
1423
- curFull,
1424
- action,
1425
- );
1426
- })
1427
- .flat();
1428
-
1429
- const jsonDropIndexesForAllAlteredTables = alteredTables
1430
- .map((it) => {
1431
- return prepareDropIndexesJson(
1432
- it.name,
1433
- it.schema,
1434
- it.deletedIndexes || {},
1435
- );
1436
- })
1437
- .flat();
1438
-
1439
- const jsonCreatePoliciesStatements: JsonCreatePolicyStatement[] = [];
1440
- const jsonDropPoliciesStatements: JsonDropPolicyStatement[] = [];
1441
- const jsonAlterPoliciesStatements: JsonAlterPolicyStatement[] = [];
1442
- const jsonRenamePoliciesStatements: JsonRenamePolicyStatement[] = [];
1443
-
1444
- const jsonRenameIndPoliciesStatements: JsonIndRenamePolicyStatement[] = [];
1445
- const jsonCreateIndPoliciesStatements: JsonCreateIndPolicyStatement[] = [];
1446
- const jsonDropIndPoliciesStatements: JsonDropIndPolicyStatement[] = [];
1447
- const jsonAlterIndPoliciesStatements: JsonAlterIndPolicyStatement[] = [];
1448
-
1449
- const jsonEnableRLSStatements: JsonEnableRLSStatement[] = [];
1450
- const jsonDisableRLSStatements: JsonDisableRLSStatement[] = [];
1451
-
1452
- for (let it of indPolicyRenames) {
1453
- jsonRenameIndPoliciesStatements.push(
1454
- ...prepareRenameIndPolicyJsons([it]),
1455
- );
1456
- }
1457
-
1458
- for (const it of indPolicyCreates) {
1459
- jsonCreateIndPoliciesStatements.push(
1460
- ...prepareCreateIndPolicyJsons(
1461
- it.policies,
1462
- ),
1463
- );
1464
- }
1465
-
1466
- for (const it of indPolicyDeletes) {
1467
- jsonDropIndPoliciesStatements.push(
1468
- ...prepareDropIndPolicyJsons(
1469
- it.policies,
1470
- ),
1471
- );
1472
- }
1473
-
1474
- typedResult.alteredPolicies.forEach(({ values }) => {
1475
- // return prepareAlterIndPolicyJson(json1.policies[it.name], json2.policies[it.name]);
1476
-
1477
- const policy = action === 'push' ? PgSquasher.unsquashPolicyPush(values) : PgSquasher.unsquashPolicy(values);
1478
-
1479
- const newPolicy = action === 'push'
1480
- ? PgSquasher.unsquashPolicyPush(json2.policies[policy.name].values)
1481
- : PgSquasher.unsquashPolicy(json2.policies[policy.name].values);
1482
- const oldPolicy = action === 'push'
1483
- ? PgSquasher.unsquashPolicyPush(json2.policies[policy.name].values)
1484
- : PgSquasher.unsquashPolicy(json1.policies[policy.name].values);
1485
-
1486
- if (newPolicy.as !== oldPolicy.as) {
1487
- jsonDropIndPoliciesStatements.push(
1488
- ...prepareDropIndPolicyJsons(
1489
- [oldPolicy],
1490
- ),
1491
- );
1492
-
1493
- jsonCreateIndPoliciesStatements.push(
1494
- ...prepareCreateIndPolicyJsons(
1495
- [newPolicy],
1496
- ),
1497
- );
1498
- return;
1499
- }
1500
-
1501
- if (newPolicy.for !== oldPolicy.for) {
1502
- jsonDropIndPoliciesStatements.push(
1503
- ...prepareDropIndPolicyJsons(
1504
- [oldPolicy],
1505
- ),
1506
- );
1507
-
1508
- jsonCreateIndPoliciesStatements.push(
1509
- ...prepareCreateIndPolicyJsons(
1510
- [newPolicy],
1511
- ),
1512
- );
1513
- return;
1514
- }
1515
-
1516
- // alter
1517
- jsonAlterIndPoliciesStatements.push(
1518
- prepareAlterIndPolicyJson(
1519
- oldPolicy,
1520
- newPolicy,
1521
- ),
1522
- );
1523
- });
1524
-
1525
- for (let it of policyRenames) {
1526
- jsonRenamePoliciesStatements.push(
1527
- ...prepareRenamePolicyJsons(it.table, it.schema, it.renames),
1528
- );
1529
- }
1530
-
1531
- for (const it of policyCreates) {
1532
- jsonCreatePoliciesStatements.push(
1533
- ...prepareCreatePolicyJsons(
1534
- it.table,
1535
- it.schema,
1536
- it.columns,
1537
- ),
1538
- );
1539
- }
1540
-
1541
- for (const it of policyDeletes) {
1542
- jsonDropPoliciesStatements.push(
1543
- ...prepareDropPolicyJsons(
1544
- it.table,
1545
- it.schema,
1546
- it.columns,
1547
- ),
1548
- );
1549
- }
1550
-
1551
- alteredTables.forEach((it) => {
1552
- // handle policies
1553
- Object.keys(it.alteredPolicies).forEach((policyName: string) => {
1554
- const newPolicy = action === 'push'
1555
- ? PgSquasher.unsquashPolicyPush(it.alteredPolicies[policyName].__new)
1556
- : PgSquasher.unsquashPolicy(it.alteredPolicies[policyName].__new);
1557
- const oldPolicy = action === 'push'
1558
- ? PgSquasher.unsquashPolicyPush(it.alteredPolicies[policyName].__old)
1559
- : PgSquasher.unsquashPolicy(it.alteredPolicies[policyName].__old);
1560
-
1561
- if (newPolicy.as !== oldPolicy.as) {
1562
- jsonDropPoliciesStatements.push(
1563
- ...prepareDropPolicyJsons(
1564
- it.name,
1565
- it.schema,
1566
- [oldPolicy],
1567
- ),
1568
- );
1569
-
1570
- jsonCreatePoliciesStatements.push(
1571
- ...prepareCreatePolicyJsons(
1572
- it.name,
1573
- it.schema,
1574
- [newPolicy],
1575
- ),
1576
- );
1577
- return;
1578
- }
1579
-
1580
- if (newPolicy.for !== oldPolicy.for) {
1581
- jsonDropPoliciesStatements.push(
1582
- ...prepareDropPolicyJsons(
1583
- it.name,
1584
- it.schema,
1585
- [oldPolicy],
1586
- ),
1587
- );
1588
-
1589
- jsonCreatePoliciesStatements.push(
1590
- ...prepareCreatePolicyJsons(
1591
- it.name,
1592
- it.schema,
1593
- [newPolicy],
1594
- ),
1595
- );
1596
- return;
1597
- }
1598
-
1599
- // alter
1600
- jsonAlterPoliciesStatements.push(
1601
- prepareAlterPolicyJson(
1602
- it.name,
1603
- it.schema,
1604
- it.alteredPolicies[policyName].__old,
1605
- it.alteredPolicies[policyName].__new,
1606
- ),
1607
- );
1608
- });
1609
-
1610
- // Handle enabling and disabling RLS
1611
- for (const table of Object.values(json2.tables)) {
1612
- const policiesInCurrentState = Object.keys(table.policies);
1613
- const tableInPreviousState =
1614
- columnsPatchedSnap1.tables[`${table.schema === '' ? 'public' : table.schema}.${table.name}`];
1615
- const policiesInPreviousState = tableInPreviousState ? Object.keys(tableInPreviousState.policies) : [];
1616
-
1617
- // const indPoliciesInCurrentState = Object.keys(table.policies);
1618
- // const indPoliciesInPreviousState = Object.keys(columnsPatchedSnap1.policies);
1619
-
1620
- if (
1621
- (policiesInPreviousState.length === 0 && policiesInCurrentState.length > 0) && !table.isRLSEnabled
1622
- ) {
1623
- jsonEnableRLSStatements.push({ type: 'enable_rls', tableName: table.name, schema: table.schema });
1624
- }
1625
-
1626
- if (
1627
- (policiesInPreviousState.length > 0 && policiesInCurrentState.length === 0) && !table.isRLSEnabled
1628
- ) {
1629
- jsonDisableRLSStatements.push({ type: 'disable_rls', tableName: table.name, schema: table.schema });
1630
- }
1631
-
1632
- // handle table.isRLSEnabled
1633
- const wasRlsEnabled = tableInPreviousState ? tableInPreviousState.isRLSEnabled : false;
1634
- if (table.isRLSEnabled !== wasRlsEnabled) {
1635
- if (table.isRLSEnabled) {
1636
- // was force enabled
1637
- jsonEnableRLSStatements.push({ type: 'enable_rls', tableName: table.name, schema: table.schema });
1638
- } else if (
1639
- !table.isRLSEnabled && policiesInCurrentState.length === 0
1640
- ) {
1641
- // was force disabled
1642
- jsonDisableRLSStatements.push({ type: 'disable_rls', tableName: table.name, schema: table.schema });
1643
- }
1644
- }
1645
- }
1646
-
1647
- for (const table of Object.values(columnsPatchedSnap1.tables)) {
1648
- const tableInCurrentState = json2.tables[`${table.schema === '' ? 'public' : table.schema}.${table.name}`];
1649
-
1650
- if (tableInCurrentState === undefined && !table.isRLSEnabled) {
1651
- jsonDisableRLSStatements.push({ type: 'disable_rls', tableName: table.name, schema: table.schema });
1652
- }
1653
- }
1654
-
1655
- // handle indexes
1656
- const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
1657
- (current, item: string) => {
1658
- current[item] = it.alteredIndexes[item].__old;
1659
- return current;
1660
- },
1661
- {} as Record<string, string>,
1662
- );
1663
- const createdIndexes = Object.keys(it.alteredIndexes).reduce(
1664
- (current, item: string) => {
1665
- current[item] = it.alteredIndexes[item].__new;
1666
- return current;
1667
- },
1668
- {} as Record<string, string>,
1669
- );
1670
-
1671
- jsonCreateIndexesFoAlteredTables.push(
1672
- ...preparePgCreateIndexesJson(
1673
- it.name,
1674
- it.schema,
1675
- createdIndexes || {},
1676
- curFull,
1677
- action,
1678
- ),
1679
- );
1680
- jsonDropIndexesForAllAlteredTables.push(
1681
- ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {}),
1682
- );
1683
- });
1684
-
1685
- const jsonCreateReferencesForCreatedTables: JsonCreateReferenceStatement[] = createdTables
1686
- .map((it) => {
1687
- return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
1688
- })
1689
- .flat();
1690
-
1691
- const jsonReferencesForAlteredTables: JsonReferenceStatement[] = alteredTables
1692
- .map((it) => {
1693
- const forAdded = prepareCreateReferencesJson(
1694
- it.name,
1695
- it.schema,
1696
- it.addedForeignKeys,
1697
- );
1698
-
1699
- const forAltered = prepareDropReferencesJson(
1700
- it.name,
1701
- it.schema,
1702
- it.deletedForeignKeys,
1703
- );
1704
-
1705
- const alteredFKs = prepareAlterReferencesJson(
1706
- it.name,
1707
- it.schema,
1708
- it.alteredForeignKeys,
1709
- );
1710
-
1711
- return [...forAdded, ...forAltered, ...alteredFKs];
1712
- })
1713
- .flat();
1714
-
1715
- const jsonCreatedReferencesForAlteredTables = jsonReferencesForAlteredTables.filter((t) =>
1716
- t.type === 'create_reference'
1717
- );
1718
-
1719
- const jsonDroppedReferencesForAlteredTables = jsonReferencesForAlteredTables.filter((t) =>
1720
- t.type === 'delete_reference'
1721
- );
1722
-
1723
- // Sequences
1724
- // - create sequence ✅
1725
- // - create sequence inside schema ✅
1726
- // - rename sequence ✅
1727
- // - change sequence schema ✅
1728
- // - change sequence schema + name ✅
1729
- // - drop sequence - check if sequence is in use. If yes - ???
1730
- // - change sequence values ✅
1731
-
1732
- // Generated columns
1733
- // - add generated
1734
- // - drop generated
1735
- // - create table with generated
1736
- // - alter - should be not triggered, but should get warning
1737
-
1738
- const createDomains = createdDomains.map((domain) => prepareDomainJson(domain, 'create'));
1739
- const dropDomains = deletedDomains.map((domain) => prepareDomainJson(domain, 'drop'));
1740
-
1741
- const alterDomains: JsonDomainStatement[] = [];
1742
- const commonDomains = Object.keys(json1.domains).filter((domainKey) => json2.domains[domainKey]);
1743
-
1744
- for (const domainKey of commonDomains) {
1745
- const domain1 = json1.domains[domainKey];
1746
- const domain2 = json2.domains[domainKey];
1747
-
1748
- if (!domain1 || !domain2) continue; // Should not happen, but for type safety
1749
-
1750
- if (domain1.defaultValue !== domain2.defaultValue) {
1751
- if (domain2.defaultValue !== undefined && domain2.defaultValue !== null) {
1752
- alterDomains.push(prepareDomainJson(domain2, 'alter', 'set_default'));
1753
- } else {
1754
- alterDomains.push(prepareDomainJson(domain2, 'alter', 'drop_default'));
1755
- }
1756
- }
1757
-
1758
- if (domain1.notNull !== domain2.notNull) {
1759
- if (domain2.notNull) {
1760
- alterDomains.push(prepareDomainJson(domain2, 'alter', 'set_not_null'));
1761
- } else {
1762
- alterDomains.push(prepareDomainJson(domain2, 'alter', 'drop_not_null'));
1763
- }
1764
- }
1765
-
1766
- if (domain1.baseType !== domain2.baseType) {
1767
- // THIS IS NOT ALLOWED
1768
- console.error('this is not allowed');
1769
- }
1770
-
1771
- // Simple check constraint diff (assuming names are consistent or we just want to add/drop)
1772
- const constraints1 = domain1.checkConstraints || {};
1773
- const constraints2 = domain2.checkConstraints || {};
1774
-
1775
- const deletedConstraints = Object.keys(constraints1).filter((name) => !constraints2[name]);
1776
- const createdConstraints = Object.keys(constraints2).filter((name) => !constraints1[name]);
1777
- // For simplicity, not handling constraint alteration, only add/drop
1778
-
1779
- for (const constraintName of deletedConstraints) {
1780
- alterDomains.push({
1781
- type: 'alter_domain',
1782
- action: 'drop_constraint',
1783
- name: domain1.name,
1784
- schema: domain1.schema,
1785
- checkConstraints: [constraints1[constraintName]],
1786
- });
1787
- }
1788
-
1789
- for (const constraintName of createdConstraints) {
1790
- alterDomains.push({
1791
- type: 'alter_domain',
1792
- action: 'add_constraint',
1793
- name: domain2.name,
1794
- schema: domain2.schema,
1795
- checkConstraints: [constraints2[constraintName]], // Pass the new constraint
1796
- });
1797
- }
1798
- }
1799
-
1800
- const createEnums = createdEnums.map((it) => {
1801
- return prepareCreateEnumJson(it.name, it.schema, it.values);
1802
- }) ?? [];
1803
-
1804
- const dropEnums = deletedEnums.map((it) => {
1805
- return prepareDropEnumJson(it.name, it.schema);
1806
- });
1807
-
1808
- const moveEnums = movedEnums.map((it) => {
1809
- return prepareMoveEnumJson(it.name, it.schemaFrom, it.schemaTo);
1810
- });
1811
-
1812
- const renameEnums = renamedEnums.map((it) => {
1813
- return prepareRenameEnumJson(it.from.name, it.to.name, it.to.schema);
1814
- });
1815
-
1816
- const jsonAlterEnumsWithAddedValues = typedResult.alteredEnums
1817
- .map((it) => {
1818
- return prepareAddValuesToEnumJson(it.name, it.schema, it.addedValues);
1819
- })
1820
- .flat() ?? [];
1821
-
1822
- const jsonAlterEnumsWithDroppedValues = typedResult.alteredEnums
1823
- .map((it) => {
1824
- return prepareDropEnumValues(it.name, it.schema, it.deletedValues, curFull);
1825
- })
1826
- .flat() ?? [];
1827
-
1828
- const createSequences = createdSequences.map((it) => {
1829
- return prepareCreateSequenceJson(it);
1830
- }) ?? [];
1831
-
1832
- const dropSequences = deletedSequences.map((it) => {
1833
- return prepareDropSequenceJson(it.name, it.schema);
1834
- });
1835
-
1836
- const moveSequences = movedSequences.map((it) => {
1837
- return prepareMoveSequenceJson(it.name, it.schemaFrom, it.schemaTo);
1838
- });
1839
-
1840
- const renameSequences = renamedSequences.map((it) => {
1841
- return prepareRenameSequenceJson(it.from.name, it.to.name, it.to.schema);
1842
- });
1843
-
1844
- const jsonAlterSequences = typedResult.alteredSequences
1845
- .map((it) => {
1846
- return prepareAlterSequenceJson(it);
1847
- })
1848
- .flat() ?? [];
1849
-
1850
- ////////////
1851
-
1852
- const createRoles = createdRoles.map((it) => {
1853
- return prepareCreateRoleJson(it);
1854
- }) ?? [];
1855
-
1856
- const dropRoles = deletedRoles.map((it) => {
1857
- return prepareDropRoleJson(it.name);
1858
- });
1859
-
1860
- const renameRoles = renamedRoles.map((it) => {
1861
- return prepareRenameRoleJson(it.from.name, it.to.name);
1862
- });
1863
-
1864
- const jsonAlterRoles = typedResult.alteredRoles
1865
- .map((it) => {
1866
- return prepareAlterRoleJson(it);
1867
- })
1868
- .flat() ?? [];
1869
-
1870
- ////////////
1871
- const createSchemas = prepareCreateSchemasJson(
1872
- createdSchemas.map((it) => it.name),
1873
- );
1874
-
1875
- const renameSchemas = prepareRenameSchemasJson(
1876
- renamedSchemas.map((it) => ({ from: it.from.name, to: it.to.name })),
1877
- );
1878
-
1879
- const dropSchemas = prepareDropSchemasJson(
1880
- deletedSchemas.map((it) => it.name),
1881
- );
1882
-
1883
- const createTables = createdTables.map((it) => {
1884
- return preparePgCreateTableJson(it, curFull);
1885
- });
1886
-
1887
- jsonCreatePoliciesStatements.push(...([] as JsonCreatePolicyStatement[]).concat(
1888
- ...(createdTables.map((it) =>
1889
- prepareCreatePolicyJsons(
1890
- it.name,
1891
- it.schema,
1892
- Object.values(it.policies).map(action === 'push' ? PgSquasher.unsquashPolicyPush : PgSquasher.unsquashPolicy),
1893
- )
1894
- )),
1895
- ));
1896
- const createViews: JsonCreatePgViewStatement[] = [];
1897
- const dropViews: JsonDropViewStatement[] = [];
1898
- const renameViews: JsonRenameViewStatement[] = [];
1899
- const alterViews: JsonAlterViewStatement[] = [];
1900
-
1901
- createViews.push(
1902
- ...createdViews.filter((it) => !it.isExisting).map((it) => {
1903
- return preparePgCreateViewJson(
1904
- it.name,
1905
- it.schema,
1906
- it.definition!,
1907
- it.materialized,
1908
- it.withNoData,
1909
- it.with,
1910
- it.using,
1911
- it.tablespace,
1912
- );
1913
- }),
1914
- );
1915
-
1916
- dropViews.push(
1917
- ...deletedViews.filter((it) => !it.isExisting).map((it) => {
1918
- return prepareDropViewJson(it.name, it.schema, it.materialized);
1919
- }),
1920
- );
1921
-
1922
- renameViews.push(
1923
- ...renamedViews.filter((it) => !it.to.isExisting && !json1.views[`${it.from.schema}.${it.from.name}`].isExisting)
1924
- .map((it) => {
1925
- return prepareRenameViewJson(it.to.name, it.from.name, it.to.schema, it.to.materialized);
1926
- }),
1927
- );
1928
-
1929
- alterViews.push(
1930
- ...movedViews.filter((it) =>
1931
- !json2.views[`${it.schemaTo}.${it.name}`].isExisting && !json1.views[`${it.schemaFrom}.${it.name}`].isExisting
1932
- ).map((it) => {
1933
- return preparePgAlterViewAlterSchemaJson(
1934
- it.schemaTo,
1935
- it.schemaFrom,
1936
- it.name,
1937
- json2.views[`${it.schemaTo}.${it.name}`].materialized,
1938
- );
1939
- }),
1940
- );
1941
-
1942
- const alteredViews = typedResult.alteredViews.filter((it) => !json2.views[`${it.schema}.${it.name}`].isExisting);
1943
-
1944
- for (const alteredView of alteredViews) {
1945
- const viewKey = `${alteredView.schema}.${alteredView.name}`;
1946
-
1947
- const { materialized, with: withOption, definition, withNoData, using, tablespace } = json2.views[viewKey];
1948
-
1949
- if (alteredView.alteredExisting || (alteredView.alteredDefinition && action !== 'push')) {
1950
- dropViews.push(prepareDropViewJson(alteredView.name, alteredView.schema, materialized));
1951
-
1952
- createViews.push(
1953
- preparePgCreateViewJson(
1954
- alteredView.name,
1955
- alteredView.schema,
1956
- definition!,
1957
- materialized,
1958
- withNoData,
1959
- withOption,
1960
- using,
1961
- tablespace,
1962
- ),
1963
- );
1964
-
1965
- continue;
1966
- }
1967
-
1968
- if (alteredView.addedWithOption) {
1969
- alterViews.push(
1970
- preparePgAlterViewAddWithOptionJson(
1971
- alteredView.name,
1972
- alteredView.schema,
1973
- materialized,
1974
- alteredView.addedWithOption,
1975
- ),
1976
- );
1977
- }
1978
-
1979
- if (alteredView.deletedWithOption) {
1980
- alterViews.push(
1981
- preparePgAlterViewDropWithOptionJson(
1982
- alteredView.name,
1983
- alteredView.schema,
1984
- materialized,
1985
- alteredView.deletedWithOption,
1986
- ),
1987
- );
1988
- }
1989
-
1990
- if (alteredView.addedWith) {
1991
- alterViews.push(
1992
- preparePgAlterViewAddWithOptionJson(
1993
- alteredView.name,
1994
- alteredView.schema,
1995
- materialized,
1996
- alteredView.addedWith,
1997
- ),
1998
- );
1999
- }
2000
-
2001
- if (alteredView.deletedWith) {
2002
- alterViews.push(
2003
- preparePgAlterViewDropWithOptionJson(
2004
- alteredView.name,
2005
- alteredView.schema,
2006
- materialized,
2007
- alteredView.deletedWith,
2008
- ),
2009
- );
2010
- }
2011
-
2012
- if (alteredView.alteredWith) {
2013
- alterViews.push(
2014
- preparePgAlterViewAddWithOptionJson(
2015
- alteredView.name,
2016
- alteredView.schema,
2017
- materialized,
2018
- alteredView.alteredWith,
2019
- ),
2020
- );
2021
- }
2022
-
2023
- if (alteredView.alteredTablespace) {
2024
- alterViews.push(
2025
- preparePgAlterViewAlterTablespaceJson(
2026
- alteredView.name,
2027
- alteredView.schema,
2028
- materialized,
2029
- alteredView.alteredTablespace.__new,
2030
- ),
2031
- );
2032
- }
2033
-
2034
- if (alteredView.alteredUsing) {
2035
- alterViews.push(
2036
- preparePgAlterViewAlterUsingJson(
2037
- alteredView.name,
2038
- alteredView.schema,
2039
- materialized,
2040
- alteredView.alteredUsing.__new,
2041
- ),
2042
- );
2043
- }
2044
- }
2045
-
2046
- jsonStatements.push(...createSchemas);
2047
- jsonStatements.push(...renameSchemas);
2048
-
2049
- jsonStatements.push(...createDomains);
2050
- jsonStatements.push(...alterDomains);
2051
-
2052
- jsonStatements.push(...createEnums);
2053
- jsonStatements.push(...moveEnums);
2054
- jsonStatements.push(...renameEnums);
2055
- jsonStatements.push(...jsonAlterEnumsWithAddedValues);
2056
-
2057
- jsonStatements.push(...createSequences);
2058
- jsonStatements.push(...moveSequences);
2059
- jsonStatements.push(...renameSequences);
2060
- jsonStatements.push(...jsonAlterSequences);
2061
-
2062
- jsonStatements.push(...renameRoles);
2063
- jsonStatements.push(...dropRoles);
2064
- jsonStatements.push(...createRoles);
2065
- jsonStatements.push(...jsonAlterRoles);
2066
-
2067
- jsonStatements.push(...createTables);
2068
-
2069
- jsonStatements.push(...jsonEnableRLSStatements);
2070
- jsonStatements.push(...jsonDisableRLSStatements);
2071
- jsonStatements.push(...dropViews);
2072
- jsonStatements.push(...renameViews);
2073
- jsonStatements.push(...alterViews);
2074
-
2075
- jsonStatements.push(...jsonDropTables);
2076
- jsonStatements.push(...jsonSetTableSchemas);
2077
- jsonStatements.push(...jsonRenameTables);
2078
- jsonStatements.push(...jsonRenameColumnsStatements);
2079
-
2080
- jsonStatements.push(...jsonDeletedUniqueConstraints);
2081
- jsonStatements.push(...jsonDeletedCheckConstraints);
2082
-
2083
- jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
2084
-
2085
- // Will need to drop indexes before changing any columns in table
2086
- // Then should go column alternations and then index creation
2087
- jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
2088
-
2089
- jsonStatements.push(...jsonDeletedCompositePKs);
2090
- jsonStatements.push(...jsonTableAlternations);
2091
- jsonStatements.push(...jsonAddedCompositePKs);
2092
- jsonStatements.push(...jsonAddColumnsStatemets);
2093
-
2094
- jsonStatements.push(...jsonCreateReferencesForCreatedTables);
2095
- jsonStatements.push(...jsonCreateIndexesForCreatedTables);
2096
-
2097
- jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
2098
- jsonStatements.push(...jsonCreateIndexesFoAlteredTables);
2099
-
2100
- jsonStatements.push(...jsonDropColumnsStatemets);
2101
- jsonStatements.push(...jsonAlteredCompositePKs);
2102
-
2103
- jsonStatements.push(...jsonAddedUniqueConstraints);
2104
- jsonStatements.push(...jsonCreatedCheckConstraints);
2105
-
2106
- jsonStatements.push(...jsonAlteredUniqueConstraints);
2107
- jsonStatements.push(...jsonAlterEnumsWithDroppedValues);
2108
-
2109
- jsonStatements.push(...createViews);
2110
-
2111
- jsonStatements.push(...jsonRenamePoliciesStatements);
2112
- jsonStatements.push(...jsonDropPoliciesStatements);
2113
- jsonStatements.push(...jsonCreatePoliciesStatements);
2114
- jsonStatements.push(...jsonAlterPoliciesStatements);
2115
-
2116
- jsonStatements.push(...jsonRenameIndPoliciesStatements);
2117
- jsonStatements.push(...jsonDropIndPoliciesStatements);
2118
- jsonStatements.push(...jsonCreateIndPoliciesStatements);
2119
- jsonStatements.push(...jsonAlterIndPoliciesStatements);
2120
-
2121
- jsonStatements.push(...dropEnums);
2122
- jsonStatements.push(...dropSequences);
2123
- jsonStatements.push(...dropSchemas);
2124
- jsonStatements.push(...dropDomains);
2125
-
2126
- // generate filters
2127
- const filteredJsonStatements = jsonStatements.filter((st) => {
2128
- if (st.type === 'alter_table_alter_column_drop_notnull') {
2129
- if (
2130
- jsonStatements.find(
2131
- (it) =>
2132
- it.type === 'alter_table_alter_column_drop_identity'
2133
- && it.tableName === st.tableName
2134
- && it.schema === st.schema,
2135
- )
2136
- ) {
2137
- return false;
2138
- }
2139
- }
2140
- if (st.type === 'alter_table_alter_column_set_notnull') {
2141
- if (
2142
- jsonStatements.find(
2143
- (it) =>
2144
- it.type === 'alter_table_alter_column_set_identity'
2145
- && it.tableName === st.tableName
2146
- && it.schema === st.schema,
2147
- )
2148
- ) {
2149
- return false;
2150
- }
2151
- }
2152
- return true;
2153
- });
2154
-
2155
- // enum filters
2156
- // Need to find add and drop enum values in same enum and remove add values
2157
- const filteredEnumsJsonStatements = filteredJsonStatements.filter((st) => {
2158
- if (st.type === 'alter_type_add_value') {
2159
- if (
2160
- jsonStatements.find(
2161
- (it) =>
2162
- it.type === 'alter_type_drop_value'
2163
- && it.name === st.name
2164
- && it.schema === st.schema,
2165
- )
2166
- ) {
2167
- return false;
2168
- }
2169
- }
2170
- return true;
2171
- });
2172
-
2173
- const sqlStatements = fromJson(filteredEnumsJsonStatements, 'postgresql', action);
2174
-
2175
- const uniqueSqlStatements: string[] = [];
2176
- sqlStatements.forEach((ss) => {
2177
- if (!uniqueSqlStatements.includes(ss)) {
2178
- uniqueSqlStatements.push(ss);
2179
- }
2180
- });
2181
-
2182
- const rSchemas = renamedSchemas.map((it) => ({
2183
- from: it.from.name,
2184
- to: it.to.name,
2185
- }));
2186
-
2187
- const rTables = renamedTables.map((it) => {
2188
- return { from: it.from, to: it.to };
2189
- });
2190
-
2191
- const _meta = prepareMigrationMeta(rSchemas, rTables, rColumns);
2192
-
2193
- return {
2194
- statements: filteredEnumsJsonStatements,
2195
- sqlStatements: uniqueSqlStatements,
2196
- _meta,
2197
- };
2198
- };
2199
-
2200
- export const applyMysqlSnapshotsDiff = async (
2201
- json1: MySqlSchemaSquashed,
2202
- json2: MySqlSchemaSquashed,
2203
- tablesResolver: (
2204
- input: ResolverInput<Table>,
2205
- ) => Promise<ResolverOutputWithMoved<Table>>,
2206
- columnsResolver: (
2207
- input: ColumnsResolverInput<Column>,
2208
- ) => Promise<ColumnsResolverOutput<Column>>,
2209
- viewsResolver: (
2210
- input: ResolverInput<ViewSquashed & { schema: '' }>,
2211
- ) => Promise<ResolverOutputWithMoved<ViewSquashed>>,
2212
- prevFull: MySqlSchema,
2213
- curFull: MySqlSchema,
2214
- action?: 'push' | undefined,
2215
- ): Promise<{
2216
- statements: JsonStatement[];
2217
- sqlStatements: string[];
2218
- _meta:
2219
- | {
2220
- schemas: {};
2221
- tables: {};
2222
- columns: {};
2223
- }
2224
- | undefined;
2225
- }> => {
2226
- // squash indexes and fks
2227
-
2228
- // squash uniqueIndexes and uniqueConstraint into constraints object
2229
- // it should be done for mysql only because it has no diffs for it
2230
-
2231
- // TODO: @AndriiSherman
2232
- // Add an upgrade to v6 and move all snaphosts to this strcutre
2233
- // After that we can generate mysql in 1 object directly(same as sqlite)
2234
- for (const tableName in json1.tables) {
2235
- const table = json1.tables[tableName];
2236
- for (const indexName in table.indexes) {
2237
- const index = MySqlSquasher.unsquashIdx(table.indexes[indexName]);
2238
- if (index.isUnique) {
2239
- table.uniqueConstraints[indexName] = MySqlSquasher.squashUnique({
2240
- name: index.name,
2241
- columns: index.columns,
2242
- });
2243
- delete json1.tables[tableName].indexes[index.name];
2244
- }
2245
- }
2246
- }
2247
-
2248
- for (const tableName in json2.tables) {
2249
- const table = json2.tables[tableName];
2250
- for (const indexName in table.indexes) {
2251
- const index = MySqlSquasher.unsquashIdx(table.indexes[indexName]);
2252
- if (index.isUnique) {
2253
- table.uniqueConstraints[indexName] = MySqlSquasher.squashUnique({
2254
- name: index.name,
2255
- columns: index.columns,
2256
- });
2257
- delete json2.tables[tableName].indexes[index.name];
2258
- }
2259
- }
2260
- }
2261
-
2262
- const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
2263
-
2264
- const {
2265
- created: createdTables,
2266
- deleted: deletedTables,
2267
- renamed: renamedTables, // renamed or moved
2268
- } = await tablesResolver({
2269
- created: tablesDiff.added,
2270
- deleted: tablesDiff.deleted,
2271
- });
2272
-
2273
- const tablesPatchedSnap1 = copy(json1);
2274
- tablesPatchedSnap1.tables = mapEntries(tablesPatchedSnap1.tables, (_, it) => {
2275
- const { name } = nameChangeFor(it, renamedTables);
2276
- it.name = name;
2277
- return [name, it];
2278
- });
2279
-
2280
- const res = diffColumns(tablesPatchedSnap1.tables, json2.tables);
2281
- const columnRenames = [] as {
2282
- table: string;
2283
- renames: { from: Column; to: Column }[];
2284
- }[];
2285
-
2286
- const columnCreates = [] as {
2287
- table: string;
2288
- columns: Column[];
2289
- }[];
2290
-
2291
- const columnDeletes = [] as {
2292
- table: string;
2293
- columns: Column[];
2294
- }[];
2295
-
2296
- for (let entry of Object.values(res)) {
2297
- const { renamed, created, deleted } = await columnsResolver({
2298
- tableName: entry.name,
2299
- schema: entry.schema,
2300
- deleted: entry.columns.deleted,
2301
- created: entry.columns.added,
2302
- });
2303
-
2304
- if (created.length > 0) {
2305
- columnCreates.push({
2306
- table: entry.name,
2307
- columns: created,
2308
- });
2309
- }
2310
-
2311
- if (deleted.length > 0) {
2312
- columnDeletes.push({
2313
- table: entry.name,
2314
- columns: deleted,
2315
- });
2316
- }
2317
-
2318
- if (renamed.length > 0) {
2319
- columnRenames.push({
2320
- table: entry.name,
2321
- renames: renamed,
2322
- });
2323
- }
2324
- }
2325
-
2326
- const columnRenamesDict = columnRenames.reduce(
2327
- (acc, it) => {
2328
- acc[it.table] = it.renames;
2329
- return acc;
2330
- },
2331
- {} as Record<
2332
- string,
2333
- {
2334
- from: Named;
2335
- to: Named;
2336
- }[]
2337
- >,
2338
- );
2339
-
2340
- const columnsPatchedSnap1 = copy(tablesPatchedSnap1);
2341
- columnsPatchedSnap1.tables = mapEntries(
2342
- columnsPatchedSnap1.tables,
2343
- (tableKey, tableValue) => {
2344
- const patchedColumns = mapKeys(
2345
- tableValue.columns,
2346
- (columnKey, column) => {
2347
- const rens = columnRenamesDict[tableValue.name] || [];
2348
- const newName = columnChangeFor(columnKey, rens);
2349
- column.name = newName;
2350
- return newName;
2351
- },
2352
- );
2353
-
2354
- tableValue.columns = patchedColumns;
2355
- return [tableKey, tableValue];
2356
- },
2357
- );
2358
-
2359
- const viewsDiff = diffSchemasOrTables(json1.views, json2.views);
2360
-
2361
- const {
2362
- created: createdViews,
2363
- deleted: deletedViews,
2364
- renamed: renamedViews, // renamed or moved
2365
- } = await viewsResolver({
2366
- created: viewsDiff.added,
2367
- deleted: viewsDiff.deleted,
2368
- });
2369
-
2370
- const renamesViewDic: Record<string, { to: string; from: string }> = {};
2371
- renamedViews.forEach((it) => {
2372
- renamesViewDic[it.from.name] = { to: it.to.name, from: it.from.name };
2373
- });
2374
-
2375
- const viewsPatchedSnap1 = copy(columnsPatchedSnap1);
2376
- viewsPatchedSnap1.views = mapEntries(
2377
- viewsPatchedSnap1.views,
2378
- (viewKey, viewValue) => {
2379
- const rename = renamesViewDic[viewValue.name];
2380
-
2381
- if (rename) {
2382
- viewValue.name = rename.to;
2383
- viewKey = rename.to;
2384
- }
2385
-
2386
- return [viewKey, viewValue];
2387
- },
2388
- );
2389
-
2390
- const diffResult = applyJsonDiff(viewsPatchedSnap1, json2);
2391
-
2392
- const typedResult: DiffResultMysql = diffResultSchemeMysql.parse(diffResult);
2393
-
2394
- const jsonStatements: JsonStatement[] = [];
2395
-
2396
- const jsonCreateIndexesForCreatedTables = createdTables
2397
- .map((it) => {
2398
- return prepareCreateIndexesJson(
2399
- it.name,
2400
- it.schema,
2401
- it.indexes,
2402
- curFull.internal,
2403
- );
2404
- })
2405
- .flat();
2406
-
2407
- const jsonDropTables = deletedTables.map((it) => {
2408
- return prepareDropTableJson(it);
2409
- });
2410
-
2411
- const jsonRenameTables = renamedTables.map((it) => {
2412
- return prepareRenameTableJson(it.from, it.to);
2413
- });
2414
-
2415
- const alteredTables = typedResult.alteredTablesWithColumns;
2416
-
2417
- const jsonAddedCompositePKs: JsonCreateCompositePK[] = [];
2418
- const jsonDeletedCompositePKs: JsonDeleteCompositePK[] = [];
2419
- const jsonAlteredCompositePKs: JsonAlterCompositePK[] = [];
2420
-
2421
- const jsonAddedUniqueConstraints: JsonCreateUniqueConstraint[] = [];
2422
- const jsonDeletedUniqueConstraints: JsonDeleteUniqueConstraint[] = [];
2423
- const jsonAlteredUniqueConstraints: JsonAlterUniqueConstraint[] = [];
2424
-
2425
- const jsonCreatedCheckConstraints: JsonCreateCheckConstraint[] = [];
2426
- const jsonDeletedCheckConstraints: JsonDeleteCheckConstraint[] = [];
2427
-
2428
- const jsonRenameColumnsStatements: JsonRenameColumnStatement[] = columnRenames
2429
- .map((it) => prepareRenameColumns(it.table, '', it.renames))
2430
- .flat();
2431
-
2432
- const jsonAddColumnsStatemets: JsonAddColumnStatement[] = columnCreates
2433
- .map((it) => _prepareAddColumns(it.table, '', it.columns))
2434
- .flat();
2435
-
2436
- const jsonDropColumnsStatemets: JsonDropColumnStatement[] = columnDeletes
2437
- .map((it) => _prepareDropColumns(it.table, '', it.columns))
2438
- .flat();
2439
-
2440
- alteredTables.forEach((it) => {
2441
- // This part is needed to make sure that same columns in a table are not triggered for change
2442
- // there is a case where orm and kit are responsible for pk name generation and one of them is not sorting name
2443
- // We double-check that pk with same set of columns are both in added and deleted diffs
2444
- let addedColumns: string[] = [];
2445
- for (const addedPkName of Object.keys(it.addedCompositePKs)) {
2446
- const addedPkColumns = it.addedCompositePKs[addedPkName];
2447
- addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
2448
- }
2449
-
2450
- let deletedColumns: string[] = [];
2451
- for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
2452
- const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
2453
- deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
2454
- }
2455
-
2456
- // Don't need to sort, but need to add tests for it
2457
- // addedColumns.sort();
2458
- // deletedColumns.sort();
2459
- const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
2460
-
2461
- let addedCompositePKs: JsonCreateCompositePK[] = [];
2462
- let deletedCompositePKs: JsonDeleteCompositePK[] = [];
2463
- let alteredCompositePKs: JsonAlterCompositePK[] = [];
2464
-
2465
- addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
2466
- it.name,
2467
- it.addedCompositePKs,
2468
- prevFull,
2469
- curFull,
2470
- );
2471
- deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
2472
- it.name,
2473
- it.deletedCompositePKs,
2474
- prevFull,
2475
- );
2476
- // }
2477
- alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
2478
- it.name,
2479
- it.alteredCompositePKs,
2480
- prevFull,
2481
- curFull,
2482
- );
2483
-
2484
- // add logic for unique constraints
2485
- let addedUniqueConstraints: JsonCreateUniqueConstraint[] = [];
2486
- let deletedUniqueConstraints: JsonDeleteUniqueConstraint[] = [];
2487
- let alteredUniqueConstraints: JsonAlterUniqueConstraint[] = [];
2488
-
2489
- let createdCheckConstraints: JsonCreateCheckConstraint[] = [];
2490
- let deletedCheckConstraints: JsonDeleteCheckConstraint[] = [];
2491
-
2492
- addedUniqueConstraints = prepareAddUniqueConstraint(
2493
- it.name,
2494
- it.schema,
2495
- it.addedUniqueConstraints,
2496
- );
2497
- deletedUniqueConstraints = prepareDeleteUniqueConstraint(
2498
- it.name,
2499
- it.schema,
2500
- it.deletedUniqueConstraints,
2501
- );
2502
- if (it.alteredUniqueConstraints) {
2503
- const added: Record<string, string> = {};
2504
- const deleted: Record<string, string> = {};
2505
- for (const k of Object.keys(it.alteredUniqueConstraints)) {
2506
- added[k] = it.alteredUniqueConstraints[k].__new;
2507
- deleted[k] = it.alteredUniqueConstraints[k].__old;
2508
- }
2509
- addedUniqueConstraints.push(
2510
- ...prepareAddUniqueConstraint(it.name, it.schema, added),
2511
- );
2512
- deletedUniqueConstraints.push(
2513
- ...prepareDeleteUniqueConstraint(it.name, it.schema, deleted),
2514
- );
2515
- }
2516
-
2517
- createdCheckConstraints = prepareAddCheckConstraint(it.name, it.schema, it.addedCheckConstraints);
2518
- deletedCheckConstraints = prepareDeleteCheckConstraint(
2519
- it.name,
2520
- it.schema,
2521
- it.deletedCheckConstraints,
2522
- );
2523
-
2524
- // skip for push
2525
- if (it.alteredCheckConstraints && action !== 'push') {
2526
- const added: Record<string, string> = {};
2527
- const deleted: Record<string, string> = {};
2528
-
2529
- for (const k of Object.keys(it.alteredCheckConstraints)) {
2530
- added[k] = it.alteredCheckConstraints[k].__new;
2531
- deleted[k] = it.alteredCheckConstraints[k].__old;
2532
- }
2533
- createdCheckConstraints.push(...prepareAddCheckConstraint(it.name, it.schema, added));
2534
- deletedCheckConstraints.push(...prepareDeleteCheckConstraint(it.name, it.schema, deleted));
2535
- }
2536
-
2537
- jsonAddedCompositePKs.push(...addedCompositePKs);
2538
- jsonDeletedCompositePKs.push(...deletedCompositePKs);
2539
- jsonAlteredCompositePKs.push(...alteredCompositePKs);
2540
-
2541
- jsonAddedUniqueConstraints.push(...addedUniqueConstraints);
2542
- jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
2543
- jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
2544
-
2545
- jsonCreatedCheckConstraints.push(...createdCheckConstraints);
2546
- jsonDeletedCheckConstraints.push(...deletedCheckConstraints);
2547
- });
2548
-
2549
- const rColumns = jsonRenameColumnsStatements.map((it) => {
2550
- const tableName = it.tableName;
2551
- const schema = it.schema;
2552
- return {
2553
- from: { schema, table: tableName, column: it.oldColumnName },
2554
- to: { schema, table: tableName, column: it.newColumnName },
2555
- };
2556
- });
2557
-
2558
- const jsonTableAlternations = alteredTables
2559
- .map((it) => {
2560
- return prepareAlterColumnsMysql(
2561
- it.name,
2562
- it.schema,
2563
- it.altered,
2564
- json1,
2565
- json2,
2566
- action,
2567
- );
2568
- })
2569
- .flat();
2570
-
2571
- const jsonCreateIndexesForAllAlteredTables = alteredTables
2572
- .map((it) => {
2573
- return prepareCreateIndexesJson(
2574
- it.name,
2575
- it.schema,
2576
- it.addedIndexes || {},
2577
- curFull.internal,
2578
- );
2579
- })
2580
- .flat();
2581
-
2582
- const jsonDropIndexesForAllAlteredTables = alteredTables
2583
- .map((it) => {
2584
- return prepareDropIndexesJson(
2585
- it.name,
2586
- it.schema,
2587
- it.deletedIndexes || {},
2588
- );
2589
- })
2590
- .flat();
2591
-
2592
- alteredTables.forEach((it) => {
2593
- const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
2594
- (current, item: string) => {
2595
- current[item] = it.alteredIndexes[item].__old;
2596
- return current;
2597
- },
2598
- {} as Record<string, string>,
2599
- );
2600
- const createdIndexes = Object.keys(it.alteredIndexes).reduce(
2601
- (current, item: string) => {
2602
- current[item] = it.alteredIndexes[item].__new;
2603
- return current;
2604
- },
2605
- {} as Record<string, string>,
2606
- );
2607
-
2608
- jsonCreateIndexesForAllAlteredTables.push(
2609
- ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {}),
2610
- );
2611
- jsonDropIndexesForAllAlteredTables.push(
2612
- ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {}),
2613
- );
2614
- });
2615
-
2616
- const jsonCreateReferencesForCreatedTables: JsonCreateReferenceStatement[] = createdTables
2617
- .map((it) => {
2618
- return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
2619
- })
2620
- .flat();
2621
-
2622
- const jsonReferencesForAllAlteredTables: JsonReferenceStatement[] = alteredTables
2623
- .map((it) => {
2624
- const forAdded = prepareCreateReferencesJson(
2625
- it.name,
2626
- it.schema,
2627
- it.addedForeignKeys,
2628
- );
2629
-
2630
- const forAltered = prepareDropReferencesJson(
2631
- it.name,
2632
- it.schema,
2633
- it.deletedForeignKeys,
2634
- );
2635
-
2636
- const alteredFKs = prepareAlterReferencesJson(
2637
- it.name,
2638
- it.schema,
2639
- it.alteredForeignKeys,
2640
- );
2641
-
2642
- return [...forAdded, ...forAltered, ...alteredFKs];
2643
- })
2644
- .flat();
2645
-
2646
- const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
2647
- (t) => t.type === 'create_reference',
2648
- );
2649
- const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
2650
- (t) => t.type === 'delete_reference',
2651
- );
2652
-
2653
- const jsonMySqlCreateTables = createdTables.map((it) => {
2654
- return prepareMySqlCreateTableJson(
2655
- it,
2656
- curFull as MySqlSchema,
2657
- curFull.internal,
2658
- );
2659
- });
2660
-
2661
- const createViews: JsonCreateMySqlViewStatement[] = [];
2662
- const dropViews: JsonDropViewStatement[] = [];
2663
- const renameViews: JsonRenameViewStatement[] = [];
2664
- const alterViews: JsonAlterMySqlViewStatement[] = [];
2665
-
2666
- createViews.push(
2667
- ...createdViews.filter((it) => !it.isExisting).map((it) => {
2668
- return prepareMySqlCreateViewJson(
2669
- it.name,
2670
- it.definition!,
2671
- it.meta,
2672
- );
2673
- }),
2674
- );
2675
-
2676
- dropViews.push(
2677
- ...deletedViews.filter((it) => !it.isExisting).map((it) => {
2678
- return prepareDropViewJson(it.name);
2679
- }),
2680
- );
2681
-
2682
- renameViews.push(
2683
- ...renamedViews.filter((it) => !it.to.isExisting && !json1.views[it.from.name].isExisting).map((it) => {
2684
- return prepareRenameViewJson(it.to.name, it.from.name);
2685
- }),
2686
- );
2687
-
2688
- const alteredViews = typedResult.alteredViews.filter((it) => !json2.views[it.name].isExisting);
2689
-
2690
- for (const alteredView of alteredViews) {
2691
- const { definition, meta } = json2.views[alteredView.name];
2692
-
2693
- if (alteredView.alteredExisting) {
2694
- dropViews.push(prepareDropViewJson(alteredView.name));
2695
-
2696
- createViews.push(
2697
- prepareMySqlCreateViewJson(
2698
- alteredView.name,
2699
- definition!,
2700
- meta,
2701
- ),
2702
- );
2703
-
2704
- continue;
2705
- }
2706
-
2707
- if (alteredView.alteredDefinition && action !== 'push') {
2708
- createViews.push(
2709
- prepareMySqlCreateViewJson(
2710
- alteredView.name,
2711
- definition!,
2712
- meta,
2713
- true,
2714
- ),
2715
- );
2716
- continue;
2717
- }
2718
-
2719
- if (alteredView.alteredMeta) {
2720
- const view = curFull['views'][alteredView.name];
2721
- alterViews.push(
2722
- prepareMySqlAlterView(view),
2723
- );
2724
- }
2725
- }
2726
-
2727
- jsonStatements.push(...jsonMySqlCreateTables);
2728
-
2729
- jsonStatements.push(...jsonDropTables);
2730
- jsonStatements.push(...jsonRenameTables);
2731
- jsonStatements.push(...jsonRenameColumnsStatements);
2732
-
2733
- jsonStatements.push(...dropViews);
2734
- jsonStatements.push(...renameViews);
2735
- jsonStatements.push(...alterViews);
2736
-
2737
- jsonStatements.push(...jsonDeletedUniqueConstraints);
2738
- jsonStatements.push(...jsonDeletedCheckConstraints);
2739
-
2740
- jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
2741
-
2742
- // Will need to drop indexes before changing any columns in table
2743
- // Then should go column alternations and then index creation
2744
- jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
2745
-
2746
- jsonStatements.push(...jsonDeletedCompositePKs);
2747
- jsonStatements.push(...jsonTableAlternations);
2748
- jsonStatements.push(...jsonAddedCompositePKs);
2749
- jsonStatements.push(...jsonAddColumnsStatemets);
2750
-
2751
- jsonStatements.push(...jsonAddedUniqueConstraints);
2752
- jsonStatements.push(...jsonDeletedUniqueConstraints);
2753
-
2754
- jsonStatements.push(...jsonCreateReferencesForCreatedTables);
2755
- jsonStatements.push(...jsonCreateIndexesForCreatedTables);
2756
- jsonStatements.push(...jsonCreatedCheckConstraints);
2757
-
2758
- jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
2759
- jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
2760
-
2761
- jsonStatements.push(...jsonDropColumnsStatemets);
2762
-
2763
- // jsonStatements.push(...jsonDeletedCompositePKs);
2764
- // jsonStatements.push(...jsonAddedCompositePKs);
2765
- jsonStatements.push(...jsonAlteredCompositePKs);
2766
-
2767
- jsonStatements.push(...createViews);
2768
-
2769
- jsonStatements.push(...jsonAlteredUniqueConstraints);
2770
-
2771
- const sqlStatements = fromJson(jsonStatements, 'mysql');
2772
-
2773
- const uniqueSqlStatements: string[] = [];
2774
- sqlStatements.forEach((ss) => {
2775
- if (!uniqueSqlStatements.includes(ss)) {
2776
- uniqueSqlStatements.push(ss);
2777
- }
2778
- });
2779
-
2780
- const rTables = renamedTables.map((it) => {
2781
- return { from: it.from, to: it.to };
2782
- });
2783
-
2784
- const _meta = prepareMigrationMeta([], rTables, rColumns);
2785
-
2786
- return {
2787
- statements: jsonStatements,
2788
- sqlStatements: uniqueSqlStatements,
2789
- _meta,
2790
- };
2791
- };
2792
-
2793
- export const applySingleStoreSnapshotsDiff = async (
2794
- json1: SingleStoreSchemaSquashed,
2795
- json2: SingleStoreSchemaSquashed,
2796
- tablesResolver: (
2797
- input: ResolverInput<Table>,
2798
- ) => Promise<ResolverOutputWithMoved<Table>>,
2799
- columnsResolver: (
2800
- input: ColumnsResolverInput<Column>,
2801
- ) => Promise<ColumnsResolverOutput<Column>>,
2802
- /* viewsResolver: (
2803
- input: ResolverInput<ViewSquashed & { schema: '' }>,
2804
- ) => Promise<ResolverOutputWithMoved<ViewSquashed>>, */
2805
- prevFull: SingleStoreSchema,
2806
- curFull: SingleStoreSchema,
2807
- action?: 'push' | undefined,
2808
- ): Promise<{
2809
- statements: JsonStatement[];
2810
- sqlStatements: string[];
2811
- _meta:
2812
- | {
2813
- schemas: {};
2814
- tables: {};
2815
- columns: {};
2816
- }
2817
- | undefined;
2818
- }> => {
2819
- // squash indexes and fks
2820
-
2821
- // squash uniqueIndexes and uniqueConstraint into constraints object
2822
- // it should be done for singlestore only because it has no diffs for it
2823
-
2824
- // TODO: @AndriiSherman
2825
- // Add an upgrade to v6 and move all snaphosts to this strcutre
2826
- // After that we can generate singlestore in 1 object directly(same as sqlite)
2827
- for (const tableName in json1.tables) {
2828
- const table = json1.tables[tableName];
2829
- for (const indexName in table.indexes) {
2830
- const index = SingleStoreSquasher.unsquashIdx(table.indexes[indexName]);
2831
- if (index.isUnique) {
2832
- table.uniqueConstraints[indexName] = SingleStoreSquasher.squashUnique({
2833
- name: index.name,
2834
- columns: index.columns,
2835
- });
2836
- delete json1.tables[tableName].indexes[index.name];
2837
- }
2838
- }
2839
- }
2840
-
2841
- for (const tableName in json2.tables) {
2842
- const table = json2.tables[tableName];
2843
- for (const indexName in table.indexes) {
2844
- const index = SingleStoreSquasher.unsquashIdx(table.indexes[indexName]);
2845
- if (index.isUnique) {
2846
- table.uniqueConstraints[indexName] = SingleStoreSquasher.squashUnique({
2847
- name: index.name,
2848
- columns: index.columns,
2849
- });
2850
- delete json2.tables[tableName].indexes[index.name];
2851
- }
2852
- }
2853
- }
2854
-
2855
- const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
2856
-
2857
- const {
2858
- created: createdTables,
2859
- deleted: deletedTables,
2860
- renamed: renamedTables, // renamed or moved
2861
- } = await tablesResolver({
2862
- created: tablesDiff.added,
2863
- deleted: tablesDiff.deleted,
2864
- });
2865
-
2866
- const tablesPatchedSnap1 = copy(json1);
2867
- tablesPatchedSnap1.tables = mapEntries(tablesPatchedSnap1.tables, (_, it) => {
2868
- const { name } = nameChangeFor(it, renamedTables);
2869
- it.name = name;
2870
- return [name, it];
2871
- });
2872
-
2873
- const res = diffColumns(tablesPatchedSnap1.tables, json2.tables);
2874
- const columnRenames = [] as {
2875
- table: string;
2876
- renames: { from: Column; to: Column }[];
2877
- }[];
2878
-
2879
- const columnCreates = [] as {
2880
- table: string;
2881
- columns: Column[];
2882
- }[];
2883
-
2884
- const columnDeletes = [] as {
2885
- table: string;
2886
- columns: Column[];
2887
- }[];
2888
-
2889
- for (let entry of Object.values(res)) {
2890
- const { renamed, created, deleted } = await columnsResolver({
2891
- tableName: entry.name,
2892
- schema: entry.schema,
2893
- deleted: entry.columns.deleted,
2894
- created: entry.columns.added,
2895
- });
2896
-
2897
- if (created.length > 0) {
2898
- columnCreates.push({
2899
- table: entry.name,
2900
- columns: created,
2901
- });
2902
- }
2903
-
2904
- if (deleted.length > 0) {
2905
- columnDeletes.push({
2906
- table: entry.name,
2907
- columns: deleted,
2908
- });
2909
- }
2910
-
2911
- if (renamed.length > 0) {
2912
- columnRenames.push({
2913
- table: entry.name,
2914
- renames: renamed,
2915
- });
2916
- }
2917
- }
2918
-
2919
- const columnRenamesDict = columnRenames.reduce(
2920
- (acc, it) => {
2921
- acc[it.table] = it.renames;
2922
- return acc;
2923
- },
2924
- {} as Record<
2925
- string,
2926
- {
2927
- from: Named;
2928
- to: Named;
2929
- }[]
2930
- >,
2931
- );
2932
-
2933
- const columnsPatchedSnap1 = copy(tablesPatchedSnap1);
2934
- columnsPatchedSnap1.tables = mapEntries(
2935
- columnsPatchedSnap1.tables,
2936
- (tableKey, tableValue) => {
2937
- const patchedColumns = mapKeys(
2938
- tableValue.columns,
2939
- (columnKey, column) => {
2940
- const rens = columnRenamesDict[tableValue.name] || [];
2941
- const newName = columnChangeFor(columnKey, rens);
2942
- column.name = newName;
2943
- return newName;
2944
- },
2945
- );
2946
-
2947
- tableValue.columns = patchedColumns;
2948
- return [tableKey, tableValue];
2949
- },
2950
- );
2951
-
2952
- /* const viewsDiff = diffSchemasOrTables(json1.views, json2.views);
2953
-
2954
- const {
2955
- created: createdViews,
2956
- deleted: deletedViews,
2957
- renamed: renamedViews, // renamed or moved
2958
- } = await viewsResolver({
2959
- created: viewsDiff.added,
2960
- deleted: viewsDiff.deleted,
2961
- });
2962
-
2963
- const renamesViewDic: Record<string, { to: string; from: string }> = {};
2964
- renamedViews.forEach((it) => {
2965
- renamesViewDic[it.from.name] = { to: it.to.name, from: it.from.name };
2966
- });
2967
-
2968
- const viewsPatchedSnap1 = copy(columnsPatchedSnap1);
2969
- viewsPatchedSnap1.views = mapEntries(
2970
- viewsPatchedSnap1.views,
2971
- (viewKey, viewValue) => {
2972
- const rename = renamesViewDic[viewValue.name];
2973
-
2974
- if (rename) {
2975
- viewValue.name = rename.to;
2976
- viewKey = rename.to;
2977
- }
2978
-
2979
- return [viewKey, viewValue];
2980
- },
2981
- );
2982
- */
2983
- const diffResult = applyJsonDiff(columnsPatchedSnap1, json2); // replace columnsPatchedSnap1 with viewsPatchedSnap1
2984
-
2985
- const typedResult: DiffResultSingleStore = diffResultSchemeSingleStore.parse(diffResult);
2986
-
2987
- const jsonStatements: JsonStatement[] = [];
2988
-
2989
- const jsonCreateIndexesForCreatedTables = createdTables
2990
- .map((it) => {
2991
- return prepareCreateIndexesJson(
2992
- it.name,
2993
- it.schema,
2994
- it.indexes,
2995
- curFull.internal,
2996
- );
2997
- })
2998
- .flat();
2999
-
3000
- const jsonDropTables = deletedTables.map((it) => {
3001
- return prepareDropTableJson(it);
3002
- });
3003
-
3004
- const jsonRenameTables = renamedTables.map((it) => {
3005
- return prepareRenameTableJson(it.from, it.to);
3006
- });
3007
-
3008
- const alteredTables = typedResult.alteredTablesWithColumns;
3009
-
3010
- const jsonAddedCompositePKs: JsonCreateCompositePK[] = [];
3011
-
3012
- const jsonAddedUniqueConstraints: JsonCreateUniqueConstraint[] = [];
3013
- const jsonDeletedUniqueConstraints: JsonDeleteUniqueConstraint[] = [];
3014
- const jsonAlteredUniqueConstraints: JsonAlterUniqueConstraint[] = [];
3015
-
3016
- const jsonRenameColumnsStatements: JsonRenameColumnStatement[] = columnRenames
3017
- .map((it) => prepareRenameColumns(it.table, '', it.renames))
3018
- .flat();
3019
-
3020
- const jsonAddColumnsStatemets: JsonAddColumnStatement[] = columnCreates
3021
- .map((it) => _prepareAddColumns(it.table, '', it.columns))
3022
- .flat();
3023
-
3024
- const jsonDropColumnsStatemets: JsonDropColumnStatement[] = columnDeletes
3025
- .map((it) => _prepareDropColumns(it.table, '', it.columns))
3026
- .flat();
3027
-
3028
- alteredTables.forEach((it) => {
3029
- // This part is needed to make sure that same columns in a table are not triggered for change
3030
- // there is a case where orm and kit are responsible for pk name generation and one of them is not sorting name
3031
- // We double-check that pk with same set of columns are both in added and deleted diffs
3032
- let addedColumns: string[] = [];
3033
- for (const addedPkName of Object.keys(it.addedCompositePKs)) {
3034
- const addedPkColumns = it.addedCompositePKs[addedPkName];
3035
- addedColumns = SingleStoreSquasher.unsquashPK(addedPkColumns).columns;
3036
- }
3037
-
3038
- let deletedColumns: string[] = [];
3039
- for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
3040
- const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
3041
- deletedColumns = SingleStoreSquasher.unsquashPK(deletedPkColumns).columns;
3042
- }
3043
-
3044
- // Don't need to sort, but need to add tests for it
3045
- // addedColumns.sort();
3046
- // deletedColumns.sort();
3047
- const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
3048
-
3049
- // add logic for unique constraints
3050
- let addedUniqueConstraints: JsonCreateUniqueConstraint[] = [];
3051
- let deletedUniqueConstraints: JsonDeleteUniqueConstraint[] = [];
3052
- let alteredUniqueConstraints: JsonAlterUniqueConstraint[] = [];
3053
-
3054
- let createdCheckConstraints: JsonCreateCheckConstraint[] = [];
3055
- let deletedCheckConstraints: JsonDeleteCheckConstraint[] = [];
3056
-
3057
- addedUniqueConstraints = prepareAddUniqueConstraint(
3058
- it.name,
3059
- it.schema,
3060
- it.addedUniqueConstraints,
3061
- );
3062
- deletedUniqueConstraints = prepareDeleteUniqueConstraint(
3063
- it.name,
3064
- it.schema,
3065
- it.deletedUniqueConstraints,
3066
- );
3067
- if (it.alteredUniqueConstraints) {
3068
- const added: Record<string, string> = {};
3069
- const deleted: Record<string, string> = {};
3070
- for (const k of Object.keys(it.alteredUniqueConstraints)) {
3071
- added[k] = it.alteredUniqueConstraints[k].__new;
3072
- deleted[k] = it.alteredUniqueConstraints[k].__old;
3073
- }
3074
- addedUniqueConstraints.push(
3075
- ...prepareAddUniqueConstraint(it.name, it.schema, added),
3076
- );
3077
- deletedUniqueConstraints.push(
3078
- ...prepareDeleteUniqueConstraint(it.name, it.schema, deleted),
3079
- );
3080
- }
3081
-
3082
- createdCheckConstraints = prepareAddCheckConstraint(it.name, it.schema, it.addedCheckConstraints);
3083
- deletedCheckConstraints = prepareDeleteCheckConstraint(
3084
- it.name,
3085
- it.schema,
3086
- it.deletedCheckConstraints,
3087
- );
3088
-
3089
- // skip for push
3090
- if (it.alteredCheckConstraints && action !== 'push') {
3091
- const added: Record<string, string> = {};
3092
- const deleted: Record<string, string> = {};
3093
-
3094
- for (const k of Object.keys(it.alteredCheckConstraints)) {
3095
- added[k] = it.alteredCheckConstraints[k].__new;
3096
- deleted[k] = it.alteredCheckConstraints[k].__old;
3097
- }
3098
- createdCheckConstraints.push(...prepareAddCheckConstraint(it.name, it.schema, added));
3099
- deletedCheckConstraints.push(...prepareDeleteCheckConstraint(it.name, it.schema, deleted));
3100
- }
3101
-
3102
- jsonAddedUniqueConstraints.push(...addedUniqueConstraints);
3103
- jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
3104
- jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
3105
- });
3106
-
3107
- const rColumns = jsonRenameColumnsStatements.map((it) => {
3108
- const tableName = it.tableName;
3109
- const schema = it.schema;
3110
- return {
3111
- from: { schema, table: tableName, column: it.oldColumnName },
3112
- to: { schema, table: tableName, column: it.newColumnName },
3113
- };
3114
- });
3115
-
3116
- const jsonTableAlternations = alteredTables
3117
- .map((it) => {
3118
- return prepareAlterColumnsMysql(
3119
- it.name,
3120
- it.schema,
3121
- it.altered,
3122
- json1,
3123
- json2,
3124
- action,
3125
- );
3126
- })
3127
- .flat();
3128
-
3129
- const jsonCreateIndexesForAllAlteredTables = alteredTables
3130
- .map((it) => {
3131
- return prepareCreateIndexesJson(
3132
- it.name,
3133
- it.schema,
3134
- it.addedIndexes || {},
3135
- curFull.internal,
3136
- );
3137
- })
3138
- .flat();
3139
-
3140
- const jsonDropIndexesForAllAlteredTables = alteredTables
3141
- .map((it) => {
3142
- return prepareDropIndexesJson(
3143
- it.name,
3144
- it.schema,
3145
- it.deletedIndexes || {},
3146
- );
3147
- })
3148
- .flat();
3149
-
3150
- alteredTables.forEach((it) => {
3151
- const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
3152
- (current, item: string) => {
3153
- current[item] = it.alteredIndexes[item].__old;
3154
- return current;
3155
- },
3156
- {} as Record<string, string>,
3157
- );
3158
- const createdIndexes = Object.keys(it.alteredIndexes).reduce(
3159
- (current, item: string) => {
3160
- current[item] = it.alteredIndexes[item].__new;
3161
- return current;
3162
- },
3163
- {} as Record<string, string>,
3164
- );
3165
-
3166
- jsonCreateIndexesForAllAlteredTables.push(
3167
- ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {}),
3168
- );
3169
- jsonDropIndexesForAllAlteredTables.push(
3170
- ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {}),
3171
- );
3172
- });
3173
-
3174
- const jsonSingleStoreCreateTables = createdTables.map((it) => {
3175
- return prepareSingleStoreCreateTableJson(
3176
- it,
3177
- curFull as SingleStoreSchema,
3178
- curFull.internal,
3179
- );
3180
- });
3181
-
3182
- /* const createViews: JsonCreateSingleStoreViewStatement[] = [];
3183
- const dropViews: JsonDropViewStatement[] = [];
3184
- const renameViews: JsonRenameViewStatement[] = [];
3185
- const alterViews: JsonAlterSingleStoreViewStatement[] = [];
3186
-
3187
- createViews.push(
3188
- ...createdViews.filter((it) => !it.isExisting).map((it) => {
3189
- return prepareSingleStoreCreateViewJson(
3190
- it.name,
3191
- it.definition!,
3192
- it.meta,
3193
- );
3194
- }),
3195
- );
3196
-
3197
- dropViews.push(
3198
- ...deletedViews.filter((it) => !it.isExisting).map((it) => {
3199
- return prepareDropViewJson(it.name);
3200
- }),
3201
- );
3202
-
3203
- renameViews.push(
3204
- ...renamedViews.filter((it) => !it.to.isExisting && !json1.views[it.from.name].isExisting).map((it) => {
3205
- return prepareRenameViewJson(it.to.name, it.from.name);
3206
- }),
3207
- );
3208
-
3209
- const alteredViews = typedResult.alteredViews.filter((it) => !json2.views[it.name].isExisting);
3210
-
3211
- for (const alteredView of alteredViews) {
3212
- const { definition, meta } = json2.views[alteredView.name];
3213
-
3214
- if (alteredView.alteredExisting) {
3215
- dropViews.push(prepareDropViewJson(alteredView.name));
3216
-
3217
- createViews.push(
3218
- prepareSingleStoreCreateViewJson(
3219
- alteredView.name,
3220
- definition!,
3221
- meta,
3222
- ),
3223
- );
3224
-
3225
- continue;
3226
- }
3227
-
3228
- if (alteredView.alteredDefinition && action !== 'push') {
3229
- createViews.push(
3230
- prepareSingleStoreCreateViewJson(
3231
- alteredView.name,
3232
- definition!,
3233
- meta,
3234
- true,
3235
- ),
3236
- );
3237
- continue;
3238
- }
3239
-
3240
- if (alteredView.alteredMeta) {
3241
- const view = curFull['views'][alteredView.name];
3242
- alterViews.push(
3243
- prepareSingleStoreAlterView(view),
3244
- );
3245
- }
3246
- } */
3247
-
3248
- jsonStatements.push(...jsonSingleStoreCreateTables);
3249
-
3250
- jsonStatements.push(...jsonDropTables);
3251
- jsonStatements.push(...jsonRenameTables);
3252
- jsonStatements.push(...jsonRenameColumnsStatements);
3253
-
3254
- /*jsonStatements.push(...createViews);
3255
- jsonStatements.push(...dropViews);
3256
- jsonStatements.push(...renameViews);
3257
- jsonStatements.push(...alterViews);
3258
- */
3259
- jsonStatements.push(...jsonDeletedUniqueConstraints);
3260
-
3261
- // Will need to drop indexes before changing any columns in table
3262
- // Then should go column alternations and then index creation
3263
- jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
3264
-
3265
- jsonStatements.push(...jsonTableAlternations);
3266
- jsonStatements.push(...jsonAddedCompositePKs);
3267
-
3268
- jsonStatements.push(...jsonAddedUniqueConstraints);
3269
- jsonStatements.push(...jsonDeletedUniqueConstraints);
3270
-
3271
- jsonStatements.push(...jsonAddColumnsStatemets);
3272
-
3273
- jsonStatements.push(...jsonCreateIndexesForCreatedTables);
3274
-
3275
- jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
3276
-
3277
- jsonStatements.push(...jsonDropColumnsStatemets);
3278
-
3279
- jsonStatements.push(...jsonAddedCompositePKs);
3280
-
3281
- jsonStatements.push(...jsonAlteredUniqueConstraints);
3282
-
3283
- const combinedJsonStatements = singleStoreCombineStatements(jsonStatements, json2);
3284
- const sqlStatements = fromJson(combinedJsonStatements, 'singlestore');
3285
-
3286
- const uniqueSqlStatements: string[] = [];
3287
- sqlStatements.forEach((ss) => {
3288
- if (!uniqueSqlStatements.includes(ss)) {
3289
- uniqueSqlStatements.push(ss);
3290
- }
3291
- });
3292
-
3293
- const rTables = renamedTables.map((it) => {
3294
- return { from: it.from, to: it.to };
3295
- });
3296
-
3297
- const _meta = prepareMigrationMeta([], rTables, rColumns);
3298
-
3299
- return {
3300
- statements: combinedJsonStatements,
3301
- sqlStatements: uniqueSqlStatements,
3302
- _meta,
3303
- };
3304
- };
3305
-
3306
- export const applySqliteSnapshotsDiff = async (
3307
- json1: SQLiteSchemaSquashed,
3308
- json2: SQLiteSchemaSquashed,
3309
- tablesResolver: (
3310
- input: ResolverInput<Table>,
3311
- ) => Promise<ResolverOutputWithMoved<Table>>,
3312
- columnsResolver: (
3313
- input: ColumnsResolverInput<Column>,
3314
- ) => Promise<ColumnsResolverOutput<Column>>,
3315
- viewsResolver: (
3316
- input: ResolverInput<SqliteView & { schema: '' }>,
3317
- ) => Promise<ResolverOutputWithMoved<SqliteView>>,
3318
- prevFull: SQLiteSchema,
3319
- curFull: SQLiteSchema,
3320
- action?: 'push' | undefined,
3321
- ): Promise<{
3322
- statements: JsonStatement[];
3323
- sqlStatements: string[];
3324
- _meta:
3325
- | {
3326
- schemas: {};
3327
- tables: {};
3328
- columns: {};
3329
- }
3330
- | undefined;
3331
- }> => {
3332
- const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
3333
-
3334
- const {
3335
- created: createdTables,
3336
- deleted: deletedTables,
3337
- renamed: renamedTables,
3338
- } = await tablesResolver({
3339
- created: tablesDiff.added,
3340
- deleted: tablesDiff.deleted,
3341
- });
3342
-
3343
- const tablesPatchedSnap1 = copy(json1);
3344
- tablesPatchedSnap1.tables = mapEntries(tablesPatchedSnap1.tables, (_, it) => {
3345
- const { name } = nameChangeFor(it, renamedTables);
3346
- it.name = name;
3347
- return [name, it];
3348
- });
3349
-
3350
- const res = diffColumns(tablesPatchedSnap1.tables, json2.tables);
3351
-
3352
- const columnRenames = [] as {
3353
- table: string;
3354
- renames: { from: Column; to: Column }[];
3355
- }[];
3356
-
3357
- const columnCreates = [] as {
3358
- table: string;
3359
- columns: Column[];
3360
- }[];
3361
-
3362
- const columnDeletes = [] as {
3363
- table: string;
3364
- columns: Column[];
3365
- }[];
3366
-
3367
- for (let entry of Object.values(res)) {
3368
- const { renamed, created, deleted } = await columnsResolver({
3369
- tableName: entry.name,
3370
- schema: entry.schema,
3371
- deleted: entry.columns.deleted,
3372
- created: entry.columns.added,
3373
- });
3374
-
3375
- if (created.length > 0) {
3376
- columnCreates.push({
3377
- table: entry.name,
3378
- columns: created,
3379
- });
3380
- }
3381
-
3382
- if (deleted.length > 0) {
3383
- columnDeletes.push({
3384
- table: entry.name,
3385
- columns: deleted,
3386
- });
3387
- }
3388
-
3389
- if (renamed.length > 0) {
3390
- columnRenames.push({
3391
- table: entry.name,
3392
- renames: renamed,
3393
- });
3394
- }
3395
- }
3396
-
3397
- const columnRenamesDict = columnRenames.reduce(
3398
- (acc, it) => {
3399
- acc[it.table] = it.renames;
3400
- return acc;
3401
- },
3402
- {} as Record<
3403
- string,
3404
- {
3405
- from: Named;
3406
- to: Named;
3407
- }[]
3408
- >,
3409
- );
3410
-
3411
- const columnsPatchedSnap1 = copy(tablesPatchedSnap1);
3412
- columnsPatchedSnap1.tables = mapEntries(
3413
- columnsPatchedSnap1.tables,
3414
- (tableKey, tableValue) => {
3415
- const patchedColumns = mapKeys(
3416
- tableValue.columns,
3417
- (columnKey, column) => {
3418
- const rens = columnRenamesDict[tableValue.name] || [];
3419
- const newName = columnChangeFor(columnKey, rens);
3420
- column.name = newName;
3421
- return newName;
3422
- },
3423
- );
3424
-
3425
- tableValue.columns = patchedColumns;
3426
- return [tableKey, tableValue];
3427
- },
3428
- );
3429
-
3430
- const viewsDiff = diffSchemasOrTables(json1.views, json2.views);
3431
-
3432
- const {
3433
- created: createdViews,
3434
- deleted: deletedViews,
3435
- renamed: renamedViews, // renamed or moved
3436
- } = await viewsResolver({
3437
- created: viewsDiff.added,
3438
- deleted: viewsDiff.deleted,
3439
- });
3440
-
3441
- const renamesViewDic: Record<string, { to: string; from: string }> = {};
3442
- renamedViews.forEach((it) => {
3443
- renamesViewDic[it.from.name] = { to: it.to.name, from: it.from.name };
3444
- });
3445
-
3446
- const viewsPatchedSnap1 = copy(columnsPatchedSnap1);
3447
- viewsPatchedSnap1.views = mapEntries(
3448
- viewsPatchedSnap1.views,
3449
- (viewKey, viewValue) => {
3450
- const rename = renamesViewDic[viewValue.name];
3451
-
3452
- if (rename) {
3453
- viewValue.name = rename.to;
3454
- }
3455
-
3456
- return [viewKey, viewValue];
3457
- },
3458
- );
3459
-
3460
- const diffResult = applyJsonDiff(viewsPatchedSnap1, json2);
3461
-
3462
- const typedResult = diffResultSchemeSQLite.parse(diffResult);
3463
-
3464
- // Map array of objects to map
3465
- const tablesMap: {
3466
- [key: string]: (typeof typedResult.alteredTablesWithColumns)[number];
3467
- } = {};
3468
-
3469
- typedResult.alteredTablesWithColumns.forEach((obj) => {
3470
- tablesMap[obj.name] = obj;
3471
- });
3472
-
3473
- const jsonCreateTables = createdTables.map((it) => {
3474
- return prepareSQLiteCreateTable(it, action);
3475
- });
3476
-
3477
- const jsonCreateIndexesForCreatedTables = createdTables
3478
- .map((it) => {
3479
- return prepareCreateIndexesJson(
3480
- it.name,
3481
- it.schema,
3482
- it.indexes,
3483
- curFull.internal,
3484
- );
3485
- })
3486
- .flat();
3487
-
3488
- const jsonDropTables = deletedTables.map((it) => {
3489
- return prepareDropTableJson(it);
3490
- });
3491
-
3492
- const jsonRenameTables = renamedTables.map((it) => {
3493
- return prepareRenameTableJson(it.from, it.to);
3494
- });
3495
-
3496
- const jsonRenameColumnsStatements: JsonRenameColumnStatement[] = columnRenames
3497
- .map((it) => prepareRenameColumns(it.table, '', it.renames))
3498
- .flat();
3499
-
3500
- const jsonDropColumnsStatemets: JsonDropColumnStatement[] = columnDeletes
3501
- .map((it) => _prepareDropColumns(it.table, '', it.columns))
3502
- .flat();
3503
-
3504
- const jsonAddColumnsStatemets: JsonSqliteAddColumnStatement[] = columnCreates
3505
- .map((it) => {
3506
- return _prepareSqliteAddColumns(
3507
- it.table,
3508
- it.columns,
3509
- tablesMap[it.table] && tablesMap[it.table].addedForeignKeys
3510
- ? Object.values(tablesMap[it.table].addedForeignKeys)
3511
- : [],
3512
- );
3513
- })
3514
- .flat();
3515
-
3516
- const allAltered = typedResult.alteredTablesWithColumns;
3517
-
3518
- const jsonAddedCompositePKs: JsonCreateCompositePK[] = [];
3519
- const jsonDeletedCompositePKs: JsonDeleteCompositePK[] = [];
3520
- const jsonAlteredCompositePKs: JsonAlterCompositePK[] = [];
3521
-
3522
- const jsonAddedUniqueConstraints: JsonCreateUniqueConstraint[] = [];
3523
- const jsonDeletedUniqueConstraints: JsonDeleteUniqueConstraint[] = [];
3524
- const jsonAlteredUniqueConstraints: JsonAlterUniqueConstraint[] = [];
3525
-
3526
- const jsonDeletedCheckConstraints: JsonDeleteCheckConstraint[] = [];
3527
- const jsonCreatedCheckConstraints: JsonCreateCheckConstraint[] = [];
3528
-
3529
- allAltered.forEach((it) => {
3530
- // This part is needed to make sure that same columns in a table are not triggered for change
3531
- // there is a case where orm and kit are responsible for pk name generation and one of them is not sorting name
3532
- // We double-check that pk with same set of columns are both in added and deleted diffs
3533
- let addedColumns: string[] = [];
3534
- for (const addedPkName of Object.keys(it.addedCompositePKs)) {
3535
- const addedPkColumns = it.addedCompositePKs[addedPkName];
3536
- addedColumns = SQLiteSquasher.unsquashPK(addedPkColumns);
3537
- }
3538
-
3539
- let deletedColumns: string[] = [];
3540
- for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
3541
- const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
3542
- deletedColumns = SQLiteSquasher.unsquashPK(deletedPkColumns);
3543
- }
3544
-
3545
- // Don't need to sort, but need to add tests for it
3546
- // addedColumns.sort();
3547
- // deletedColumns.sort();
3548
-
3549
- const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
3550
-
3551
- let addedCompositePKs: JsonCreateCompositePK[] = [];
3552
- let deletedCompositePKs: JsonDeleteCompositePK[] = [];
3553
- let alteredCompositePKs: JsonAlterCompositePK[] = [];
3554
- if (doPerformDeleteAndCreate) {
3555
- addedCompositePKs = prepareAddCompositePrimaryKeySqlite(
3556
- it.name,
3557
- it.addedCompositePKs,
3558
- );
3559
- deletedCompositePKs = prepareDeleteCompositePrimaryKeySqlite(
3560
- it.name,
3561
- it.deletedCompositePKs,
3562
- );
3563
- }
3564
- alteredCompositePKs = prepareAlterCompositePrimaryKeySqlite(
3565
- it.name,
3566
- it.alteredCompositePKs,
3567
- );
3568
-
3569
- // add logic for unique constraints
3570
- let addedUniqueConstraints: JsonCreateUniqueConstraint[] = [];
3571
- let deletedUniqueConstraints: JsonDeleteUniqueConstraint[] = [];
3572
- let alteredUniqueConstraints: JsonAlterUniqueConstraint[] = [];
3573
-
3574
- addedUniqueConstraints = prepareAddUniqueConstraint(
3575
- it.name,
3576
- it.schema,
3577
- it.addedUniqueConstraints,
3578
- );
3579
- deletedUniqueConstraints = prepareDeleteUniqueConstraint(
3580
- it.name,
3581
- it.schema,
3582
- it.deletedUniqueConstraints,
3583
- );
3584
- if (it.alteredUniqueConstraints) {
3585
- const added: Record<string, string> = {};
3586
- const deleted: Record<string, string> = {};
3587
- for (const k of Object.keys(it.alteredUniqueConstraints)) {
3588
- added[k] = it.alteredUniqueConstraints[k].__new;
3589
- deleted[k] = it.alteredUniqueConstraints[k].__old;
3590
- }
3591
- addedUniqueConstraints.push(
3592
- ...prepareAddUniqueConstraint(it.name, it.schema, added),
3593
- );
3594
- deletedUniqueConstraints.push(
3595
- ...prepareDeleteUniqueConstraint(it.name, it.schema, deleted),
3596
- );
3597
- }
3598
-
3599
- let createdCheckConstraints: JsonCreateCheckConstraint[] = [];
3600
- let deletedCheckConstraints: JsonDeleteCheckConstraint[] = [];
3601
-
3602
- addedUniqueConstraints = prepareAddUniqueConstraint(
3603
- it.name,
3604
- it.schema,
3605
- it.addedUniqueConstraints,
3606
- );
3607
- deletedUniqueConstraints = prepareDeleteUniqueConstraint(
3608
- it.name,
3609
- it.schema,
3610
- it.deletedUniqueConstraints,
3611
- );
3612
- if (it.alteredUniqueConstraints) {
3613
- const added: Record<string, string> = {};
3614
- const deleted: Record<string, string> = {};
3615
- for (const k of Object.keys(it.alteredUniqueConstraints)) {
3616
- added[k] = it.alteredUniqueConstraints[k].__new;
3617
- deleted[k] = it.alteredUniqueConstraints[k].__old;
3618
- }
3619
- addedUniqueConstraints.push(
3620
- ...prepareAddUniqueConstraint(it.name, it.schema, added),
3621
- );
3622
- deletedUniqueConstraints.push(
3623
- ...prepareDeleteUniqueConstraint(it.name, it.schema, deleted),
3624
- );
3625
- }
3626
-
3627
- createdCheckConstraints = prepareAddCheckConstraint(it.name, it.schema, it.addedCheckConstraints);
3628
- deletedCheckConstraints = prepareDeleteCheckConstraint(
3629
- it.name,
3630
- it.schema,
3631
- it.deletedCheckConstraints,
3632
- );
3633
-
3634
- // skip for push
3635
- if (it.alteredCheckConstraints && action !== 'push') {
3636
- const added: Record<string, string> = {};
3637
- const deleted: Record<string, string> = {};
3638
-
3639
- for (const k of Object.keys(it.alteredCheckConstraints)) {
3640
- added[k] = it.alteredCheckConstraints[k].__new;
3641
- deleted[k] = it.alteredCheckConstraints[k].__old;
3642
- }
3643
- createdCheckConstraints.push(...prepareAddCheckConstraint(it.name, it.schema, added));
3644
- deletedCheckConstraints.push(...prepareDeleteCheckConstraint(it.name, it.schema, deleted));
3645
- }
3646
-
3647
- jsonAddedCompositePKs.push(...addedCompositePKs);
3648
- jsonDeletedCompositePKs.push(...deletedCompositePKs);
3649
- jsonAlteredCompositePKs.push(...alteredCompositePKs);
3650
-
3651
- jsonAddedUniqueConstraints.push(...addedUniqueConstraints);
3652
- jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
3653
- jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
3654
-
3655
- jsonCreatedCheckConstraints.push(...createdCheckConstraints);
3656
- jsonDeletedCheckConstraints.push(...deletedCheckConstraints);
3657
- });
3658
-
3659
- const rColumns = jsonRenameColumnsStatements.map((it) => {
3660
- const tableName = it.tableName;
3661
- const schema = it.schema;
3662
- return {
3663
- from: { schema, table: tableName, column: it.oldColumnName },
3664
- to: { schema, table: tableName, column: it.newColumnName },
3665
- };
3666
- });
3667
-
3668
- const jsonTableAlternations = allAltered
3669
- .map((it) => {
3670
- return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
3671
- })
3672
- .flat();
3673
-
3674
- const jsonCreateIndexesForAllAlteredTables = allAltered
3675
- .map((it) => {
3676
- return prepareCreateIndexesJson(
3677
- it.name,
3678
- it.schema,
3679
- it.addedIndexes || {},
3680
- curFull.internal,
3681
- );
3682
- })
3683
- .flat();
3684
-
3685
- const jsonDropIndexesForAllAlteredTables = allAltered
3686
- .map((it) => {
3687
- return prepareDropIndexesJson(
3688
- it.name,
3689
- it.schema,
3690
- it.deletedIndexes || {},
3691
- );
3692
- })
3693
- .flat();
3694
-
3695
- allAltered.forEach((it) => {
3696
- const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
3697
- (current, item: string) => {
3698
- current[item] = it.alteredIndexes[item].__old;
3699
- return current;
3700
- },
3701
- {} as Record<string, string>,
3702
- );
3703
- const createdIndexes = Object.keys(it.alteredIndexes).reduce(
3704
- (current, item: string) => {
3705
- current[item] = it.alteredIndexes[item].__new;
3706
- return current;
3707
- },
3708
- {} as Record<string, string>,
3709
- );
3710
-
3711
- jsonCreateIndexesForAllAlteredTables.push(
3712
- ...prepareCreateIndexesJson(
3713
- it.name,
3714
- it.schema,
3715
- createdIndexes || {},
3716
- curFull.internal,
3717
- ),
3718
- );
3719
- jsonDropIndexesForAllAlteredTables.push(
3720
- ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {}),
3721
- );
3722
- });
3723
-
3724
- const jsonReferencesForAllAlteredTables: JsonReferenceStatement[] = allAltered
3725
- .map((it) => {
3726
- const forAdded = prepareCreateReferencesJson(
3727
- it.name,
3728
- it.schema,
3729
- it.addedForeignKeys,
3730
- );
3731
-
3732
- const forAltered = prepareDropReferencesJson(
3733
- it.name,
3734
- it.schema,
3735
- it.deletedForeignKeys,
3736
- );
3737
-
3738
- const alteredFKs = prepareAlterReferencesJson(
3739
- it.name,
3740
- it.schema,
3741
- it.alteredForeignKeys,
3742
- );
3743
-
3744
- return [...forAdded, ...forAltered, ...alteredFKs];
3745
- })
3746
- .flat();
3747
-
3748
- const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
3749
- (t) => t.type === 'create_reference',
3750
- );
3751
- const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
3752
- (t) => t.type === 'delete_reference',
3753
- );
3754
-
3755
- const createViews: JsonCreateSqliteViewStatement[] = [];
3756
- const dropViews: JsonDropViewStatement[] = [];
3757
-
3758
- createViews.push(
3759
- ...createdViews.filter((it) => !it.isExisting).map((it) => {
3760
- return prepareSqliteCreateViewJson(
3761
- it.name,
3762
- it.definition!,
3763
- );
3764
- }),
3765
- );
3766
-
3767
- dropViews.push(
3768
- ...deletedViews.filter((it) => !it.isExisting).map((it) => {
3769
- return prepareDropViewJson(it.name);
3770
- }),
3771
- );
3772
-
3773
- dropViews.push(
3774
- ...renamedViews.filter((it) => !it.to.isExisting).map((it) => {
3775
- return prepareDropViewJson(it.from.name);
3776
- }),
3777
- );
3778
- createViews.push(
3779
- ...renamedViews.filter((it) => !it.to.isExisting).map((it) => {
3780
- return prepareSqliteCreateViewJson(it.to.name, it.to.definition!);
3781
- }),
3782
- );
3783
-
3784
- const alteredViews = typedResult.alteredViews.filter((it) => !json2.views[it.name].isExisting);
3785
-
3786
- for (const alteredView of alteredViews) {
3787
- const { definition } = json2.views[alteredView.name];
3788
-
3789
- if (alteredView.alteredExisting || (alteredView.alteredDefinition && action !== 'push')) {
3790
- dropViews.push(prepareDropViewJson(alteredView.name));
3791
-
3792
- createViews.push(
3793
- prepareSqliteCreateViewJson(
3794
- alteredView.name,
3795
- definition!,
3796
- ),
3797
- );
3798
- }
3799
- }
3800
-
3801
- const jsonStatements: JsonStatement[] = [];
3802
- jsonStatements.push(...jsonCreateTables);
3803
-
3804
- jsonStatements.push(...jsonDropTables);
3805
- jsonStatements.push(...jsonRenameTables);
3806
- jsonStatements.push(...jsonRenameColumnsStatements);
3807
-
3808
- jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
3809
- jsonStatements.push(...jsonDeletedCheckConstraints);
3810
-
3811
- // Will need to drop indexes before changing any columns in table
3812
- // Then should go column alternations and then index creation
3813
- jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
3814
-
3815
- jsonStatements.push(...jsonDeletedCompositePKs);
3816
- jsonStatements.push(...jsonTableAlternations);
3817
- jsonStatements.push(...jsonAddedCompositePKs);
3818
- jsonStatements.push(...jsonAddColumnsStatemets);
3819
-
3820
- jsonStatements.push(...jsonCreateIndexesForCreatedTables);
3821
- jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
3822
-
3823
- jsonStatements.push(...jsonCreatedCheckConstraints);
3824
-
3825
- jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
3826
-
3827
- jsonStatements.push(...jsonDropColumnsStatemets);
3828
-
3829
- // jsonStatements.push(...jsonDeletedCompositePKs);
3830
- // jsonStatements.push(...jsonAddedCompositePKs);
3831
- jsonStatements.push(...jsonAlteredCompositePKs);
3832
-
3833
- jsonStatements.push(...jsonAlteredUniqueConstraints);
3834
-
3835
- jsonStatements.push(...dropViews);
3836
- jsonStatements.push(...createViews);
3837
-
3838
- const combinedJsonStatements = sqliteCombineStatements(jsonStatements, json2, action);
3839
- const sqlStatements = fromJson(combinedJsonStatements, 'sqlite');
3840
-
3841
- const uniqueSqlStatements: string[] = [];
3842
- sqlStatements.forEach((ss) => {
3843
- if (!uniqueSqlStatements.includes(ss)) {
3844
- uniqueSqlStatements.push(ss);
3845
- }
3846
- });
3847
-
3848
- const rTables = renamedTables.map((it) => {
3849
- return { from: it.from, to: it.to };
3850
- });
3851
-
3852
- const _meta = prepareMigrationMeta([], rTables, rColumns);
3853
-
3854
- return {
3855
- statements: combinedJsonStatements,
3856
- sqlStatements: uniqueSqlStatements,
3857
- _meta,
3858
- };
3859
- };
3860
-
3861
- export const applyLibSQLSnapshotsDiff = async (
3862
- json1: SQLiteSchemaSquashed,
3863
- json2: SQLiteSchemaSquashed,
3864
- tablesResolver: (
3865
- input: ResolverInput<Table>,
3866
- ) => Promise<ResolverOutputWithMoved<Table>>,
3867
- columnsResolver: (
3868
- input: ColumnsResolverInput<Column>,
3869
- ) => Promise<ColumnsResolverOutput<Column>>,
3870
- viewsResolver: (
3871
- input: ResolverInput<SqliteView & { schema: '' }>,
3872
- ) => Promise<ResolverOutputWithMoved<SqliteView>>,
3873
- prevFull: SQLiteSchema,
3874
- curFull: SQLiteSchema,
3875
- action?: 'push',
3876
- ): Promise<{
3877
- statements: JsonStatement[];
3878
- sqlStatements: string[];
3879
- _meta:
3880
- | {
3881
- schemas: {};
3882
- tables: {};
3883
- columns: {};
3884
- }
3885
- | undefined;
3886
- }> => {
3887
- const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
3888
- const {
3889
- created: createdTables,
3890
- deleted: deletedTables,
3891
- renamed: renamedTables,
3892
- } = await tablesResolver({
3893
- created: tablesDiff.added,
3894
- deleted: tablesDiff.deleted,
3895
- });
3896
-
3897
- const tablesPatchedSnap1 = copy(json1);
3898
- tablesPatchedSnap1.tables = mapEntries(tablesPatchedSnap1.tables, (_, it) => {
3899
- const { name } = nameChangeFor(it, renamedTables);
3900
- it.name = name;
3901
- return [name, it];
3902
- });
3903
-
3904
- const res = diffColumns(tablesPatchedSnap1.tables, json2.tables);
3905
-
3906
- const columnRenames = [] as {
3907
- table: string;
3908
- renames: { from: Column; to: Column }[];
3909
- }[];
3910
-
3911
- const columnCreates = [] as {
3912
- table: string;
3913
- columns: Column[];
3914
- }[];
3915
-
3916
- const columnDeletes = [] as {
3917
- table: string;
3918
- columns: Column[];
3919
- }[];
3920
-
3921
- for (let entry of Object.values(res)) {
3922
- const { renamed, created, deleted } = await columnsResolver({
3923
- tableName: entry.name,
3924
- schema: entry.schema,
3925
- deleted: entry.columns.deleted,
3926
- created: entry.columns.added,
3927
- });
3928
-
3929
- if (created.length > 0) {
3930
- columnCreates.push({
3931
- table: entry.name,
3932
- columns: created,
3933
- });
3934
- }
3935
-
3936
- if (deleted.length > 0) {
3937
- columnDeletes.push({
3938
- table: entry.name,
3939
- columns: deleted,
3940
- });
3941
- }
3942
-
3943
- if (renamed.length > 0) {
3944
- columnRenames.push({
3945
- table: entry.name,
3946
- renames: renamed,
3947
- });
3948
- }
3949
- }
3950
-
3951
- const columnRenamesDict = columnRenames.reduce(
3952
- (acc, it) => {
3953
- acc[it.table] = it.renames;
3954
- return acc;
3955
- },
3956
- {} as Record<
3957
- string,
3958
- {
3959
- from: Named;
3960
- to: Named;
3961
- }[]
3962
- >,
3963
- );
3964
-
3965
- const columnsPatchedSnap1 = copy(tablesPatchedSnap1);
3966
- columnsPatchedSnap1.tables = mapEntries(
3967
- columnsPatchedSnap1.tables,
3968
- (tableKey, tableValue) => {
3969
- const patchedColumns = mapKeys(
3970
- tableValue.columns,
3971
- (columnKey, column) => {
3972
- const rens = columnRenamesDict[tableValue.name] || [];
3973
- const newName = columnChangeFor(columnKey, rens);
3974
- column.name = newName;
3975
- return newName;
3976
- },
3977
- );
3978
-
3979
- tableValue.columns = patchedColumns;
3980
- return [tableKey, tableValue];
3981
- },
3982
- );
3983
-
3984
- const viewsDiff = diffSchemasOrTables(json1.views, json2.views);
3985
-
3986
- const {
3987
- created: createdViews,
3988
- deleted: deletedViews,
3989
- renamed: renamedViews, // renamed or moved
3990
- } = await viewsResolver({
3991
- created: viewsDiff.added,
3992
- deleted: viewsDiff.deleted,
3993
- });
3994
-
3995
- const renamesViewDic: Record<string, { to: string; from: string }> = {};
3996
- renamedViews.forEach((it) => {
3997
- renamesViewDic[it.from.name] = { to: it.to.name, from: it.from.name };
3998
- });
3999
-
4000
- const viewsPatchedSnap1 = copy(columnsPatchedSnap1);
4001
- viewsPatchedSnap1.views = mapEntries(
4002
- viewsPatchedSnap1.views,
4003
- (viewKey, viewValue) => {
4004
- const rename = renamesViewDic[viewValue.name];
4005
-
4006
- if (rename) {
4007
- viewValue.name = rename.to;
4008
- }
4009
-
4010
- return [viewKey, viewValue];
4011
- },
4012
- );
4013
-
4014
- const diffResult = applyJsonDiff(viewsPatchedSnap1, json2);
4015
-
4016
- const typedResult = diffResultSchemeSQLite.parse(diffResult);
4017
-
4018
- // Map array of objects to map
4019
- const tablesMap: {
4020
- [key: string]: (typeof typedResult.alteredTablesWithColumns)[number];
4021
- } = {};
4022
-
4023
- typedResult.alteredTablesWithColumns.forEach((obj) => {
4024
- tablesMap[obj.name] = obj;
4025
- });
4026
-
4027
- const jsonCreateTables = createdTables.map((it) => {
4028
- return prepareSQLiteCreateTable(it, action);
4029
- });
4030
-
4031
- const jsonCreateIndexesForCreatedTables = createdTables
4032
- .map((it) => {
4033
- return prepareCreateIndexesJson(
4034
- it.name,
4035
- it.schema,
4036
- it.indexes,
4037
- curFull.internal,
4038
- );
4039
- })
4040
- .flat();
4041
-
4042
- const jsonDropTables = deletedTables.map((it) => {
4043
- return prepareDropTableJson(it);
4044
- });
4045
-
4046
- const jsonRenameTables = renamedTables.map((it) => {
4047
- return prepareRenameTableJson(it.from, it.to);
4048
- });
4049
-
4050
- const jsonRenameColumnsStatements: JsonRenameColumnStatement[] = columnRenames
4051
- .map((it) => prepareRenameColumns(it.table, '', it.renames))
4052
- .flat();
4053
-
4054
- const jsonDropColumnsStatemets: JsonDropColumnStatement[] = columnDeletes
4055
- .map((it) => _prepareDropColumns(it.table, '', it.columns))
4056
- .flat();
4057
-
4058
- const jsonAddColumnsStatemets: JsonSqliteAddColumnStatement[] = columnCreates
4059
- .map((it) => {
4060
- return _prepareSqliteAddColumns(
4061
- it.table,
4062
- it.columns,
4063
- tablesMap[it.table] && tablesMap[it.table].addedForeignKeys
4064
- ? Object.values(tablesMap[it.table].addedForeignKeys)
4065
- : [],
4066
- );
4067
- })
4068
- .flat();
4069
-
4070
- const rColumns = jsonRenameColumnsStatements.map((it) => {
4071
- const tableName = it.tableName;
4072
- const schema = it.schema;
4073
- return {
4074
- from: { schema, table: tableName, column: it.oldColumnName },
4075
- to: { schema, table: tableName, column: it.newColumnName },
4076
- };
4077
- });
4078
-
4079
- const rTables = renamedTables.map((it) => {
4080
- return { from: it.from, to: it.to };
4081
- });
4082
-
4083
- const _meta = prepareMigrationMeta([], rTables, rColumns);
4084
-
4085
- const allAltered = typedResult.alteredTablesWithColumns;
4086
-
4087
- const jsonAddedCompositePKs: JsonCreateCompositePK[] = [];
4088
- const jsonDeletedCompositePKs: JsonDeleteCompositePK[] = [];
4089
- const jsonAlteredCompositePKs: JsonAlterCompositePK[] = [];
4090
-
4091
- const jsonAddedUniqueConstraints: JsonCreateUniqueConstraint[] = [];
4092
- const jsonDeletedUniqueConstraints: JsonDeleteUniqueConstraint[] = [];
4093
- const jsonAlteredUniqueConstraints: JsonAlterUniqueConstraint[] = [];
4094
-
4095
- const jsonDeletedCheckConstraints: JsonDeleteCheckConstraint[] = [];
4096
- const jsonCreatedCheckConstraints: JsonCreateCheckConstraint[] = [];
4097
-
4098
- allAltered.forEach((it) => {
4099
- // This part is needed to make sure that same columns in a table are not triggered for change
4100
- // there is a case where orm and kit are responsible for pk name generation and one of them is not sorting name
4101
- // We double-check that pk with same set of columns are both in added and deleted diffs
4102
- let addedColumns: string[] = [];
4103
- for (const addedPkName of Object.keys(it.addedCompositePKs)) {
4104
- const addedPkColumns = it.addedCompositePKs[addedPkName];
4105
- addedColumns = SQLiteSquasher.unsquashPK(addedPkColumns);
4106
- }
4107
-
4108
- let deletedColumns: string[] = [];
4109
- for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
4110
- const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
4111
- deletedColumns = SQLiteSquasher.unsquashPK(deletedPkColumns);
4112
- }
4113
-
4114
- // Don't need to sort, but need to add tests for it
4115
- // addedColumns.sort();
4116
- // deletedColumns.sort();
4117
-
4118
- const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
4119
-
4120
- let addedCompositePKs: JsonCreateCompositePK[] = [];
4121
- let deletedCompositePKs: JsonDeleteCompositePK[] = [];
4122
- let alteredCompositePKs: JsonAlterCompositePK[] = [];
4123
- if (doPerformDeleteAndCreate) {
4124
- addedCompositePKs = prepareAddCompositePrimaryKeySqlite(
4125
- it.name,
4126
- it.addedCompositePKs,
4127
- );
4128
- deletedCompositePKs = prepareDeleteCompositePrimaryKeySqlite(
4129
- it.name,
4130
- it.deletedCompositePKs,
4131
- );
4132
- }
4133
- alteredCompositePKs = prepareAlterCompositePrimaryKeySqlite(
4134
- it.name,
4135
- it.alteredCompositePKs,
4136
- );
4137
-
4138
- // add logic for unique constraints
4139
- let addedUniqueConstraints: JsonCreateUniqueConstraint[] = [];
4140
- let deletedUniqueConstraints: JsonDeleteUniqueConstraint[] = [];
4141
- let alteredUniqueConstraints: JsonAlterUniqueConstraint[] = [];
4142
-
4143
- let createdCheckConstraints: JsonCreateCheckConstraint[] = [];
4144
- let deletedCheckConstraints: JsonDeleteCheckConstraint[] = [];
4145
-
4146
- addedUniqueConstraints = prepareAddUniqueConstraint(
4147
- it.name,
4148
- it.schema,
4149
- it.addedUniqueConstraints,
4150
- );
4151
-
4152
- deletedUniqueConstraints = prepareDeleteUniqueConstraint(
4153
- it.name,
4154
- it.schema,
4155
- it.deletedUniqueConstraints,
4156
- );
4157
- if (it.alteredUniqueConstraints) {
4158
- const added: Record<string, string> = {};
4159
- const deleted: Record<string, string> = {};
4160
- for (const k of Object.keys(it.alteredUniqueConstraints)) {
4161
- added[k] = it.alteredUniqueConstraints[k].__new;
4162
- deleted[k] = it.alteredUniqueConstraints[k].__old;
4163
- }
4164
- addedUniqueConstraints.push(
4165
- ...prepareAddUniqueConstraint(it.name, it.schema, added),
4166
- );
4167
- deletedUniqueConstraints.push(
4168
- ...prepareDeleteUniqueConstraint(it.name, it.schema, deleted),
4169
- );
4170
- }
4171
-
4172
- createdCheckConstraints = prepareAddCheckConstraint(it.name, it.schema, it.addedCheckConstraints);
4173
- deletedCheckConstraints = prepareDeleteCheckConstraint(
4174
- it.name,
4175
- it.schema,
4176
- it.deletedCheckConstraints,
4177
- );
4178
-
4179
- // skip for push
4180
- if (it.alteredCheckConstraints && action !== 'push') {
4181
- const added: Record<string, string> = {};
4182
- const deleted: Record<string, string> = {};
4183
-
4184
- for (const k of Object.keys(it.alteredCheckConstraints)) {
4185
- added[k] = it.alteredCheckConstraints[k].__new;
4186
- deleted[k] = it.alteredCheckConstraints[k].__old;
4187
- }
4188
- createdCheckConstraints.push(...prepareAddCheckConstraint(it.name, it.schema, added));
4189
- deletedCheckConstraints.push(...prepareDeleteCheckConstraint(it.name, it.schema, deleted));
4190
- }
4191
-
4192
- jsonAddedCompositePKs.push(...addedCompositePKs);
4193
- jsonDeletedCompositePKs.push(...deletedCompositePKs);
4194
- jsonAlteredCompositePKs.push(...alteredCompositePKs);
4195
-
4196
- jsonAddedUniqueConstraints.push(...addedUniqueConstraints);
4197
- jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
4198
- jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
4199
-
4200
- jsonCreatedCheckConstraints.push(...createdCheckConstraints);
4201
- jsonDeletedCheckConstraints.push(...deletedCheckConstraints);
4202
- });
4203
-
4204
- const jsonTableAlternations = allAltered
4205
- .map((it) => {
4206
- return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
4207
- })
4208
- .flat();
4209
-
4210
- const jsonCreateIndexesForAllAlteredTables = allAltered
4211
- .map((it) => {
4212
- return prepareCreateIndexesJson(
4213
- it.name,
4214
- it.schema,
4215
- it.addedIndexes || {},
4216
- curFull.internal,
4217
- );
4218
- })
4219
- .flat();
4220
-
4221
- const jsonDropIndexesForAllAlteredTables = allAltered
4222
- .map((it) => {
4223
- return prepareDropIndexesJson(
4224
- it.name,
4225
- it.schema,
4226
- it.deletedIndexes || {},
4227
- );
4228
- })
4229
- .flat();
4230
-
4231
- allAltered.forEach((it) => {
4232
- const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
4233
- (current, item: string) => {
4234
- current[item] = it.alteredIndexes[item].__old;
4235
- return current;
4236
- },
4237
- {} as Record<string, string>,
4238
- );
4239
- const createdIndexes = Object.keys(it.alteredIndexes).reduce(
4240
- (current, item: string) => {
4241
- current[item] = it.alteredIndexes[item].__new;
4242
- return current;
4243
- },
4244
- {} as Record<string, string>,
4245
- );
4246
-
4247
- jsonCreateIndexesForAllAlteredTables.push(
4248
- ...prepareCreateIndexesJson(
4249
- it.name,
4250
- it.schema,
4251
- createdIndexes || {},
4252
- curFull.internal,
4253
- ),
4254
- );
4255
- jsonDropIndexesForAllAlteredTables.push(
4256
- ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {}),
4257
- );
4258
- });
4259
-
4260
- const jsonReferencesForAllAlteredTables: JsonReferenceStatement[] = allAltered
4261
- .map((it) => {
4262
- const forAdded = prepareLibSQLCreateReferencesJson(
4263
- it.name,
4264
- it.schema,
4265
- it.addedForeignKeys,
4266
- json2,
4267
- action,
4268
- );
4269
-
4270
- const forAltered = prepareLibSQLDropReferencesJson(
4271
- it.name,
4272
- it.schema,
4273
- it.deletedForeignKeys,
4274
- json2,
4275
- _meta,
4276
- action,
4277
- );
4278
-
4279
- const alteredFKs = prepareAlterReferencesJson(it.name, it.schema, it.alteredForeignKeys);
4280
-
4281
- return [...forAdded, ...forAltered, ...alteredFKs];
4282
- })
4283
- .flat();
4284
-
4285
- const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
4286
- (t) => t.type === 'create_reference',
4287
- );
4288
- const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
4289
- (t) => t.type === 'delete_reference',
4290
- );
4291
-
4292
- const createViews: JsonCreateSqliteViewStatement[] = [];
4293
- const dropViews: JsonDropViewStatement[] = [];
4294
-
4295
- createViews.push(
4296
- ...createdViews.filter((it) => !it.isExisting).map((it) => {
4297
- return prepareSqliteCreateViewJson(
4298
- it.name,
4299
- it.definition!,
4300
- );
4301
- }),
4302
- );
4303
-
4304
- dropViews.push(
4305
- ...deletedViews.filter((it) => !it.isExisting).map((it) => {
4306
- return prepareDropViewJson(it.name);
4307
- }),
4308
- );
4309
-
4310
- // renames
4311
- dropViews.push(
4312
- ...renamedViews.filter((it) => !it.to.isExisting).map((it) => {
4313
- return prepareDropViewJson(it.from.name);
4314
- }),
4315
- );
4316
- createViews.push(
4317
- ...renamedViews.filter((it) => !it.to.isExisting).map((it) => {
4318
- return prepareSqliteCreateViewJson(it.to.name, it.to.definition!);
4319
- }),
4320
- );
4321
-
4322
- const alteredViews = typedResult.alteredViews.filter((it) => !json2.views[it.name].isExisting);
4323
-
4324
- for (const alteredView of alteredViews) {
4325
- const { definition } = json2.views[alteredView.name];
4326
-
4327
- if (alteredView.alteredExisting || (alteredView.alteredDefinition && action !== 'push')) {
4328
- dropViews.push(prepareDropViewJson(alteredView.name));
4329
-
4330
- createViews.push(
4331
- prepareSqliteCreateViewJson(
4332
- alteredView.name,
4333
- definition!,
4334
- ),
4335
- );
4336
- }
4337
- }
4338
-
4339
- const jsonStatements: JsonStatement[] = [];
4340
- jsonStatements.push(...jsonCreateTables);
4341
-
4342
- jsonStatements.push(...jsonDropTables);
4343
- jsonStatements.push(...jsonRenameTables);
4344
- jsonStatements.push(...jsonRenameColumnsStatements);
4345
-
4346
- jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
4347
-
4348
- jsonStatements.push(...jsonDeletedCheckConstraints);
4349
-
4350
- // Will need to drop indexes before changing any columns in table
4351
- // Then should go column alternations and then index creation
4352
- jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
4353
-
4354
- jsonStatements.push(...jsonDeletedCompositePKs);
4355
- jsonStatements.push(...jsonTableAlternations);
4356
- jsonStatements.push(...jsonAddedCompositePKs);
4357
- jsonStatements.push(...jsonAddColumnsStatemets);
4358
-
4359
- jsonStatements.push(...jsonCreateIndexesForCreatedTables);
4360
- jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
4361
- jsonStatements.push(...jsonCreatedCheckConstraints);
4362
-
4363
- jsonStatements.push(...dropViews);
4364
- jsonStatements.push(...createViews);
4365
-
4366
- jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
4367
-
4368
- jsonStatements.push(...jsonDropColumnsStatemets);
4369
-
4370
- jsonStatements.push(...jsonAlteredCompositePKs);
4371
-
4372
- jsonStatements.push(...jsonAlteredUniqueConstraints);
4373
-
4374
- const combinedJsonStatements = libSQLCombineStatements(jsonStatements, json2, action);
4375
-
4376
- const sqlStatements = fromJson(
4377
- combinedJsonStatements,
4378
- 'turso',
4379
- action,
4380
- json2,
4381
- );
4382
-
4383
- const uniqueSqlStatements: string[] = [];
4384
- sqlStatements.forEach((ss) => {
4385
- if (!uniqueSqlStatements.includes(ss)) {
4386
- uniqueSqlStatements.push(ss);
4387
- }
4388
- });
4389
-
4390
- return {
4391
- statements: combinedJsonStatements,
4392
- sqlStatements: uniqueSqlStatements,
4393
- _meta,
4394
- };
4395
- };
4396
-
4397
- // explicitely ask if tables were renamed, if yes - add those to altered tables, otherwise - deleted
4398
- // double check if user wants to delete particular table and warn him on data loss