@anthonyalayo/drizzle-kit 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/.gitignore +25 -0
  2. package/README.md +79 -0
  3. package/build.dev.ts +51 -0
  4. package/build.ts +112 -0
  5. package/imports-checker/analyze.ts +78 -0
  6. package/imports-checker/checker.ts +296 -0
  7. package/imports-checker/grammar/grammar.ohm +121 -0
  8. package/imports-checker/grammar/grammar.ohm-bundle.d.ts +164 -0
  9. package/imports-checker/grammar/grammar.ohm-bundle.js +753 -0
  10. package/imports-checker/index.ts +48 -0
  11. package/package.json +142 -0
  12. package/patches/difflib@0.2.4.patch +31 -0
  13. package/src/@types/utils.ts +58 -0
  14. package/src/api.ts +504 -0
  15. package/src/cli/commands/_es5.ts +2 -0
  16. package/src/cli/commands/check.ts +52 -0
  17. package/src/cli/commands/drop.ts +60 -0
  18. package/src/cli/commands/introspect.ts +772 -0
  19. package/src/cli/commands/libSqlPushUtils.ts +355 -0
  20. package/src/cli/commands/migrate.ts +1526 -0
  21. package/src/cli/commands/mysqlIntrospect.ts +53 -0
  22. package/src/cli/commands/mysqlPushUtils.ts +352 -0
  23. package/src/cli/commands/mysqlUp.ts +102 -0
  24. package/src/cli/commands/pgIntrospect.ts +55 -0
  25. package/src/cli/commands/pgPushUtils.ts +269 -0
  26. package/src/cli/commands/pgUp.ts +217 -0
  27. package/src/cli/commands/push.ts +637 -0
  28. package/src/cli/commands/singlestoreIntrospect.ts +53 -0
  29. package/src/cli/commands/singlestorePushUtils.ts +456 -0
  30. package/src/cli/commands/singlestoreUp.ts +1 -0
  31. package/src/cli/commands/sqliteIntrospect.ts +96 -0
  32. package/src/cli/commands/sqlitePushUtils.ts +322 -0
  33. package/src/cli/commands/sqliteUp.ts +52 -0
  34. package/src/cli/commands/utils.ts +858 -0
  35. package/src/cli/connections.ts +914 -0
  36. package/src/cli/index.ts +48 -0
  37. package/src/cli/schema.ts +794 -0
  38. package/src/cli/selector-ui.ts +38 -0
  39. package/src/cli/utils.ts +112 -0
  40. package/src/cli/validations/cli.ts +79 -0
  41. package/src/cli/validations/common.ts +193 -0
  42. package/src/cli/validations/libsql.ts +27 -0
  43. package/src/cli/validations/mysql.ts +61 -0
  44. package/src/cli/validations/outputs.ts +91 -0
  45. package/src/cli/validations/postgres.ts +83 -0
  46. package/src/cli/validations/singlestore.ts +61 -0
  47. package/src/cli/validations/sqlite.ts +104 -0
  48. package/src/cli/validations/studio.ts +24 -0
  49. package/src/cli/views.ts +658 -0
  50. package/src/extensions/getTablesFilterByExtensions.ts +16 -0
  51. package/src/extensions/vector.ts +10 -0
  52. package/src/global.ts +61 -0
  53. package/src/index.ts +326 -0
  54. package/src/introspect-mysql.ts +1030 -0
  55. package/src/introspect-pg.ts +1435 -0
  56. package/src/introspect-singlestore.ts +913 -0
  57. package/src/introspect-sqlite.ts +533 -0
  58. package/src/jsonDiffer.js +882 -0
  59. package/src/jsonStatements.ts +3590 -0
  60. package/src/loader.mjs +57 -0
  61. package/src/migrationPreparator.ts +217 -0
  62. package/src/schemaValidator.ts +23 -0
  63. package/src/serializer/index.ts +142 -0
  64. package/src/serializer/mysqlImports.ts +38 -0
  65. package/src/serializer/mysqlSchema.ts +423 -0
  66. package/src/serializer/mysqlSerializer.ts +999 -0
  67. package/src/serializer/pgImports.ts +106 -0
  68. package/src/serializer/pgSchema.ts +937 -0
  69. package/src/serializer/pgSerializer.ts +2223 -0
  70. package/src/serializer/singlestoreImports.ts +38 -0
  71. package/src/serializer/singlestoreSchema.ts +257 -0
  72. package/src/serializer/singlestoreSerializer.ts +767 -0
  73. package/src/serializer/sqliteImports.ts +41 -0
  74. package/src/serializer/sqliteSchema.ts +352 -0
  75. package/src/serializer/sqliteSerializer.ts +931 -0
  76. package/src/serializer/studio.ts +694 -0
  77. package/src/serializer/utils.ts +45 -0
  78. package/src/simulator.ts +157 -0
  79. package/src/snapshotsDiffer.ts +4398 -0
  80. package/src/sqlgenerator.ts +4212 -0
  81. package/src/statementCombiner.ts +597 -0
  82. package/src/utils/certs.ts +44 -0
  83. package/src/utils/words.ts +1333 -0
  84. package/src/utils.ts +370 -0
  85. package/tests/bin.test.ts +58 -0
  86. package/tests/cli/d1http.config.ts +12 -0
  87. package/tests/cli/drizzle.config.ts +9 -0
  88. package/tests/cli/durable-sqlite.config.ts +7 -0
  89. package/tests/cli/expo.config.ts +7 -0
  90. package/tests/cli/postgres.config.ts +13 -0
  91. package/tests/cli/postgres2.config.ts +17 -0
  92. package/tests/cli/schema.ts +1 -0
  93. package/tests/cli/turso.config.ts +10 -0
  94. package/tests/cli-export.test.ts +79 -0
  95. package/tests/cli-generate.test.ts +259 -0
  96. package/tests/cli-migrate.test.ts +103 -0
  97. package/tests/cli-push.test.ts +126 -0
  98. package/tests/common.ts +15 -0
  99. package/tests/indexes/common.ts +21 -0
  100. package/tests/indexes/pg.test.ts +245 -0
  101. package/tests/introspect/libsql.test.ts +35 -0
  102. package/tests/introspect/mysql.test.ts +319 -0
  103. package/tests/introspect/pg.test.ts +894 -0
  104. package/tests/introspect/singlestore.test.ts +275 -0
  105. package/tests/introspect/sqlite.test.ts +122 -0
  106. package/tests/libsql-checks.test.ts +308 -0
  107. package/tests/libsql-statements.test.ts +989 -0
  108. package/tests/libsql-views.test.ts +218 -0
  109. package/tests/migrate/libsq-schema.ts +6 -0
  110. package/tests/migrate/libsql-migrate.test.ts +58 -0
  111. package/tests/migrate/migrations/0000_little_blizzard.sql +4 -0
  112. package/tests/migrate/migrations/0001_nebulous_storm.sql +10 -0
  113. package/tests/migrate/migrations/meta/0000_snapshot.json +40 -0
  114. package/tests/migrate/migrations/meta/0001_snapshot.json +40 -0
  115. package/tests/migrate/migrations/meta/_journal.json +20 -0
  116. package/tests/mysql-checks.test.ts +291 -0
  117. package/tests/mysql-generated.test.ts +1290 -0
  118. package/tests/mysql-schemas.test.ts +156 -0
  119. package/tests/mysql-views.test.ts +553 -0
  120. package/tests/mysql.test.ts +863 -0
  121. package/tests/pg-array.test.ts +368 -0
  122. package/tests/pg-checks.test.ts +329 -0
  123. package/tests/pg-columns.test.ts +484 -0
  124. package/tests/pg-domains.test.ts +306 -0
  125. package/tests/pg-enums.test.ts +751 -0
  126. package/tests/pg-generated.test.ts +529 -0
  127. package/tests/pg-identity.test.ts +447 -0
  128. package/tests/pg-schemas.test.ts +105 -0
  129. package/tests/pg-sequences.test.ts +298 -0
  130. package/tests/pg-tables.test.ts +1021 -0
  131. package/tests/pg-views.test.ts +1929 -0
  132. package/tests/push/common.ts +58 -0
  133. package/tests/push/libsql.test.ts +1400 -0
  134. package/tests/push/mysql-push.test.ts +345 -0
  135. package/tests/push/mysql.test.ts +788 -0
  136. package/tests/push/pg.test.ts +4341 -0
  137. package/tests/push/singlestore-push.test.ts +894 -0
  138. package/tests/push/singlestore.test.ts +444 -0
  139. package/tests/push/sqlite.test.ts +1613 -0
  140. package/tests/rls/pg-policy.test.ts +1656 -0
  141. package/tests/rls/pg-role.test.ts +234 -0
  142. package/tests/schemaDiffer.ts +2852 -0
  143. package/tests/singlestore-generated.test.ts +1290 -0
  144. package/tests/singlestore-schemas.test.ts +155 -0
  145. package/tests/singlestore.test.ts +978 -0
  146. package/tests/sqlite-checks.test.ts +308 -0
  147. package/tests/sqlite-columns.test.ts +1049 -0
  148. package/tests/sqlite-generated.test.ts +1726 -0
  149. package/tests/sqlite-tables.test.ts +637 -0
  150. package/tests/sqlite-views.test.ts +218 -0
  151. package/tests/statements-combiner/libsql-statements-combiner.test.ts +1812 -0
  152. package/tests/statements-combiner/singlestore-statements-combiner.test.ts +882 -0
  153. package/tests/statements-combiner/sqlite-statements-combiner.test.ts +1211 -0
  154. package/tests/test/sqlite.test.ts +39 -0
  155. package/tests/testsinglestore.ts +29 -0
  156. package/tests/validations.test.ts +869 -0
  157. package/tests/wrap-param.test.ts +16 -0
  158. package/tsconfig.build.json +4 -0
  159. package/tsconfig.cli-types.json +9 -0
  160. package/tsconfig.json +28 -0
  161. package/vitest.config.ts +34 -0
