2020-11-05 18:58:23 +02:00
|
|
|
import shim from '../../../shim';
|
|
|
|
import MigrationHandler from '../MigrationHandler';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Setting from '../../../models/Setting';
|
2021-01-29 20:45:11 +02:00
|
|
|
import { reg } from '../../../registry';
|
2021-08-12 17:54:10 +02:00
|
|
|
const { useEffect, useState } = shim.react();
|
2020-08-02 13:28:50 +02:00
|
|
|
|
|
|
|
export interface SyncTargetUpgradeResult {
|
2020-11-12 21:29:22 +02:00
|
|
|
done: boolean;
|
|
|
|
error: any;
|
2020-08-02 13:28:50 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export default function useSyncTargetUpgrade(): SyncTargetUpgradeResult {
|
2020-11-05 18:58:23 +02:00
|
|
|
const [upgradeResult, setUpgradeResult] = useState({
|
2020-08-02 13:28:50 +02:00
|
|
|
done: false,
|
|
|
|
error: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
async function upgradeSyncTarget() {
|
2020-08-29 20:12:02 +02:00
|
|
|
reg.logger().info('useSyncTargetUpgrade: Starting process...');
|
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
let error = null;
|
|
|
|
try {
|
2020-08-29 20:12:02 +02:00
|
|
|
reg.logger().info('useSyncTargetUpgrade: Acquire synchronizer...');
|
2020-08-02 13:28:50 +02:00
|
|
|
const synchronizer = await reg.syncTarget().synchronizer();
|
|
|
|
|
2020-08-29 20:12:02 +02:00
|
|
|
reg.logger().info('useSyncTargetUpgrade: Create migration handler...');
|
2020-08-02 13:28:50 +02:00
|
|
|
const migrationHandler = new MigrationHandler(
|
|
|
|
synchronizer.api(),
|
2021-08-12 17:54:10 +02:00
|
|
|
reg.db(),
|
2020-08-02 13:28:50 +02:00
|
|
|
synchronizer.lockHandler(),
|
|
|
|
Setting.value('appType'),
|
|
|
|
Setting.value('clientId')
|
|
|
|
);
|
|
|
|
|
2020-08-29 20:12:02 +02:00
|
|
|
reg.logger().info('useSyncTargetUpgrade: Start upgrade...');
|
2020-08-02 13:28:50 +02:00
|
|
|
await migrationHandler.upgrade();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
2020-08-29 20:12:02 +02:00
|
|
|
reg.logger().info('useSyncTargetUpgrade: Error:', error);
|
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
if (!error) {
|
|
|
|
Setting.setValue('sync.upgradeState', Setting.SYNC_UPGRADE_STATE_IDLE);
|
|
|
|
await Setting.saveAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
setUpgradeResult({
|
|
|
|
done: true,
|
|
|
|
error: error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(function() {
|
2020-11-25 16:40:25 +02:00
|
|
|
void upgradeSyncTarget();
|
2020-08-02 13:28:50 +02:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return upgradeResult;
|
|
|
|
}
|