add profileID

This commit is contained in:
fyears 2024-03-17 16:35:02 +08:00
parent d7ff793715
commit c828a341ba
3 changed files with 98 additions and 30 deletions

View File

@ -113,7 +113,8 @@ const fromSyncMappingsToPrevSyncRecords = (
*/ */
const migrateDBsFrom20220326To20240220 = async ( const migrateDBsFrom20220326To20240220 = async (
db: InternalDBs, db: InternalDBs,
vaultRandomID: string vaultRandomID: string,
profileID: string
) => { ) => {
const oldVer = 20220326; const oldVer = 20220326;
const newVer = 20240220; const newVer = 20240220;
@ -123,7 +124,12 @@ const migrateDBsFrom20220326To20240220 = async (
const syncMappings = await getAllSyncMetaMappingByVault(db, vaultRandomID); const syncMappings = await getAllSyncMetaMappingByVault(db, vaultRandomID);
const prevSyncRecords = fromSyncMappingsToPrevSyncRecords(syncMappings); const prevSyncRecords = fromSyncMappingsToPrevSyncRecords(syncMappings);
for (const prevSyncRecord of prevSyncRecords) { for (const prevSyncRecord of prevSyncRecords) {
await upsertPrevSyncRecordByVault(db, vaultRandomID, prevSyncRecord); await upsertPrevSyncRecordByVaultAndProfile(
db,
vaultRandomID,
profileID,
prevSyncRecord
);
} }
// // clear not used data // // clear not used data
@ -140,7 +146,8 @@ const migrateDBs = async (
db: InternalDBs, db: InternalDBs,
oldVer: number, oldVer: number,
newVer: number, newVer: number,
vaultRandomID: string vaultRandomID: string,
profileID: string
) => { ) => {
if (oldVer === newVer) { if (oldVer === newVer) {
return; return;
@ -155,7 +162,7 @@ const migrateDBs = async (
} }
if (oldVer === 20220326 && newVer === 20240220) { if (oldVer === 20220326 && newVer === 20240220) {
return await migrateDBsFrom20220326To20240220(db, vaultRandomID); return await migrateDBsFrom20220326To20240220(db, vaultRandomID, profileID);
} }
if (newVer < oldVer) { if (newVer < oldVer) {
@ -169,7 +176,8 @@ const migrateDBs = async (
export const prepareDBs = async ( export const prepareDBs = async (
vaultBasePath: string, vaultBasePath: string,
vaultRandomIDFromOldConfigFile: string vaultRandomIDFromOldConfigFile: string,
profileID: string
) => { ) => {
const db = { const db = {
versionTbl: localforage.createInstance({ versionTbl: localforage.createInstance({
@ -259,7 +267,8 @@ export const prepareDBs = async (
db, db,
originalVersion, originalVersion,
DEFAULT_DB_VERSION_NUMBER, DEFAULT_DB_VERSION_NUMBER,
vaultRandomID vaultRandomID,
profileID
); );
} }
@ -414,16 +423,17 @@ export const clearExpiredSyncPlanRecords = async (db: InternalDBs) => {
await Promise.all(ps); await Promise.all(ps);
}; };
export const getAllPrevSyncRecordsByVault = async ( export const getAllPrevSyncRecordsByVaultAndProfile = async (
db: InternalDBs, db: InternalDBs,
vaultRandomID: string vaultRandomID: string,
profileID: string
) => { ) => {
// console.debug('inside getAllPrevSyncRecordsByVault') // console.debug('inside getAllPrevSyncRecordsByVaultAndProfile')
const keys = await db.prevSyncRecordsTbl.keys(); const keys = await db.prevSyncRecordsTbl.keys();
// console.debug(`inside getAllPrevSyncRecordsByVault, keys=${keys}`) // console.debug(`inside getAllPrevSyncRecordsByVaultAndProfile, keys=${keys}`)
const res: Entity[] = []; const res: Entity[] = [];
for (const key of keys) { for (const key of keys) {
if (key.startsWith(`${vaultRandomID}\t`)) { if (key.startsWith(`${vaultRandomID}\t${profileID}\t`)) {
const val: Entity | null = await db.prevSyncRecordsTbl.getItem(key); const val: Entity | null = await db.prevSyncRecordsTbl.getItem(key);
if (val !== null) { if (val !== null) {
res.push(val); res.push(val);
@ -433,23 +443,27 @@ export const getAllPrevSyncRecordsByVault = async (
return res; return res;
}; };
export const upsertPrevSyncRecordByVault = async ( export const upsertPrevSyncRecordByVaultAndProfile = async (
db: InternalDBs, db: InternalDBs,
vaultRandomID: string, vaultRandomID: string,
profileID: string,
prevSync: Entity prevSync: Entity
) => { ) => {
await db.prevSyncRecordsTbl.setItem( await db.prevSyncRecordsTbl.setItem(
`${vaultRandomID}\t${prevSync.key}`, `${vaultRandomID}\t${profileID}\t${prevSync.key}`,
prevSync prevSync
); );
}; };
export const clearPrevSyncRecordByVault = async ( export const clearPrevSyncRecordByVaultAndProfile = async (
db: InternalDBs, db: InternalDBs,
vaultRandomID: string, vaultRandomID: string,
profileID: string,
key: string key: string
) => { ) => {
await db.prevSyncRecordsTbl.removeItem(`${vaultRandomID}\t${key}`); await db.prevSyncRecordsTbl.removeItem(
`${vaultRandomID}\t${profileID}\t${key}`
);
}; };
export const clearAllPrevSyncRecordByVault = async ( export const clearAllPrevSyncRecordByVault = async (

View File

@ -33,7 +33,7 @@ import {
clearAllLoggerOutputRecords, clearAllLoggerOutputRecords,
upsertLastSuccessSyncTimeByVault, upsertLastSuccessSyncTimeByVault,
getLastSuccessSyncTimeByVault, getLastSuccessSyncTimeByVault,
getAllPrevSyncRecordsByVault, getAllPrevSyncRecordsByVaultAndProfile,
} from "./localdb"; } from "./localdb";
import { RemoteClient } from "./remote"; import { RemoteClient } from "./remote";
import { import {
@ -149,6 +149,8 @@ export default class RemotelySavePlugin extends Plugin {
return this.i18n.t(x, vars); return this.i18n.t(x, vars);
}; };
const profileID = this.getCurrProfileID();
const getNotice = (x: string, timeout?: number) => { const getNotice = (x: string, timeout?: number) => {
// only show notices in manual mode // only show notices in manual mode
// no notice in auto mode // no notice in auto mode
@ -278,9 +280,10 @@ export default class RemotelySavePlugin extends Plugin {
getNotice(t("syncrun_step5")); getNotice(t("syncrun_step5"));
} }
this.syncStatus = "getting_local_prev_sync"; this.syncStatus = "getting_local_prev_sync";
const prevSyncEntityList = await getAllPrevSyncRecordsByVault( const prevSyncEntityList = await getAllPrevSyncRecordsByVaultAndProfile(
this.db, this.db,
this.vaultRandomID this.vaultRandomID,
profileID
); );
console.debug("prevSyncEntityList:"); console.debug("prevSyncEntityList:");
console.debug(prevSyncEntityList); console.debug(prevSyncEntityList);
@ -330,6 +333,7 @@ export default class RemotelySavePlugin extends Plugin {
mixedEntityMappings, mixedEntityMappings,
client, client,
this.vaultRandomID, this.vaultRandomID,
profileID,
this.app.vault, this.app.vault,
this.settings.password, this.settings.password,
this.settings.concurrency ?? 5, this.settings.concurrency ?? 5,
@ -433,6 +437,9 @@ export default class RemotelySavePlugin extends Plugin {
await this.loadSettings(); await this.loadSettings();
// MUST after loadSettings and before prepareDB
const profileID: string = this.getCurrProfileID();
// lang should be load early, but after settings // lang should be load early, but after settings
this.i18n = new I18n(this.settings.lang!, async (lang: LangTypeAndAuto) => { this.i18n = new I18n(this.settings.lang!, async (lang: LangTypeAndAuto) => {
this.settings.lang = lang; this.settings.lang = lang;
@ -458,7 +465,8 @@ export default class RemotelySavePlugin extends Plugin {
try { try {
await this.prepareDBAndVaultRandomID( await this.prepareDBAndVaultRandomID(
vaultBasePath, vaultBasePath,
vaultRandomIDFromOldConfigFile vaultRandomIDFromOldConfigFile,
profileID
); );
} catch (err: any) { } catch (err: any) {
new Notice( new Notice(
@ -866,6 +874,17 @@ export default class RemotelySavePlugin extends Plugin {
await this.saveData(normalConfigToMessy(this.settings)); await this.saveData(normalConfigToMessy(this.settings));
} }
/**
* After 202403 the data should be of profile based.
*/
getCurrProfileID() {
if (this.settings.serviceType !== undefined) {
return `${this.settings.serviceType}-default-1`;
} else {
throw Error("unknown serviceType in the setting!");
}
}
async checkIfOauthExpires() { async checkIfOauthExpires() {
let needSave: boolean = false; let needSave: boolean = false;
const current = Date.now(); const current = Date.now();
@ -975,11 +994,13 @@ export default class RemotelySavePlugin extends Plugin {
async prepareDBAndVaultRandomID( async prepareDBAndVaultRandomID(
vaultBasePath: string, vaultBasePath: string,
vaultRandomIDFromOldConfigFile: string vaultRandomIDFromOldConfigFile: string,
profileID: string
) { ) {
const { db, vaultRandomID } = await prepareDBs( const { db, vaultRandomID } = await prepareDBs(
vaultBasePath, vaultBasePath,
vaultRandomIDFromOldConfigFile vaultRandomIDFromOldConfigFile,
profileID
); );
this.db = db; this.db = db;
this.vaultRandomID = vaultRandomID; this.vaultRandomID = vaultRandomID;

View File

@ -34,8 +34,8 @@ import { Vault } from "obsidian";
import AggregateError from "aggregate-error"; import AggregateError from "aggregate-error";
import { import {
InternalDBs, InternalDBs,
clearPrevSyncRecordByVault, clearPrevSyncRecordByVaultAndProfile,
upsertPrevSyncRecordByVault, upsertPrevSyncRecordByVaultAndProfile,
} from "./localdb"; } from "./localdb";
export type SyncStatusType = export type SyncStatusType =
@ -872,6 +872,7 @@ const splitThreeStepsOnEntityMappings = (
const dispatchOperationToActualV3 = async ( const dispatchOperationToActualV3 = async (
key: string, key: string,
vaultRandomID: string, vaultRandomID: string,
profileID: string,
r: MixedEntity, r: MixedEntity,
client: RemoteClient, client: RemoteClient,
db: InternalDBs, db: InternalDBs,
@ -887,7 +888,7 @@ const dispatchOperationToActualV3 = async (
// )}` // )}`
// ); // );
if (r.decision === "only_history") { if (r.decision === "only_history") {
clearPrevSyncRecordByVault(db, vaultRandomID, key); clearPrevSyncRecordByVaultAndProfile(db, vaultRandomID, profileID, key);
} else if ( } else if (
r.decision === "equal" || r.decision === "equal" ||
r.decision === "folder_to_skip" || r.decision === "folder_to_skip" ||
@ -921,7 +922,12 @@ const dispatchOperationToActualV3 = async (
); );
await decryptRemoteEntityInplace(entity, password); await decryptRemoteEntityInplace(entity, password);
await fullfillMTimeOfRemoteEntityInplace(entity, mtimeCli); await fullfillMTimeOfRemoteEntityInplace(entity, mtimeCli);
await upsertPrevSyncRecordByVault(db, vaultRandomID, entity); await upsertPrevSyncRecordByVaultAndProfile(
db,
vaultRandomID,
profileID,
entity
);
} }
} else if ( } else if (
r.decision === "modified_remote" || r.decision === "modified_remote" ||
@ -938,15 +944,30 @@ const dispatchOperationToActualV3 = async (
password, password,
r.remote!.keyEnc r.remote!.keyEnc
); );
await upsertPrevSyncRecordByVault(db, vaultRandomID, r.remote!); await upsertPrevSyncRecordByVaultAndProfile(
db,
vaultRandomID,
profileID,
r.remote!
);
} else if (r.decision === "deleted_local") { } else if (r.decision === "deleted_local") {
// local is deleted, we need to delete remote now // local is deleted, we need to delete remote now
await client.deleteFromRemote(r.key, password, r.remote!.keyEnc); await client.deleteFromRemote(r.key, password, r.remote!.keyEnc);
await clearPrevSyncRecordByVault(db, vaultRandomID, r.key); await clearPrevSyncRecordByVaultAndProfile(
db,
vaultRandomID,
profileID,
r.key
);
} else if (r.decision === "deleted_remote") { } else if (r.decision === "deleted_remote") {
// remote is deleted, we need to delete local now // remote is deleted, we need to delete local now
await localDeleteFunc(r.key); await localDeleteFunc(r.key);
await clearPrevSyncRecordByVault(db, vaultRandomID, r.key); await clearPrevSyncRecordByVaultAndProfile(
db,
vaultRandomID,
profileID,
r.key
);
} else if ( } else if (
r.decision === "conflict_created_keep_both" || r.decision === "conflict_created_keep_both" ||
r.decision === "conflict_modified_keep_both" r.decision === "conflict_modified_keep_both"
@ -964,11 +985,21 @@ const dispatchOperationToActualV3 = async (
// we need to decrypt the key!!! // we need to decrypt the key!!!
await decryptRemoteEntityInplace(entity, password); await decryptRemoteEntityInplace(entity, password);
await fullfillMTimeOfRemoteEntityInplace(entity, mtimeCli); await fullfillMTimeOfRemoteEntityInplace(entity, mtimeCli);
await upsertPrevSyncRecordByVault(db, vaultRandomID, entity); await upsertPrevSyncRecordByVaultAndProfile(
db,
vaultRandomID,
profileID,
entity
);
} else if (r.decision === "folder_to_be_deleted") { } else if (r.decision === "folder_to_be_deleted") {
await localDeleteFunc(r.key); await localDeleteFunc(r.key);
await client.deleteFromRemote(r.key, password, r.remote!.keyEnc); await client.deleteFromRemote(r.key, password, r.remote!.keyEnc);
await clearPrevSyncRecordByVault(db, vaultRandomID, r.key); await clearPrevSyncRecordByVaultAndProfile(
db,
vaultRandomID,
profileID,
r.key
);
} else { } else {
throw Error(`don't know how to dispatch decision: ${JSON.stringify(r)}`); throw Error(`don't know how to dispatch decision: ${JSON.stringify(r)}`);
} }
@ -978,6 +1009,7 @@ export const doActualSync = async (
mixedEntityMappings: Record<string, MixedEntity>, mixedEntityMappings: Record<string, MixedEntity>,
client: RemoteClient, client: RemoteClient,
vaultRandomID: string, vaultRandomID: string,
profileID: string,
vault: Vault, vault: Vault,
password: string, password: string,
concurrency: number, concurrency: number,
@ -1043,6 +1075,7 @@ export const doActualSync = async (
await dispatchOperationToActualV3( await dispatchOperationToActualV3(
key, key,
vaultRandomID, vaultRandomID,
profileID,
val, val,
client, client,
db, db,