Skip to content

Commit

Permalink
SQLite: move migration into into new()
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Dec 27, 2024
1 parent c6e27d0 commit 16654b1
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 23 deletions.
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
1 change: 0 additions & 1 deletion crates/cdk-integration-tests/src/bin/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ async fn main() -> Result<()> {
}
"SQLITE" => {
let sqlite_db = MintSqliteDatabase::new(&get_temp_dir().join("mint")).await?;
sqlite_db.migrate().await;
start_fake_mint(addr, port, sqlite_db).await?;
}
"REDB" => {
Expand Down
1 change: 0 additions & 1 deletion crates/cdk-integration-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ async fn main() -> Result<()> {
}
"SQLITE" => {
let sqlite_db = MintSqliteDatabase::new(&get_temp_dir().join("mint")).await?;
sqlite_db.migrate().await;
start_cln_mint(addr, port, sqlite_db).await?;
}
"REDB" => {
Expand Down
3 changes: 0 additions & 3 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ async fn main() -> anyhow::Result<()> {
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 => {
Expand Down
3 changes: 3 additions & 0 deletions crates/cdk-sqlite/src/mint/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub enum Error {
/// SQLX Error
#[error(transparent)]
SQLX(#[from] sqlx::Error),
/// SQLX Migration Error
#[error(transparent)]
SQLXMigration(#[from] sqlx::migrate::MigrateError),
/// NUT00 Error
#[error(transparent)]
CDKNUT00(#[from] cdk::nuts::nut00::Error),
Expand Down
10 changes: 2 additions & 8 deletions crates/cdk-sqlite/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,9 @@ impl MintSqliteDatabase {
.connect_with(db_options)
.await?;

Ok(Self { pool })
}
sqlx::migrate!("./src/mint/migrations").run(&pool).await?;

/// Migrate [`MintSqliteDatabase`]
pub async fn migrate(&self) {
sqlx::migrate!("./src/mint/migrations")
.run(&self.pool)
.await
.expect("Could not run migrations");
Ok(Self { pool })
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/cdk-sqlite/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub enum Error {
/// SQLX Error
#[error(transparent)]
SQLX(#[from] sqlx::Error),
/// SQLX Migration Error
#[error(transparent)]
SQLXMigration(#[from] sqlx::migrate::MigrateError),
/// Serde Error
#[error(transparent)]
Serde(#[from] serde_json::Error),
Expand Down
10 changes: 2 additions & 8 deletions crates/cdk-sqlite/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,9 @@ impl WalletSqliteDatabase {

let pool = SqlitePool::connect(path).await?;

Ok(Self { pool })
}
sqlx::migrate!("./src/wallet/migrations").run(&pool).await?;

/// Migrate [`WalletSqliteDatabase`]
pub async fn migrate(&self) {
sqlx::migrate!("./src/wallet/migrations")
.run(&self.pool)
.await
.expect("Could not run migrations");
Ok(Self { pool })
}

async fn set_proof_state(&self, y: PublicKey, state: State) -> Result<(), cdk_database::Error> {
Expand Down

0 comments on commit 16654b1

Please sign in to comment.