2020-08-02 13:28:50 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import MigrationHandler from 'lib/services/synchronizer/MigrationHandler';
|
2020-10-09 19:35:46 +02:00
|
|
|
const Setting = require('lib/models/Setting').default;
|
2020-08-02 13:28:50 +02:00
|
|
|
const { reg } = require('lib/registry');
|
|
|
|
|
|
|
|
export interface SyncTargetUpgradeResult {
|
|
|
|
done: boolean,
|
|
|
|
error: any,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function useSyncTargetUpgrade():SyncTargetUpgradeResult {
|
|
|
|
const [upgradeResult, setUpgradeResult] = useState<SyncTargetUpgradeResult>({
|
|
|
|
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;
|
|
|
|
}
|