mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Moved backend code into its own subfolder
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
pub mod devices;
|
||||
|
||||
use include_dir::{Dir, include_dir};
|
||||
use log::{debug, error};
|
||||
use rusqlite::Connection;
|
||||
use rusqlite_migration::Migrations;
|
||||
use std::result::Result;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use crate::settings;
|
||||
|
||||
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/database_migrations");
|
||||
|
||||
// Define migrations. These are applied atomically.
|
||||
static MIGRATIONS: LazyLock<Migrations<'static>> =
|
||||
LazyLock::new(|| Migrations::from_directory(&MIGRATIONS_DIR).unwrap());
|
||||
|
||||
pub fn init_db() -> Result<Connection, String> {
|
||||
if settings::CONFIG.database.path.is_empty() {
|
||||
error!("Database path not set. Make sure to define database.path in your config file.");
|
||||
Err(format!(
|
||||
"Database path not set. Make sure to define database.path in your config file."
|
||||
))
|
||||
} else {
|
||||
debug!("Opening database at {}.", settings::CONFIG.database.path);
|
||||
|
||||
let database_path = settings::CONFIG.database.path.clone();
|
||||
|
||||
let mut conn = match Connection::open(database_path) {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
error!("Error opening database (oott.db): {error}");
|
||||
return Err(format!("Error opening database (oott.db): {error}"));
|
||||
}
|
||||
};
|
||||
|
||||
debug!("Database open, executing migrations if needed.");
|
||||
// Update the database schema, atomically
|
||||
match MIGRATIONS.to_latest(&mut conn) {
|
||||
Ok(_) => {
|
||||
debug!("Database up to date.");
|
||||
Ok(conn)
|
||||
}
|
||||
Err(error) => {
|
||||
error!("Error updating database: {error}");
|
||||
Err(format!("Error updating database: {error}"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user