Skip to content

Commit

Permalink
Refactor upgrade logic and fix darwin version (fix #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
mason1900 authored and umonaca committed Feb 21, 2021
1 parent 81f7d1f commit 4c96f92
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 52 deletions.
48 changes: 1 addition & 47 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const compareVersions = require('compare-versions');
const versionWithoutVerTracking = '0.4.1';
// Before the following version, db path is using the absolute path in databaseFolderDir of config.json
const versionDbRelativePath = '0.5.8';
// Before the following version, there is a hash collision issue in the VA table
const versionVAHashCollision = '0.6.0-rc.2'

let config = {};

Expand Down Expand Up @@ -125,56 +123,13 @@ const updateConfig = () => {
console.log('如需指定其它位置,请阅读0.6.0-rc.0更新说明');
}

if (compareVersions.compare(cfg.version, versionVAHashCollision, '<')) {
console.log('\n');
console.log(' ! 新版解决了旧版扫描时将かの仔和こっこ识别为同一个人的问题');
console.log(' ! 建议进行扫描以自动修复这一问题');
const lockConfig = { fixVA: true };
updateLock.createLockFile(lockConfig);
}

if (countChanged || cfg.version !== pjson.version) {
cfg.version = pjson.version;
setConfig(cfg)
}
}

// Upgrade lock for VA bug fix (maybe needed in the future)
// Note: hosisted
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();

class publicConfig {
get rewindSeekTime() {
return config.rewindSeekTime;
Expand Down Expand Up @@ -207,6 +162,5 @@ if (!fs.existsSync(configPath)) {
}

module.exports = {
setConfig, updateConfig, config, sharedConfigHandle,
updateLock
setConfig, updateConfig, config, sharedConfigHandle, configFolderDir
};
18 changes: 15 additions & 3 deletions database/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const pjson = require('../package.json');
const compareVersions = require('compare-versions');

const { config, updateConfig } = require('../config');
const { applyFix } = require('../upgrade');

// 数据库结构
const createSchema = () => knex.schema
Expand Down Expand Up @@ -120,9 +121,16 @@ const initApp = async () => {

if (databaseExist && compareVersions.compare(currentVersion, configVersion, '>')) {
console.log('升级中');
await fixMigrations();
await runMigrations();
updateConfig();
const oldVersion = config.version;
try {
await applyFix(oldVersion);
await fixMigrations();
await runMigrations();
updateConfig();
} catch (error) {
console.log('升级迁移过程中出错,请在GitHub issues中报告作者')
console.error(error);
}
} else if (!databaseExist) {
await createSchema()
try { // 创建内置的管理员账号
Expand All @@ -142,6 +150,10 @@ const initApp = async () => {
console.error(` ! 在构建数据库结构过程中出错: ${err.message}`);
process.exit(1);
}
if (compareVersions.compare(currentVersion, configVersion, '>')) {
// Update config only. Do not apply fix to database.
updateConfig();
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion filesystem/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const { getFolderList, deleteCoverImageFromDisk, saveCoverImageToDisk } = requir
const { md5 } = require('../auth/utils');
const { nameToUUID } = require('../scraper/utils');

const { config, updateLock } = require('../config');
const { config } = require('../config');
const { updateLock } = require('../upgrade');

// 只有在子进程中 process 对象才有 send() 方法
process.send = process.send || function () {};
Expand Down
3 changes: 2 additions & 1 deletion routes/version.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express');
const router = express.Router();
const { config, updateLock } = require('../config');
const { config } = require('../config');
const { updateLock } = require('../upgrade');
const axios = require('axios');
const pjson = require('../package.json');
const compareVersions = require('compare-versions');
Expand Down
83 changes: 83 additions & 0 deletions upgrade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}

0 comments on commit 4c96f92

Please sign in to comment.