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 0f55037
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 97 deletions.
11 changes: 5 additions & 6 deletions crates/cdk-redb/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::sync::Arc;

use cdk_common::mint_url::MintUrl;
use cdk_common::{Amount, CurrencyUnit, MeltQuoteState, MintQuoteState};
Expand Down Expand Up @@ -105,7 +104,7 @@ impl From<V0MeltQuote> for V1MeltQuote {
}
}

fn migrate_mint_quotes_00_to_01(db: Arc<Database>) -> Result<(), Error> {
fn migrate_mint_quotes_00_to_01(db: &Database) -> Result<(), Error> {
let read_txn = db.begin_read().map_err(Error::from)?;
let table = read_txn
.open_table(MINT_QUOTES_TABLE)
Expand Down Expand Up @@ -158,7 +157,7 @@ fn migrate_mint_quotes_00_to_01(db: Arc<Database>) -> Result<(), Error> {
Ok(())
}

fn migrate_melt_quotes_00_to_01(db: Arc<Database>) -> Result<(), Error> {
fn migrate_melt_quotes_00_to_01(db: &Database) -> Result<(), Error> {
let read_txn = db.begin_read().map_err(Error::from)?;
let table = read_txn
.open_table(MELT_QUOTES_TABLE)
Expand Down Expand Up @@ -211,13 +210,13 @@ fn migrate_melt_quotes_00_to_01(db: Arc<Database>) -> Result<(), Error> {
Ok(())
}

pub(crate) fn migrate_00_to_01(db: Arc<Database>) -> Result<u32, Error> {
pub(crate) fn migrate_00_to_01(db: &Database) -> Result<u32, Error> {
tracing::info!("Starting Migrations of mint quotes from 00 to 01");
migrate_mint_quotes_00_to_01(Arc::clone(&db))?;
migrate_mint_quotes_00_to_01(db)?;
tracing::info!("Finished Migrations of mint quotes from 00 to 01");

tracing::info!("Starting Migrations of melt quotes from 00 to 01");
migrate_melt_quotes_00_to_01(Arc::clone(&db))?;
migrate_melt_quotes_00_to_01(db)?;
tracing::info!("Finished Migrations of melt quotes from 00 to 01");
Ok(1)
}
13 changes: 6 additions & 7 deletions crates/cdk-redb/src/mint/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use core::str;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;

use cdk_common::mint::MintQuote;
use cdk_common::mint_url::MintUrl;
Expand All @@ -22,23 +21,23 @@ const SPENT_PROOFS_TABLE: TableDefinition<[u8; 33], &str> = TableDefinition::new
const QUOTE_PROOFS_TABLE: MultimapTableDefinition<&str, [u8; 33]> =
MultimapTableDefinition::new("quote_proofs");

pub fn migrate_01_to_02(db: Arc<Database>) -> Result<u32, Error> {
pub fn migrate_01_to_02(db: &Database) -> Result<u32, Error> {
migrate_mint_quotes_01_to_02(db)?;
Ok(2)
}

pub fn migrate_02_to_03(db: Arc<Database>) -> Result<u32, Error> {
pub fn migrate_02_to_03(db: &Database) -> Result<u32, Error> {
migrate_mint_proofs_02_to_03(db)?;
Ok(3)
}
pub fn migrate_03_to_04(db: Arc<Database>) -> Result<u32, Error> {
pub fn migrate_03_to_04(db: &Database) -> Result<u32, Error> {
let write_txn = db.begin_write()?;
let _ = write_txn.open_multimap_table(QUOTE_PROOFS_TABLE)?;
let _ = write_txn.open_multimap_table(QUOTE_SIGNATURES_TABLE)?;
Ok(4)
}

pub fn migrate_04_to_05(db: Arc<Database>) -> Result<u32, Error> {
pub fn migrate_04_to_05(db: &Database) -> Result<u32, Error> {
let write_txn = db.begin_write()?;

// Mint quotes
Expand Down Expand Up @@ -213,7 +212,7 @@ impl From<V1MintQuote> for MintQuote {
}
}

fn migrate_mint_quotes_01_to_02(db: Arc<Database>) -> Result<(), Error> {
fn migrate_mint_quotes_01_to_02(db: &Database) -> Result<(), Error> {
let read_txn = db.begin_read().map_err(Error::from)?;
let table = read_txn
.open_table(ID_STR_MINT_QUOTES_TABLE)
Expand Down Expand Up @@ -266,7 +265,7 @@ fn migrate_mint_quotes_01_to_02(db: Arc<Database>) -> Result<(), Error> {
Ok(())
}

fn migrate_mint_proofs_02_to_03(db: Arc<Database>) -> Result<(), Error> {
fn migrate_mint_proofs_02_to_03(db: &Database) -> Result<(), Error> {
let pending_proofs: Vec<([u8; 33], Option<Proof>)>;
let spent_proofs: Vec<([u8; 33], Option<Proof>)>;

Expand Down
169 changes: 85 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,106 @@ 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 = Database::create(db_file_path)?;

Self::migrate(&db)?;

Ok(Self { db: Arc::new(db) })
}

fn migrate(db: &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)?;
}

if current_file_version == 1 {
current_file_version = migrate_01_to_02(db)?;
}

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

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

if current_file_version == 4 {
current_file_version = migrate_04_to_05(db)?;
}
Ordering::Greater => {

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 0f55037

Please sign in to comment.