Skip to main content

bitwarden_encrypted_migrator/
migration.rs

1//! The encrypted migrations and the order in which they run.
2//!
3//! To add a migration, add its module in `migrations/`, add a variant to [`Migration`], and add the
4//! migration to [`Migration::all`] in the position where it must run.
5
6use bitwarden_core::{Client, FromClient};
7use tracing::{Instrument, info_span};
8
9use crate::migrations::account_recovery_v2_upgrade::AccountRecoveryV2UpgradeMigration;
10
11/// An encrypted migration. A migration does not change data that is already in the current
12/// format, so it can run again.
13// An enum rather than a trait object, because API futures are not `Send` on wasm32.
14pub(crate) enum Migration {
15    AccountRecoveryV2Upgrade(AccountRecoveryV2UpgradeMigration),
16}
17
18impl Migration {
19    /// Every migration, in the order they run.
20    pub(crate) fn all(client: &Client) -> Vec<Migration> {
21        vec![Migration::AccountRecoveryV2Upgrade(
22            AccountRecoveryV2UpgradeMigration::from_client(client),
23        )]
24    }
25
26    pub(crate) fn name(&self) -> &'static str {
27        match self {
28            Migration::AccountRecoveryV2Upgrade(_) => "account_recovery_v2_upgrade",
29        }
30    }
31
32    /// Runs the migration. A migration logs its own failures and is retried on the next run.
33    pub(crate) async fn run(&self) {
34        match self {
35            Migration::AccountRecoveryV2Upgrade(migration) => migration.run().await,
36        }
37    }
38}
39
40/// Runs the migrations one after another, in registration order.
41pub(crate) async fn run_migrations(migrations: &[Migration]) {
42    for migration in migrations {
43        migration
44            .run()
45            .instrument(info_span!("encrypted_migration", name = migration.name()))
46            .await;
47    }
48}