Skip to content

Commit

Permalink
Mint Red DB: extract migrate() logic to own function
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Feb 6, 2025
1 parent 0f651b4 commit 90d853c
Showing 1 changed file with 88 additions and 84 deletions.
172 changes: 88 additions & 84 deletions crates/cdk-redb/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,105 +57,109 @@ impl MintRedbDatabase {
pub fn new(work_dir: &Path, _backups_to_keep: u8) -> Result<Self, Error> {
let db_file_path = work_dir.join("cdk-mintd.redb");

{
let db = Arc::new(Database::create(&db_file_path)?);

// Check database version
let read_txn = db.begin_read()?;
let table = read_txn.open_table(CONFIG_TABLE);

let db_version = match table {
Ok(table) => table.get("db_version")?.map(|v| v.value().to_owned()),
Err(_) => None,
};
match db_version {
Some(db_version) => {
let mut current_file_version = u32::from_str(&db_version)?;
match current_file_version.cmp(&DATABASE_VERSION) {
Ordering::Less => {
tracing::info!(
"Database needs to be upgraded at {} current is {}",
current_file_version,
DATABASE_VERSION
);
if current_file_version == 0 {
current_file_version = migrate_00_to_01(Arc::clone(&db))?;
}

if current_file_version == 1 {
current_file_version = migrate_01_to_02(Arc::clone(&db))?;
}

if current_file_version == 2 {
current_file_version = migrate_02_to_03(Arc::clone(&db))?;
}

if current_file_version == 3 {
current_file_version = migrate_03_to_04(Arc::clone(&db))?;
}

if current_file_version == 4 {
current_file_version = migrate_04_to_05(Arc::clone(&db))?;
}

if current_file_version != DATABASE_VERSION {
tracing::warn!(
"Database upgrade did not complete at {} current is {}",
current_file_version,
DATABASE_VERSION
);
return Err(Error::UnknownDatabaseVersion);
}

let write_txn = db.begin_write()?;
{
let mut table = write_txn.open_table(CONFIG_TABLE)?;

table
.insert("db_version", DATABASE_VERSION.to_string().as_str())?;
}

write_txn.commit()?;
let db_inner = Arc::new(Database::create(db_file_path)?);
let redb_db = Self {
db: db_inner.clone(),
};

Self::migrate(db_inner.clone())?;

Ok(redb_db)
}

fn migrate(db: Arc<Database>) -> Result<(), Error> {
// Check database version
let read_txn = db.begin_read()?;
let table = read_txn.open_table(CONFIG_TABLE);

let db_version = match table {
Ok(table) => table.get("db_version")?.map(|v| v.value().to_owned()),
Err(_) => None,
};
match db_version {
Some(db_version) => {
let mut current_file_version = u32::from_str(&db_version)?;
match current_file_version.cmp(&DATABASE_VERSION) {
Ordering::Less => {
tracing::info!(
"Database needs to be upgraded at {} current is {}",
current_file_version,
DATABASE_VERSION
);
if current_file_version == 0 {
current_file_version = migrate_00_to_01(db.clone())?;
}

if current_file_version == 1 {
current_file_version = migrate_01_to_02(db.clone())?;
}
Ordering::Equal => {
tracing::info!("Database is at current version {}", DATABASE_VERSION);

if current_file_version == 2 {
current_file_version = migrate_02_to_03(db.clone())?;
}

if current_file_version == 3 {
current_file_version = migrate_03_to_04(db.clone())?;
}
Ordering::Greater => {

if current_file_version == 4 {
current_file_version = migrate_04_to_05(db.clone())?;
}

if current_file_version != DATABASE_VERSION {
tracing::warn!(
"Database upgrade did not complete at {} current is {}",
current_file_version,
DATABASE_VERSION
);
return Err(Error::UnknownDatabaseVersion);
}

let write_txn = db.begin_write()?;
{
let mut table = write_txn.open_table(CONFIG_TABLE)?;

table.insert("db_version", DATABASE_VERSION.to_string().as_str())?;
}

write_txn.commit()?;
}
}
None => {
let write_txn = db.begin_write()?;
{
let mut table = write_txn.open_table(CONFIG_TABLE)?;
// Open all tables to init a new db
let _ = write_txn.open_table(ACTIVE_KEYSETS_TABLE)?;
let _ = write_txn.open_table(KEYSETS_TABLE)?;
let _ = write_txn.open_table(MINT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MELT_QUOTES_TABLE)?;
let _ = write_txn.open_table(PROOFS_TABLE)?;
let _ = write_txn.open_table(PROOFS_STATE_TABLE)?;
let _ = write_txn.open_table(BLINDED_SIGNATURES)?;
let _ = write_txn.open_multimap_table(QUOTE_PROOFS_TABLE)?;
let _ = write_txn.open_multimap_table(QUOTE_SIGNATURES_TABLE)?;

table.insert("db_version", DATABASE_VERSION.to_string().as_str())?;
Ordering::Equal => {
tracing::info!("Database is at current version {}", DATABASE_VERSION);
}

write_txn.commit()?;
Ordering::Greater => {
tracing::warn!(
"Database upgrade did not complete at {} current is {}",
current_file_version,
DATABASE_VERSION
);
return Err(Error::UnknownDatabaseVersion);
}
}
}
None => {
let write_txn = db.begin_write()?;
{
let mut table = write_txn.open_table(CONFIG_TABLE)?;
// Open all tables to init a new db
let _ = write_txn.open_table(ACTIVE_KEYSETS_TABLE)?;
let _ = write_txn.open_table(KEYSETS_TABLE)?;
let _ = write_txn.open_table(MINT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MELT_QUOTES_TABLE)?;
let _ = write_txn.open_table(PROOFS_TABLE)?;
let _ = write_txn.open_table(PROOFS_STATE_TABLE)?;
let _ = write_txn.open_table(BLINDED_SIGNATURES)?;
let _ = write_txn.open_multimap_table(QUOTE_PROOFS_TABLE)?;
let _ = write_txn.open_multimap_table(QUOTE_SIGNATURES_TABLE)?;

table.insert("db_version", DATABASE_VERSION.to_string().as_str())?;
}

write_txn.commit()?;
}
drop(db);
}

let db = Database::create(db_file_path)?;
Ok(Self { db: Arc::new(db) })
Ok(())
}
}

Expand Down

0 comments on commit 90d853c

Please sign in to comment.