mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 11:47:25 +01:00
Add original database code
This commit is contained in:
parent
08be3cb613
commit
7eacfe21f4
26 changed files with 2059 additions and 24 deletions
54
src/db/mod.rs
Normal file
54
src/db/mod.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use diesel::prelude::*;
|
||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||
use log::info;
|
||||
|
||||
pub use diesel::SqliteConnection;
|
||||
|
||||
pub mod ensembles;
|
||||
pub use ensembles::*;
|
||||
|
||||
pub mod error;
|
||||
pub use error::*;
|
||||
|
||||
pub mod instruments;
|
||||
pub use instruments::*;
|
||||
|
||||
pub mod medium;
|
||||
pub use medium::*;
|
||||
|
||||
pub mod persons;
|
||||
pub use persons::*;
|
||||
|
||||
pub mod recordings;
|
||||
pub use recordings::*;
|
||||
|
||||
pub mod works;
|
||||
pub use works::*;
|
||||
|
||||
mod schema;
|
||||
|
||||
// This makes the SQL migration scripts accessible from the code.
|
||||
const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
||||
|
||||
/// Connect to a Musicus database running migrations if necessary.
|
||||
pub fn connect(file_name: &str) -> Result<SqliteConnection> {
|
||||
info!("Opening database file '{}'", file_name);
|
||||
let mut connection = SqliteConnection::establish(file_name)?;
|
||||
diesel::sql_query("PRAGMA foreign_keys = ON").execute(&mut connection)?;
|
||||
|
||||
info!("Running migrations if necessary");
|
||||
connection.run_pending_migrations(MIGRATIONS)?;
|
||||
|
||||
Ok(connection)
|
||||
}
|
||||
|
||||
/// Generate a random string suitable as an item ID.
|
||||
pub fn generate_id() -> String {
|
||||
uuid::Uuid::new_v4().simple().to_string()
|
||||
}
|
||||
|
||||
/// Defer all foreign keys for the next transaction.
|
||||
fn defer_foreign_keys(connection: &mut SqliteConnection) -> Result<()> {
|
||||
diesel::sql_query("PRAGMA defer_foreign_keys = ON").execute(connection)?;
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue