1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 17:23:37 +00:00

PM-3585 Improve state migrations (#5009)

* WIP: safer state migrations

Co-authored-by: Justin Baur <justindbaur@users.noreply.github.com>

* Add min version check and remove old migrations

Co-authored-by: Oscar Hinton <Hinton@users.noreply.github.com>

* Add rollback and version checking

* Add state version move migration

* Expand tests and improve typing for Migrations

* Remove StateMigration Service

* Rewrite version 5 and 6 migrations

* Add all but initial migration to supported migrations

* Handle stateVersion location in migrator update versions

* Move to unique migrations directory

* Disallow imports outside of state-migrations

* Lint and test fixes

* Do not run migrations if we cannot determine state

* Fix desktop background StateService build

* Document Migration builder class

* Add debug logging to migrations

* Comment on migrator overrides

* Use specific property names

* `npm run prettier` 🤖

* Insert new migration

* Set stateVersion when creating new globals object

* PR comments

* Fix migrate imports

* Move migration building into `migrate` function

* Export current version from migration definitions

* Move file version concerns to migrator

* Update migrate spec to reflect new version requirements

* Fix import paths

* Prefer unique state data

* Remove unnecessary async

* Prefer to not use `any`

---------

Co-authored-by: Justin Baur <justindbaur@users.noreply.github.com>
Co-authored-by: Oscar Hinton <Hinton@users.noreply.github.com>
This commit is contained in:
Matt Gibson
2023-08-30 12:57:20 -05:00
committed by GitHub
parent b444eed0b5
commit 3340af8084
51 changed files with 1538 additions and 980 deletions

View File

@@ -0,0 +1,90 @@
import { MockProxy } from "jest-mock-extended";
import { MigrationHelper } from "../migration-helper";
import { mockMigrationHelper } from "../migration-helper.spec";
import { MoveStateVersionMigrator } from "./8-move-state-version";
function migrateExampleJSON() {
return {
global: {
stateVersion: 6,
otherStuff: "otherStuff1",
},
otherStuff: "otherStuff2",
};
}
function rollbackExampleJSON() {
return {
global: {
otherStuff: "otherStuff1",
},
stateVersion: 7,
otherStuff: "otherStuff2",
};
}
describe("moveStateVersion", () => {
let helper: MockProxy<MigrationHelper>;
let sut: MoveStateVersionMigrator;
describe("migrate", () => {
beforeEach(() => {
helper = mockMigrationHelper(migrateExampleJSON());
sut = new MoveStateVersionMigrator(7, 8);
});
it("should move state version to root", async () => {
await sut.migrate(helper);
expect(helper.set).toHaveBeenCalledWith("stateVersion", 6);
});
it("should remove state version from global", async () => {
await sut.migrate(helper);
expect(helper.set).toHaveBeenCalledWith("global", {
otherStuff: "otherStuff1",
});
});
it("should throw if state version not found", async () => {
helper.get.mockReturnValue({ otherStuff: "otherStuff1" } as any);
await expect(sut.migrate(helper)).rejects.toThrow(
"Migration failed, state version not found"
);
});
it("should update version up", async () => {
await sut.updateVersion(helper, "up");
expect(helper.set).toHaveBeenCalledTimes(1);
expect(helper.set).toHaveBeenCalledWith("stateVersion", 8);
});
});
describe("rollback", () => {
beforeEach(() => {
helper = mockMigrationHelper(rollbackExampleJSON());
sut = new MoveStateVersionMigrator(7, 8);
});
it("should move state version to global", async () => {
await sut.rollback(helper);
expect(helper.set).toHaveBeenCalledWith("global", {
stateVersion: 7,
otherStuff: "otherStuff1",
});
expect(helper.set).toHaveBeenCalledWith("stateVersion", undefined);
});
it("should update version down", async () => {
await sut.updateVersion(helper, "down");
expect(helper.set).toHaveBeenCalledTimes(1);
expect(helper.set).toHaveBeenCalledWith("global", {
stateVersion: 7,
otherStuff: "otherStuff1",
});
});
});
});