make table strict, fix bug where app crashes on empty db
This commit is contained in:
parent
a182e4e0cb
commit
18de2714d0
3 changed files with 5 additions and 9 deletions
BIN
foods.db
BIN
foods.db
Binary file not shown.
|
|
@ -6,4 +6,4 @@ CREATE TABLE IF NOT EXISTS food (
|
|||
target_servings INTEGER NOT NULL DEFAULT 1,
|
||||
actual_servings INTEGER NOT NULL DEFAULT 0,
|
||||
color TEXT NOT NULL DEFAULT 'white'
|
||||
);
|
||||
) STRICT;
|
||||
|
|
|
|||
12
src/main.rs
12
src/main.rs
|
|
@ -81,7 +81,6 @@ struct PreparedStatements {}
|
|||
|
||||
impl<'conn> PreparedStatements {
|
||||
fn check(conn: &Connection) -> rusqlite::Result<Self> {
|
||||
conn.prepare_cached(include_str!("create_tables.sql"))?;
|
||||
conn.prepare_cached(include_str!("increase.sql"))?;
|
||||
conn.prepare_cached(include_str!("decrease.sql"))?;
|
||||
conn.prepare_cached(include_str!("get_food.sql"))?;
|
||||
|
|
@ -91,11 +90,6 @@ impl<'conn> PreparedStatements {
|
|||
Ok(PreparedStatements {})
|
||||
}
|
||||
|
||||
fn create_tables(conn: &'conn Connection) -> CachedStatement<'conn> {
|
||||
conn.prepare_cached(include_str!("create_tables.sql"))
|
||||
.expect("cached statement is invalid")
|
||||
}
|
||||
|
||||
fn increase(conn: &'conn Connection) -> CachedStatement<'conn> {
|
||||
conn.prepare_cached(include_str!("increase.sql"))
|
||||
.expect("cached statement is invalid")
|
||||
|
|
@ -149,12 +143,14 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
let db_connecion_str = "./foods.db".to_string();
|
||||
debug!(db_connecion_str, "opening database");
|
||||
let conn = Connection::open(db_connecion_str).expect("failed to open database");
|
||||
PreparedStatements::check(&conn).expect("failed to prepare sql statements");
|
||||
if let Err(e) = PreparedStatements::create_tables(&conn).execute(()) {
|
||||
|
||||
if let Err(e) = conn.execute(include_str!("create_tables.sql"), ()) {
|
||||
error!(?e, "failed to create tables");
|
||||
panic!("failed to create tables: {:#?}", e);
|
||||
}
|
||||
|
||||
PreparedStatements::check(&conn).expect("failed to prepare sql statements");
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(root))
|
||||
.route("/increase/{id}", post(increase))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue