mirror of
https://github.com/bitwarden/browser
synced 2025-12-14 23:33:31 +00:00
[PM-6032] Run migrations in main process (#7795)
* Run Migrations in Desktop Main Process * Add `waitForMigrations` method * Add `InitOptions` * Fix Destructuring
This commit is contained in:
@@ -36,6 +36,20 @@ import { EncString } from "../models/domain/enc-string";
|
||||
import { StorageOptions } from "../models/domain/storage-options";
|
||||
import { SymmetricCryptoKey } from "../models/domain/symmetric-crypto-key";
|
||||
|
||||
/**
|
||||
* Options for customizing the initiation behavior.
|
||||
*/
|
||||
export type InitOptions = {
|
||||
/**
|
||||
* Whether or not to run state migrations as part of the init process. Defaults to true.
|
||||
*
|
||||
* If false, the init method will instead wait for migrations to complete before doing its
|
||||
* other init operations. Make sure migrations have either already completed, or will complete
|
||||
* before calling {@link StateService.init} with `runMigrations: false`.
|
||||
*/
|
||||
runMigrations?: boolean;
|
||||
};
|
||||
|
||||
export abstract class StateService<T extends Account = Account> {
|
||||
accounts$: Observable<{ [userId: string]: T }>;
|
||||
activeAccount$: Observable<string>;
|
||||
@@ -44,7 +58,7 @@ export abstract class StateService<T extends Account = Account> {
|
||||
addAccount: (account: T) => Promise<void>;
|
||||
setActiveUser: (userId: string) => Promise<void>;
|
||||
clean: (options?: StorageOptions) => Promise<UserId>;
|
||||
init: () => Promise<void>;
|
||||
init: (initOptions?: InitOptions) => Promise<void>;
|
||||
|
||||
getAccessToken: (options?: StorageOptions) => Promise<string>;
|
||||
setAccessToken: (value: string, options?: StorageOptions) => Promise<void>;
|
||||
|
||||
@@ -16,6 +16,7 @@ import { VaultTimeoutAction } from "../../enums/vault-timeout-action.enum";
|
||||
import { EventData } from "../../models/data/event.data";
|
||||
import { WindowState } from "../../models/domain/window-state";
|
||||
import { migrate } from "../../state-migrations";
|
||||
import { waitForMigrations } from "../../state-migrations/migrate";
|
||||
import { GeneratorOptions } from "../../tools/generator/generator-options";
|
||||
import { GeneratedPasswordHistory, PasswordGeneratorOptions } from "../../tools/generator/password";
|
||||
import { UsernameGeneratorOptions } from "../../tools/generator/username";
|
||||
@@ -33,7 +34,10 @@ import { CollectionView } from "../../vault/models/view/collection.view";
|
||||
import { AddEditCipherInfo } from "../../vault/types/add-edit-cipher-info";
|
||||
import { EnvironmentService } from "../abstractions/environment.service";
|
||||
import { LogService } from "../abstractions/log.service";
|
||||
import { StateService as StateServiceAbstraction } from "../abstractions/state.service";
|
||||
import {
|
||||
InitOptions,
|
||||
StateService as StateServiceAbstraction,
|
||||
} from "../abstractions/state.service";
|
||||
import {
|
||||
AbstractMemoryStorageService,
|
||||
AbstractStorageService,
|
||||
@@ -126,12 +130,20 @@ export class StateService<
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
async init(): Promise<void> {
|
||||
async init(initOptions: InitOptions = {}): Promise<void> {
|
||||
// Deconstruct and apply defaults
|
||||
const { runMigrations = true } = initOptions;
|
||||
if (this.hasBeenInited) {
|
||||
return;
|
||||
}
|
||||
|
||||
await migrate(this.storageService, this.logService);
|
||||
if (runMigrations) {
|
||||
await migrate(this.storageService, this.logService);
|
||||
} else {
|
||||
// It may have been requested to not run the migrations but we should defensively not
|
||||
// continue this method until migrations have a chance to be completed elsewhere.
|
||||
await waitForMigrations(this.storageService, this.logService);
|
||||
}
|
||||
|
||||
await this.state().then(async (state) => {
|
||||
if (state == null) {
|
||||
|
||||
Reference in New Issue
Block a user