File tree 5 files changed +172
-25
lines changed
packages/models/migrations
5 files changed +172
-25
lines changed Original file line number Diff line number Diff line change 3
3
4
4
const models = require ( "../../../models" ) ;
5
5
const { mongoose, connect } = models . db ;
6
- const moment = require ( "moment" ) ;
7
6
8
7
const { Box } = models ;
9
8
@@ -16,7 +15,7 @@ const migrate = function migrate() {
16
15
. find ( { } )
17
16
. next ( )
18
17
. then ( function ( latestVersion ) {
19
- if ( latestVersion . schemaVersion !== 8 ) {
18
+ if ( latestVersion . schemaVersion !== 10 ) {
20
19
throw new Error ( "Unexpected schema version... Exiting!" ) ;
21
20
}
22
21
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ /* eslint-disable */
2
+ "use strict" ;
3
+
4
+ const models = require ( "../../../models" ) ;
5
+ const { mongoose, connect } = models . db ;
6
+ const { Box } = models ;
7
+ const crypto = require ( 'crypto' )
8
+
9
+ const migrate = function migrate ( ) {
10
+ const schemaVersion = mongoose . connection . db . collection ( "schemaVersion" ) ;
11
+
12
+ console . log ( 'Starting "update access token" migration' ) ;
13
+
14
+ return schemaVersion
15
+ . find ( { } )
16
+ . next ( )
17
+ . then ( function ( latestVersion ) {
18
+ if ( latestVersion . schemaVersion !== 7 ) {
19
+ throw new Error ( "Unexpected schema version... Exiting!" ) ;
20
+ }
21
+
22
+ return Box . find ( { access_token : { $exists : false } } ) . exec ( ) ;
23
+ } )
24
+ . then ( function ( boxes ) {
25
+ const promises = [ ] ;
26
+ for ( let index = 0 ; index < boxes . length ; index ++ ) {
27
+ const box = boxes [ index ] ;
28
+
29
+ box . set ( "access_token" , crypto . randomBytes ( 32 ) . toString ( 'hex' ) ) ;
30
+ promises . push ( box . save ( ) ) ;
31
+ }
32
+
33
+ return Promise . all ( promises ) . then ( function ( ) {
34
+ console . log ( "Migration done!" ) ;
35
+
36
+ return schemaVersion . update ( { } , { $inc : { schemaVersion : 1 } } ) ;
37
+ } ) ;
38
+ } ) ;
39
+ } ;
40
+
41
+ // Connect to db and run migration
42
+ connect ( )
43
+ . then ( function ( ) {
44
+ migrate ( )
45
+ . then ( function ( ) {
46
+ mongoose . disconnect ( ) ;
47
+ } )
48
+ . catch ( function ( err ) {
49
+ console . log ( err ) ;
50
+ mongoose . disconnect ( ) ;
51
+ } ) ;
52
+ } )
53
+ . catch ( function ( err ) {
54
+ console . log ( err ) ;
55
+ } ) ;
Original file line number Diff line number Diff line change
1
+ /* eslint-disable */
2
+ "use strict" ;
3
+
4
+ const models = require ( "../../../models" ) ;
5
+ const { mongoose, connect } = models . db ;
6
+ const { Box } = models ;
7
+
8
+ const migrate = function migrate ( ) {
9
+ const schemaVersion = mongoose . connection . db . collection ( "schemaVersion" ) ;
10
+
11
+ console . log ( 'Starting "fix corrupt sensors" migration' ) ;
12
+
13
+ return schemaVersion
14
+ . find ( { } )
15
+ . next ( )
16
+ . then ( function ( latestVersion ) {
17
+ if ( latestVersion . schemaVersion !== 8 ) {
18
+ throw new Error ( "Unexpected schema version... Exiting!" ) ;
19
+ }
20
+
21
+ return Box . find ( { "sensors" : { $elemMatch : { title : { $exists : false } } } } ) . exec ( ) ;
22
+ } )
23
+ . then ( function ( boxes ) {
24
+ console . log ( "Found boxes: " , boxes . length )
25
+ const promises = [ ] ;
26
+ for ( let index = 0 ; index < boxes . length ; index ++ ) {
27
+ const box = boxes [ index ] ;
28
+
29
+ let sensors = box . sensors . filter ( sensor => {
30
+ if ( sensor . title && sensor . unit ) {
31
+ return true ;
32
+ } else {
33
+ return false ;
34
+ }
35
+ } )
36
+ box . set ( "sensors" , sensors ) ;
37
+ promises . push ( box . save ( ) ) ;
38
+ }
39
+
40
+ return Promise . all ( promises ) . then ( function ( ) {
41
+ console . log ( "Migration done!" ) ;
42
+
43
+ return schemaVersion . update ( { } , { $inc : { schemaVersion : 1 } } ) ;
44
+ } ) ;
45
+ } ) ;
46
+ } ;
47
+
48
+ // Connect to db and run migration
49
+ connect ( )
50
+ . then ( function ( ) {
51
+ migrate ( )
52
+ . then ( function ( ) {
53
+ mongoose . disconnect ( ) ;
54
+ } )
55
+ . catch ( function ( err ) {
56
+ console . log ( err ) ;
57
+ mongoose . disconnect ( ) ;
58
+ } ) ;
59
+ } )
60
+ . catch ( function ( err ) {
61
+ console . log ( err ) ;
62
+ } ) ;
Original file line number Diff line number Diff line change
1
+ /* eslint-disable */
2
+ "use strict" ;
3
+
4
+ const models = require ( "../../../models" ) ;
5
+ const { mongoose, connect } = models . db ;
6
+ const { Box } = models ;
7
+
8
+ const migrate = function migrate ( ) {
9
+ const schemaVersion = mongoose . connection . db . collection ( "schemaVersion" ) ;
10
+
11
+ console . log ( 'Starting "update useAuth" migration' ) ;
12
+
13
+ return schemaVersion
14
+ . find ( { } )
15
+ . next ( )
16
+ . then ( function ( latestVersion ) {
17
+ if ( latestVersion . schemaVersion !== 9 ) {
18
+ throw new Error ( "Unexpected schema version... Exiting!" ) ;
19
+ }
20
+
21
+ return Box . find ( { useAuth : { $exists : false } } ) . exec ( ) ;
22
+ } )
23
+ . then ( function ( boxes ) {
24
+ const promises = [ ] ;
25
+ for ( let index = 0 ; index < boxes . length ; index ++ ) {
26
+ const box = boxes [ index ] ;
27
+
28
+ box . set ( "useAuth" , false ) ;
29
+ promises . push ( box . save ( ) ) ;
30
+ }
31
+
32
+ return Promise . all ( promises ) . then ( function ( ) {
33
+ console . log ( "Migration done!" ) ;
34
+
35
+ return schemaVersion . update ( { } , { $inc : { schemaVersion : 1 } } ) ;
36
+ } ) ;
37
+ } ) ;
38
+ } ;
39
+
40
+ // Connect to db and run migration
41
+ connect ( )
42
+ . then ( function ( ) {
43
+ migrate ( )
44
+ . then ( function ( ) {
45
+ mongoose . disconnect ( ) ;
46
+ } )
47
+ . catch ( function ( err ) {
48
+ console . log ( err ) ;
49
+ mongoose . disconnect ( ) ;
50
+ } ) ;
51
+ } )
52
+ . catch ( function ( err ) {
53
+ console . log ( err ) ;
54
+ } ) ;
You can’t perform that action at this time.
0 commit comments