You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mediarepo/mediarepo-daemon/mediarepo-database/src/lib.rs

33 lines
962 B
Rust

use std::time::Duration;
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
use sqlx::migrate::MigrateDatabase;
use mediarepo_core::error::RepoDatabaseResult;
pub mod entities;
pub mod queries;
/// Connects to the database, runs migrations and returns the RepoDatabase wrapper type
pub async fn get_database<S: AsRef<str>>(uri: S) -> RepoDatabaseResult<DatabaseConnection> {
migrate(uri.as_ref()).await?;
let mut opt = ConnectOptions::new(uri.as_ref().to_string());
opt.connect_timeout(Duration::from_secs(10))
.idle_timeout(Duration::from_secs(10));
let conn = Database::connect(opt).await?;
Ok(conn)
}
async fn migrate(uri: &str) -> RepoDatabaseResult<()> {
use sqlx::Connection;
if !sqlx::Sqlite::database_exists(uri).await? {
sqlx::Sqlite::create_database(uri).await?;
}
let mut conn = sqlx::SqliteConnection::connect(uri).await?;
sqlx::migrate!().run(&mut conn).await?;
Ok(())
}