feat: basic music backend
This commit is contained in:
57
src/database/mod.rs
Normal file
57
src/database/mod.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
pub mod paths;
|
||||
pub mod tracks;
|
||||
pub mod artists;
|
||||
|
||||
use r2d2::{Pool, PooledConnection};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
|
||||
pub fn establish_connection() -> Pool<SqliteConnectionManager> {
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
|
||||
let manager = SqliteConnectionManager::file(database_url);
|
||||
let pool = Pool::new(manager).expect("Error creating SQLite pool");
|
||||
|
||||
pool
|
||||
}
|
||||
|
||||
pub fn initialize_database(
|
||||
connection: &PooledConnection<SqliteConnectionManager>,
|
||||
) -> Result<(), r2d2_sqlite::rusqlite::Error> {
|
||||
connection.execute(
|
||||
"
|
||||
CREATE TABLE IF NOT EXISTS library_paths (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
path TEXT NOT NULL
|
||||
);
|
||||
",
|
||||
[],
|
||||
)?;
|
||||
|
||||
connection.execute(
|
||||
"CREATE TABLE IF NOT EXISTS artists (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
name TEXT NOT NULL
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
|
||||
connection
|
||||
.execute(
|
||||
"
|
||||
CREATE TABLE IF NOT EXISTS tracks (
|
||||
hash TEXT PRIMARY KEY NOT NULL,
|
||||
library_path_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
artist_id INTEGER NOT NULL,
|
||||
path TEXT NOT NULL,
|
||||
FOREIGN KEY (library_path_id) REFERENCES library_paths (id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (artist_id) REFERENCES artists (id) ON DELETE CASCADE
|
||||
);
|
||||
",
|
||||
[],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user