drizzle-kit 0.9.41 → 0.9.42

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.
@@ -1,482 +0,0 @@
1
- import { AlteredColumn, Column, Index, Table } from "./snapshotsDiffer";
2
-
3
- export interface JsonCreateTableStatement {
4
- type: "create_table";
5
- tableName: string;
6
- columns: Column[];
7
- }
8
-
9
- export interface JsonDropTableStatement {
10
- type: "drop_table";
11
- tableName: string;
12
- }
13
-
14
- export interface JsonRenameTableStatement {
15
- type: "rename_table";
16
- tableNameFrom: string;
17
- tableNameTo: string;
18
- }
19
-
20
- export interface JsonCreateEnumStatement {
21
- type: "create_type_enum";
22
- name: string;
23
- values: string[];
24
- }
25
-
26
- export interface JsonAddValueToEnumStatement {
27
- type: "alter_type_add_value";
28
- name: string;
29
- value: string;
30
- }
31
-
32
- export interface JsonDropColumnStatement {
33
- type: "alter_table_drop_column";
34
- tableName: string;
35
- columnName: string;
36
- }
37
-
38
- export interface JsonAddColumnStatement {
39
- type: "alter_table_add_column";
40
- tableName: string;
41
- column: Column;
42
- }
43
-
44
- export interface JsonCreateIndexStatement {
45
- type: "create_index";
46
- tableName: string;
47
- indexName: string;
48
- value: string;
49
- isUnique: boolean;
50
- }
51
-
52
- export interface JsonReferenceStatement {
53
- type: "create_reference" | "alter_reference" | "delete_reference";
54
- fromTable: string;
55
- toTable: string;
56
- fromColumn: string;
57
- toColumn: string;
58
- foreignKeyName: string;
59
- onDelete?: string;
60
- onUpdate?: string;
61
- }
62
-
63
- export interface JsonCreateReferenceStatement extends JsonReferenceStatement {
64
- type: "create_reference";
65
- }
66
-
67
- export interface JsonAlterReferenceStatement extends JsonReferenceStatement {
68
- type: "alter_reference";
69
- oldFkey: string;
70
- }
71
-
72
- export interface JsonDeleteReferenceStatement extends JsonReferenceStatement {
73
- type: "delete_reference";
74
- }
75
-
76
- export interface JsonDropIndexStatement {
77
- type: "drop_index";
78
- tableName: string;
79
- indexName: string;
80
- }
81
-
82
- export interface JsonRenameColumnStatement {
83
- type: "alter_table_rename_column";
84
- tableName: string;
85
- oldColumnName: string;
86
- newColumnName: string;
87
- }
88
-
89
- export interface JsonAlterColumnTypeStatement {
90
- type: "alter_table_alter_column_set_type";
91
- tableName: string;
92
- columnName: string;
93
- newDataType: string;
94
- }
95
-
96
- export interface JsonAlterColumnSetDefaultStatement {
97
- type: "alter_table_alter_column_set_default";
98
- tableName: string;
99
- columnName: string;
100
- newDefaultValue: string;
101
- }
102
-
103
- export interface JsonAlterColumnDropDefaultStatement {
104
- type: "alter_table_alter_column_drop_default";
105
- tableName: string;
106
- columnName: string;
107
- }
108
-
109
- export interface JsonAlterColumnSetNotNullStatement {
110
- type: "alter_table_alter_column_set_notnull";
111
- tableName: string;
112
- columnName: string;
113
- }
114
-
115
- export interface JsonAlterColumnDropNotNullStatement {
116
- type: "alter_table_alter_column_drop_notnull";
117
- tableName: string;
118
- columnName: string;
119
- }
120
-
121
- export type JsonAlterColumnStatement =
122
- | JsonRenameColumnStatement
123
- | JsonAlterColumnTypeStatement
124
- | JsonAlterColumnSetDefaultStatement
125
- | JsonAlterColumnDropDefaultStatement
126
- | JsonAlterColumnSetNotNullStatement
127
- | JsonAlterColumnDropNotNullStatement;
128
-
129
- export type JsonStatement =
130
- | JsonAlterColumnStatement
131
- | JsonCreateTableStatement
132
- | JsonDropTableStatement
133
- | JsonRenameTableStatement
134
- | JsonCreateEnumStatement
135
- | JsonAddValueToEnumStatement
136
- | JsonDropColumnStatement
137
- | JsonAddColumnStatement
138
- | JsonCreateIndexStatement
139
- | JsonCreateReferenceStatement
140
- | JsonAlterReferenceStatement
141
- | JsonDeleteReferenceStatement
142
- | JsonDropIndexStatement;
143
-
144
- export const prepareCreateTableJson = (
145
- table: Table
146
- ): JsonCreateTableStatement => {
147
- const { name, columns } = table;
148
- return {
149
- type: "create_table",
150
- tableName: name,
151
- columns: Object.values(columns),
152
- };
153
- };
154
-
155
- export const prepareDropTableJson = (table: Table): JsonDropTableStatement => {
156
- return {
157
- type: "drop_table",
158
- tableName: table.name,
159
- };
160
- };
161
-
162
- export const prepareRenameTableJson = (
163
- tableFrom: Table,
164
- tableTo: Table
165
- ): JsonRenameTableStatement => {
166
- return {
167
- type: "rename_table",
168
- tableNameFrom: tableFrom.name,
169
- tableNameTo: tableTo.name,
170
- };
171
- };
172
-
173
- export const prepareCreateEnumJson = (
174
- name: string,
175
- values: string[]
176
- ): JsonCreateEnumStatement => {
177
- return {
178
- type: "create_type_enum",
179
- name: name,
180
- values,
181
- };
182
- };
183
-
184
- // https://blog.yo1.dog/updating-enum-values-in-postgresql-the-safe-and-easy-way/
185
- export const prepareAddValuesToEnumJson = (
186
- name: string,
187
- values: string[]
188
- ): JsonAddValueToEnumStatement[] => {
189
- return values.map((it) => {
190
- return {
191
- type: "alter_type_add_value",
192
- name: name,
193
- value: it,
194
- };
195
- });
196
- };
197
-
198
- export const prepareRenameColumns = (
199
- tableName: string,
200
- pairs: { from: Column; to: Column }[]
201
- ): JsonRenameColumnStatement[] => {
202
- return pairs.map((it) => {
203
- return {
204
- type: "alter_table_rename_column",
205
- tableName: tableName,
206
- oldColumnName: it.from.name,
207
- newColumnName: it.to.name,
208
- };
209
- });
210
- };
211
-
212
- export const prepareAlterTableColumnsJson = (
213
- tableName: string,
214
- deleted: Column[],
215
- added: Column[],
216
- altered: AlteredColumn[]
217
- ) => {
218
- const statements: JsonStatement[] = [];
219
-
220
- const dropColumns = _prepareDropColumns(tableName, deleted);
221
- const addColumns = _prepareAddColumns(tableName, added);
222
- const alterColumns = _prepareAlterColumns(tableName, altered);
223
-
224
- statements.push(...dropColumns);
225
- statements.push(...addColumns);
226
- statements.push(...alterColumns);
227
-
228
- return statements;
229
- };
230
-
231
- const _prepareDropColumns = (
232
- taleName: string,
233
- columns: Column[]
234
- ): JsonDropColumnStatement[] => {
235
- return columns.map((it) => {
236
- return {
237
- type: "alter_table_drop_column",
238
- tableName: taleName,
239
- columnName: it.name,
240
- };
241
- });
242
- };
243
-
244
- const _prepareAddColumns = (
245
- tableName: string,
246
- columns: Column[]
247
- ): JsonAddColumnStatement[] => {
248
- return columns.map((it) => {
249
- return {
250
- type: "alter_table_add_column",
251
- tableName: tableName,
252
- column: it,
253
- };
254
- });
255
- };
256
-
257
- const _prepareAlterColumns = (
258
- tableName: string,
259
- columns: AlteredColumn[]
260
- ): JsonAlterColumnStatement[] => {
261
- let statements: JsonAlterColumnStatement[] = [];
262
-
263
- for (const column of columns) {
264
- // TODO: rename column
265
- const columnName =
266
- typeof column.name !== "string" ? column.name.new : column.name;
267
-
268
- if (typeof column.name !== "string") {
269
- statements.push({
270
- type: "alter_table_rename_column",
271
- tableName,
272
- oldColumnName: column.name.old,
273
- newColumnName: column.name.new,
274
- });
275
- }
276
-
277
- if (column.type?.type === "changed") {
278
- statements.push({
279
- type: "alter_table_alter_column_set_type",
280
- tableName,
281
- columnName,
282
- newDataType: column.type.new,
283
- });
284
- }
285
-
286
- if (column.defaultValue?.type === "added") {
287
- statements.push({
288
- type: "alter_table_alter_column_set_default",
289
- tableName,
290
- columnName,
291
- newDefaultValue: column.defaultValue.value,
292
- });
293
- }
294
-
295
- if (column.defaultValue?.type === "changed") {
296
- statements.push({
297
- type: "alter_table_alter_column_set_default",
298
- tableName,
299
- columnName,
300
- newDefaultValue: column.defaultValue.new,
301
- });
302
- }
303
-
304
- if (column.defaultValue?.type === "deleted") {
305
- statements.push({
306
- type: "alter_table_alter_column_drop_default",
307
- tableName,
308
- columnName,
309
- });
310
- }
311
-
312
- if (column.notNull?.type === "added") {
313
- statements.push({
314
- type: "alter_table_alter_column_set_notnull",
315
- tableName,
316
- columnName,
317
- });
318
- }
319
-
320
- if (column.notNull?.type === "changed") {
321
- const type = column.notNull.new
322
- ? "alter_table_alter_column_set_notnull"
323
- : "alter_table_alter_column_drop_notnull";
324
- statements.push({
325
- type: type,
326
- tableName,
327
- columnName,
328
- });
329
- }
330
-
331
- if (column.notNull?.type === "deleted") {
332
- statements.push({
333
- type: "alter_table_alter_column_drop_notnull",
334
- tableName,
335
- columnName,
336
- });
337
- }
338
-
339
- if (column.notNull?.type === "added") {
340
- statements.push({
341
- type: "alter_table_alter_column_set_notnull",
342
- tableName,
343
- columnName,
344
- });
345
- }
346
-
347
- if (column.notNull?.type === "changed") {
348
- const type = column.notNull.new
349
- ? "alter_table_alter_column_set_notnull"
350
- : "alter_table_alter_column_drop_notnull";
351
- statements.push({
352
- type: type,
353
- tableName,
354
- columnName,
355
- });
356
- }
357
-
358
- if (column.notNull?.type === "deleted") {
359
- statements.push({
360
- type: "alter_table_alter_column_drop_notnull",
361
- tableName,
362
- columnName,
363
- });
364
- }
365
- }
366
-
367
- return statements;
368
- };
369
-
370
- export const prepareCreateIndexesJson = (
371
- tableName: string,
372
- indexes: Index[]
373
- ): JsonCreateIndexStatement[] => {
374
- return indexes.map((index) => {
375
- return {
376
- type: "create_index",
377
- tableName,
378
- indexName: index.name,
379
- value: index.columns.join(", "),
380
- isUnique: index.isUnique,
381
- };
382
- });
383
- };
384
-
385
- export const prepareCreateReferencesJson = (
386
- tableName: string,
387
- columns: Column[]
388
- ): JsonCreateReferenceStatement[] => {
389
- return columns
390
- .filter((it) => {
391
- return it.references !== undefined;
392
- })
393
- .map((entry) => {
394
- const column = entry;
395
- const references = column.references!!;
396
- const [fkName, toTable, toColumn, onDelete, onUpdate] =
397
- references.split(";");
398
- return {
399
- type: "create_reference",
400
- fromTable: tableName,
401
- toTable: toTable,
402
- fromColumn: column.name,
403
- toColumn: toColumn,
404
- foreignKeyName: fkName,
405
- onDelete: onDelete,
406
- onUpdate: onUpdate,
407
- };
408
- });
409
- };
410
-
411
- export const prepareAlterReferencesJson = (
412
- tableName: string,
413
- columns: AlteredColumn[]
414
- ): JsonReferenceStatement[] => {
415
- const result: JsonReferenceStatement[] = columns
416
- .filter((it) => {
417
- return it.references !== undefined;
418
- })
419
- .map((column): JsonReferenceStatement => {
420
- const references = column.references!!;
421
- const fromColumnName =
422
- typeof column.name === "string" ? column.name : column.name.new;
423
-
424
- if (references.type === "added") {
425
- const [fkName, toTable, toColumn, onDelete, onUpdate] =
426
- references.value.split(";");
427
- return {
428
- type: "create_reference",
429
- fromTable: tableName,
430
- toTable: toTable,
431
- fromColumn: fromColumnName,
432
- toColumn: toColumn,
433
- foreignKeyName: fkName,
434
- onDelete: onDelete,
435
- onUpdate: onUpdate,
436
- };
437
- } else if (references.type === "changed") {
438
- const [oldFkey] = references.old.split(";");
439
- const [fkName, toTable, toColumn, onDelete, onUpdate] =
440
- references.old.split(";");
441
- const alterReference: JsonAlterReferenceStatement = {
442
- type: "alter_reference",
443
- fromTable: tableName,
444
- toTable: toTable,
445
- fromColumn: fromColumnName,
446
- toColumn: toColumn,
447
- foreignKeyName: fkName,
448
- onDelete: onDelete,
449
- onUpdate: onUpdate,
450
- oldFkey: oldFkey
451
- };
452
- return alterReference;
453
- } else {
454
- const [fkName, toTable, toColumn, onDelete, onUpdate] =
455
- references.value.split(";");
456
- return {
457
- type: "delete_reference",
458
- fromTable: tableName,
459
- toTable: toTable,
460
- fromColumn: fromColumnName,
461
- toColumn: toColumn,
462
- foreignKeyName: fkName,
463
- onDelete: onDelete,
464
- onUpdate: onUpdate,
465
- };
466
- }
467
- });
468
- return result
469
- };
470
-
471
- export const prepareDropIndexesJson = (
472
- tableName: string,
473
- indexes: Index[]
474
- ): JsonDropIndexStatement[] => {
475
- return indexes.map((index) => {
476
- return {
477
- type: "drop_index",
478
- tableName,
479
- indexName: index.name,
480
- };
481
- });
482
- };
@@ -1,44 +0,0 @@
1
- import fs from 'fs'
2
- import serialize from './serializer'
3
-
4
- // TODO: export as a function w
5
-
6
- const dry = {
7
- version: "1",
8
- tables: {},
9
- enums: {}
10
- }
11
-
12
- const prepareMigration = (
13
- migrationRootFolderName: string = 'drizzle',
14
- dataFolderPath: string
15
- ): { prev: any, cur: any } => {
16
- const root = migrationRootFolderName
17
- const files = fs.readdirSync('./')
18
- const drizzleFolder = files.find((it) => {
19
- return it === root
20
- })
21
-
22
- if (!drizzleFolder) {
23
- fs.mkdirSync(root)
24
- }
25
-
26
- const migrationFolders = fs.readdirSync(`./${root}`)
27
-
28
- let prevSnapshot;
29
-
30
- if (migrationFolders.length === 0) {
31
- prevSnapshot = dry
32
- } else {
33
- migrationFolders.sort()
34
- const lastSnapshotFolder = migrationFolders[migrationFolders.length - 1]
35
- prevSnapshot = JSON.parse(fs.readFileSync(`./${root}/${lastSnapshotFolder}/snapshot.json`).toString())
36
- }
37
-
38
-
39
- const result = serialize(dataFolderPath)
40
-
41
- return { prev: prevSnapshot, cur: result }
42
- }
43
-
44
- export default prepareMigration;