4
4
*/
5
5
6
6
import _ from 'lodash' ;
7
+ import sequelize from 'sequelize' ;
7
8
import models from '../../src/models' ;
8
9
import { ATTACHMENT_TYPES } from '../../src/constants' ;
9
10
@@ -14,20 +15,41 @@ console.log('Migrate project.bookmarks to project.attachments for all projects i
14
15
*
15
16
* @returns {Promise } the DB data
16
17
*/
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
+ } ) ;
18
26
19
27
/**
20
28
* Executes the bookmarks migration to link attachments
21
29
* @returns {Promise } resolved when migration is complete
22
30
*/
23
31
const migrateBookmarks = async ( ) => {
24
- const projects = await getAllProjectsFromDB ( ) ;
32
+ const projects = await getProjectsWithBookmarks ( ) ;
33
+ let count = 0 ;
34
+
35
+ console . log ( `Found ${ projects . length } projects.` ) ;
25
36
26
37
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` ) ;
28
46
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 => ( {
31
53
projectId : project . id ,
32
54
type : ATTACHMENT_TYPES . LINK ,
33
55
title : b . title ,
@@ -37,17 +59,25 @@ const migrateBookmarks = async () => {
37
59
updatedAt : _ . isNil ( b . updatedAt ) ? project . updatedAt : b . updatedAt ,
38
60
updatedBy : _ . isNil ( b . updatedBy ) ? project . updatedBy : b . updatedBy ,
39
61
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.` ) ;
41
72
} ) ;
42
- project . bookmarks = [ ] ;
43
- await project . save ( ) ;
44
73
}
45
74
} ;
46
75
47
76
migrateBookmarks ( ) . then ( ( ) => {
48
77
console . log ( 'Migration of projects bookmarks to project links attachments finished!' ) ;
49
78
process . exit ( ) ;
50
79
} ) . catch ( ( e ) => {
51
- console . log ( e ) ;
80
+ console . error ( 'Migration of projects bookmarks to project links attachments failed!' ) ;
81
+ console . error ( e ) ;
52
82
process . exit ( ) ;
53
83
} ) ;
0 commit comments