Skip to content

Commit

Permalink
Merge pull request #180 from rustaceanrob/const-10-27
Browse files Browse the repository at this point in the history
refact(db): use `const` for known files and paths
  • Loading branch information
rustaceanrob authored Oct 27, 2024
2 parents a83eeb1 + 8cd160e commit 10a3ce1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/db/sqlite/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::db::error::{SqlHeaderStoreError, SqlInitializationError};
use crate::db::traits::HeaderStore;
use crate::prelude::FutureResult;

use super::{DATA_DIR, DEFAULT_CWD};

const FILE_NAME: &str = "headers.db";
// Labels for the schema table
const SCHEMA_TABLE_NAME: &str = "header_schema_versions";
const SCHEMA_COLUMN: &str = "schema_key";
Expand Down Expand Up @@ -42,13 +45,13 @@ impl SqliteHeaderDb {
/// Create a new [`SqliteHeaderDb`] with an optional file path. If no path is provided,
/// the file will be stored in a `data` subdirectory where the program is ran.
pub fn new(network: Network, path: Option<PathBuf>) -> Result<Self, SqlInitializationError> {
let mut path = path.unwrap_or_else(|| PathBuf::from("."));
path.push("data");
let mut path = path.unwrap_or_else(|| PathBuf::from(DEFAULT_CWD));
path.push(DATA_DIR);
path.push(network.to_string());
if !path.exists() {
fs::create_dir_all(&path)?;
}
let conn = Connection::open(path.join("headers.db"))?;
let conn = Connection::open(path.join(FILE_NAME))?;
// Create the schema version
let schema_table_query = format!(
"CREATE TABLE IF NOT EXISTS {SCHEMA_TABLE_NAME} ({SCHEMA_COLUMN} TEXT PRIMARY KEY, {VERSION_COLUMN} INTEGER NOT NULL)");
Expand Down
3 changes: 3 additions & 0 deletions src/db/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
pub mod headers;
/// SQL peer storage.
pub mod peers;

pub(crate) const DEFAULT_CWD: &str = ".";
pub(crate) const DATA_DIR: &str = "data";
9 changes: 6 additions & 3 deletions src/db/sqlite/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ use crate::db::traits::PeerStore;
use crate::db::{PeerStatus, PersistedPeer};
use crate::prelude::FutureResult;

use super::{DATA_DIR, DEFAULT_CWD};

const BUGGED_SERVICE: u64 = 0xfffffffffffff3b0;

const FILE_NAME: &str = "peers.db";
// Labels for the schema table
const SCHEMA_TABLE_NAME: &str = "peer_schema_versions";
const SCHEMA_COLUMN: &str = "schema_key";
Expand Down Expand Up @@ -42,13 +45,13 @@ impl SqlitePeerDb {
/// the file will be stored in a `data` subdirectory where the program is ran.
pub fn new(network: Network, path: Option<PathBuf>) -> Result<Self, SqlInitializationError> {
// Open a connection
let mut path = path.unwrap_or_else(|| PathBuf::from("."));
path.push("data");
let mut path = path.unwrap_or_else(|| PathBuf::from(DEFAULT_CWD));
path.push(DATA_DIR);
path.push(network.to_string());
if !path.exists() {
fs::create_dir_all(&path)?
}
let conn = Connection::open(path.join("peers.db"))?;
let conn = Connection::open(path.join(FILE_NAME))?;
// Create the schema version
let schema_table_query = format!("CREATE TABLE IF NOT EXISTS {SCHEMA_TABLE_NAME} ({SCHEMA_COLUMN} TEXT PRIMARY KEY, {VERSION_COLUMN} INTEGER NOT NULL)");
// Update the schema version
Expand Down

0 comments on commit 10a3ce1

Please sign in to comment.