-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'hotfix-rc-cli' of https://github.com/bitwarden/clients …
…into hotfix-rc-cli
- Loading branch information
Showing
4 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
libs/common/src/state-migrations/migrations/69-migrate-incorrect-folder-key.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
libs/common/src/state-migrations/migrations/69-migrate-incorrect-folder-key.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters