1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 07:43:35 +00:00

Move lastSync State (#10272)

This commit is contained in:
Justin Baur
2024-08-06 15:01:42 -04:00
committed by GitHub
parent 887b98988a
commit dcb21c2685
22 changed files with 198 additions and 110 deletions

View File

@@ -64,13 +64,15 @@ import { PasswordOptionsMigrator } from "./migrations/63-migrate-password-settin
import { GeneratorHistoryMigrator } from "./migrations/64-migrate-generator-history";
import { ForwarderOptionsMigrator } from "./migrations/65-migrate-forwarder-settings";
import { MoveFinalDesktopSettingsMigrator } from "./migrations/66-move-final-desktop-settings";
import { RemoveUnassignedItemsBannerDismissed } from "./migrations/67-remove-unassigned-items-banner-dismissed";
import { MoveLastSyncDate } from "./migrations/68-move-last-sync-date";
import { MoveBiometricAutoPromptToAccount } from "./migrations/7-move-biometric-auto-prompt-to-account";
import { MoveStateVersionMigrator } from "./migrations/8-move-state-version";
import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-settings-to-global";
import { MinVersionMigrator } from "./migrations/min-version";
export const MIN_VERSION = 3;
export const CURRENT_VERSION = 66;
export const CURRENT_VERSION = 68;
export type MinVersion = typeof MIN_VERSION;
export function createMigrationBuilder() {
@@ -138,7 +140,9 @@ export function createMigrationBuilder() {
.with(PasswordOptionsMigrator, 62, 63)
.with(GeneratorHistoryMigrator, 63, 64)
.with(ForwarderOptionsMigrator, 64, 65)
.with(MoveFinalDesktopSettingsMigrator, 65, CURRENT_VERSION);
.with(MoveFinalDesktopSettingsMigrator, 65, 66)
.with(RemoveUnassignedItemsBannerDismissed, 66, 67)
.with(MoveLastSyncDate, 67, CURRENT_VERSION);
}
export async function currentVersion(

View File

@@ -0,0 +1,91 @@
import { runMigrator } from "../migration-helper.spec";
import { MoveLastSyncDate } from "./68-move-last-sync-date";
describe("MoveLastSyncDate", () => {
const sut = new MoveLastSyncDate(67, 68);
it("migrates data", async () => {
const output = await runMigrator(sut, {
global_account_accounts: {
user1: null,
user2: null,
user3: null,
user4: null,
user5: null,
},
user1: {
profile: {
lastSync: "2024-07-24T14:27:25.703Z",
},
},
user2: {},
user3: { profile: null },
user4: { profile: {} },
user5: { profile: { lastSync: null } },
});
expect(output).toEqual({
global_account_accounts: {
user1: null,
user2: null,
user3: null,
user4: null,
user5: null,
},
user1: {
profile: {},
},
user2: {},
user3: { profile: null },
user4: { profile: {} },
user5: { profile: { lastSync: null } },
user_user1_sync_lastSync: "2024-07-24T14:27:25.703Z",
});
});
it("rolls back data", async () => {
const output = await runMigrator(
sut,
{
global_account_accounts: {
user1: null,
user2: null,
user3: null,
user4: null,
user5: null,
},
user1: {
profile: {
extraProperty: "hello",
},
},
user2: {},
user3: { profile: null },
user4: { profile: {} },
user5: { profile: { lastSync: null } },
user_user1_sync_lastSync: "2024-07-24T14:27:25.703Z",
},
"rollback",
);
expect(output).toEqual({
global_account_accounts: {
user1: null,
user2: null,
user3: null,
user4: null,
user5: null,
},
user1: {
profile: {
lastSync: "2024-07-24T14:27:25.703Z",
extraProperty: "hello",
},
},
user2: {},
user3: { profile: null },
user4: { profile: {} },
user5: { profile: { lastSync: null } },
});
});
});

View File

@@ -0,0 +1,49 @@
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
import { Migrator } from "../migrator";
type ExpectedAccount = {
profile?: {
lastSync?: string;
};
};
const LAST_SYNC_KEY: KeyDefinitionLike = {
key: "lastSync",
stateDefinition: {
name: "sync",
},
};
export class MoveLastSyncDate extends Migrator<67, 68> {
async migrate(helper: MigrationHelper): Promise<void> {
async function migrateAccount(userId: string, account: ExpectedAccount) {
const value = account?.profile?.lastSync;
if (value != null) {
await helper.setToUser(userId, LAST_SYNC_KEY, value);
delete account.profile.lastSync;
await helper.set(userId, account);
}
}
const accounts = await helper.getAccounts<ExpectedAccount>();
await Promise.all(accounts.map(({ userId, account }) => migrateAccount(userId, account)));
}
async rollback(helper: MigrationHelper): Promise<void> {
async function rollbackAccount(userId: string, account: ExpectedAccount) {
const value = await helper.getFromUser<string>(userId, LAST_SYNC_KEY);
if (value != null) {
account ??= {};
account.profile ??= {};
account.profile.lastSync = value;
await helper.set(userId, account);
await helper.removeFromUser(userId, LAST_SYNC_KEY);
}
}
const accounts = await helper.getAccounts<ExpectedAccount>();
await Promise.all(accounts.map(({ userId, account }) => rollbackAccount(userId, account)));
}
}