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

[PM-17541] Fix folder service key definition (#13060)

* [PM-17541] Switch folder key definition back to "folders" and add migration script for users that have switched to the incorrect key

* [PM-17541] Fix import path

* [PM-17541] Fix implicit any in spec file
This commit is contained in:
Shane Melton
2025-01-24 10:12:52 -08:00
committed by GitHub
parent b396dda406
commit b1744c4e0a
4 changed files with 148 additions and 3 deletions

View File

@@ -0,0 +1,98 @@
import { runMigrator } from "../migration-helper.spec";
import { MigrateIncorrectFolderKey } from "./69-migrate-incorrect-folder-key";
function exampleJSON() {
return {
global_account_accounts: {
user1: null as any,
user2: null as any,
},
user_user1_folder_folder: {
// Incorrect "folder" key
folderId1: {
id: "folderId1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
folderId2: {
id: "folderId2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
user_user2_folder_folder: null as any,
};
}
describe("MigrateIncorrectFolderKey", () => {
const sut = new MigrateIncorrectFolderKey(68, 69);
it("migrates data", async () => {
const output = await runMigrator(sut, exampleJSON());
expect(output).toEqual({
global_account_accounts: {
user1: null,
user2: null,
},
user_user1_folder_folders: {
// Correct "folders" key
folderId1: {
id: "folderId1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
folderId2: {
id: "folderId2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
});
});
it("rolls back data", async () => {
const output = await runMigrator(
sut,
{
global_account_accounts: {
user1: null,
user2: null,
},
user_user1_folder_folders: {
folderId1: {
id: "folderId1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
folderId2: {
id: "folderId2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
},
"rollback",
);
expect(output).toEqual({
global_account_accounts: {
user1: null,
user2: null,
},
user_user1_folder_folder: {
// Incorrect "folder" key
folderId1: {
id: "folderId1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
folderId2: {
id: "folderId2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
});
});
});

View File

@@ -0,0 +1,45 @@
import {
KeyDefinitionLike,
MigrationHelper,
} from "@bitwarden/common/state-migrations/migration-helper";
import { Migrator } from "@bitwarden/common/state-migrations/migrator";
const BAD_FOLDER_KEY: KeyDefinitionLike = {
key: "folder", // We inadvertently changed the key from "folders" to "folder"
stateDefinition: {
name: "folder",
},
};
const GOOD_FOLDER_KEY: KeyDefinitionLike = {
key: "folders", // We should keep the key as "folders"
stateDefinition: {
name: "folder",
},
};
export class MigrateIncorrectFolderKey extends Migrator<68, 69> {
async migrate(helper: MigrationHelper): Promise<void> {
async function migrateUser(userId: string) {
const value = await helper.getFromUser(userId, BAD_FOLDER_KEY);
if (value != null) {
await helper.setToUser(userId, GOOD_FOLDER_KEY, value);
}
await helper.removeFromUser(userId, BAD_FOLDER_KEY);
}
const users = await helper.getKnownUserIds();
await Promise.all(users.map((userId) => migrateUser(userId)));
}
async rollback(helper: MigrationHelper): Promise<void> {
async function rollbackUser(userId: string) {
const value = await helper.getFromUser(userId, GOOD_FOLDER_KEY);
if (value != null) {
await helper.setToUser(userId, BAD_FOLDER_KEY, value);
}
await helper.removeFromUser(userId, GOOD_FOLDER_KEY);
}
const users = await helper.getKnownUserIds();
await Promise.all(users.map((userId) => rollbackUser(userId)));
}
}