@anthonyalayo/drizzle-kit 1.0.1 → 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 +2 -2
  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,637 +0,0 @@
1
- import chalk from 'chalk';
2
- import { randomUUID } from 'crypto';
3
- import { render } from 'hanji';
4
- import { serializePg } from 'src/serializer';
5
- import { fromJson } from '../../sqlgenerator';
6
- import { Select } from '../selector-ui';
7
- import { Entities } from '../validations/cli';
8
- import { CasingType } from '../validations/common';
9
- import { LibSQLCredentials } from '../validations/libsql';
10
- import type { MysqlCredentials } from '../validations/mysql';
11
- import { withStyle } from '../validations/outputs';
12
- import type { PostgresCredentials } from '../validations/postgres';
13
- import { SingleStoreCredentials } from '../validations/singlestore';
14
- import type { SqliteCredentials } from '../validations/sqlite';
15
- import { libSqlLogSuggestionsAndReturn } from './libSqlPushUtils';
16
- import {
17
- filterStatements as mySqlFilterStatements,
18
- logSuggestionsAndReturn as mySqlLogSuggestionsAndReturn,
19
- } from './mysqlPushUtils';
20
- import { pgSuggestions } from './pgPushUtils';
21
- import {
22
- filterStatements as singleStoreFilterStatements,
23
- logSuggestionsAndReturn as singleStoreLogSuggestionsAndReturn,
24
- } from './singlestorePushUtils';
25
- import { logSuggestionsAndReturn as sqliteSuggestions } from './sqlitePushUtils';
26
-
27
- export const mysqlPush = async (
28
- schemaPath: string | string[],
29
- credentials: MysqlCredentials,
30
- tablesFilter: string[],
31
- strict: boolean,
32
- verbose: boolean,
33
- force: boolean,
34
- casing: CasingType | undefined,
35
- ) => {
36
- const { connectToMySQL } = await import('../connections');
37
- const { mysqlPushIntrospect } = await import('./mysqlIntrospect');
38
-
39
- const { db, database } = await connectToMySQL(credentials);
40
-
41
- const { schema } = await mysqlPushIntrospect(db, database, tablesFilter);
42
- const { prepareMySQLPush } = await import('./migrate');
43
-
44
- const statements = await prepareMySQLPush(schemaPath, schema, casing);
45
-
46
- const filteredStatements = mySqlFilterStatements(
47
- statements.statements ?? [],
48
- statements.validatedCur,
49
- statements.validatedPrev,
50
- );
51
-
52
- try {
53
- if (filteredStatements.length === 0) {
54
- render(`[${chalk.blue('i')}] No changes detected`);
55
- } else {
56
- const {
57
- shouldAskForApprove,
58
- statementsToExecute,
59
- columnsToRemove,
60
- tablesToRemove,
61
- tablesToTruncate,
62
- infoToPrint,
63
- } = await mySqlLogSuggestionsAndReturn(
64
- db,
65
- filteredStatements,
66
- statements.validatedCur,
67
- );
68
-
69
- const filteredSqlStatements = fromJson(filteredStatements, 'mysql');
70
-
71
- const uniqueSqlStatementsToExecute: string[] = [];
72
- statementsToExecute.forEach((ss) => {
73
- if (!uniqueSqlStatementsToExecute.includes(ss)) {
74
- uniqueSqlStatementsToExecute.push(ss);
75
- }
76
- });
77
- const uniqueFilteredSqlStatements: string[] = [];
78
- filteredSqlStatements.forEach((ss) => {
79
- if (!uniqueFilteredSqlStatements.includes(ss)) {
80
- uniqueFilteredSqlStatements.push(ss);
81
- }
82
- });
83
-
84
- if (verbose) {
85
- console.log();
86
- console.log(
87
- withStyle.warning('You are about to execute current statements:'),
88
- );
89
- console.log();
90
- console.log(
91
- [...uniqueSqlStatementsToExecute, ...uniqueFilteredSqlStatements]
92
- .map((s) => chalk.blue(s))
93
- .join('\n'),
94
- );
95
- console.log();
96
- }
97
-
98
- if (!force && strict) {
99
- if (!shouldAskForApprove) {
100
- const { status, data } = await render(
101
- new Select(['No, abort', `Yes, I want to execute all statements`]),
102
- );
103
- if (data?.index === 0) {
104
- render(`[${chalk.red('x')}] All changes were aborted`);
105
- process.exit(0);
106
- }
107
- }
108
- }
109
-
110
- if (!force && shouldAskForApprove) {
111
- console.log(withStyle.warning('Found data-loss statements:'));
112
- console.log(infoToPrint.join('\n'));
113
- console.log();
114
- console.log(
115
- chalk.red.bold(
116
- 'THIS ACTION WILL CAUSE DATA LOSS AND CANNOT BE REVERTED\n',
117
- ),
118
- );
119
-
120
- console.log(chalk.white('Do you still want to push changes?'));
121
-
122
- const { status, data } = await render(
123
- new Select([
124
- 'No, abort',
125
- `Yes, I want to${
126
- tablesToRemove.length > 0
127
- ? ` remove ${tablesToRemove.length} ${tablesToRemove.length > 1 ? 'tables' : 'table'},`
128
- : ' '
129
- }${
130
- columnsToRemove.length > 0
131
- ? ` remove ${columnsToRemove.length} ${columnsToRemove.length > 1 ? 'columns' : 'column'},`
132
- : ' '
133
- }${
134
- tablesToTruncate.length > 0
135
- ? ` truncate ${tablesToTruncate.length} ${tablesToTruncate.length > 1 ? 'tables' : 'table'}`
136
- : ''
137
- }`
138
- .replace(/(^,)|(,$)/g, '')
139
- .replace(/ +(?= )/g, ''),
140
- ]),
141
- );
142
- if (data?.index === 0) {
143
- render(`[${chalk.red('x')}] All changes were aborted`);
144
- process.exit(0);
145
- }
146
- }
147
-
148
- for (const dStmnt of uniqueSqlStatementsToExecute) {
149
- await db.query(dStmnt);
150
- }
151
-
152
- for (const statement of uniqueFilteredSqlStatements) {
153
- await db.query(statement);
154
- }
155
- if (filteredStatements.length > 0) {
156
- render(`[${chalk.green('✓')}] Changes applied`);
157
- } else {
158
- render(`[${chalk.blue('i')}] No changes detected`);
159
- }
160
- }
161
- } catch (e) {
162
- console.log(e);
163
- }
164
- };
165
-
166
- export const singlestorePush = async (
167
- schemaPath: string | string[],
168
- credentials: SingleStoreCredentials,
169
- tablesFilter: string[],
170
- strict: boolean,
171
- verbose: boolean,
172
- force: boolean,
173
- casing: CasingType | undefined,
174
- ) => {
175
- const { connectToSingleStore } = await import('../connections');
176
- const { singlestorePushIntrospect } = await import('./singlestoreIntrospect');
177
-
178
- const { db, database } = await connectToSingleStore(credentials);
179
-
180
- const { schema } = await singlestorePushIntrospect(
181
- db,
182
- database,
183
- tablesFilter,
184
- );
185
- const { prepareSingleStorePush } = await import('./migrate');
186
-
187
- const statements = await prepareSingleStorePush(schemaPath, schema, casing);
188
-
189
- const filteredStatements = singleStoreFilterStatements(
190
- statements.statements ?? [],
191
- statements.validatedCur,
192
- statements.validatedPrev,
193
- );
194
-
195
- try {
196
- if (filteredStatements.length === 0) {
197
- render(`[${chalk.blue('i')}] No changes detected`);
198
- } else {
199
- const {
200
- shouldAskForApprove,
201
- statementsToExecute,
202
- columnsToRemove,
203
- tablesToRemove,
204
- tablesToTruncate,
205
- infoToPrint,
206
- schemasToRemove,
207
- } = await singleStoreLogSuggestionsAndReturn(
208
- db,
209
- filteredStatements,
210
- statements.validatedCur,
211
- statements.validatedPrev,
212
- );
213
-
214
- if (verbose) {
215
- console.log();
216
- console.log(
217
- withStyle.warning('You are about to execute current statements:'),
218
- );
219
- console.log();
220
- console.log(statementsToExecute.map((s) => chalk.blue(s)).join('\n'));
221
- console.log();
222
- }
223
-
224
- if (!force && strict) {
225
- if (!shouldAskForApprove) {
226
- const { status, data } = await render(
227
- new Select(['No, abort', `Yes, I want to execute all statements`]),
228
- );
229
- if (data?.index === 0) {
230
- render(`[${chalk.red('x')}] All changes were aborted`);
231
- process.exit(0);
232
- }
233
- }
234
- }
235
-
236
- if (!force && shouldAskForApprove) {
237
- console.log(withStyle.warning('Found data-loss statements:'));
238
- console.log(infoToPrint.join('\n'));
239
- console.log();
240
- console.log(
241
- chalk.red.bold(
242
- 'THIS ACTION WILL CAUSE DATA LOSS AND CANNOT BE REVERTED\n',
243
- ),
244
- );
245
-
246
- console.log(chalk.white('Do you still want to push changes?'));
247
-
248
- const { status, data } = await render(
249
- new Select([
250
- 'No, abort',
251
- `Yes, I want to${
252
- tablesToRemove.length > 0
253
- ? ` remove ${tablesToRemove.length} ${tablesToRemove.length > 1 ? 'tables' : 'table'},`
254
- : ' '
255
- }${
256
- columnsToRemove.length > 0
257
- ? ` remove ${columnsToRemove.length} ${columnsToRemove.length > 1 ? 'columns' : 'column'},`
258
- : ' '
259
- }${
260
- tablesToTruncate.length > 0
261
- ? ` truncate ${tablesToTruncate.length} ${tablesToTruncate.length > 1 ? 'tables' : 'table'}`
262
- : ''
263
- }`
264
- .replace(/(^,)|(,$)/g, '')
265
- .replace(/ +(?= )/g, ''),
266
- ]),
267
- );
268
- if (data?.index === 0) {
269
- render(`[${chalk.red('x')}] All changes were aborted`);
270
- process.exit(0);
271
- }
272
- }
273
-
274
- for (const dStmnt of statementsToExecute) {
275
- await db.query(dStmnt);
276
- }
277
-
278
- if (filteredStatements.length > 0) {
279
- render(`[${chalk.green('✓')}] Changes applied`);
280
- } else {
281
- render(`[${chalk.blue('i')}] No changes detected`);
282
- }
283
- }
284
- } catch (e) {
285
- console.log(e);
286
- }
287
- };
288
-
289
- export const pgPush = async (
290
- schemaPath: string | string[],
291
- verbose: boolean,
292
- strict: boolean,
293
- credentials: PostgresCredentials,
294
- tablesFilter: string[],
295
- schemasFilter: string[],
296
- entities: Entities,
297
- force: boolean,
298
- casing: CasingType | undefined,
299
- ) => {
300
- const { preparePostgresDB } = await import('../connections');
301
- const { pgPushIntrospect } = await import('./pgIntrospect');
302
-
303
- const db = await preparePostgresDB(credentials);
304
- const serialized = await serializePg(schemaPath, casing, schemasFilter);
305
-
306
- const { schema } = await pgPushIntrospect(db, tablesFilter, schemasFilter, entities, serialized);
307
-
308
- const { preparePgPush } = await import('./migrate');
309
-
310
- const statements = await preparePgPush(
311
- { id: randomUUID(), prevId: schema.id, ...serialized },
312
- schema,
313
- );
314
-
315
- try {
316
- if (statements.sqlStatements.length === 0) {
317
- render(`[${chalk.blue('i')}] No changes detected`);
318
- } else {
319
- // const filteredStatements = filterStatements(statements.statements);
320
- const {
321
- shouldAskForApprove,
322
- statementsToExecute,
323
- columnsToRemove,
324
- tablesToRemove,
325
- matViewsToRemove,
326
- tablesToTruncate,
327
- infoToPrint,
328
- schemasToRemove,
329
- } = await pgSuggestions(db, statements.statements);
330
-
331
- if (verbose) {
332
- console.log();
333
- // console.log(chalk.gray('Verbose logs:'));
334
- console.log(
335
- withStyle.warning('You are about to execute current statements:'),
336
- );
337
- console.log();
338
- console.log(statementsToExecute.map((s) => chalk.blue(s)).join('\n'));
339
- console.log();
340
- }
341
-
342
- if (!force && strict) {
343
- if (!shouldAskForApprove) {
344
- const { status, data } = await render(
345
- new Select(['No, abort', `Yes, I want to execute all statements`]),
346
- );
347
- if (data?.index === 0) {
348
- render(`[${chalk.red('x')}] All changes were aborted`);
349
- process.exit(0);
350
- }
351
- }
352
- }
353
-
354
- if (!force && shouldAskForApprove) {
355
- console.log(withStyle.warning('Found data-loss statements:'));
356
- console.log(infoToPrint.join('\n'));
357
- console.log();
358
- console.log(
359
- chalk.red.bold(
360
- 'THIS ACTION WILL CAUSE DATA LOSS AND CANNOT BE REVERTED\n',
361
- ),
362
- );
363
-
364
- console.log(chalk.white('Do you still want to push changes?'));
365
-
366
- const { status, data } = await render(
367
- new Select([
368
- 'No, abort',
369
- `Yes, I want to${
370
- tablesToRemove.length > 0
371
- ? ` remove ${tablesToRemove.length} ${tablesToRemove.length > 1 ? 'tables' : 'table'},`
372
- : ' '
373
- }${
374
- columnsToRemove.length > 0
375
- ? ` remove ${columnsToRemove.length} ${columnsToRemove.length > 1 ? 'columns' : 'column'},`
376
- : ' '
377
- }${
378
- tablesToTruncate.length > 0
379
- ? ` truncate ${tablesToTruncate.length} ${tablesToTruncate.length > 1 ? 'tables' : 'table'}`
380
- : ''
381
- }${
382
- matViewsToRemove.length > 0
383
- ? ` remove ${matViewsToRemove.length} ${
384
- matViewsToRemove.length > 1 ? 'materialized views' : 'materialize view'
385
- },`
386
- : ' '
387
- }`
388
- .replace(/(^,)|(,$)/g, '')
389
- .replace(/ +(?= )/g, ''),
390
- ]),
391
- );
392
- if (data?.index === 0) {
393
- render(`[${chalk.red('x')}] All changes were aborted`);
394
- process.exit(0);
395
- }
396
- }
397
-
398
- for (const dStmnt of statementsToExecute) {
399
- await db.query(dStmnt);
400
- }
401
-
402
- if (statements.statements.length > 0) {
403
- render(`[${chalk.green('✓')}] Changes applied`);
404
- } else {
405
- render(`[${chalk.blue('i')}] No changes detected`);
406
- }
407
- }
408
- } catch (e) {
409
- console.error(e);
410
- }
411
- };
412
-
413
- export const sqlitePush = async (
414
- schemaPath: string | string[],
415
- verbose: boolean,
416
- strict: boolean,
417
- credentials: SqliteCredentials,
418
- tablesFilter: string[],
419
- force: boolean,
420
- casing: CasingType | undefined,
421
- ) => {
422
- const { connectToSQLite } = await import('../connections');
423
- const { sqlitePushIntrospect } = await import('./sqliteIntrospect');
424
-
425
- const db = await connectToSQLite(credentials);
426
- const { schema } = await sqlitePushIntrospect(db, tablesFilter);
427
- const { prepareSQLitePush } = await import('./migrate');
428
-
429
- const statements = await prepareSQLitePush(schemaPath, schema, casing);
430
-
431
- if (statements.sqlStatements.length === 0) {
432
- render(`\n[${chalk.blue('i')}] No changes detected`);
433
- } else {
434
- const {
435
- shouldAskForApprove,
436
- statementsToExecute,
437
- columnsToRemove,
438
- tablesToRemove,
439
- tablesToTruncate,
440
- infoToPrint,
441
- schemasToRemove,
442
- } = await sqliteSuggestions(
443
- db,
444
- statements.statements,
445
- statements.squashedPrev,
446
- statements.squashedCur,
447
- statements.meta!,
448
- );
449
-
450
- if (verbose && statementsToExecute.length > 0) {
451
- console.log();
452
- console.log(
453
- withStyle.warning('You are about to execute current statements:'),
454
- );
455
- console.log();
456
- console.log(statementsToExecute.map((s) => chalk.blue(s)).join('\n'));
457
- console.log();
458
- }
459
-
460
- if (!force && strict) {
461
- if (!shouldAskForApprove) {
462
- const { status, data } = await render(
463
- new Select(['No, abort', `Yes, I want to execute all statements`]),
464
- );
465
- if (data?.index === 0) {
466
- render(`[${chalk.red('x')}] All changes were aborted`);
467
- process.exit(0);
468
- }
469
- }
470
- }
471
-
472
- if (!force && shouldAskForApprove) {
473
- console.log(withStyle.warning('Found data-loss statements:'));
474
- console.log(infoToPrint.join('\n'));
475
- console.log();
476
- console.log(
477
- chalk.red.bold(
478
- 'THIS ACTION WILL CAUSE DATA LOSS AND CANNOT BE REVERTED\n',
479
- ),
480
- );
481
-
482
- console.log(chalk.white('Do you still want to push changes?'));
483
-
484
- const { status, data } = await render(
485
- new Select([
486
- 'No, abort',
487
- `Yes, I want to${
488
- tablesToRemove.length > 0
489
- ? ` remove ${tablesToRemove.length} ${tablesToRemove.length > 1 ? 'tables' : 'table'},`
490
- : ' '
491
- }${
492
- columnsToRemove.length > 0
493
- ? ` remove ${columnsToRemove.length} ${columnsToRemove.length > 1 ? 'columns' : 'column'},`
494
- : ' '
495
- }${
496
- tablesToTruncate.length > 0
497
- ? ` truncate ${tablesToTruncate.length} ${tablesToTruncate.length > 1 ? 'tables' : 'table'}`
498
- : ''
499
- }`
500
- .trimEnd()
501
- .replace(/(^,)|(,$)/g, '')
502
- .replace(/ +(?= )/g, ''),
503
- ]),
504
- );
505
- if (data?.index === 0) {
506
- render(`[${chalk.red('x')}] All changes were aborted`);
507
- process.exit(0);
508
- }
509
- }
510
-
511
- if (statementsToExecute.length === 0) {
512
- render(`\n[${chalk.blue('i')}] No changes detected`);
513
- } else {
514
- if (!('driver' in credentials)) {
515
- await db.run('begin');
516
- try {
517
- for (const dStmnt of statementsToExecute) {
518
- await db.run(dStmnt);
519
- }
520
- await db.run('commit');
521
- } catch (e) {
522
- console.error(e);
523
- await db.run('rollback');
524
- process.exit(1);
525
- }
526
- }
527
- render(`[${chalk.green('✓')}] Changes applied`);
528
- }
529
- }
530
- };
531
-
532
- export const libSQLPush = async (
533
- schemaPath: string | string[],
534
- verbose: boolean,
535
- strict: boolean,
536
- credentials: LibSQLCredentials,
537
- tablesFilter: string[],
538
- force: boolean,
539
- casing: CasingType | undefined,
540
- ) => {
541
- const { connectToLibSQL } = await import('../connections');
542
- const { sqlitePushIntrospect } = await import('./sqliteIntrospect');
543
-
544
- const db = await connectToLibSQL(credentials);
545
- const { schema } = await sqlitePushIntrospect(db, tablesFilter);
546
-
547
- const { prepareLibSQLPush } = await import('./migrate');
548
-
549
- const statements = await prepareLibSQLPush(schemaPath, schema, casing);
550
-
551
- if (statements.sqlStatements.length === 0) {
552
- render(`\n[${chalk.blue('i')}] No changes detected`);
553
- } else {
554
- const {
555
- shouldAskForApprove,
556
- statementsToExecute,
557
- columnsToRemove,
558
- tablesToRemove,
559
- tablesToTruncate,
560
- infoToPrint,
561
- } = await libSqlLogSuggestionsAndReturn(
562
- db,
563
- statements.statements,
564
- statements.squashedPrev,
565
- statements.squashedCur,
566
- statements.meta!,
567
- );
568
-
569
- if (verbose && statementsToExecute.length > 0) {
570
- console.log();
571
- console.log(
572
- withStyle.warning('You are about to execute current statements:'),
573
- );
574
- console.log();
575
- console.log(statementsToExecute.map((s) => chalk.blue(s)).join('\n'));
576
- console.log();
577
- }
578
-
579
- if (!force && strict) {
580
- if (!shouldAskForApprove) {
581
- const { status, data } = await render(
582
- new Select(['No, abort', `Yes, I want to execute all statements`]),
583
- );
584
- if (data?.index === 0) {
585
- render(`[${chalk.red('x')}] All changes were aborted`);
586
- process.exit(0);
587
- }
588
- }
589
- }
590
-
591
- if (!force && shouldAskForApprove) {
592
- console.log(withStyle.warning('Found data-loss statements:'));
593
- console.log(infoToPrint.join('\n'));
594
- console.log();
595
- console.log(
596
- chalk.red.bold(
597
- 'THIS ACTION WILL CAUSE DATA LOSS AND CANNOT BE REVERTED\n',
598
- ),
599
- );
600
-
601
- console.log(chalk.white('Do you still want to push changes?'));
602
-
603
- const { status, data } = await render(
604
- new Select([
605
- 'No, abort',
606
- `Yes, I want to${
607
- tablesToRemove.length > 0
608
- ? ` remove ${tablesToRemove.length} ${tablesToRemove.length > 1 ? 'tables' : 'table'},`
609
- : ' '
610
- }${
611
- columnsToRemove.length > 0
612
- ? ` remove ${columnsToRemove.length} ${columnsToRemove.length > 1 ? 'columns' : 'column'},`
613
- : ' '
614
- }${
615
- tablesToTruncate.length > 0
616
- ? ` truncate ${tablesToTruncate.length} ${tablesToTruncate.length > 1 ? 'tables' : 'table'}`
617
- : ''
618
- }`
619
- .trimEnd()
620
- .replace(/(^,)|(,$)/g, '')
621
- .replace(/ +(?= )/g, ''),
622
- ]),
623
- );
624
- if (data?.index === 0) {
625
- render(`[${chalk.red('x')}] All changes were aborted`);
626
- process.exit(0);
627
- }
628
- }
629
-
630
- if (statementsToExecute.length === 0) {
631
- render(`\n[${chalk.blue('i')}] No changes detected`);
632
- } else {
633
- await db.batchWithPragma!(statementsToExecute);
634
- render(`[${chalk.green('✓')}] Changes applied`);
635
- }
636
- }
637
- };
@@ -1,53 +0,0 @@
1
- import { renderWithTask } from 'hanji';
2
- import { Minimatch } from 'minimatch';
3
- import { originUUID } from '../../global';
4
- import type { SingleStoreSchema } from '../../serializer/singlestoreSchema';
5
- import { fromDatabase } from '../../serializer/singlestoreSerializer';
6
- import type { DB } from '../../utils';
7
- import { ProgressView } from '../views';
8
-
9
- export const singlestorePushIntrospect = async (
10
- db: DB,
11
- databaseName: string,
12
- filters: string[],
13
- ) => {
14
- const matchers = filters.map((it) => {
15
- return new Minimatch(it);
16
- });
17
-
18
- const filter = (tableName: string) => {
19
- if (matchers.length === 0) return true;
20
-
21
- let flags: boolean[] = [];
22
-
23
- for (let matcher of matchers) {
24
- if (matcher.negate) {
25
- if (!matcher.match(tableName)) {
26
- flags.push(false);
27
- }
28
- }
29
-
30
- if (matcher.match(tableName)) {
31
- flags.push(true);
32
- }
33
- }
34
-
35
- if (flags.length > 0) {
36
- return flags.every(Boolean);
37
- }
38
- return false;
39
- };
40
-
41
- const progress = new ProgressView(
42
- 'Pulling schema from database...',
43
- 'Pulling schema from database...',
44
- );
45
- const res = await renderWithTask(
46
- progress,
47
- fromDatabase(db, databaseName, filter),
48
- );
49
-
50
- const schema = { id: originUUID, prevId: '', ...res } as SingleStoreSchema;
51
- const { internal, ...schemaWithoutInternals } = schema;
52
- return { schema: schemaWithoutInternals };
53
- };