Skip to content

Commit

Permalink
embedded database migrations into binary, fixed distribution problems
Browse files Browse the repository at this point in the history
  • Loading branch information
9-FS committed Dec 31, 2024
1 parent 6391620 commit 8e48fcc
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT"
name = "nhentai_archivist"
readme = "readme.md"
repository = "https://github.com/9-FS/nhentai_archivist"
version = "3.8.0"
version = "3.8.1"

[dependencies]
chrono = { version = "^0.4.0", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
nhentai_archivist:
container_name: "nhentai_archivist"
image: "ghcr.io/9-fs/nhentai_archivist:3.8.0"
image: "ghcr.io/9-fs/nhentai_archivist:3.8.1"
environment:
HOST_OS: "Unraid"
TZ: "UTC"
Expand Down
10 changes: 3 additions & 7 deletions src/connect_to_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ use sqlx::migrate::MigrateDatabase;
///
/// # Arguments
/// - `db_url`: url to database file, might not be local but is recommended to be so
/// - `migrations_path`: path to directory containing migration files
///
/// # Returns
/// - connection pool to database or error
pub async fn connect_to_db(db_url: &str, db_migrations_path: &str) -> Result<sqlx::SqlitePool, sqlx::Error>
pub async fn connect_to_db(db_url: &str) -> Result<sqlx::SqlitePool, sqlx::Error>
{
let db: sqlx::SqlitePool; // database connection pool

Expand Down Expand Up @@ -51,11 +50,8 @@ pub async fn connect_to_db(db_url: &str, db_migrations_path: &str) -> Result<sql
.synchronous(sqlx::sqlite::SqliteSynchronous::Normal)); // ensure data is written to disk after each transaction for consistent state
log::info!("Connected to database at \"{db_url}\".");

if std::path::Path::new(db_migrations_path).exists() // if migrations path exists
{
sqlx::migrate::Migrator::new(std::path::Path::new(db_migrations_path)).await?.run(&db).await?; // run migrations to create and update tables
log::debug!("Executed migrations at \"{db_migrations_path}\".");
}
sqlx::migrate!("./db_migrations/").run(&db).await?; // run migrations to create and update tables
log::debug!("Executed migrations at \"./db_migrations/\".");

return Ok(db);
}
3 changes: 1 addition & 2 deletions src/main_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::hentai::*;
pub async fn main_inner(config: Config) -> Result<(), Error>
{
const DB_FILEPATH: &str = "./db/db.sqlite"; // database filepath
const DB_MIGRATIONS_PATH: &str = "./db_migrations/"; // migrations path
const HTTP_TIMEOUT: u64 = 30; // connection timeout
const NHENTAI_HENTAI_SEARCH_URL: &str = "https://nhentai.net/api/gallery/"; // nhentai search by id api url
const NHENTAI_TAG_SEARCH_URL: &str = "https://nhentai.net/api/galleries/search"; // nhentai search by tag api url
Expand Down Expand Up @@ -78,7 +77,7 @@ pub async fn main_inner(config: Config) -> Result<(), Error>
break 'iteration; // if server mode: only abort iteration, go straight to sleeping
}

match connect_to_db(DB_FILEPATH, DB_MIGRATIONS_PATH).await // connect to database
match connect_to_db(DB_FILEPATH).await // connect to database
{
Ok(o) => db = o,
Err(e) =>
Expand Down

0 comments on commit 8e48fcc

Please sign in to comment.