44 */
55
66import _ from 'lodash' ;
7+ import sequelize from 'sequelize' ;
78import models from '../../src/models' ;
89import { ATTACHMENT_TYPES } from '../../src/constants' ;
910
@@ -14,20 +15,41 @@ console.log('Migrate project.bookmarks to project.attachments for all projects i
1415 *
1516 * @returns {Promise } the DB data
1617 */
17- const getAllProjectsFromDB = ( ) => models . Project . findAll ( { raw : false } ) ;
18+ const getProjectsWithBookmarks = ( ) => models . Project . findAll ( {
19+ raw : false ,
20+ attributes : [ 'id' , 'bookmarks' ] ,
21+ where : sequelize . where (
22+ sequelize . fn ( 'json_array_length' , sequelize . col ( 'bookmarks' ) ) ,
23+ { [ sequelize . Op . gt ] : 0 } ,
24+ ) ,
25+ } ) ;
1826
1927/**
2028 * Executes the bookmarks migration to link attachments
2129 * @returns {Promise } resolved when migration is complete
2230 */
2331const migrateBookmarks = async ( ) => {
24- const projects = await getAllProjectsFromDB ( ) ;
32+ const projects = await getProjectsWithBookmarks ( ) ;
33+ let count = 0 ;
34+
35+ console . log ( `Found ${ projects . length } projects.` ) ;
2536
2637 for ( const project of projects ) {
27- const bookmarks = _ . get ( project , 'bookmarks' ) ;
38+ await models . sequelize . transaction ( async ( tr ) => { // eslint-disable-line no-loop-func
39+ count += 1 ;
40+ const percentage = Math . round ( ( count / projects . length ) * 100 ) ;
41+
42+ console . log ( `Processing project id ${ project . id } : ${ count } /${ projects . length } (${ percentage } %)...` ) ;
43+
44+ const bookmarks = _ . get ( project , 'bookmarks' , [ ] ) ;
45+ console . log ( `Processing project id ${ project . id } : found ${ bookmarks . length } bookmarks` ) ;
2846
29- _ . each ( bookmarks , async ( b ) => {
30- await models . ProjectAttachment . create ( {
47+ if ( bookmarks . length === 0 ) {
48+ console . log ( `Processing project id ${ project . id } : skipped.` ) ;
49+ return ;
50+ }
51+
52+ const attachments = bookmarks . map ( b => ( {
3153 projectId : project . id ,
3254 type : ATTACHMENT_TYPES . LINK ,
3355 title : b . title ,
@@ -37,17 +59,25 @@ const migrateBookmarks = async () => {
3759 updatedAt : _ . isNil ( b . updatedAt ) ? project . updatedAt : b . updatedAt ,
3860 updatedBy : _ . isNil ( b . updatedBy ) ? project . updatedBy : b . updatedBy ,
3961 tags : [ ] ,
40- } ) ;
62+ } ) ) ;
63+
64+ await models . ProjectAttachment . bulkCreate ( attachments , { transaction : tr } ) ;
65+ console . log ( `Processing project id ${ project . id } : attachments created.` ) ;
66+
67+ project . bookmarks = [ ] ;
68+ await project . save ( { transaction : tr } ) ;
69+ console . log ( `Processing project id ${ project . id } : bookmarks removed.` ) ;
70+
71+ console . log ( `Processing project id ${ project . id } : done.` ) ;
4172 } ) ;
42- project . bookmarks = [ ] ;
43- await project . save ( ) ;
4473 }
4574} ;
4675
4776migrateBookmarks ( ) . then ( ( ) => {
4877 console . log ( 'Migration of projects bookmarks to project links attachments finished!' ) ;
4978 process . exit ( ) ;
5079} ) . catch ( ( e ) => {
51- console . log ( e ) ;
80+ console . error ( 'Migration of projects bookmarks to project links attachments failed!' ) ;
81+ console . error ( e ) ;
5282 process . exit ( ) ;
5383} ) ;
0 commit comments