Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite: move migration into into new() #517

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions crates/cdk-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ async fn main() -> Result<()> {
let sql_path = work_dir.join("cdk-cli.sqlite");
let sql = WalletSqliteDatabase::new(&sql_path).await?;

sql.migrate().await;

Arc::new(sql)
}
"redb" => {
Expand Down
71 changes: 71 additions & 0 deletions crates/cdk-common/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ mod mint;
#[cfg(feature = "wallet")]
mod wallet;

use std::path::{Path, PathBuf};

use cashu::util::unix_time;
#[cfg(feature = "mint")]
pub use mint::Database as MintDatabase;
#[cfg(feature = "wallet")]
Expand Down Expand Up @@ -32,3 +35,71 @@ pub enum Error {
#[error("Unknown Quote")]
UnknownQuote,
}

/// Creates the backups folder, if missing. Enforces the configured backups limit by removing
/// older backups, if necessary.
///
/// # Arguments
///
/// * `work_dir`: the working directory in which the backups folder will be created
/// * `extension`: the DB backup file extension, which usually indicates the type of DB used
/// * `backups_to_keep`: configured number of backups to keep
///
/// # Returns
///
/// Full path of the new backup, if one is to be created
pub fn prepare_backup(
work_dir: &Path,
extension: &str,
backups_to_keep: u8,
) -> Result<Option<PathBuf>, Error> {
let prefix = "backup_";

let backups_dir_path = work_dir.join("backups");
if !backups_dir_path.exists() {
std::fs::create_dir_all(&backups_dir_path)
.map_err(|e| Error::Database(format!("Failed to create backups folder: {e}").into()))?;
}

let mut existing_backups: Vec<PathBuf> = std::fs::read_dir(&backups_dir_path)
.map_err(|e| Error::Database(format!("Failed to list existing backups: {e}").into()))?
.filter_map(|entry| entry.ok())
.map(|entry| entry.path())
.filter(|path| {
if let Some(file_name) = path.file_name() {
if let Some(file_name_str) = file_name.to_str() {
return file_name_str.starts_with(prefix)
&& file_name_str.ends_with(&format!(".{}", extension));
}
}
false
})
.collect();

// Sort backup files by name (which includes timestamp) in descending order
existing_backups.sort();
existing_backups.reverse();
tracing::info!("Found backups: {existing_backups:#?}");

// Remove excess backups
tracing::info!("Keeping {backups_to_keep} backups");
let backup_files_to_delete: Vec<_> = match backups_to_keep as usize {
0 | 1 => existing_backups.iter().collect(),
n => existing_backups.iter().skip(n - 1).collect(),
};
for backup in backup_files_to_delete {
tracing::info!("Removing old backup: {:?}", backup);
std::fs::remove_file(backup)
.map_err(|e| Error::Database(format!("Failed to remove old backup: {e}").into()))?
}

match backups_to_keep {
0 => Ok(None),
_ => {
let new_backup_filename = format!("{}{}.{}", prefix, unix_time(), extension);
let new_backup_path = backups_dir_path.join(new_backup_filename);
tracing::info!("New backup file path: {new_backup_path:?}");
Ok(Some(new_backup_path))
}
}
}
9 changes: 5 additions & 4 deletions crates/cdk-integration-tests/src/bin/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ async fn main() -> Result<()> {
let addr = "127.0.0.1";
let port = 8086;

let mint_db_kind = env::var("MINT_DATABASE")?;
let temp_work_dir = get_temp_dir().join("mint");
std::fs::create_dir_all(&temp_work_dir)?;

let mint_db_kind = env::var("MINT_DATABASE")?;
match mint_db_kind.as_str() {
"MEMORY" => {
start_fake_mint(addr, port, MintMemoryDatabase::default()).await?;
}
"SQLITE" => {
let sqlite_db = MintSqliteDatabase::new(&get_temp_dir().join("mint")).await?;
sqlite_db.migrate().await;
let sqlite_db = MintSqliteDatabase::new(&temp_work_dir, 0).await?;
start_fake_mint(addr, port, sqlite_db).await?;
}
"REDB" => {
let redb_db = MintRedbDatabase::new(&get_temp_dir().join("mint")).unwrap();
let redb_db = MintRedbDatabase::new(&temp_work_dir, 0)?;
start_fake_mint(addr, port, redb_db).await?;
}
_ => panic!("Unknown mint db type: {}", mint_db_kind),
Expand Down
12 changes: 6 additions & 6 deletions crates/cdk-integration-tests/src/bin/regtest_mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ async fn main() -> Result<()> {
let mint_db_kind = env::var("MINT_DATABASE")?;

let lnd_mint_db_path = get_temp_dir().join("lnd_mint");
std::fs::create_dir_all(&lnd_mint_db_path)?;
let cln_mint_db_path = get_temp_dir().join("cln_mint");
std::fs::create_dir_all(&cln_mint_db_path)?;

let cln_backend = create_cln_backend(&cln_client).await?;
let lnd_mint_port = 8087;
Expand Down Expand Up @@ -178,28 +180,26 @@ async fn main() -> Result<()> {
}
"SQLITE" => {
tokio::spawn(async move {
let cln_sqlite_db = MintSqliteDatabase::new(&cln_mint_db_path)
let cln_sqlite_db = MintSqliteDatabase::new(&cln_mint_db_path, 0)
.await
.expect("Could not create CLN mint db");
cln_sqlite_db.migrate().await;
create_mint(mint_addr, cln_mint_port, cln_sqlite_db, cln_backend)
.await
.expect("Could not start cln mint");
});

let lnd_sqlite_db = MintSqliteDatabase::new(&lnd_mint_db_path).await?;
lnd_sqlite_db.migrate().await;
let lnd_sqlite_db = MintSqliteDatabase::new(&lnd_mint_db_path, 0).await?;
create_mint(mint_addr, lnd_mint_port, lnd_sqlite_db, lnd_backend).await?;
}
"REDB" => {
tokio::spawn(async move {
let cln_redb_db = MintRedbDatabase::new(&cln_mint_db_path).unwrap();
let cln_redb_db = MintRedbDatabase::new(&cln_mint_db_path, 0).unwrap();
create_mint(mint_addr, cln_mint_port, cln_redb_db, cln_backend)
.await
.expect("Could not start cln mint");
});

let lnd_redb_db = MintRedbDatabase::new(&lnd_mint_db_path).unwrap();
let lnd_redb_db = MintRedbDatabase::new(&lnd_mint_db_path, 0)?;
create_mint(mint_addr, lnd_mint_port, lnd_redb_db, lnd_backend).await?;
}
_ => panic!("Unknown mint db type: {}", mint_db_kind),
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk-mintd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tracing = { version = "0.1", default-features = false, features = [
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
futures = { version = "0.3.28", default-features = false }
serde = { version = "1", default-features = false, features = ["derive"] }
bip39 = "2.0"
bip39 = { version = "2.0", features = ["rand"] }
tower-http = { version = "0.4.4", features = ["cors", "compression-full"] }
lightning-invoice = { version = "0.32.0", features = ["serde", "std"] }
home = "0.5.5"
Expand Down
1 change: 1 addition & 0 deletions crates/cdk-mintd/example.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tti = 60
[database]
# Database engine (sqlite/redb) defaults to sqlite
# engine = "sqlite"
# backups_to_keep = 3

[ln]
# Required ln backend `cln`, `lnd`, `strike`, `fakewallet`, 'lnbits', 'phoenixd'
Expand Down
1 change: 1 addition & 0 deletions crates/cdk-mintd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl std::str::FromStr for DatabaseEngine {
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Database {
pub engine: DatabaseEngine,
pub backups_to_keep: u8,
}

/// CDK settings, derived from `config.toml`
Expand Down
11 changes: 8 additions & 3 deletions crates/cdk-mintd/src/env_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use crate::config::{

pub const ENV_WORK_DIR: &str = "CDK_MINTD_WORK_DIR";

pub const DATABASE_ENV_VAR: &str = "CDK_MINTD_DATABASE";
pub const ENV_DATABASE: &str = "CDK_MINTD_DATABASE";
pub const ENV_BACKUPS_TO_KEEP: &str = "CDK_MINTD_BACKUPS_TO_KEEP";
pub const ENV_URL: &str = "CDK_MINTD_URL";
pub const ENV_LISTEN_HOST: &str = "CDK_MINTD_LISTEN_HOST";
pub const ENV_LISTEN_PORT: &str = "CDK_MINTD_LISTEN_PORT";
Expand Down Expand Up @@ -73,9 +74,13 @@ pub const ENV_FAKE_WALLET_MAX_DELAY: &str = "CDK_MINTD_FAKE_WALLET_MAX_DELAY";

impl Settings {
pub fn from_env(&mut self) -> Result<Self> {
if let Ok(database) = env::var(DATABASE_ENV_VAR) {
if let Ok(database) = env::var(ENV_DATABASE) {
let engine = DatabaseEngine::from_str(&database).map_err(|err| anyhow!(err))?;
self.database = Database { engine };
let backups_to_keep = u8::from_str(ENV_BACKUPS_TO_KEEP).unwrap_or_default();
self.database = Database {
engine,
backups_to_keep,
};
}

self.info = self.info.clone().from_env();
Expand Down
13 changes: 3 additions & 10 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,14 @@ async fn main() -> anyhow::Result<()> {
// This check for any settings defined in ENV VARs
// ENV VARS will take **priority** over those in the config
let settings = settings.from_env()?;
let backups_to_keep = settings.database.backups_to_keep;

let localstore: Arc<dyn MintDatabase<Err = cdk_database::Error> + Send + Sync> =
match settings.database.engine {
DatabaseEngine::Sqlite => {
let sql_db_path = work_dir.join("cdk-mintd.sqlite");
let sqlite_db = MintSqliteDatabase::new(&sql_db_path).await?;

sqlite_db.migrate().await;

Arc::new(sqlite_db)
}
DatabaseEngine::Redb => {
let redb_path = work_dir.join("cdk-mintd.redb");
Arc::new(MintRedbDatabase::new(&redb_path)?)
Arc::new(MintSqliteDatabase::new(&work_dir, backups_to_keep).await?)
}
DatabaseEngine::Redb => Arc::new(MintRedbDatabase::new(&work_dir, backups_to_keep)?),
};

mint_builder = mint_builder.with_localstore(localstore);
Expand Down
3 changes: 3 additions & 0 deletions crates/cdk-redb/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum Error {
/// CDK Database Error
#[error(transparent)]
CDKDatabase(#[from] cdk_common::database::Error),
/// Error while trying to prepare or executing a DB backup
#[error("Error while doing DB backup: {0}")]
DbBackup(cdk_common::database::Error),
/// CDK Mint Url Error
#[error(transparent)]
CDKMintUrl(#[from] cdk_common::mint_url::Error),
Expand Down
Loading
Loading