forked from redlink-gmbh/smarti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate-07_0.7.1-to-0.8.0.js
143 lines (136 loc) · 5.07 KB
/
migrate-07_0.7.1-to-0.8.0.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
/*
* Copyright 2018 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 = 6, versionTo = 7;
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() {
// Drop analysis cache
db.getCollection('analysis').drop();
// Configure chatpal-provider for all clients that have a conversationsearch provider enabled
var configuration = db.getCollection('configuration');
var configs = configuration.find({ $and: [
{'config.queryBuilder': { $elemMatch: { type: 'conversationsearch' }}},
{'config.queryBuilder': { $not: { $elemMatch: { type: 'rocketchatsearch' }}}},
]});
configs.forEach(function (c) {
var conversationSearch = c.config.queryBuilder.find(function(b) { return b.type === 'conversationsearch'; });
configuration.update({_id: c._id}, {
$push: {
'config.queryBuilder': {
name : 'rocketchatsearch',
displayName : 'rocketchatsearch',
type : 'rocketchatsearch',
enabled : conversationSearch.enabled,
unbound : false,
configuration : {
excludeCurrentChannel : false,
payload : {
rows : 10
}
}
}
}
});
});
}