@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,345 @@
1
+ import Docker from 'dockerode';
2
+ import { sql } from 'drizzle-orm';
3
+ import { check, int, mysqlTable, mysqlView } from 'drizzle-orm/mysql-core';
4
+ import fs from 'fs';
5
+ import getPort from 'get-port';
6
+ import { Connection, createConnection } from 'mysql2/promise';
7
+ import { diffTestSchemasPushMysql } from 'tests/schemaDiffer';
8
+ import { v4 as uuid } from 'uuid';
9
+ import { afterAll, beforeAll, expect, test } from 'vitest';
10
+
11
+ let client: Connection;
12
+ let mysqlContainer: Docker.Container;
13
+
14
+ async function createDockerDB(): Promise<string> {
15
+ const docker = new Docker();
16
+ const port = await getPort({ port: 3306 });
17
+ const image = 'mysql:8';
18
+
19
+ const pullStream = await docker.pull(image);
20
+ await new Promise((resolve, reject) =>
21
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
22
+ docker.modem.followProgress(pullStream, (err) => err ? reject(err) : resolve(err))
23
+ );
24
+
25
+ mysqlContainer = await docker.createContainer({
26
+ Image: image,
27
+ Env: ['MYSQL_ROOT_PASSWORD=mysql', 'MYSQL_DATABASE=drizzle'],
28
+ name: `drizzle-integration-tests-${uuid()}`,
29
+ HostConfig: {
30
+ AutoRemove: true,
31
+ PortBindings: {
32
+ '3306/tcp': [{ HostPort: `${port}` }],
33
+ },
34
+ },
35
+ });
36
+
37
+ await mysqlContainer.start();
38
+
39
+ return `mysql://root:mysql@127.0.0.1:${port}/drizzle`;
40
+ }
41
+
42
+ beforeAll(async () => {
43
+ const connectionString = process.env.MYSQL_CONNECTION_STRING ?? await createDockerDB();
44
+
45
+ const sleep = 1000;
46
+ let timeLeft = 20000;
47
+ let connected = false;
48
+ let lastError: unknown | undefined;
49
+ do {
50
+ try {
51
+ client = await createConnection(connectionString);
52
+ await client.connect();
53
+ connected = true;
54
+ break;
55
+ } catch (e) {
56
+ lastError = e;
57
+ await new Promise((resolve) => setTimeout(resolve, sleep));
58
+ timeLeft -= sleep;
59
+ }
60
+ } while (timeLeft > 0);
61
+ if (!connected) {
62
+ console.error('Cannot connect to MySQL');
63
+ await client?.end().catch(console.error);
64
+ await mysqlContainer?.stop().catch(console.error);
65
+ throw lastError;
66
+ }
67
+ });
68
+
69
+ afterAll(async () => {
70
+ await client?.end().catch(console.error);
71
+ await mysqlContainer?.stop().catch(console.error);
72
+ });
73
+
74
+ if (!fs.existsSync('tests/push/mysql')) {
75
+ fs.mkdirSync('tests/push/mysql');
76
+ }
77
+
78
+ test('add check constraint to table', async () => {
79
+ const schema1 = {
80
+ test: mysqlTable('test', {
81
+ id: int('id').primaryKey(),
82
+ values: int('values'),
83
+ }),
84
+ };
85
+ const schema2 = {
86
+ test: mysqlTable('test', {
87
+ id: int('id').primaryKey(),
88
+ values: int('values'),
89
+ }, (table) => ({
90
+ checkConstraint1: check('some_check1', sql`${table.values} < 100`),
91
+ checkConstraint2: check('some_check2', sql`'test' < 100`),
92
+ })),
93
+ };
94
+
95
+ const { statements, sqlStatements } = await diffTestSchemasPushMysql(
96
+ client,
97
+ schema1,
98
+ schema2,
99
+ [],
100
+ 'drizzle',
101
+ false,
102
+ );
103
+
104
+ expect(statements).toStrictEqual([
105
+ {
106
+ type: 'create_check_constraint',
107
+ tableName: 'test',
108
+ schema: '',
109
+ data: 'some_check1;\`test\`.\`values\` < 100',
110
+ },
111
+ {
112
+ data: "some_check2;'test' < 100",
113
+ schema: '',
114
+ tableName: 'test',
115
+ type: 'create_check_constraint',
116
+ },
117
+ ]);
118
+ expect(sqlStatements).toStrictEqual([
119
+ 'ALTER TABLE \`test\` ADD CONSTRAINT \`some_check1\` CHECK (\`test\`.\`values\` < 100);',
120
+ `ALTER TABLE \`test\` ADD CONSTRAINT \`some_check2\` CHECK ('test' < 100);`,
121
+ ]);
122
+
123
+ await client.query(`DROP TABLE \`test\`;`);
124
+ });
125
+
126
+ test('drop check constraint to table', async () => {
127
+ const schema1 = {
128
+ test: mysqlTable('test', {
129
+ id: int('id').primaryKey(),
130
+ values: int('values'),
131
+ }, (table) => ({
132
+ checkConstraint1: check('some_check1', sql`${table.values} < 100`),
133
+ checkConstraint2: check('some_check2', sql`'test' < 100`),
134
+ })),
135
+ };
136
+ const schema2 = {
137
+ test: mysqlTable('test', {
138
+ id: int('id').primaryKey(),
139
+ values: int('values'),
140
+ }),
141
+ };
142
+
143
+ const { statements, sqlStatements } = await diffTestSchemasPushMysql(
144
+ client,
145
+ schema1,
146
+ schema2,
147
+ [],
148
+ 'drizzle',
149
+ false,
150
+ );
151
+
152
+ expect(statements).toStrictEqual([
153
+ {
154
+ type: 'delete_check_constraint',
155
+ tableName: 'test',
156
+ schema: '',
157
+ constraintName: 'some_check1',
158
+ },
159
+ {
160
+ constraintName: 'some_check2',
161
+ schema: '',
162
+ tableName: 'test',
163
+ type: 'delete_check_constraint',
164
+ },
165
+ ]);
166
+ expect(sqlStatements).toStrictEqual([
167
+ 'ALTER TABLE \`test\` DROP CONSTRAINT \`some_check1\`;',
168
+ `ALTER TABLE \`test\` DROP CONSTRAINT \`some_check2\`;`,
169
+ ]);
170
+
171
+ await client.query(`DROP TABLE \`test\`;`);
172
+ });
173
+
174
+ test('db has checks. Push with same names', async () => {
175
+ const schema1 = {
176
+ test: mysqlTable('test', {
177
+ id: int('id').primaryKey(),
178
+ values: int('values').default(1),
179
+ }, (table) => ({
180
+ checkConstraint: check('some_check', sql`${table.values} < 100`),
181
+ })),
182
+ };
183
+ const schema2 = {
184
+ test: mysqlTable('test', {
185
+ id: int('id').primaryKey(),
186
+ values: int('values').default(1),
187
+ }, (table) => ({
188
+ checkConstraint: check('some_check', sql`some new value`),
189
+ })),
190
+ };
191
+
192
+ const { statements, sqlStatements } = await diffTestSchemasPushMysql(
193
+ client,
194
+ schema1,
195
+ schema2,
196
+ [],
197
+ 'drizzle',
198
+ );
199
+
200
+ expect(statements).toStrictEqual([]);
201
+ expect(sqlStatements).toStrictEqual([]);
202
+
203
+ await client.query(`DROP TABLE \`test\`;`);
204
+ });
205
+
206
+ test('create view', async () => {
207
+ const table = mysqlTable('test', {
208
+ id: int('id').primaryKey(),
209
+ });
210
+
211
+ const schema1 = {
212
+ test: table,
213
+ };
214
+
215
+ const schema2 = {
216
+ test: table,
217
+ view: mysqlView('view').as((qb) => qb.select().from(table)),
218
+ };
219
+
220
+ const { statements, sqlStatements } = await diffTestSchemasPushMysql(
221
+ client,
222
+ schema1,
223
+ schema2,
224
+ [],
225
+ 'drizzle',
226
+ false,
227
+ );
228
+
229
+ expect(statements).toStrictEqual([
230
+ {
231
+ definition: 'select \`id\` from \`test\`',
232
+ name: 'view',
233
+ type: 'mysql_create_view',
234
+ replace: false,
235
+ sqlSecurity: 'definer',
236
+ withCheckOption: undefined,
237
+ algorithm: 'undefined',
238
+ },
239
+ ]);
240
+ expect(sqlStatements).toStrictEqual([
241
+ `CREATE ALGORITHM = undefined
242
+ SQL SECURITY definer
243
+ VIEW \`view\` AS (select \`id\` from \`test\`);`,
244
+ ]);
245
+
246
+ await client.query(`DROP TABLE \`test\`;`);
247
+ });
248
+
249
+ test('drop view', async () => {
250
+ const table = mysqlTable('test', {
251
+ id: int('id').primaryKey(),
252
+ });
253
+
254
+ const schema1 = {
255
+ test: table,
256
+ view: mysqlView('view').as((qb) => qb.select().from(table)),
257
+ };
258
+
259
+ const schema2 = {
260
+ test: table,
261
+ };
262
+
263
+ const { statements, sqlStatements } = await diffTestSchemasPushMysql(
264
+ client,
265
+ schema1,
266
+ schema2,
267
+ [],
268
+ 'drizzle',
269
+ false,
270
+ );
271
+
272
+ expect(statements).toStrictEqual([
273
+ {
274
+ name: 'view',
275
+ type: 'drop_view',
276
+ },
277
+ ]);
278
+ expect(sqlStatements).toStrictEqual([
279
+ 'DROP VIEW \`view\`;',
280
+ ]);
281
+ await client.query(`DROP TABLE \`test\`;`);
282
+ await client.query(`DROP VIEW \`view\`;`);
283
+ });
284
+
285
+ test('alter view ".as"', async () => {
286
+ const table = mysqlTable('test', {
287
+ id: int('id').primaryKey(),
288
+ });
289
+
290
+ const schema1 = {
291
+ test: table,
292
+ view: mysqlView('view').as((qb) => qb.select().from(table).where(sql`${table.id} = 1`)),
293
+ };
294
+
295
+ const schema2 = {
296
+ test: table,
297
+ view: mysqlView('view').as((qb) => qb.select().from(table)),
298
+ };
299
+
300
+ const { statements, sqlStatements } = await diffTestSchemasPushMysql(
301
+ client,
302
+ schema1,
303
+ schema2,
304
+ [],
305
+ 'drizzle',
306
+ false,
307
+ );
308
+
309
+ expect(statements.length).toBe(0);
310
+ expect(sqlStatements.length).toBe(0);
311
+
312
+ await client.query(`DROP TABLE \`test\`;`);
313
+ await client.query(`DROP VIEW \`view\`;`);
314
+ });
315
+
316
+ test('alter meta options with distinct in definition', async () => {
317
+ const table = mysqlTable('test', {
318
+ id: int('id').primaryKey(),
319
+ });
320
+
321
+ const schema1 = {
322
+ test: table,
323
+ view: mysqlView('view').withCheckOption('cascaded').sqlSecurity('definer').algorithm('merge').as((
324
+ qb,
325
+ ) => qb.selectDistinct().from(table).where(sql`${table.id} = 1`)),
326
+ };
327
+
328
+ const schema2 = {
329
+ test: table,
330
+ view: mysqlView('view').withCheckOption('cascaded').sqlSecurity('definer').algorithm('undefined').as((qb) =>
331
+ qb.selectDistinct().from(table)
332
+ ),
333
+ };
334
+
335
+ await expect(diffTestSchemasPushMysql(
336
+ client,
337
+ schema1,
338
+ schema2,
339
+ [],
340
+ 'drizzle',
341
+ false,
342
+ )).rejects.toThrowError();
343
+
344
+ await client.query(`DROP TABLE \`test\`;`);
345
+ });