@@ -0,0 +1,978 @@
1
+ import { sql } from 'drizzle-orm';
2
+ import {
3
+ index,
4
+ int,
5
+ json,
6
+ primaryKey,
7
+ serial,
8
+ singlestoreSchema,
9
+ singlestoreTable,
10
+ text,
11
+ uniqueIndex,
12
+ } from 'drizzle-orm/singlestore-core';
13
+ import { expect, test } from 'vitest';
14
+ import { diffTestSchemasSingleStore } from './schemaDiffer';
15
+
16
+ test('add table #1', async () => {
17
+ const to = {
18
+ users: singlestoreTable('users', {}),
19
+ };
20
+
21
+ const { statements } = await diffTestSchemasSingleStore({}, to, []);
22
+
23
+ expect(statements.length).toBe(1);
24
+ expect(statements[0]).toStrictEqual({
25
+ type: 'create_table',
26
+ tableName: 'users',
27
+ schema: undefined,
28
+ columns: [],
29
+ compositePKs: [],
30
+ internals: {
31
+ tables: {},
32
+ indexes: {},
33
+ },
34
+ uniqueConstraints: [],
35
+ compositePkName: '',
36
+ });
37
+ });
38
+
39
+ test('add table #2', async () => {
40
+ const to = {
41
+ users: singlestoreTable('users', {
42
+ id: serial('id').primaryKey(),
43
+ }),
44
+ };
45
+
46
+ const { statements } = await diffTestSchemasSingleStore({}, to, []);
47
+
48
+ expect(statements.length).toBe(1);
49
+ expect(statements[0]).toStrictEqual({
50
+ type: 'create_table',
51
+ tableName: 'users',
52
+ schema: undefined,
53
+ columns: [
54
+ {
55
+ name: 'id',
56
+ notNull: true,
57
+ primaryKey: false,
58
+ type: 'serial',
59
+ autoincrement: true,
60
+ },
61
+ ],
62
+ compositePKs: ['users_id;id'],
63
+ compositePkName: 'users_id',
64
+ uniqueConstraints: [],
65
+ internals: {
66
+ tables: {},
67
+ indexes: {},
68
+ },
69
+ });
70
+ });
71
+
72
+ test('add table #3', async () => {
73
+ const to = {
74
+ users: singlestoreTable(
75
+ 'users',
76
+ {
77
+ id: serial('id'),
78
+ },
79
+ (t) => {
80
+ return {
81
+ pk: primaryKey({
82
+ name: 'users_pk',
83
+ columns: [t.id],
84
+ }),
85
+ };
86
+ },
87
+ ),
88
+ };
89
+
90
+ const { statements } = await diffTestSchemasSingleStore({}, to, []);
91
+
92
+ expect(statements.length).toBe(1);
93
+ expect(statements[0]).toStrictEqual({
94
+ type: 'create_table',
95
+ tableName: 'users',
96
+ schema: undefined,
97
+ columns: [
98
+ {
99
+ name: 'id',
100
+ notNull: true,
101
+ primaryKey: false,
102
+ type: 'serial',
103
+ autoincrement: true,
104
+ },
105
+ ],
106
+ compositePKs: ['users_pk;id'],
107
+ uniqueConstraints: [],
108
+ compositePkName: 'users_pk',
109
+ internals: {
110
+ tables: {},
111
+ indexes: {},
112
+ },
113
+ });
114
+ });
115
+
116
+ test('add table #4', async () => {
117
+ const to = {
118
+ users: singlestoreTable('users', {}),
119
+ posts: singlestoreTable('posts', {}),
120
+ };
121
+
122
+ const { statements } = await diffTestSchemasSingleStore({}, to, []);
123
+
124
+ expect(statements.length).toBe(2);
125
+ expect(statements[0]).toStrictEqual({
126
+ type: 'create_table',
127
+ tableName: 'users',
128
+ schema: undefined,
129
+ columns: [],
130
+ internals: {
131
+ tables: {},
132
+ indexes: {},
133
+ },
134
+ compositePKs: [],
135
+ uniqueConstraints: [],
136
+ compositePkName: '',
137
+ });
138
+ expect(statements[1]).toStrictEqual({
139
+ type: 'create_table',
140
+ tableName: 'posts',
141
+ schema: undefined,
142
+ columns: [],
143
+ compositePKs: [],
144
+ internals: {
145
+ tables: {},
146
+ indexes: {},
147
+ },
148
+ uniqueConstraints: [],
149
+ compositePkName: '',
150
+ });
151
+ });
152
+
153
+ test('add table #5', async () => {
154
+ const schema = singlestoreSchema('folder');
155
+ const from = {
156
+ schema,
157
+ };
158
+
159
+ const to = {
160
+ schema,
161
+ users: schema.table('users', {}),
162
+ };
163
+
164
+ const { statements } = await diffTestSchemasSingleStore(from, to, []);
165
+
166
+ expect(statements.length).toBe(0);
167
+ });
168
+
169
+ test('add table #6', async () => {
170
+ const from = {
171
+ users1: singlestoreTable('users1', {}),
172
+ };
173
+
174
+ const to = {
175
+ users2: singlestoreTable('users2', {}),
176
+ };
177
+
178
+ const { statements } = await diffTestSchemasSingleStore(from, to, []);
179
+
180
+ expect(statements.length).toBe(2);
181
+ expect(statements[0]).toStrictEqual({
182
+ type: 'create_table',
183
+ tableName: 'users2',
184
+ schema: undefined,
185
+ columns: [],
186
+ internals: {
187
+ tables: {},
188
+ indexes: {},
189
+ },
190
+ compositePKs: [],
191
+ uniqueConstraints: [],
192
+ compositePkName: '',
193
+ });
194
+ expect(statements[1]).toStrictEqual({
195
+ policies: [],
196
+ type: 'drop_table',
197
+ tableName: 'users1',
198
+ schema: undefined,
199
+ });
200
+ });
201
+
202
+ test('add table #7', async () => {
203
+ const from = {
204
+ users1: singlestoreTable('users1', {}),
205
+ };
206
+
207
+ const to = {
208
+ users: singlestoreTable('users', {}),
209
+ users2: singlestoreTable('users2', {}),
210
+ };
211
+
212
+ const { statements } = await diffTestSchemasSingleStore(from, to, [
213
+ 'public.users1->public.users2',
214
+ ]);
215
+
216
+ expect(statements.length).toBe(2);
217
+ expect(statements[0]).toStrictEqual({
218
+ type: 'rename_table',
219
+ tableNameFrom: 'users1',
220
+ tableNameTo: 'users2',
221
+ fromSchema: undefined,
222
+ toSchema: undefined,
223
+ });
224
+ expect(statements[1]).toStrictEqual({
225
+ type: 'create_table',
226
+ tableName: 'users',
227
+ schema: undefined,
228
+ columns: [],
229
+ compositePKs: [],
230
+ uniqueConstraints: [],
231
+ internals: {
232
+ tables: {},
233
+ indexes: {},
234
+ },
235
+ compositePkName: '',
236
+ });
237
+ });
238
+
239
+ test('add schema + table #1', async () => {
240
+ const schema = singlestoreSchema('folder');
241
+
242
+ const to = {
243
+ schema,
244
+ users: schema.table('users', {}),
245
+ };
246
+
247
+ const { statements } = await diffTestSchemasSingleStore({}, to, []);
248
+
249
+ expect(statements.length).toBe(0);
250
+ });
251
+
252
+ test('change schema with tables #1', async () => {
253
+ const schema = singlestoreSchema('folder');
254
+ const schema2 = singlestoreSchema('folder2');
255
+ const from = {
256
+ schema,
257
+ users: schema.table('users', {}),
258
+ };
259
+ const to = {
260
+ schema2,
261
+ users: schema2.table('users', {}),
262
+ };
263
+
264
+ const { statements } = await diffTestSchemasSingleStore(from, to, [
265
+ 'folder->folder2',
266
+ ]);
267
+
268
+ expect(statements.length).toBe(0);
269
+ });
270
+
271
+ test('change table schema #1', async () => {
272
+ const schema = singlestoreSchema('folder');
273
+ const from = {
274
+ schema,
275
+ users: singlestoreTable('users', {}),
276
+ };
277
+ const to = {
278
+ schema,
279
+ users: schema.table('users', {}),
280
+ };
281
+
282
+ const { statements } = await diffTestSchemasSingleStore(from, to, [
283
+ 'public.users->folder.users',
284
+ ]);
285
+
286
+ expect(statements.length).toBe(1);
287
+ expect(statements[0]).toStrictEqual({
288
+ policies: [],
289
+ type: 'drop_table',
290
+ tableName: 'users',
291
+ schema: undefined,
292
+ });
293
+ });
294
+
295
+ test('change table schema #2', async () => {
296
+ const schema = singlestoreSchema('folder');
297
+ const from = {
298
+ schema,
299
+ users: schema.table('users', {}),
300
+ };
301
+ const to = {
302
+ schema,
303
+ users: singlestoreTable('users', {}),
304
+ };
305
+
306
+ const { statements } = await diffTestSchemasSingleStore(from, to, [
307
+ 'folder.users->public.users',
308
+ ]);
309
+
310
+ expect(statements.length).toBe(1);
311
+ expect(statements[0]).toStrictEqual({
312
+ type: 'create_table',
313
+ tableName: 'users',
314
+ schema: undefined,
315
+ columns: [],
316
+ uniqueConstraints: [],
317
+ compositePkName: '',
318
+ compositePKs: [],
319
+ internals: {
320
+ tables: {},
321
+ indexes: {},
322
+ },
323
+ });
324
+ });
325
+
326
+ test('change table schema #3', async () => {
327
+ const schema1 = singlestoreSchema('folder1');
328
+ const schema2 = singlestoreSchema('folder2');
329
+ const from = {
330
+ schema1,
331
+ schema2,
332
+ users: schema1.table('users', {}),
333
+ };
334
+ const to = {
335
+ schema1,
336
+ schema2,
337
+ users: schema2.table('users', {}),
338
+ };
339
+
340
+ const { statements } = await diffTestSchemasSingleStore(from, to, [
341
+ 'folder1.users->folder2.users',
342
+ ]);
343
+
344
+ expect(statements.length).toBe(0);
345
+ });
346
+
347
+ test('change table schema #4', async () => {
348
+ const schema1 = singlestoreSchema('folder1');
349
+ const schema2 = singlestoreSchema('folder2');
350
+ const from = {
351
+ schema1,
352
+ users: schema1.table('users', {}),
353
+ };
354
+ const to = {
355
+ schema1,
356
+ schema2, // add schema
357
+ users: schema2.table('users', {}), // move table
358
+ };
359
+
360
+ const { statements } = await diffTestSchemasSingleStore(from, to, [
361
+ 'folder1.users->folder2.users',
362
+ ]);
363
+
364
+ expect(statements.length).toBe(0);
365
+ });
366
+
367
+ test('change table schema #5', async () => {
368
+ const schema1 = singlestoreSchema('folder1');
369
+ const schema2 = singlestoreSchema('folder2');
370
+ const from = {
371
+ schema1, // remove schema
372
+ users: schema1.table('users', {}),
373
+ };
374
+ const to = {
375
+ schema2, // add schema
376
+ users: schema2.table('users', {}), // move table
377
+ };
378
+
379
+ const { statements } = await diffTestSchemasSingleStore(from, to, [
380
+ 'folder1.users->folder2.users',
381
+ ]);
382
+
383
+ expect(statements.length).toBe(0);
384
+ });
385
+
386
+ test('change table schema #5', async () => {
387
+ const schema1 = singlestoreSchema('folder1');
388
+ const schema2 = singlestoreSchema('folder2');
389
+ const from = {
390
+ schema1,
391
+ schema2,
392
+ users: schema1.table('users', {}),
393
+ };
394
+ const to = {
395
+ schema1,
396
+ schema2,
397
+ users: schema2.table('users2', {}), // rename and move table
398
+ };
399
+
400
+ const { statements } = await diffTestSchemasSingleStore(from, to, [
401
+ 'folder1.users->folder2.users2',
402
+ ]);
403
+
404
+ expect(statements.length).toBe(0);
405
+ });
406
+
407
+ test('change table schema #6', async () => {
408
+ const schema1 = singlestoreSchema('folder1');
409
+ const schema2 = singlestoreSchema('folder2');
410
+ const from = {
411
+ schema1,
412
+ users: schema1.table('users', {}),
413
+ };
414
+ const to = {
415
+ schema2, // rename schema
416
+ users: schema2.table('users2', {}), // rename table
417
+ };
418
+
419
+ const { statements } = await diffTestSchemasSingleStore(from, to, [
420
+ 'folder1->folder2',
421
+ 'folder2.users->folder2.users2',
422
+ ]);
423
+
424
+ expect(statements.length).toBe(0);
425
+ });
426
+
427
+ test('add table #10', async () => {
428
+ const to = {
429
+ users: singlestoreTable('table', {
430
+ json: json('json').default({}),
431
+ }),
432
+ };
433
+
434
+ const { sqlStatements } = await diffTestSchemasSingleStore({}, to, []);
435
+ expect(sqlStatements.length).toBe(1);
436
+ expect(sqlStatements[0]).toBe(
437
+ "CREATE TABLE `table` (\n\t`json` json DEFAULT '{}'\n);\n",
438
+ );
439
+ });
440
+
441
+ test('add table #11', async () => {
442
+ const to = {
443
+ users: singlestoreTable('table', {
444
+ json: json('json').default([]),
445
+ }),
446
+ };
447
+
448
+ const { sqlStatements } = await diffTestSchemasSingleStore({}, to, []);
449
+ expect(sqlStatements.length).toBe(1);
450
+ expect(sqlStatements[0]).toBe(
451
+ "CREATE TABLE `table` (\n\t`json` json DEFAULT '[]'\n);\n",
452
+ );
453
+ });
454
+
455
+ test('add table #12', async () => {
456
+ const to = {
457
+ users: singlestoreTable('table', {
458
+ json: json('json').default([1, 2, 3]),
459
+ }),
460
+ };
461
+
462
+ const { sqlStatements } = await diffTestSchemasSingleStore({}, to, []);
463
+ expect(sqlStatements.length).toBe(1);
464
+ expect(sqlStatements[0]).toBe(
465
+ "CREATE TABLE `table` (\n\t`json` json DEFAULT '[1,2,3]'\n);\n",
466
+ );
467
+ });
468
+
469
+ test('add table #13', async () => {
470
+ const to = {
471
+ users: singlestoreTable('table', {
472
+ json: json('json').default({ key: 'value' }),
473
+ }),
474
+ };
475
+
476
+ const { sqlStatements } = await diffTestSchemasSingleStore({}, to, []);
477
+ expect(sqlStatements.length).toBe(1);
478
+ expect(sqlStatements[0]).toBe(
479
+ 'CREATE TABLE `table` (\n\t`json` json DEFAULT \'{"key":"value"}\'\n);\n',
480
+ );
481
+ });
482
+
483
+ test('add table #14', async () => {
484
+ const to = {
485
+ users: singlestoreTable('table', {
486
+ json: json('json').default({
487
+ key: 'value',
488
+ arr: [1, 2, 3],
489
+ }),
490
+ }),
491
+ };
492
+
493
+ const { sqlStatements } = await diffTestSchemasSingleStore({}, to, []);
494
+ expect(sqlStatements.length).toBe(1);
495
+ expect(sqlStatements[0]).toBe(
496
+ 'CREATE TABLE `table` (\n\t`json` json DEFAULT \'{"key":"value","arr":[1,2,3]}\'\n);\n',
497
+ );
498
+ });
499
+
500
+ // TODO: add bson type tests
501
+
502
+ // TODO: add blob type tests
503
+
504
+ // TODO: add uuid type tests
505
+
506
+ // TODO: add guid type tests
507
+
508
+ // TODO: add vector type tests
509
+
510
+ // TODO: add geopoint type tests
511
+
512
+ test('drop index', async () => {
513
+ const from = {
514
+ users: singlestoreTable(
515
+ 'table',
516
+ {
517
+ name: text('name'),
518
+ },
519
+ (t) => {
520
+ return {
521
+ idx: index('name_idx').on(t.name),
522
+ };
523
+ },
524
+ ),
525
+ };
526
+
527
+ const to = {
528
+ users: singlestoreTable('table', {
529
+ name: text('name'),
530
+ }),
531
+ };
532
+
533
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, []);
534
+ expect(sqlStatements.length).toBe(1);
535
+ expect(sqlStatements[0]).toBe('DROP INDEX `name_idx` ON `table`;');
536
+ });
537
+
538
+ test('add table with indexes', async () => {
539
+ const from = {};
540
+
541
+ const to = {
542
+ users: singlestoreTable(
543
+ 'users',
544
+ {
545
+ id: serial('id').primaryKey(),
546
+ name: text('name'),
547
+ email: text('email'),
548
+ },
549
+ (t) => ({
550
+ uniqueExpr: uniqueIndex('uniqueExpr').on(sql`(lower(${t.email}))`),
551
+ indexExpr: index('indexExpr').on(sql`(lower(${t.email}))`),
552
+ indexExprMultiple: index('indexExprMultiple').on(
553
+ sql`(lower(${t.email}))`,
554
+ sql`(lower(${t.email}))`,
555
+ ),
556
+
557
+ uniqueCol: uniqueIndex('uniqueCol').on(t.email),
558
+ indexCol: index('indexCol').on(t.email),
559
+ indexColMultiple: index('indexColMultiple').on(t.email, t.email),
560
+
561
+ indexColExpr: index('indexColExpr').on(
562
+ sql`(lower(${t.email}))`,
563
+ t.email,
564
+ ),
565
+ }),
566
+ ),
567
+ };
568
+
569
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, []);
570
+ expect(sqlStatements.length).toBe(6);
571
+ expect(sqlStatements).toStrictEqual([
572
+ `CREATE TABLE \`users\` (\n\t\`id\` serial AUTO_INCREMENT NOT NULL,\n\t\`name\` text,\n\t\`email\` text,\n\tCONSTRAINT \`users_id\` PRIMARY KEY(\`id\`),\n\tCONSTRAINT \`uniqueExpr\` UNIQUE((lower(\`email\`))),\n\tCONSTRAINT \`uniqueCol\` UNIQUE(\`email\`)
573
+ );
574
+ `,
575
+ 'CREATE INDEX `indexExpr` ON `users` ((lower(`email`)));',
576
+ 'CREATE INDEX `indexExprMultiple` ON `users` ((lower(`email`)),(lower(`email`)));',
577
+ 'CREATE INDEX `indexCol` ON `users` (`email`);',
578
+ 'CREATE INDEX `indexColMultiple` ON `users` (`email`,`email`);',
579
+ 'CREATE INDEX `indexColExpr` ON `users` ((lower(`email`)),`email`);',
580
+ ]);
581
+ });
582
+
583
+ test('rename table', async () => {
584
+ const from = {
585
+ table: singlestoreTable('table', {
586
+ json: json('json').default([]),
587
+ }),
588
+ };
589
+
590
+ const to = {
591
+ table1: singlestoreTable('table1', {
592
+ json1: json('json').default([]),
593
+ }),
594
+ };
595
+
596
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, [`public.table->public.table1`]);
597
+ expect(sqlStatements.length).toBe(1);
598
+ expect(sqlStatements[0]).toBe(
599
+ 'ALTER TABLE `table` RENAME TO `table1`;',
600
+ );
601
+ });
602
+
603
+ test('rename column', async () => {
604
+ const from = {
605
+ users: singlestoreTable('table', {
606
+ json: json('json').default([]),
607
+ }),
608
+ };
609
+
610
+ const to = {
611
+ users: singlestoreTable('table', {
612
+ json1: json('json1').default([]),
613
+ }),
614
+ };
615
+
616
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, [`public.table.json->public.table.json1`]);
617
+ expect(sqlStatements.length).toBe(1);
618
+ expect(sqlStatements[0]).toBe(
619
+ 'ALTER TABLE `table` CHANGE `json` `json1`;',
620
+ );
621
+ });
622
+
623
+ test('change data type', async () => {
624
+ const from = {
625
+ table: singlestoreTable('table', {
626
+ id: int(),
627
+ age: text(),
628
+ }),
629
+ };
630
+
631
+ const to = {
632
+ table: singlestoreTable('table', {
633
+ id: int(),
634
+ age: int(),
635
+ }),
636
+ };
637
+
638
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, []);
639
+ expect(sqlStatements.length).toBe(4);
640
+ expect(sqlStatements[0]).toBe(
641
+ `CREATE TABLE \`__new_table\` (
642
+ \t\`id\` int,
643
+ \t\`age\` int
644
+ );\n`,
645
+ );
646
+ expect(sqlStatements[1]).toBe(
647
+ 'INSERT INTO `__new_table`(`id`, `age`) SELECT `id`, `age` FROM `table`;',
648
+ );
649
+ expect(sqlStatements[2]).toBe(
650
+ 'DROP TABLE `table`;',
651
+ );
652
+ expect(sqlStatements[3]).toBe(
653
+ 'ALTER TABLE `__new_table` RENAME TO `table`;',
654
+ );
655
+ });
656
+
657
+ test('drop not null', async () => {
658
+ const from = {
659
+ table: singlestoreTable('table', {
660
+ id: int().notNull(),
661
+ age: int(),
662
+ }),
663
+ };
664
+
665
+ const to = {
666
+ table: singlestoreTable('table', {
667
+ id: int(),
668
+ age: int(),
669
+ }),
670
+ };
671
+
672
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, []);
673
+ expect(sqlStatements.length).toBe(4);
674
+ expect(sqlStatements[0]).toBe(
675
+ `CREATE TABLE \`__new_table\` (
676
+ \t\`id\` int,
677
+ \t\`age\` int
678
+ );\n`,
679
+ );
680
+ expect(sqlStatements[1]).toBe(
681
+ 'INSERT INTO `__new_table`(`id`, `age`) SELECT `id`, `age` FROM `table`;',
682
+ );
683
+ expect(sqlStatements[2]).toBe(
684
+ 'DROP TABLE `table`;',
685
+ );
686
+ expect(sqlStatements[3]).toBe(
687
+ 'ALTER TABLE `__new_table` RENAME TO `table`;',
688
+ );
689
+ });
690
+
691
+ test('set not null', async () => {
692
+ const from = {
693
+ table: singlestoreTable('table', {
694
+ id: int(),
695
+ age: int(),
696
+ }),
697
+ };
698
+
699
+ const to = {
700
+ table: singlestoreTable('table', {
701
+ id: int().notNull(),
702
+ age: int(),
703
+ }),
704
+ };
705
+
706
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, []);
707
+ expect(sqlStatements.length).toBe(4);
708
+ expect(sqlStatements[0]).toBe(
709
+ `CREATE TABLE \`__new_table\` (
710
+ \t\`id\` int NOT NULL,
711
+ \t\`age\` int
712
+ );\n`,
713
+ );
714
+ expect(sqlStatements[1]).toBe(
715
+ 'INSERT INTO `__new_table`(`id`, `age`) SELECT `id`, `age` FROM `table`;',
716
+ );
717
+ expect(sqlStatements[2]).toBe(
718
+ 'DROP TABLE `table`;',
719
+ );
720
+ expect(sqlStatements[3]).toBe(
721
+ 'ALTER TABLE `__new_table` RENAME TO `table`;',
722
+ );
723
+ });
724
+
725
+ test('set default with not null column', async () => {
726
+ const from = {
727
+ table: singlestoreTable('table', {
728
+ id: int().notNull(),
729
+ age: int(),
730
+ }),
731
+ };
732
+
733
+ const to = {
734
+ table: singlestoreTable('table', {
735
+ id: int().notNull().default(1),
736
+ age: int(),
737
+ }),
738
+ };
739
+
740
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, []);
741
+ expect(sqlStatements.length).toBe(4);
742
+ expect(sqlStatements[0]).toBe(
743
+ `CREATE TABLE \`__new_table\` (
744
+ \t\`id\` int NOT NULL DEFAULT 1,
745
+ \t\`age\` int
746
+ );\n`,
747
+ );
748
+ expect(sqlStatements[1]).toBe(
749
+ 'INSERT INTO `__new_table`(`id`, `age`) SELECT `id`, `age` FROM `table`;',
750
+ );
751
+ expect(sqlStatements[2]).toBe(
752
+ 'DROP TABLE `table`;',
753
+ );
754
+ expect(sqlStatements[3]).toBe(
755
+ 'ALTER TABLE `__new_table` RENAME TO `table`;',
756
+ );
757
+ });
758
+
759
+ test('drop default with not null column', async () => {
760
+ const from = {
761
+ table: singlestoreTable('table', {
762
+ id: int().notNull().default(1),
763
+ age: int(),
764
+ }),
765
+ };
766
+
767
+ const to = {
768
+ table: singlestoreTable('table', {
769
+ id: int().notNull(),
770
+ age: int(),
771
+ }),
772
+ };
773
+
774
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, []);
775
+ expect(sqlStatements.length).toBe(4);
776
+ expect(sqlStatements[0]).toBe(
777
+ `CREATE TABLE \`__new_table\` (
778
+ \t\`id\` int NOT NULL,
779
+ \t\`age\` int
780
+ );\n`,
781
+ );
782
+ expect(sqlStatements[1]).toBe(
783
+ 'INSERT INTO `__new_table`(`id`, `age`) SELECT `id`, `age` FROM `table`;',
784
+ );
785
+ expect(sqlStatements[2]).toBe(
786
+ 'DROP TABLE `table`;',
787
+ );
788
+ expect(sqlStatements[3]).toBe(
789
+ 'ALTER TABLE `__new_table` RENAME TO `table`;',
790
+ );
791
+ });
792
+
793
+ test('set default', async () => {
794
+ const from = {
795
+ table: singlestoreTable('table', {
796
+ id: int(),
797
+ age: int(),
798
+ }),
799
+ };
800
+
801
+ const to = {
802
+ table: singlestoreTable('table', {
803
+ id: int().default(1),
804
+ age: int(),
805
+ }),
806
+ };
807
+
808
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, []);
809
+ expect(sqlStatements.length).toBe(1);
810
+ expect(sqlStatements[0]).toBe(
811
+ 'ALTER TABLE `table` MODIFY COLUMN `id` int DEFAULT 1;',
812
+ );
813
+ });
814
+
815
+ test('drop default', async () => {
816
+ const from = {
817
+ table: singlestoreTable('table', {
818
+ id: int().default(1),
819
+ age: int(),
820
+ }),
821
+ };
822
+
823
+ const to = {
824
+ table: singlestoreTable('table', {
825
+ id: int(),
826
+ age: int(),
827
+ }),
828
+ };
829
+
830
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, []);
831
+ expect(sqlStatements.length).toBe(1);
832
+ expect(sqlStatements[0]).toBe(
833
+ 'ALTER TABLE `table` MODIFY COLUMN `id` int;',
834
+ );
835
+ });
836
+
837
+ test('set pk', async () => {
838
+ const from = {
839
+ table: singlestoreTable('table', {
840
+ id: int(),
841
+ age: int(),
842
+ }),
843
+ };
844
+
845
+ const to = {
846
+ table: singlestoreTable('table', {
847
+ id: int().primaryKey(),
848
+ age: int(),
849
+ }),
850
+ };
851
+
852
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, []);
853
+ expect(sqlStatements.length).toBe(4);
854
+ expect(sqlStatements[0]).toBe(
855
+ `CREATE TABLE \`__new_table\` (
856
+ \t\`id\` int NOT NULL,
857
+ \t\`age\` int,
858
+ \tCONSTRAINT \`table_id\` PRIMARY KEY(\`id\`)
859
+ );\n`,
860
+ );
861
+ expect(sqlStatements[1]).toBe(
862
+ 'INSERT INTO `__new_table`(`id`, `age`) SELECT `id`, `age` FROM `table`;',
863
+ );
864
+ expect(sqlStatements[2]).toBe(
865
+ 'DROP TABLE `table`;',
866
+ );
867
+ expect(sqlStatements[3]).toBe(
868
+ 'ALTER TABLE `__new_table` RENAME TO `table`;',
869
+ );
870
+ });
871
+
872
+ test('drop pk', async () => {
873
+ const from = {
874
+ table: singlestoreTable('table', {
875
+ id: int().primaryKey(),
876
+ age: int(),
877
+ }),
878
+ };
879
+
880
+ const to = {
881
+ table: singlestoreTable('table', {
882
+ id: int(),
883
+ age: int(),
884
+ }),
885
+ };
886
+
887
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, []);
888
+ expect(sqlStatements.length).toBe(4);
889
+ expect(sqlStatements[0]).toBe(
890
+ `CREATE TABLE \`__new_table\` (
891
+ \t\`id\` int,
892
+ \t\`age\` int
893
+ );\n`,
894
+ );
895
+ expect(sqlStatements[1]).toBe(
896
+ 'INSERT INTO `__new_table`(`id`, `age`) SELECT `id`, `age` FROM `table`;',
897
+ );
898
+ expect(sqlStatements[2]).toBe(
899
+ 'DROP TABLE `table`;',
900
+ );
901
+ expect(sqlStatements[3]).toBe(
902
+ 'ALTER TABLE `__new_table` RENAME TO `table`;',
903
+ );
904
+ });
905
+
906
+ test('set not null + rename column on table with indexes', async () => {
907
+ const from = {
908
+ table: singlestoreTable('table', {
909
+ id: int('id').default(1),
910
+ age: int(),
911
+ }),
912
+ };
913
+
914
+ const to = {
915
+ table: singlestoreTable('table', {
916
+ id3: int('id3').notNull().default(1),
917
+ age: int(),
918
+ }),
919
+ };
920
+
921
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, [`public.table.id->public.table.id3`]);
922
+ expect(sqlStatements.length).toBe(5);
923
+ expect(sqlStatements[0]).toBe(
924
+ 'ALTER TABLE \`table\` CHANGE `id` `id3`;',
925
+ );
926
+ expect(sqlStatements[1]).toBe(
927
+ `CREATE TABLE \`__new_table\` (
928
+ \t\`id3\` int NOT NULL DEFAULT 1,
929
+ \t\`age\` int
930
+ );\n`,
931
+ );
932
+ expect(sqlStatements[2]).toBe(
933
+ 'INSERT INTO `__new_table`(`id3`, `age`) SELECT `id3`, `age` FROM `table`;',
934
+ );
935
+ expect(sqlStatements[3]).toBe(
936
+ 'DROP TABLE `table`;',
937
+ );
938
+ expect(sqlStatements[4]).toBe(
939
+ 'ALTER TABLE `__new_table` RENAME TO `table`;',
940
+ );
941
+ });
942
+
943
+ test('set not null + rename table on table with indexes', async () => {
944
+ const from = {
945
+ table: singlestoreTable('table', {
946
+ id: int('id').default(1),
947
+ age: int(),
948
+ }),
949
+ };
950
+
951
+ const to = {
952
+ table1: singlestoreTable('table1', {
953
+ id: int('id').notNull().default(1),
954
+ age: int(),
955
+ }),
956
+ };
957
+
958
+ const { sqlStatements } = await diffTestSchemasSingleStore(from, to, [`public.table->public.table1`]);
959
+ expect(sqlStatements.length).toBe(5);
960
+ expect(sqlStatements[0]).toBe(
961
+ 'ALTER TABLE `table` RENAME TO `table1`;',
962
+ );
963
+ expect(sqlStatements[1]).toBe(
964
+ `CREATE TABLE \`__new_table1\` (
965
+ \t\`id\` int NOT NULL DEFAULT 1,
966
+ \t\`age\` int
967
+ );\n`,
968
+ );
969
+ expect(sqlStatements[2]).toBe(
970
+ 'INSERT INTO `__new_table1`(\`id\`, \`age\`) SELECT \`id\`, \`age\` FROM `table1`;',
971
+ );
972
+ expect(sqlStatements[3]).toBe(
973
+ 'DROP TABLE `table1`;',
974
+ );
975
+ expect(sqlStatements[4]).toBe(
976
+ 'ALTER TABLE `__new_table1` RENAME TO `table1`;',
977
+ );
978
+ });