@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,658 @@
1
+ import chalk from 'chalk';
2
+ import { Prompt, render, SelectState, TaskView } from 'hanji';
3
+ import type { CommonSchema } from '../schemaValidator';
4
+ import { objectValues } from '../utils';
5
+ import type { Named, NamedWithSchema } from './commands/migrate';
6
+
7
+ export const warning = (msg: string) => {
8
+ render(`[${chalk.yellow('Warning')}] ${msg}`);
9
+ };
10
+ export const err = (msg: string) => {
11
+ render(`${chalk.bold.red('Error')} ${msg}`);
12
+ };
13
+
14
+ export const info = (msg: string, greyMsg: string = ''): string => {
15
+ return `${chalk.blue.bold('Info:')} ${msg} ${greyMsg ? chalk.grey(greyMsg) : ''}`.trim();
16
+ };
17
+ export const grey = (msg: string): string => {
18
+ return chalk.grey(msg);
19
+ };
20
+
21
+ export const error = (error: string, greyMsg: string = ''): string => {
22
+ return `${chalk.bgRed.bold(' Error ')} ${error} ${greyMsg ? chalk.grey(greyMsg) : ''}`.trim();
23
+ };
24
+
25
+ export const schema = (schema: CommonSchema): string => {
26
+ type TableEntry = (typeof schema)['tables'][keyof (typeof schema)['tables']];
27
+ const tables = Object.values(schema.tables) as unknown as TableEntry[];
28
+
29
+ let msg = chalk.bold(`${tables.length} tables\n`);
30
+
31
+ msg += tables
32
+ .map((t) => {
33
+ const columnsCount = Object.values(t.columns).length;
34
+ const indexesCount = Object.values(t.indexes).length;
35
+ let foreignKeys: number = 0;
36
+ // Singlestore doesn't have foreign keys
37
+ if (schema.dialect !== 'singlestore') {
38
+ // @ts-expect-error
39
+ foreignKeys = Object.values(t.foreignKeys).length;
40
+ }
41
+
42
+ return `${chalk.bold.blue(t.name)} ${
43
+ chalk.gray(
44
+ `${columnsCount} columns ${indexesCount} indexes ${foreignKeys} fks`,
45
+ )
46
+ }`;
47
+ })
48
+ .join('\n');
49
+
50
+ msg += '\n';
51
+
52
+ const enums = objectValues(
53
+ 'enums' in schema
54
+ ? 'values' in schema['enums']
55
+ ? schema['enums']
56
+ : {}
57
+ : {},
58
+ );
59
+
60
+ if (enums.length > 0) {
61
+ msg += '\n';
62
+ msg += chalk.bold(`${enums.length} enums\n`);
63
+
64
+ msg += enums
65
+ .map((it) => {
66
+ return `${chalk.bold.blue(it.name)} ${
67
+ chalk.gray(
68
+ `[${Object.values(it.values).join(', ')}]`,
69
+ )
70
+ }`;
71
+ })
72
+ .join('\n');
73
+ msg += '\n';
74
+ }
75
+ return msg;
76
+ };
77
+
78
+ export interface RenamePropmtItem<T> {
79
+ from: T;
80
+ to: T;
81
+ }
82
+
83
+ export const isRenamePromptItem = <T extends Named>(
84
+ item: RenamePropmtItem<T> | T,
85
+ ): item is RenamePropmtItem<T> => {
86
+ return 'from' in item && 'to' in item;
87
+ };
88
+
89
+ export class ResolveColumnSelect<T extends Named> extends Prompt<
90
+ RenamePropmtItem<T> | T
91
+ > {
92
+ private readonly data: SelectState<RenamePropmtItem<T> | T>;
93
+
94
+ constructor(
95
+ private readonly tableName: string,
96
+ private readonly base: Named,
97
+ data: (RenamePropmtItem<T> | T)[],
98
+ ) {
99
+ super();
100
+ this.on('attach', (terminal) => terminal.toggleCursor('hide'));
101
+ this.data = new SelectState(data);
102
+ this.data.bind(this);
103
+ }
104
+
105
+ render(status: 'idle' | 'submitted' | 'aborted'): string {
106
+ if (status === 'submitted' || status === 'aborted') {
107
+ return '\n';
108
+ }
109
+
110
+ let text = `\nIs ${
111
+ chalk.bold.blue(
112
+ this.base.name,
113
+ )
114
+ } column in ${
115
+ chalk.bold.blue(
116
+ this.tableName,
117
+ )
118
+ } table created or renamed from another column?\n`;
119
+
120
+ const isSelectedRenamed = isRenamePromptItem(
121
+ this.data.items[this.data.selectedIdx],
122
+ );
123
+
124
+ const selectedPrefix = isSelectedRenamed
125
+ ? chalk.yellow('❯ ')
126
+ : chalk.green('❯ ');
127
+
128
+ const labelLength: number = this.data.items
129
+ .filter((it) => isRenamePromptItem(it))
130
+ .map((it: RenamePropmtItem<T>) => {
131
+ return this.base.name.length + 3 + it['from'].name.length;
132
+ })
133
+ .reduce((a, b) => {
134
+ if (a > b) {
135
+ return a;
136
+ }
137
+ return b;
138
+ }, 0);
139
+
140
+ this.data.items.forEach((it, idx) => {
141
+ const isSelected = idx === this.data.selectedIdx;
142
+ const isRenamed = isRenamePromptItem(it);
143
+ const title = isRenamed
144
+ ? `${it.from.name} › ${it.to.name}`.padEnd(labelLength, ' ')
145
+ : it.name.padEnd(labelLength, ' ');
146
+ const label = isRenamed
147
+ ? `${chalk.yellow('~')} ${title} ${chalk.gray('rename column')}`
148
+ : `${chalk.green('+')} ${title} ${chalk.gray('create column')}`;
149
+
150
+ text += isSelected ? `${selectedPrefix}${label}` : ` ${label}`;
151
+ text += idx != this.data.items.length - 1 ? '\n' : '';
152
+ });
153
+ return text;
154
+ }
155
+
156
+ result(): RenamePropmtItem<T> | T {
157
+ return this.data.items[this.data.selectedIdx]!;
158
+ }
159
+ }
160
+
161
+ export const tableKey = (it: NamedWithSchema) => {
162
+ return it.schema === 'public' || !it.schema
163
+ ? it.name
164
+ : `${it.schema}.${it.name}`;
165
+ };
166
+
167
+ export class ResolveSelectNamed<T extends Named> extends Prompt<
168
+ RenamePropmtItem<T> | T
169
+ > {
170
+ private readonly state: SelectState<RenamePropmtItem<T> | T>;
171
+
172
+ constructor(
173
+ private readonly base: T,
174
+ data: (RenamePropmtItem<T> | T)[],
175
+ private readonly entityType: 'role' | 'policy',
176
+ ) {
177
+ super();
178
+ this.on('attach', (terminal) => terminal.toggleCursor('hide'));
179
+ this.state = new SelectState(data);
180
+ this.state.bind(this);
181
+ this.base = base;
182
+ }
183
+
184
+ render(status: 'idle' | 'submitted' | 'aborted'): string {
185
+ if (status === 'submitted' || status === 'aborted') {
186
+ return '';
187
+ }
188
+ const key = this.base.name;
189
+
190
+ let text = `\nIs ${chalk.bold.blue(key)} ${this.entityType} created or renamed from another ${this.entityType}?\n`;
191
+
192
+ const isSelectedRenamed = isRenamePromptItem(
193
+ this.state.items[this.state.selectedIdx],
194
+ );
195
+
196
+ const selectedPrefix = isSelectedRenamed
197
+ ? chalk.yellow('❯ ')
198
+ : chalk.green('❯ ');
199
+
200
+ const labelLength: number = this.state.items
201
+ .filter((it) => isRenamePromptItem(it))
202
+ .map((_) => {
203
+ const it = _ as RenamePropmtItem<T>;
204
+ const keyFrom = it.from.name;
205
+ return key.length + 3 + keyFrom.length;
206
+ })
207
+ .reduce((a, b) => {
208
+ if (a > b) {
209
+ return a;
210
+ }
211
+ return b;
212
+ }, 0);
213
+
214
+ const entityType = this.entityType;
215
+ this.state.items.forEach((it, idx) => {
216
+ const isSelected = idx === this.state.selectedIdx;
217
+ const isRenamed = isRenamePromptItem(it);
218
+
219
+ const title = isRenamed
220
+ ? `${it.from.name} › ${it.to.name}`.padEnd(labelLength, ' ')
221
+ : it.name.padEnd(labelLength, ' ');
222
+
223
+ const label = isRenamed
224
+ ? `${chalk.yellow('~')} ${title} ${chalk.gray(`rename ${entityType}`)}`
225
+ : `${chalk.green('+')} ${title} ${chalk.gray(`create ${entityType}`)}`;
226
+
227
+ text += isSelected ? `${selectedPrefix}${label}` : ` ${label}`;
228
+ text += idx != this.state.items.length - 1 ? '\n' : '';
229
+ });
230
+ return text;
231
+ }
232
+
233
+ result(): RenamePropmtItem<T> | T {
234
+ return this.state.items[this.state.selectedIdx]!;
235
+ }
236
+ }
237
+
238
+ export class ResolveSelect<T extends NamedWithSchema> extends Prompt<
239
+ RenamePropmtItem<T> | T
240
+ > {
241
+ private readonly state: SelectState<RenamePropmtItem<T> | T>;
242
+
243
+ constructor(
244
+ private readonly base: T,
245
+ data: (RenamePropmtItem<T> | T)[],
246
+ private readonly entityType: 'table' | 'domain' | 'enum' | 'sequence' | 'view' | 'role',
247
+ ) {
248
+ super();
249
+ this.on('attach', (terminal) => terminal.toggleCursor('hide'));
250
+ this.state = new SelectState(data);
251
+ this.state.bind(this);
252
+ this.base = base;
253
+ }
254
+
255
+ render(status: 'idle' | 'submitted' | 'aborted'): string {
256
+ if (status === 'submitted' || status === 'aborted') {
257
+ return '';
258
+ }
259
+ const key = tableKey(this.base);
260
+
261
+ let text = `\nIs ${chalk.bold.blue(key)} ${this.entityType} created or renamed from another ${this.entityType}?\n`;
262
+
263
+ const isSelectedRenamed = isRenamePromptItem(
264
+ this.state.items[this.state.selectedIdx],
265
+ );
266
+
267
+ const selectedPrefix = isSelectedRenamed
268
+ ? chalk.yellow('❯ ')
269
+ : chalk.green('❯ ');
270
+
271
+ const labelLength: number = this.state.items
272
+ .filter((it) => isRenamePromptItem(it))
273
+ .map((_) => {
274
+ const it = _ as RenamePropmtItem<T>;
275
+ const keyFrom = tableKey(it.from);
276
+ return key.length + 3 + keyFrom.length;
277
+ })
278
+ .reduce((a, b) => {
279
+ if (a > b) {
280
+ return a;
281
+ }
282
+ return b;
283
+ }, 0);
284
+
285
+ const entityType = this.entityType;
286
+ this.state.items.forEach((it, idx) => {
287
+ const isSelected = idx === this.state.selectedIdx;
288
+ const isRenamed = isRenamePromptItem(it);
289
+
290
+ const title = isRenamed
291
+ ? `${tableKey(it.from)} › ${tableKey(it.to)}`.padEnd(labelLength, ' ')
292
+ : tableKey(it).padEnd(labelLength, ' ');
293
+
294
+ const label = isRenamed
295
+ ? `${chalk.yellow('~')} ${title} ${chalk.gray(`rename ${entityType}`)}`
296
+ : `${chalk.green('+')} ${title} ${chalk.gray(`create ${entityType}`)}`;
297
+
298
+ text += isSelected ? `${selectedPrefix}${label}` : ` ${label}`;
299
+ text += idx != this.state.items.length - 1 ? '\n' : '';
300
+ });
301
+ return text;
302
+ }
303
+
304
+ result(): RenamePropmtItem<T> | T {
305
+ return this.state.items[this.state.selectedIdx]!;
306
+ }
307
+ }
308
+
309
+ export class ResolveSchemasSelect<T extends Named> extends Prompt<
310
+ RenamePropmtItem<T> | T
311
+ > {
312
+ private readonly state: SelectState<RenamePropmtItem<T> | T>;
313
+
314
+ constructor(private readonly base: Named, data: (RenamePropmtItem<T> | T)[]) {
315
+ super();
316
+ this.on('attach', (terminal) => terminal.toggleCursor('hide'));
317
+ this.state = new SelectState(data);
318
+ this.state.bind(this);
319
+ this.base = base;
320
+ }
321
+
322
+ render(status: 'idle' | 'submitted' | 'aborted'): string {
323
+ if (status === 'submitted' || status === 'aborted') {
324
+ return '';
325
+ }
326
+
327
+ let text = `\nIs ${
328
+ chalk.bold.blue(
329
+ this.base.name,
330
+ )
331
+ } schema created or renamed from another schema?\n`;
332
+ const isSelectedRenamed = isRenamePromptItem(
333
+ this.state.items[this.state.selectedIdx],
334
+ );
335
+ const selectedPrefix = isSelectedRenamed
336
+ ? chalk.yellow('❯ ')
337
+ : chalk.green('❯ ');
338
+
339
+ const labelLength: number = this.state.items
340
+ .filter((it) => isRenamePromptItem(it))
341
+ .map((it: RenamePropmtItem<T>) => {
342
+ return this.base.name.length + 3 + it['from'].name.length;
343
+ })
344
+ .reduce((a, b) => {
345
+ if (a > b) {
346
+ return a;
347
+ }
348
+ return b;
349
+ }, 0);
350
+
351
+ this.state.items.forEach((it, idx) => {
352
+ const isSelected = idx === this.state.selectedIdx;
353
+ const isRenamed = isRenamePromptItem(it);
354
+ const title = isRenamed
355
+ ? `${it.from.name} › ${it.to.name}`.padEnd(labelLength, ' ')
356
+ : it.name.padEnd(labelLength, ' ');
357
+ const label = isRenamed
358
+ ? `${chalk.yellow('~')} ${title} ${chalk.gray('rename schema')}`
359
+ : `${chalk.green('+')} ${title} ${chalk.gray('create schema')}`;
360
+
361
+ text += isSelected ? `${selectedPrefix}${label}` : ` ${label}`;
362
+ text += idx != this.state.items.length - 1 ? '\n' : '';
363
+ });
364
+ return text;
365
+ }
366
+
367
+ result(): RenamePropmtItem<T> | T {
368
+ return this.state.items[this.state.selectedIdx]!;
369
+ }
370
+ }
371
+
372
+ class Spinner {
373
+ private offset: number = 0;
374
+ private readonly iterator: () => void;
375
+
376
+ constructor(private readonly frames: string[]) {
377
+ this.iterator = () => {
378
+ this.offset += 1;
379
+ this.offset %= frames.length - 1;
380
+ };
381
+ }
382
+
383
+ public tick = () => {
384
+ this.iterator();
385
+ };
386
+
387
+ public value = () => {
388
+ return this.frames[this.offset];
389
+ };
390
+ }
391
+
392
+ const frames = function(values: string[]): () => string {
393
+ let index = 0;
394
+ const iterator = () => {
395
+ const frame = values[index];
396
+ index += 1;
397
+ index %= values.length;
398
+ return frame!;
399
+ };
400
+ return iterator;
401
+ };
402
+
403
+ type ValueOf<T> = T[keyof T];
404
+ export type IntrospectStatus = 'fetching' | 'done';
405
+ export type IntrospectStage =
406
+ | 'tables'
407
+ | 'columns'
408
+ | 'enums'
409
+ | 'indexes'
410
+ | 'policies'
411
+ | 'checks'
412
+ | 'fks'
413
+ | 'views';
414
+
415
+ type IntrospectState = {
416
+ [key in IntrospectStage]: {
417
+ count: number;
418
+ name: string;
419
+ status: IntrospectStatus;
420
+ };
421
+ };
422
+
423
+ export class IntrospectProgress extends TaskView {
424
+ private readonly spinner: Spinner = new Spinner('⣷⣯⣟⡿⢿⣻⣽⣾'.split(''));
425
+ private timeout: NodeJS.Timeout | undefined;
426
+
427
+ private state: IntrospectState = {
428
+ tables: {
429
+ count: 0,
430
+ name: 'tables',
431
+ status: 'fetching',
432
+ },
433
+ columns: {
434
+ count: 0,
435
+ name: 'columns',
436
+ status: 'fetching',
437
+ },
438
+ enums: {
439
+ count: 0,
440
+ name: 'enums',
441
+ status: 'fetching',
442
+ },
443
+ indexes: {
444
+ count: 0,
445
+ name: 'indexes',
446
+ status: 'fetching',
447
+ },
448
+ fks: {
449
+ count: 0,
450
+ name: 'foreign keys',
451
+ status: 'fetching',
452
+ },
453
+ policies: {
454
+ count: 0,
455
+ name: 'policies',
456
+ status: 'fetching',
457
+ },
458
+ checks: {
459
+ count: 0,
460
+ name: 'check constraints',
461
+ status: 'fetching',
462
+ },
463
+ views: {
464
+ count: 0,
465
+ name: 'views',
466
+ status: 'fetching',
467
+ },
468
+ };
469
+
470
+ constructor(private readonly hasEnums: boolean = false) {
471
+ super();
472
+ this.timeout = setInterval(() => {
473
+ this.spinner.tick();
474
+ this.requestLayout();
475
+ }, 128);
476
+
477
+ this.on('detach', () => clearInterval(this.timeout));
478
+ }
479
+
480
+ public update(
481
+ stage: IntrospectStage,
482
+ count: number,
483
+ status: IntrospectStatus,
484
+ ) {
485
+ this.state[stage].count = count;
486
+ this.state[stage].status = status;
487
+ this.requestLayout();
488
+ }
489
+
490
+ private formatCount = (count: number) => {
491
+ const width: number = Math.max.apply(
492
+ null,
493
+ Object.values(this.state).map((it) => it.count.toFixed(0).length),
494
+ );
495
+
496
+ return count.toFixed(0).padEnd(width, ' ');
497
+ };
498
+
499
+ private statusText = (spinner: string, stage: ValueOf<IntrospectState>) => {
500
+ const { name, count } = stage;
501
+ const isDone = stage.status === 'done';
502
+
503
+ const prefix = isDone ? `[${chalk.green('✓')}]` : `[${spinner}]`;
504
+
505
+ const formattedCount = this.formatCount(count);
506
+ const suffix = isDone
507
+ ? `${formattedCount} ${name} fetched`
508
+ : `${formattedCount} ${name} fetching`;
509
+
510
+ return `${prefix} ${suffix}\n`;
511
+ };
512
+
513
+ render(): string {
514
+ let info = '';
515
+ const spin = this.spinner.value();
516
+ info += this.statusText(spin, this.state.tables);
517
+ info += this.statusText(spin, this.state.columns);
518
+ info += this.hasEnums ? this.statusText(spin, this.state.enums) : '';
519
+ info += this.statusText(spin, this.state.indexes);
520
+ info += this.statusText(spin, this.state.fks);
521
+ info += this.statusText(spin, this.state.policies);
522
+ info += this.statusText(spin, this.state.checks);
523
+ info += this.statusText(spin, this.state.views);
524
+
525
+ return info;
526
+ }
527
+ }
528
+
529
+ export class MigrateProgress extends TaskView {
530
+ private readonly spinner: Spinner = new Spinner('⣷⣯⣟⡿⢿⣻⣽⣾'.split(''));
531
+ private timeout: NodeJS.Timeout | undefined;
532
+
533
+ constructor() {
534
+ super();
535
+ this.timeout = setInterval(() => {
536
+ this.spinner.tick();
537
+ this.requestLayout();
538
+ }, 128);
539
+
540
+ this.on('detach', () => clearInterval(this.timeout));
541
+ }
542
+
543
+ render(status: 'pending' | 'done'): string {
544
+ if (status === 'pending') {
545
+ const spin = this.spinner.value();
546
+ return `[${spin}] applying migrations...`;
547
+ }
548
+ return `[${chalk.green('✓')}] migrations applied successfully!`;
549
+ }
550
+ }
551
+
552
+ export class ProgressView extends TaskView {
553
+ private readonly spinner: Spinner = new Spinner('⣷⣯⣟⡿⢿⣻⣽⣾'.split(''));
554
+ private timeout: NodeJS.Timeout | undefined;
555
+
556
+ constructor(
557
+ private readonly progressText: string,
558
+ private readonly successText: string,
559
+ ) {
560
+ super();
561
+ this.timeout = setInterval(() => {
562
+ this.spinner.tick();
563
+ this.requestLayout();
564
+ }, 128);
565
+
566
+ this.on('detach', () => clearInterval(this.timeout));
567
+ }
568
+
569
+ render(status: 'pending' | 'done'): string {
570
+ if (status === 'pending') {
571
+ const spin = this.spinner.value();
572
+ return `[${spin}] ${this.progressText}\n`;
573
+ }
574
+ return `[${chalk.green('✓')}] ${this.successText}\n`;
575
+ }
576
+ }
577
+
578
+ export class DropMigrationView<T extends { tag: string }> extends Prompt<T> {
579
+ private readonly data: SelectState<T>;
580
+
581
+ constructor(data: T[]) {
582
+ super();
583
+ this.on('attach', (terminal) => terminal.toggleCursor('hide'));
584
+ this.data = new SelectState(data);
585
+ this.data.selectedIdx = data.length - 1;
586
+ this.data.bind(this);
587
+ }
588
+
589
+ render(status: 'idle' | 'submitted' | 'aborted'): string {
590
+ if (status === 'submitted' || status === 'aborted') {
591
+ return '\n';
592
+ }
593
+
594
+ let text = chalk.bold('Please select migration to drop:\n');
595
+ const selectedPrefix = chalk.yellow('❯ ');
596
+
597
+ const data = trimmedRange(this.data.items, this.data.selectedIdx, 9);
598
+ const labelLength: number = data.trimmed
599
+ .map((it) => it.tag.length)
600
+ .reduce((a, b) => {
601
+ if (a > b) {
602
+ return a;
603
+ }
604
+ return b;
605
+ }, 0);
606
+
607
+ text += data.startTrimmed ? ' ...\n' : '';
608
+
609
+ data.trimmed.forEach((it, idx) => {
610
+ const isSelected = idx === this.data.selectedIdx - data.offset;
611
+ let title = it.tag.padEnd(labelLength, ' ');
612
+ title = isSelected ? chalk.yellow(title) : title;
613
+
614
+ text += isSelected ? `${selectedPrefix}${title}` : ` ${title}`;
615
+ text += idx != this.data.items.length - 1 ? '\n' : '';
616
+ });
617
+
618
+ text += data.endTrimmed ? ' ...\n' : '';
619
+ return text;
620
+ }
621
+
622
+ result(): T {
623
+ return this.data.items[this.data.selectedIdx]!;
624
+ }
625
+ }
626
+
627
+ export const trimmedRange = <T>(
628
+ arr: T[],
629
+ index: number,
630
+ limitLines: number,
631
+ ): {
632
+ trimmed: T[];
633
+ offset: number;
634
+ startTrimmed: boolean;
635
+ endTrimmed: boolean;
636
+ } => {
637
+ const limit = limitLines - 2;
638
+ const sideLimit = Math.round(limit / 2);
639
+
640
+ const endTrimmed = arr.length - sideLimit > index;
641
+ const startTrimmed = index > sideLimit - 1;
642
+
643
+ const paddingStart = Math.max(index + sideLimit - arr.length, 0);
644
+ const paddingEnd = Math.min(index - sideLimit + 1, 0);
645
+
646
+ const d1 = endTrimmed ? 1 : 0;
647
+ const d2 = startTrimmed ? 0 : 1;
648
+
649
+ const start = Math.max(0, index - sideLimit + d1 - paddingStart);
650
+ const end = Math.min(arr.length, index + sideLimit + d2 - paddingEnd);
651
+
652
+ return {
653
+ trimmed: arr.slice(start, end),
654
+ offset: start,
655
+ startTrimmed,
656
+ endTrimmed,
657
+ };
658
+ };
@@ -0,0 +1,16 @@
1
+ import type { Config } from '../index';
2
+
3
+ export const getTablesFilterByExtensions = ({
4
+ extensionsFilters,
5
+ dialect,
6
+ }: Pick<Config, 'extensionsFilters' | 'dialect'>): string[] => {
7
+ if (extensionsFilters) {
8
+ if (
9
+ extensionsFilters.includes('postgis')
10
+ && dialect === 'postgresql'
11
+ ) {
12
+ return ['!geography_columns', '!geometry_columns', '!spatial_ref_sys'];
13
+ }
14
+ }
15
+ return [];
16
+ };
@@ -0,0 +1,10 @@
1
+ export const vectorOps = [
2
+ 'vector_l2_ops',
3
+ 'vector_ip_ops',
4
+ 'vector_cosine_ops',
5
+ 'vector_l1_ops',
6
+ 'bit_hamming_ops',
7
+ 'bit_jaccard_ops',
8
+ 'halfvec_l2_ops',
9
+ 'sparsevec_l2_ops',
10
+ ];