2020-11-05 18:58:23 +02:00
|
|
|
import shim from '../../../shim';
|
|
|
|
import MigrationHandler from '../MigrationHandler';
|
|
|
|
const { useEffect, useState } = shim.react();
|
|
|
|
const Setting = require('../../../models/Setting').default;
|
|
|
|
const { reg } = require('../../../registry');
|
2020-08-02 13:28:50 +02:00
|
|
|
|
|
|
|
export interface SyncTargetUpgradeResult {
|
|
|
|
done: boolean,
|
|
|
|
error: any,
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
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() {
|
|
|
|
upgradeSyncTarget();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return upgradeResult;
|
|
|
|
}
|