Skip to content

Commit

Permalink
runfix: run only upgrades above archive version number
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrykBuniX committed Dec 19, 2023
1 parent 101bc4b commit 4bbd8ef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
16 changes: 10 additions & 6 deletions src/script/backup/BackupRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export class BackupRepository {
throw new InvalidMetaDataError();
}

await this.verifyMetadata(user, files);
const archiveVersion = await this.verifyMetadata(user, files);
const fileDescriptors = Object.entries(files)
.filter(([filename]) => filename !== Filename.METADATA)
.map(([filename, content]) => {
Expand All @@ -326,7 +326,7 @@ export class BackupRepository {
const nbEntities = fileDescriptors.reduce((acc, {entities}) => acc + entities.length, 0);
initCallback(nbEntities);

await this.importHistoryData(fileDescriptors, progressCallback);
await this.importHistoryData(archiveVersion, fileDescriptors, progressCallback);
}

private async createDecryptedBackup(
Expand Down Expand Up @@ -357,6 +357,7 @@ export class BackupRepository {
}

private async importHistoryData(
archiveVersion: number,
fileDescriptors: FileDescriptor[],
progressCallback: ProgressCallback,
): Promise<void> {
Expand All @@ -378,7 +379,7 @@ export class BackupRepository {
}

// Run all the database migrations on the imported data
await this.backupService.runDbSchemaUpdates();
await this.backupService.runDbSchemaUpdates(archiveVersion);

await this.conversationRepository.updateConversations(importedConversations);
await this.conversationRepository.initAllLocal1To1Conversations();
Expand Down Expand Up @@ -477,15 +478,16 @@ export class BackupRepository {
return omit(entity, 'primary_key');
}

private async verifyMetadata(user: User, files: Record<string, Uint8Array>): Promise<void> {
private async verifyMetadata(user: User, files: Record<string, Uint8Array>): Promise<number> {
const rawData = files[Filename.METADATA];
const metaData = new TextDecoder().decode(rawData);
const parsedMetaData = JSON.parse(metaData);
this._verifyMetadata(user, parsedMetaData);
const archiveVersion = this._verifyMetadata(user, parsedMetaData);
this.logger.log('Validated metadata during history import', files);
return archiveVersion;
}

private _verifyMetadata(user: User, archiveMetadata: Metadata): void {
private _verifyMetadata(user: User, archiveMetadata: Metadata): number {
const localMetadata = this.createMetaData(user, '');
const isExpectedUserId = archiveMetadata.user_id === localMetadata.user_id;
if (!isExpectedUserId) {
Expand All @@ -500,6 +502,8 @@ export class BackupRepository {
const message = `History created from "${archiveMetadata.platform}" device cannot be imported`;
throw new IncompatiblePlatformError(message);
}

return archiveMetadata.version;
}

private mapDecodingError(decodingError: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/script/backup/BackupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ export class BackupService {
] as const;
}

async runDbSchemaUpdates(): Promise<void> {
async runDbSchemaUpdates(archiveVersion: number): Promise<void> {
const {db} = this.storageService;
if (!db) {
this.logger.warn('Database schema will not run because the database is not initialized');
return;
}
return db.runDbSchemaUpdates();
return db.runDbSchemaUpdates(archiveVersion);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/script/storage/DexieDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ export class DexieDatabase extends Dexie {
});
};

public readonly runDbSchemaUpdates = async (): Promise<void> => {
public readonly runDbSchemaUpdates = async (archiveVersion: number): Promise<void> => {
for (const {upgrade, version} of StorageSchemata.SCHEMATA) {
if (upgrade) {
// If the archive version is greater than the current version, run the upgrade
if (upgrade && version > archiveVersion) {
await this.transaction('rw', this.tables, transaction => {
this.logger.info(`Running DB schema update for version '${version}'`);
upgrade(transaction, this);
Expand Down

0 comments on commit 4bbd8ef

Please sign in to comment.