lights/src/db.rs
Andre Henriques 5c84875dd9
All checks were successful
continuous-integration/drone/push Build is passing
chore: added option to get list of active animations
2023-07-01 23:11:46 +01:00

162 lines
4.0 KiB
Rust

use std::error::Error;
use crate::DBPool;
pub type GResult = Result<(), Box<dyn Error>>;
const DATAVERSION: &str = "0.0.10";
fn create_database(pool: DBPool) -> GResult {
let conn = pool.get()?;
println!("Createing the database");
println!("Createing new meta table. With version {}", DATAVERSION);
println!("Drop old meta table");
conn.execute("DROP TABLE IF EXISTS meta;", ())?;
// Create the database
println!("create new meta table");
conn.execute(
"CREATE TABLE meta (
id INTEGER PRIMARY KEY,
version TEXT,
configured Boolean
);",
(),
)?;
println!("insert data");
conn.execute(
"INSERT INTO meta (version, configured) VALUES (?1, ?2);",
(&DATAVERSION, false),
)?;
println!(
"Createing new configuration table. With version {}",
DATAVERSION
);
println!("Drop configuration table");
conn.execute("DROP TABLE IF EXISTS configuration", ())?;
println!("Create new configuration table");
conn.execute(
"CREATE TABLE configuration (
id INTEGER PRIMARY KEY,
ledcount INTEGER,
directionX INTEGER,
directionY INTEGER,
directionZ INTEGER,
tags VARCHAR(255)
);",
(),
)?;
println!(
"Createing new Animation table. With version {}",
DATAVERSION
);
println!("Drop Animation table and references");
conn.execute("DROP TABLE IF EXISTS lightsetting", ())?;
conn.execute("DROP TABLE IF EXISTS keyframe", ())?;
conn.execute("DROP TABLE IF EXISTS animation", ())?;
println!("Create new animation table");
conn.execute(
"CREATE TABLE animation (
id INTEGER PRIMARY KEY,
priority INTEGER,
name VARCHAR(255),
repeat BOOLEAN
);",
(),
)?;
println!(
"Createing new key_frame table. With version {}",
DATAVERSION
);
println!("Drop key_frame table");
println!("Create new key_frame table");
conn.execute(
"CREATE TABLE keyframe (
id INTEGER PRIMARY KEY,
i INTEGER,
duration INTEGER,
animation INTEGER,
FOREIGN KEY(animation) REFERENCES animation(id) ON DELETE CASCADE
);",
(),
)?;
println!(
"Createing new LigthSetting table. With version {}",
DATAVERSION
);
println!("Drop LigthSetting table");
println!("Create new light_setting table");
conn.execute(
"CREATE TABLE lightsetting (
id INTEGER PRIMARY KEY,
start INTEGER,
end INTEGER,
tags VARCHAR(255),
r INTEGER,
b INTEGER,
g INTEGER,
keyframe INTEGER,
FOREIGN KEY(keyframe) REFERENCES keyframe(id) ON DELETE CASCADE
);",
(),
)?;
Ok(())
}
pub fn setup_database(pool: DBPool) -> GResult {
let conn = pool.get()?;
println!("Trying to get meta data");
let stmt = conn.prepare("SELECT version from meta");
if stmt.is_err() || stmt.as_ref().ok().is_none() {
return create_database(pool);
}
let mut stmt = stmt?;
struct Data {
version: String,
}
let version_iter = stmt.query_map([], |row| {
Ok(Data {
version: row.get(0)?,
})
});
println!("Process meta");
if version_iter.is_err() || version_iter.as_ref().ok().is_none() {
create_database(pool)?;
} else {
let version_iter = version_iter.ok().unwrap();
let mut recreate = true;
for data in version_iter {
let version = data.unwrap().version;
println!("Found version {}", version);
if !DATAVERSION.to_string().eq(&version) {
println!("Mismatched versions recreating database");
recreate = true;
break;
}
recreate = false;
}
if recreate {
create_database(pool)?;
}
}
Ok(())
}