11require ( 'dotenv' ) . config ( ) ;
22
3+ const logger = require ( '../logger' ) ;
34const _ = require ( 'lodash' ) ;
45const pluralize = require ( 'pluralize' ) ;
56const { singular } = pluralize ;
@@ -91,9 +92,11 @@ async function getModelDefs(db) {
9192
9293async function run ( ) {
9394 try {
95+ logger . info ( "Connecting to MongoDB..." )
9496 await mongo . connect ( ) ;
95-
97+
9698 const db = mongo . db ( ) ;
99+ logger . info ( "Connected! Fetching model definitions..." )
97100
98101 const models = await getModelDefs ( db ) ;
99102
@@ -102,14 +105,17 @@ async function run() {
102105 return acc ;
103106 } , { } ) ;
104107
108+ logger . info ( "Models fetched successfully. Executing pre-migration steps..." )
105109 const dialect = require ( `./dialects/${ knex . client . config . client } ` ) ( knex , inspector ) ;
106110 await dialect . delAllTables ( knex ) ;
107111 await dialect . beforeMigration ?. ( knex ) ;
112+ logger . info ( "Pre-migration steps complete" )
108113
109114 // 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..." )
110116 for ( const model of models ) {
111117 const cursor = db . collection ( model . collectionName ) . find ( ) ;
112-
118+ logger . verbose ( `Processing collection ${ model . collectionName } ` )
113119 while ( await cursor . hasNext ( ) ) {
114120 const entry = await cursor . next ( ) ;
115121 const row = transformEntry ( entry , model ) ;
@@ -122,14 +128,15 @@ async function run() {
122128 await cursor . close ( ) ;
123129 }
124130
131+ logger . info ( "Second Pass - Rows created and IDs mapped. Linking components & relations with tables..." )
125132 // 2nd pass: for each document's components & relations create the links in the right tables
126133
127134 for ( const model of models ) {
128135 const cursor = db . collection ( model . collectionName ) . find ( ) ;
129-
136+ logger . verbose ( `Processing collection ${ model . collectionName } ` )
130137 while ( await cursor . hasNext ( ) ) {
131138 const entry = await cursor . next ( ) ;
132-
139+
133140 for ( const key of Object . keys ( entry ) ) {
134141 const attribute = model . attributes [ key ] ;
135142
@@ -154,13 +161,15 @@ async function run() {
154161 } ) ;
155162
156163 if ( rows . length > 0 ) {
164+ logger . debug ( `Filling component ${ key } joining table - ${ JSON . stringify ( rows ) } ` )
157165 await knex ( linkTableName ) . insert ( rows ) ;
158166 }
159167
160168 continue ;
161169 }
162170
163171 if ( attribute . type === 'dynamiczone' ) {
172+
164173 // create compo links
165174 const linkTableName = `${ model . collectionName } _components` ;
166175
@@ -178,6 +187,7 @@ async function run() {
178187 } ) ;
179188
180189 if ( rows . length > 0 ) {
190+ logger . debug ( `Filling dynamiczone ${ key } joining table - ${ JSON . stringify ( rows ) } ` )
181191 await knex ( linkTableName ) . insert ( rows ) ;
182192 }
183193
@@ -196,7 +206,7 @@ async function run() {
196206 field : key ,
197207 order : 1 ,
198208 } ;
199-
209+ logger . debug ( `Linking single file - ${ key } - ${ JSON . stringify ( row ) } ` )
200210 await knex ( 'upload_file_morph' ) . insert ( row ) ;
201211 }
202212
@@ -210,20 +220,21 @@ async function run() {
210220 } ) ) ;
211221
212222 if ( rows . length > 0 ) {
223+ logger . debug ( `Linking multiple files - ${ key } - ${ JSON . stringify ( rows ) } ` )
213224 await knex ( 'upload_file_morph' ) . insert ( rows ) ;
214225 }
215226 }
216227
217228 if ( attribute . model || attribute . collection ) {
218229 // create relation links
219-
230+
220231 const targetModel = models . find ( ( m ) => {
221232 return (
222233 [ attribute . model , attribute . collection ] . includes ( m . modelName ) &&
223234 ( ! attribute . plugin || ( attribute . plugin && attribute . plugin === m . plugin ) )
224235 ) ;
225236 } ) ;
226-
237+
227238 const targetAttribute = targetModel ?. attributes ?. [ attribute . via ] ;
228239
229240 const isOneWay = attribute . model && ! attribute . via && attribute . model !== '*' ;
@@ -253,6 +264,7 @@ async function run() {
253264 targetAttribute ?. collection &&
254265 targetAttribute ?. collection !== '*' ;
255266
267+
256268 if ( isOneWay || isOneToOne || isManyToOne ) {
257269 // TODO: optimize with one updata at the end
258270
@@ -336,12 +348,17 @@ async function run() {
336348
337349 await dialect . afterMigration ?. ( knex ) ;
338350 }
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..." )
340358 await mongo . close ( ) ;
341359 await knex . destroy ( ) ;
342360 }
343-
344- console . log ( 'Done' ) ;
361+ logger . info ( 'Migration Complete' ) ;
345362}
346363
347- run ( ) . catch ( console . dir ) ;
364+ run ( )
0 commit comments