forked from redlink-gmbh/smarti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate-03_0.5-to-0.6.js
147 lines (139 loc) · 4.92 KB
/
migrate-03_0.5-to-0.6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright 2017 Redlink GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
var smarti = db.getCollection('smarti'),
versionFrom = 2, versionTo = 3;
while (true) {
var dbVersion = smarti.findOne({_id: 'db-version'});
if (dbVersion === null) {
print('No db-version found, are you running the migration scripts in the right order?');
break;
} else if (dbVersion.isTainted) {
print('Refuse to upgrade tainted database!');
break;
} else if (dbVersion.isUpgrading) {
print('Upgrade in progress, sleeping for 250ms');
sleep(250);
continue;
} else if (dbVersion.version !== versionFrom) {
if (dbVersion.version > versionFrom) {
print('Database already migrated, Skipping');
} else {
print('Can only migrate database.version ' + versionFrom + ', found ' + dbVersion.version);
}
break;
}
var lock = smarti.update({_id: 'db-version', version: versionFrom, isUpgrading: false, isTainted: false}, {
$set: { isUpgrading: true }
}, { upsert: true });
if (lock.nModified !== 1) {
print('Could not acquire lock, trying again.');
continue;
}
// Lock acquired
var start = new ISODate();
// Backup the database.
dbVersion = smarti.findOne({_id: 'db-version'});
if (!dbVersion.backup) {
var now = new Date(),
backupDB = db.getName() + '_' + now.getTime();
db.copyDatabase(db.getName(), backupDB);
print('Created Backup ' + backupDB + ' before starting migration');
db.getSiblingDB(backupDB)
.getCollection('smarti')
.update({_id: 'db-version'}, { $set: { isUpgrading: false }});
smarti.update({_id: 'db-version'}, {
$set: {
backup: {
name: backupDB,
version: dbVersion.version,
date: now
}
}
});
} else {
print('Backup already exists: ' + dbVersion.backup.name + ' (' + dbVersion.backup.date + ')');
}
try {
print('Starting Migration ' + versionFrom + ' --> ' + versionTo);
var result = runDatabaseMigration();
print('Completed Migration ' + versionFrom + ' --> ' + versionTo);
smarti.update({_id: 'db-version'}, {
$set: {
version: versionTo
},
$addToSet: {
history: { version: versionTo, start: start, complete: new ISODate(), result: result, success: true }
}
});
} catch (err) {
print('Error during Migration: ' + err);
smarti.update({_id: 'db-version'}, {
$set: { isTainted: true },
$addToSet: {
history: { version: versionTo, start: start, complete: new ISODate(), result: err, success: false }
}
});
dbVersion = smarti.findOne({_id: 'db-version'});
if (dbVersion.backup && dbVersion.backup.name) {
print('Restore database backup: ' + dbVersion.backup.name);
db.dropDatabase();
db.copyDatabase(dbVersion.backup.name, db.getName());
}
break;
} finally {
// release lock
smarti.update({_id: 'db-version'}, {
$set: {
isUpgrading: false
}
});
}
break;
}
function runDatabaseMigration() {
var conversations = db.getCollection('conversations');
// migrate channel_id and support_area to meta.properties (#87, #99)
conversations
.find({
'meta.properties': {$exists: false}
})
.forEach(function (c) {
var metaProps = {};
if (c.meta && c.meta.tags) {
metaProps.tags = c.meta.tags;
}
if (c.context && c.context.environment) {
if (c.context.environment.channel_id) {
metaProps.channel_id = [c.context.environment.channel_id];
}
if (c.context.environment.support_area) {
metaProps.support_area = [c.context.environment.support_area];
}
}
conversations.update({
_id: c._id
}, {
$set: {
'meta.properties': metaProps
},
$unset: {
'meta.tags': true
}
});
});
return 'success';
}