forked from kikoeru-project/kikoeru-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade.js
83 lines (76 loc) · 2.89 KB
/
upgrade.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
const fs = require('fs');
const path = require('path');
const compareVersions = require('compare-versions');
const { configFolderDir } = require('./config');
const knexMigrate = require('./database/knex-migrate');
const { knex } = require('./database/db');
// Before the following version, there is a hash collision issue in the VA table
const versionVAHashCollisionFixed = '0.6.0-rc.2'
// Before the following version, the knexfile path uses relative path to CWD, which causes a bunch of problems on Mac OS
const versionKnexfilePathFixed = '0.6.0-rc.4'
const applyFix = async (oldVersion) => {
if (compareVersions.compare(oldVersion, versionVAHashCollisionFixed, '<')) {
console.log('\n');
console.log(' ! 新版解决了旧版扫描时将かの仔和こっこ识别为同一个人的问题');
console.log(' ! 建议进行扫描以自动修复这一问题');
const lockConfig = { fixVA: true };
updateLock.createLockFile(lockConfig);
}
// A nasty bug in Mac OS version only, >= v0.6.0-rc.0 and <= v0.6.0.rc.3
// Caused by relative path in knexfile.js
// On Mac OS, the current working directory is not the location of the program
// The bug is not obvious on Windows since everyone is double clicking the program
if (compareVersions.compare(oldVersion, versionKnexfilePathFixed, '<')) {
if (process.platform === 'darwin') {
// Skip to v0.6.0-rc.0
await knexMigrate('skipAll', { to: '20210206141840' });
const results = await knex.raw('PRAGMA table_info(\'t_va\')');
if (results[0]['type'] === 'integer') {
// Fill VA ids, migrate to v0.6.0-rc.3
const log = ({ action, migration }) => console.log('Doing ' + action + ' on ' + migration);
await knexMigrate('up', { to: '20210213233544' }, log);
} else {
// Already fixed VA ids, skip to v0.6.0-rc.3
await knexMigrate('skipAll', { to: '20210213233544' });
}
}
}
}
// Upgrade lock for VA bug fix (maybe needed in the future)
// TODO: refactor to split upgrade lock from upgrade lock file
class upgradeLock {
constructor(fileName = 'update.lock') {
this.lockFileConfig = {}
this.lockFilePath = path.join(configFolderDir, fileName);
this._init();
}
_init() {
if (this.isLockFilePresent) {
this.readLockFileConfig();
}
}
get isLockFilePresent() {
return fs.existsSync(this.lockFilePath);
}
readLockFileConfig() {
this.lockFileConfig = JSON.parse(fs.readFileSync(this.lockFilePath));
}
createLockFile(lockConfig) {
this.lockFileConfig = lockConfig;
fs.writeFileSync(this.lockFilePath, JSON.stringify(this.lockFileConfig, null, "\t"));
}
updateLockFile(lockConfig) {
this.createLockFile(lockConfig);
}
removeLockFile() {
if (this.isLockFilePresent) {
fs.unlinkSync(this.lockFilePath);
}
this.lockFileConfig = {};
}
}
const updateLock = new upgradeLock();
module.exports = {
applyFix,
updateLock
}