@@ -2,32 +2,66 @@ const _ = require('lodash'),
2
2
Promise = require ( 'bluebird' ) ,
3
3
common = require ( '../../../../lib/common' ) ,
4
4
models = require ( '../../../../models' ) ,
5
- message1 = 'Updating post data (comment_id)' ,
6
- message2 = 'Updated post data (comment_id)' ,
7
- message3 = 'Rollback: Keep correct comment_id values in amp column.' ;
5
+ converters = require ( '../../../../lib/mobiledoc/converters' ) ,
6
+ message1 = 'Updating posts: apply new editor format and set comment_id field.' ,
7
+ message2 = 'Updated posts: apply new editor format and set comment_id field.' ,
8
+ message3 = 'Rollback: Updating posts: use old editor format' ,
9
+ message4 = 'Rollback: Updated posts: use old editor format' ;
8
10
9
11
module . exports . config = {
10
12
transaction : true
11
13
} ;
12
14
15
+ let mobiledocIsCompatibleWithV1 = function mobiledocIsCompatibleWithV1 ( doc ) {
16
+ if ( doc
17
+ && doc . markups . length === 0
18
+ && doc . cards . length === 1
19
+ && doc . cards [ 0 ] [ 0 ] . match ( / (?: c a r d - ) ? m a r k d o w n / )
20
+ && doc . sections . length === 1
21
+ && doc . sections [ 0 ] . length === 2
22
+ && doc . sections [ 0 ] [ 0 ] === 10
23
+ && doc . sections [ 0 ] [ 1 ] === 0
24
+ ) {
25
+ return true ;
26
+ }
27
+
28
+ return false ;
29
+ } ;
30
+
13
31
module . exports . up = ( options ) => {
14
- const postAllColumns = [ 'id' , 'comment_id' ] ;
32
+ const postAllColumns = [ 'id' , 'comment_id' , 'html' , 'mobiledoc' ] ;
15
33
16
34
let localOptions = _ . merge ( {
17
- context : { internal : true }
35
+ context : { internal : true } ,
36
+ migrating : true
18
37
} , options ) ;
19
38
20
39
common . logging . info ( message1 ) ;
21
40
22
41
return models . Post . findAll ( _ . merge ( { columns : postAllColumns } , localOptions ) )
23
42
. then ( function ( posts ) {
24
43
return Promise . map ( posts . models , function ( post ) {
25
- if ( post . get ( 'comment_id' ) ) {
26
- return Promise . resolve ( ) ;
44
+ let mobiledoc ;
45
+ let html ;
46
+
47
+ try {
48
+ mobiledoc = JSON . parse ( post . get ( 'mobiledoc' ) || null ) ;
49
+ } catch ( err ) {
50
+ common . logging . warn ( `Invalid mobiledoc structure for ${ post . id } . Falling back to blank structure.` ) ;
51
+ mobiledoc = converters . mobiledocConverter . blankStructure ( ) ;
52
+ }
53
+
54
+ // CASE: convert all old editor posts to the new editor format
55
+ // CASE: if mobiledoc field is null, we auto set a blank structure in the model layer
56
+ // CASE: if html field is null, we auto generate the html in the model layer
57
+ if ( mobiledoc && post . get ( 'html' ) && post . get ( 'html' ) . match ( / ^ < d i v c l a s s = " k g - c a r d - m a r k d o w n " > / ) ) {
58
+ html = converters . mobiledocConverter . render ( mobiledoc ) ;
27
59
}
28
60
29
61
return models . Post . edit ( {
30
- comment_id : post . id
62
+ comment_id : post . get ( 'comment_id' ) || post . id ,
63
+ html : html || post . get ( 'html' ) ,
64
+ mobiledoc : JSON . stringify ( mobiledoc )
31
65
} , _ . merge ( { id : post . id } , localOptions ) ) ;
32
66
} , { concurrency : 100 } ) ;
33
67
} )
@@ -36,7 +70,38 @@ module.exports.up = (options) => {
36
70
} ) ;
37
71
} ;
38
72
39
- module . exports . down = ( ) => {
40
- common . logging . warn ( message3 ) ;
41
- return Promise . resolve ( ) ;
73
+ module . exports . down = ( options ) => {
74
+ const postAllColumns = [ 'id' , 'html' , 'mobiledoc' ] ;
75
+
76
+ let localOptions = _ . merge ( {
77
+ context : { internal : true } ,
78
+ migrating : true
79
+ } , options ) ;
80
+
81
+ common . logging . info ( message3 ) ;
82
+
83
+ return models . Post . findAll ( _ . merge ( { columns : postAllColumns } , localOptions ) )
84
+ . then ( function ( posts ) {
85
+ return Promise . map ( posts . models , function ( post ) {
86
+ let version = 1 ;
87
+ let html ;
88
+ let mobiledoc = JSON . parse ( post . get ( 'mobiledoc' ) || null ) ;
89
+
90
+ if ( ! mobiledocIsCompatibleWithV1 ( mobiledoc ) ) {
91
+ version = 2 ;
92
+ }
93
+
94
+ // CASE: revert: all new editor posts to the old editor format
95
+ if ( mobiledoc && post . get ( 'html' ) ) {
96
+ html = converters . mobiledocConverter . render ( mobiledoc , version ) ;
97
+ }
98
+
99
+ return models . Post . edit ( {
100
+ html : html || post . get ( 'html' )
101
+ } , _ . merge ( { id : post . id } , localOptions ) ) ;
102
+ } , { concurrency : 100 } ) ;
103
+ } )
104
+ . then ( ( ) => {
105
+ common . logging . info ( message4 ) ;
106
+ } ) ;
42
107
} ;
0 commit comments