2020-11-17 15:52:47 +01:00
|
|
|
use diesel::prelude::*;
|
2020-09-28 14:15:59 +02:00
|
|
|
|
2020-11-17 15:52:47 +01:00
|
|
|
pub mod ensembles;
|
|
|
|
|
pub use ensembles::*;
|
2020-09-28 14:15:59 +02:00
|
|
|
|
2021-02-04 17:43:36 +01:00
|
|
|
pub mod error;
|
|
|
|
|
pub use error::*;
|
|
|
|
|
|
2020-11-17 15:52:47 +01:00
|
|
|
pub mod instruments;
|
|
|
|
|
pub use instruments::*;
|
2020-09-28 14:15:59 +02:00
|
|
|
|
2020-12-20 11:47:27 +01:00
|
|
|
pub mod medium;
|
|
|
|
|
pub use medium::*;
|
|
|
|
|
|
2020-11-17 15:52:47 +01:00
|
|
|
pub mod persons;
|
|
|
|
|
pub use persons::*;
|
|
|
|
|
|
|
|
|
|
pub mod recordings;
|
|
|
|
|
pub use recordings::*;
|
|
|
|
|
|
|
|
|
|
pub mod thread;
|
|
|
|
|
pub use thread::*;
|
|
|
|
|
|
|
|
|
|
pub mod works;
|
|
|
|
|
pub use works::*;
|
|
|
|
|
|
|
|
|
|
mod schema;
|
|
|
|
|
|
|
|
|
|
// This makes the SQL migration scripts accessible from the code.
|
|
|
|
|
embed_migrations!();
|
|
|
|
|
|
2020-11-28 21:45:22 +01:00
|
|
|
/// Generate a random string suitable as an item ID.
|
|
|
|
|
pub fn generate_id() -> String {
|
|
|
|
|
let mut buffer = uuid::Uuid::encode_buffer();
|
|
|
|
|
let id = uuid::Uuid::new_v4().to_simple().encode_lower(&mut buffer);
|
|
|
|
|
|
|
|
|
|
id.to_string()
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 15:52:47 +01:00
|
|
|
/// Interface to a Musicus database.
|
|
|
|
|
pub struct Database {
|
|
|
|
|
connection: SqliteConnection,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Database {
|
|
|
|
|
/// Create a new database interface and run migrations if necessary.
|
2021-02-04 17:43:36 +01:00
|
|
|
pub fn new(file_name: &str) -> DatabaseResult<Database> {
|
2020-11-17 15:52:47 +01:00
|
|
|
let connection = SqliteConnection::establish(file_name)?;
|
|
|
|
|
|
|
|
|
|
diesel::sql_query("PRAGMA foreign_keys = ON").execute(&connection)?;
|
|
|
|
|
embedded_migrations::run(&connection)?;
|
|
|
|
|
|
|
|
|
|
Ok(Database { connection })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Defer all foreign keys for the next transaction.
|
2021-02-04 17:43:36 +01:00
|
|
|
fn defer_foreign_keys(&self) -> DatabaseResult<()> {
|
2020-11-17 15:52:47 +01:00
|
|
|
diesel::sql_query("PRAGMA defer_foreign_keys = ON").execute(&self.connection)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|