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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl AppContext {
let (contested_resources) = ContestedResource::fetch_many(&sdk, query.clone())
.await
.map_err(|e| {
tracing::error!("error fetching contested resources: {}", e);
tracing::error!("Error fetching contested resources: {}", e);
if let dash_sdk::Error::Proof(
dash_sdk::ProofVerifierError::GroveDBProofVerificationError {
proof_bytes,
Expand Down Expand Up @@ -89,7 +89,7 @@ impl AppContext {
return e;
}
}
format!("error fetching contested resources: {}", e)
format!("Error fetching contested resources: {}", e)
})?;

let contested_resources_len = contested_resources.0.len();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl AppContext {
ContenderWithSerializedDocument::fetch_many(&sdk, contenders_query.clone())
.await
.map_err(|e| {
tracing::error!("error fetching contested resources: {}", e);
format!("error fetching contested resources: {}", e)
tracing::error!("Error fetching contested resources: {}", e);
format!("Error fetching contested resources: {}", e)
})?;
self.db
.insert_or_update_contenders(name, &contenders, document_type, self)
Expand Down
19 changes: 10 additions & 9 deletions src/database/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use rusqlite::{params, Connection};
use std::fs;
use std::path::Path;

pub const DEFAULT_DB_VERSION: u16 = 1;
pub const CURRENT_DB_VERSION: u16 = 2;
pub const DEFAULT_DB_VERSION: u16 = 2;
pub const DEFAULT_NETWORK: &str = "dash";

impl Database {
Expand All @@ -15,10 +14,10 @@ impl Database {
self.create_tables()?;
self.set_default_version()?;
} else {
// Perform version check and back up and recreate the database if outdated.
// If outdated, back up and either migrate or recreate the database.
if let Some(current_version) = self.is_outdated()? {
self.backup_db(db_file_path)?;
if let Err(e) = self.try_perform_migration(current_version, CURRENT_DB_VERSION) {
if let Err(e) = self.try_perform_migration(current_version, DEFAULT_DB_VERSION) {
// The migration failed
println!("Migration failed: {:?}", e);
self.recreate_db(db_file_path)?;
Expand Down Expand Up @@ -76,7 +75,8 @@ impl Database {
}
}

/// Checks if the current database version is below the minimum supported version.
/// Checks if the version in the current database settings is below DEFAULT_DB_VERSION.
/// If outdated, returns the version in the current database settings
fn is_outdated(&self) -> rusqlite::Result<Option<u16>> {
let conn = self.conn.lock().unwrap();
let version: u16 = conn
Expand All @@ -86,14 +86,14 @@ impl Database {
|row| row.get(0),
)
.unwrap_or(0); // Default to version 0 if there's no version set
if version < CURRENT_DB_VERSION {
if version < DEFAULT_DB_VERSION {
Ok(Some(version))
} else {
Ok(None)
}
}

/// Backs up the existing database with a unique timestamped filename, recreates `data.db`, and refreshes the connection.
/// Backs up the existing database with a unique timestamped filename in backups directory.
fn backup_db(&self, db_file_path: &Path) -> rusqlite::Result<()> {
if db_file_path.exists() {
// Create a "backups" folder in the same directory as `data.db` if not exists
Expand All @@ -112,15 +112,16 @@ impl Database {
let backup_filename = format!("data_backup_{}.db", timestamp);
let backup_path = backups_dir.join(backup_filename);

// Rename `data.db` to the unique backup file
// Copy `data.db` to the unique backup file
fs::copy(db_file_path, &backup_path)
.map_err(|e| rusqlite::Error::ToSqlConversionFailure(e.into()))?;
println!("Old database backed up to {:?}", backup_path);
}

Ok(())
}

/// Backs up the existing database with a unique timestamped filename, recreates `data.db`, and refreshes the connection.
/// Recreates `data.db`, and refreshes the connection.
fn recreate_db(&self, db_file_path: &Path) -> rusqlite::Result<()> {
// Remove the existing database file if it exists
if db_file_path.exists() {
Expand Down