1- import { listToMap , isDefined } from '@togglecorp/fujs' ;
2-
31import { MigrationActionItem , MigrationFileContent } from '../types' ;
42import {
5- concat ,
6- removeUndefinedKeys ,
73 getMigrationFilesAttrs ,
84 readMigrations ,
95 removeFiles ,
10- writeFilePromisify
6+ writeFilePromisify ,
7+ mergeMigrationActionItems
118} from '../utils' ;
129
13- function getCanonicalKey (
14- item : MigrationActionItem ,
15- opts : { useNewKey : boolean } ,
16- ) {
17- if ( opts . useNewKey && item . action === 'update' ) {
18- return concat (
19- item . newNamespace ?? item . namespace ,
20- item . newKey ?? item . key ,
21- ) ;
22- }
23- return concat (
24- item . namespace ,
25- item . key ,
26- ) ;
27- }
28-
29- function mergeMigrationActionItems (
30- prevMigrationActionItems : MigrationActionItem [ ] ,
31- nextMigrationActionItems : MigrationActionItem [ ] ,
32- ) {
33- interface PrevMappings {
34- [ key : string ] : MigrationActionItem ,
35- }
36-
37- const prevCanonicalKeyMappings : PrevMappings = listToMap (
38- prevMigrationActionItems ,
39- ( item ) => getCanonicalKey ( item , { useNewKey : true } ) ,
40- ( item ) => item ,
41- ) ;
42-
43- interface NextMappings {
44- [ key : string ] : MigrationActionItem | null ,
45- }
46-
47- const nextMappings = nextMigrationActionItems . reduce < NextMappings > (
48- ( acc , nextMigrationActionItem ) => {
49- const canonicalKey = getCanonicalKey ( nextMigrationActionItem , { useNewKey : false } )
50-
51- const prevItemWithCanonicalKey = prevCanonicalKeyMappings [ canonicalKey ] ;
52- // const prevItemWithKey = prevKeyMappings[nextMigrationActionItem.key];
53-
54- if ( ! prevItemWithCanonicalKey ) {
55- return {
56- ...acc ,
57- [ canonicalKey ] : nextMigrationActionItem ,
58- } ;
59- }
60-
61- if ( prevItemWithCanonicalKey . action === 'add' && nextMigrationActionItem . action === 'add' ) {
62- throw `Action 'add' already exists for '${ canonicalKey } '` ;
63- }
64- if ( prevItemWithCanonicalKey . action === 'add' && nextMigrationActionItem . action === 'remove' ) {
65- return {
66- ...acc ,
67- [ canonicalKey ] : null ,
68- } ;
69- }
70- if ( prevItemWithCanonicalKey . action === 'add' && nextMigrationActionItem . action === 'update' ) {
71- const newKey = nextMigrationActionItem . newKey
72- ?? prevItemWithCanonicalKey . key ;
73- const newNamespace = nextMigrationActionItem . newNamespace
74- ?? prevItemWithCanonicalKey . namespace ;
75-
76- const newMigrationItem = removeUndefinedKeys < MigrationActionItem > ( {
77- action : 'add' ,
78- namespace : newNamespace ,
79- key : newKey ,
80- value : nextMigrationActionItem . newValue
81- ?? prevItemWithCanonicalKey . value ,
82- } ) ;
83-
84- const newCanonicalKey = getCanonicalKey ( newMigrationItem , { useNewKey : true } ) ;
85- if ( acc [ newCanonicalKey ] !== undefined && acc [ newCanonicalKey ] !== null ) {
86- throw `Action 'update' cannot be applied to '${ newCanonicalKey } ' as the key already exists` ;
87- }
88-
89- return {
90- ...acc ,
91- // Setting null so that we remove them on the mappings.
92- // No need to set null, if we have already overridden with other value
93- [ canonicalKey ] : acc [ canonicalKey ] === undefined || acc [ canonicalKey ] === null
94- ? null
95- : acc [ canonicalKey ] ,
96- [ newCanonicalKey ] : newMigrationItem ,
97- }
98- }
99- if ( prevItemWithCanonicalKey . action === 'remove' && nextMigrationActionItem . action === 'add' ) {
100- return {
101- ...acc ,
102- [ canonicalKey ] : removeUndefinedKeys < MigrationActionItem > ( {
103- action : 'update' ,
104- namespace : prevItemWithCanonicalKey . namespace ,
105- key : prevItemWithCanonicalKey . key ,
106- newValue : nextMigrationActionItem . value ,
107- } )
108- } ;
109- }
110- if ( prevItemWithCanonicalKey . action === 'remove' && nextMigrationActionItem . action === 'remove' ) {
111- // pass
112- return acc ;
113- }
114- if ( prevItemWithCanonicalKey . action === 'remove' && nextMigrationActionItem . action === 'update' ) {
115- throw `Action 'update' cannot be applied to '${ canonicalKey } ' after action 'remove'` ;
116- }
117- if ( prevItemWithCanonicalKey . action === 'update' && nextMigrationActionItem . action === 'add' ) {
118- throw `Action 'add' cannot be applied to '${ canonicalKey } ' after action 'update'` ;
119- }
120- if ( prevItemWithCanonicalKey . action === 'update' && nextMigrationActionItem . action === 'update' ) {
121- return {
122- ...acc ,
123- [ canonicalKey ] : removeUndefinedKeys < MigrationActionItem > ( {
124- action : 'update' ,
125- namespace : prevItemWithCanonicalKey . namespace ,
126- key : prevItemWithCanonicalKey . key ,
127- newNamespace : nextMigrationActionItem . newNamespace ?? prevItemWithCanonicalKey . newNamespace ,
128- newKey : nextMigrationActionItem . newKey ?? prevItemWithCanonicalKey . newKey ,
129- newValue : nextMigrationActionItem . newValue ?? prevItemWithCanonicalKey . newValue ,
130- } ) ,
131- } ;
132- }
133- if ( prevItemWithCanonicalKey . action === 'update' && nextMigrationActionItem . action === 'remove' ) {
134- return {
135- ...acc ,
136- [ canonicalKey ] : removeUndefinedKeys < MigrationActionItem > ( {
137- action : 'remove' ,
138- namespace : prevItemWithCanonicalKey . namespace ,
139- key : prevItemWithCanonicalKey . key ,
140- } ) ,
141- } ;
142- }
143- return acc ;
144- } ,
145- { } ,
146- ) ;
147-
148- const finalMappings = {
149- ...prevCanonicalKeyMappings ,
150- ...nextMappings ,
151- } ;
152-
153- return Object . values ( finalMappings ) . filter ( isDefined ) ;
154- }
155-
15610export function merge ( migrationFileContents : MigrationFileContent [ ] ) {
15711 const migrationActionItems = migrationFileContents . reduce < MigrationActionItem [ ] > (
15812 ( acc , migrationActionItem ) => {
@@ -174,21 +28,21 @@ async function mergeMigrations(
17428) {
17529 const migrationFilesAttrs = await getMigrationFilesAttrs ( projectPath , path ) ;
17630 const selectedMigrationFilesAttrs = migrationFilesAttrs . filter (
177- ( item ) => ( item . migrationName >= from && item . migrationName <= to )
31+ ( item ) => ( item . migrationFileName >= from && item . migrationFileName <= to )
17832 ) ;
17933 console . info ( `Found ${ selectedMigrationFilesAttrs . length } migration files` ) ;
18034
18135 if ( selectedMigrationFilesAttrs . length <= 1 ) {
18236 throw 'There should be atleast 2 migration files' ;
18337 }
18438 const selectedMigrations = await readMigrations (
185- selectedMigrationFilesAttrs . map ( ( migration ) => migration . fileName ) ,
39+ selectedMigrationFilesAttrs . map ( ( migration ) => migration . filePath ) ,
18640 ) ;
18741
18842 const firstMigration = selectedMigrations [ 0 ] ;
18943 const lastMigration = selectedMigrations [ selectedMigrations . length - 1 ] ;
19044
191- const selectedMigrationsFileNames = selectedMigrationFilesAttrs . map ( ( migration ) => migration . fileName ) ;
45+ const selectedMigrationsFileNames = selectedMigrationFilesAttrs . map ( ( migration ) => migration . filePath ) ;
19246
19347 const mergedMigrationContent = {
19448 actions : merge ( selectedMigrations . map ( ( migration ) => migration . content ) ) ,
0 commit comments