1
1
require ( 'dotenv' ) . config ( ) ;
2
2
3
+ const logger = require ( '../logger' ) ;
3
4
const _ = require ( 'lodash' ) ;
4
5
const pluralize = require ( 'pluralize' ) ;
5
6
const { singular } = pluralize ;
@@ -91,9 +92,11 @@ async function getModelDefs(db) {
91
92
92
93
async function run ( ) {
93
94
try {
95
+ logger . info ( "Connecting to MongoDB..." )
94
96
await mongo . connect ( ) ;
95
-
97
+
96
98
const db = mongo . db ( ) ;
99
+ logger . info ( "Connected! Fetching model definitions..." )
97
100
98
101
const models = await getModelDefs ( db ) ;
99
102
@@ -102,14 +105,17 @@ async function run() {
102
105
return acc ;
103
106
} , { } ) ;
104
107
108
+ logger . info ( "Models fetched successfully. Executing pre-migration steps..." )
105
109
const dialect = require ( `./dialects/${ knex . client . config . client } ` ) ( knex , inspector ) ;
106
110
await dialect . delAllTables ( knex ) ;
107
111
await dialect . beforeMigration ?. ( knex ) ;
112
+ logger . info ( "Pre-migration steps complete" )
108
113
109
114
// 1st pass: for each document create a new row and store id in a map
115
+ logger . info ( "First Pass - Creating rows and mapping IDs to indexes..." )
110
116
for ( const model of models ) {
111
117
const cursor = db . collection ( model . collectionName ) . find ( ) ;
112
-
118
+ logger . verbose ( `Processing collection ${ model . collectionName } ` )
113
119
while ( await cursor . hasNext ( ) ) {
114
120
const entry = await cursor . next ( ) ;
115
121
const row = transformEntry ( entry , model ) ;
@@ -122,14 +128,15 @@ async function run() {
122
128
await cursor . close ( ) ;
123
129
}
124
130
131
+ logger . info ( "Second Pass - Rows created and IDs mapped. Linking components & relations with tables..." )
125
132
// 2nd pass: for each document's components & relations create the links in the right tables
126
133
127
134
for ( const model of models ) {
128
135
const cursor = db . collection ( model . collectionName ) . find ( ) ;
129
-
136
+ logger . verbose ( `Processing collection ${ model . collectionName } ` )
130
137
while ( await cursor . hasNext ( ) ) {
131
138
const entry = await cursor . next ( ) ;
132
-
139
+
133
140
for ( const key of Object . keys ( entry ) ) {
134
141
const attribute = model . attributes [ key ] ;
135
142
@@ -154,13 +161,15 @@ async function run() {
154
161
} ) ;
155
162
156
163
if ( rows . length > 0 ) {
164
+ logger . debug ( `Filling component ${ key } joining table - ${ JSON . stringify ( rows ) } ` )
157
165
await knex ( linkTableName ) . insert ( rows ) ;
158
166
}
159
167
160
168
continue ;
161
169
}
162
170
163
171
if ( attribute . type === 'dynamiczone' ) {
172
+
164
173
// create compo links
165
174
const linkTableName = `${ model . collectionName } _components` ;
166
175
@@ -178,6 +187,7 @@ async function run() {
178
187
} ) ;
179
188
180
189
if ( rows . length > 0 ) {
190
+ logger . debug ( `Filling dynamiczone ${ key } joining table - ${ JSON . stringify ( rows ) } ` )
181
191
await knex ( linkTableName ) . insert ( rows ) ;
182
192
}
183
193
@@ -196,7 +206,7 @@ async function run() {
196
206
field : key ,
197
207
order : 1 ,
198
208
} ;
199
-
209
+ logger . debug ( `Linking single file - ${ key } - ${ JSON . stringify ( row ) } ` )
200
210
await knex ( 'upload_file_morph' ) . insert ( row ) ;
201
211
}
202
212
@@ -210,20 +220,21 @@ async function run() {
210
220
} ) ) ;
211
221
212
222
if ( rows . length > 0 ) {
223
+ logger . debug ( `Linking multiple files - ${ key } - ${ JSON . stringify ( rows ) } ` )
213
224
await knex ( 'upload_file_morph' ) . insert ( rows ) ;
214
225
}
215
226
}
216
227
217
228
if ( attribute . model || attribute . collection ) {
218
229
// create relation links
219
-
230
+
220
231
const targetModel = models . find ( ( m ) => {
221
232
return (
222
233
[ attribute . model , attribute . collection ] . includes ( m . modelName ) &&
223
234
( ! attribute . plugin || ( attribute . plugin && attribute . plugin === m . plugin ) )
224
235
) ;
225
236
} ) ;
226
-
237
+
227
238
const targetAttribute = targetModel ?. attributes ?. [ attribute . via ] ;
228
239
229
240
const isOneWay = attribute . model && ! attribute . via && attribute . moel !== '*' ;
@@ -253,6 +264,7 @@ async function run() {
253
264
targetAttribute ?. collection &&
254
265
targetAttribute ?. collection !== '*' ;
255
266
267
+
256
268
if ( isOneWay || isOneToOne || isManyToOne ) {
257
269
// TODO: optimize with one updata at the end
258
270
@@ -336,12 +348,17 @@ async function run() {
336
348
337
349
await dialect . afterMigration ?. ( knex ) ;
338
350
}
339
- } finally {
351
+ logger . info ( "Post-migration steps complete." )
352
+ }
353
+ catch ( err ) {
354
+ logger . error ( err )
355
+ }
356
+ finally {
357
+ logger . info ( "Cleaning Up..." )
340
358
await mongo . close ( ) ;
341
359
await knex . destroy ( ) ;
342
360
}
343
-
344
- console . log ( 'Done' ) ;
361
+ logger . info ( 'Migration Complete' ) ;
345
362
}
346
363
347
- run ( ) . catch ( console . dir ) ;
364
+ run ( )
0 commit comments