forked from redlink-gmbh/smarti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate-06_0.7.0-to-0.7.1.js
114 lines (109 loc) · 3.91 KB
/
migrate-06_0.7.0-to-0.7.1.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
/*
* 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 = 5, versionTo = 6;
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() {
db.getCollection('analysis').drop();
}