Skip to content
Merged
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
5 changes: 4 additions & 1 deletion crates/sage/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ pub enum Error {
#[error("Missing asset id")]
MissingAssetId,

#[error("Database version too old")]
DatabaseVersionTooOld,

#[error("Timeout")]
Timeout(#[from] Elapsed),
}
Expand All @@ -217,7 +220,7 @@ impl Error {
| KeychainError::Bip39(..)
| KeychainError::Argon2(..) => ErrorKind::Internal,
},
Self::SqlxMigration(..) => ErrorKind::DatabaseMigration,
Self::SqlxMigration(..) | Self::DatabaseVersionTooOld => ErrorKind::DatabaseMigration,
Self::Send(..)
| Self::Io(..)
| Self::Client(..)
Expand Down
13 changes: 13 additions & 0 deletions crates/sage/src/sage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ impl Sage {

let pool = self.connect_to_database(fingerprint).await?;
let db = Database::new(pool);

db.run_rust_migrations(self.network().ticker.clone())
.await?;

Expand Down Expand Up @@ -408,11 +409,23 @@ impl Sage {
)
.await?;

let version = Self::database_version(&pool).await?;

if version < 2 {
return Err(Error::DatabaseVersionTooOld);
}
sqlx::migrate!("../../migrations").run(&pool).await?;

Ok(pool)
}

async fn database_version(pool: &SqlitePool) -> Result<i32> {
let row: (i32,) = sqlx::query_as("PRAGMA user_version")
.fetch_one(pool)
.await?;
Ok(row.0)
}

pub fn wallet_db_path(&self, fingerprint: u32) -> Result<PathBuf> {
let path = self.path.join("wallets").join(fingerprint.to_string());
fs::create_dir_all(&path)?;
Expand Down