Skip to content

Commit e7873ba

Browse files
mpfeilUmut Tas
and
Umut Tas
authored
update migrations for useAuth, access_token and grouptags (#563)
* update migrations for useAuth, access_token and grouptags * rename files * add migration to remove corrupted sensors, fix numbers Co-authored-by: Umut Tas <[email protected]>
1 parent 340df3c commit e7873ba

File tree

5 files changed

+172
-25
lines changed

5 files changed

+172
-25
lines changed

packages/models/migrations/8-9_box_grouptag/8-node-update-grouptag.js renamed to packages/models/migrations/10-11_box_grouptag/10-node-update-grouptag.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
const models = require("../../../models");
55
const { mongoose, connect } = models.db;
6-
const moment = require("moment");
76

87
const { Box } = models;
98

@@ -16,7 +15,7 @@ const migrate = function migrate() {
1615
.find({})
1716
.next()
1817
.then(function (latestVersion) {
19-
if (latestVersion.schemaVersion !== 8) {
18+
if (latestVersion.schemaVersion !== 10) {
2019
throw new Error("Unexpected schema version... Exiting!");
2120
}
2221

packages/models/migrations/7-8_box_access_token/7-mongoshell-boxes-add-field.js

-23
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
});

0 commit comments

Comments
 (0)