From 8fd940bd00b29de08aeb08d196d3a8c5d0414c68 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Fri, 25 Nov 2022 10:42:56 -0500 Subject: [PATCH] Create database if it does not exist --- ...21210140958_mealplans_primary_key.down.sql | 2 ++ ...0221210140958_mealplans_primary_key.up.sql | 2 ++ kitchen/sqlx-data.json | 20 +++++++++---------- kitchen/src/web/storage/mod.rs | 4 +++- kitchen/src/web/storage/save_meal_plan.sql | 3 ++- 5 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 kitchen/migrations/20221210140958_mealplans_primary_key.down.sql create mode 100644 kitchen/migrations/20221210140958_mealplans_primary_key.up.sql diff --git a/kitchen/migrations/20221210140958_mealplans_primary_key.down.sql b/kitchen/migrations/20221210140958_mealplans_primary_key.down.sql new file mode 100644 index 0000000..b9475d6 --- /dev/null +++ b/kitchen/migrations/20221210140958_mealplans_primary_key.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +drop index mealplan_lookup_index; \ No newline at end of file diff --git a/kitchen/migrations/20221210140958_mealplans_primary_key.up.sql b/kitchen/migrations/20221210140958_mealplans_primary_key.up.sql new file mode 100644 index 0000000..f266bd0 --- /dev/null +++ b/kitchen/migrations/20221210140958_mealplans_primary_key.up.sql @@ -0,0 +1,2 @@ +-- Add up migration script here +create unique index mealplan_lookup_index on plan_recipes (user_id, plan_date, recipe_id); \ No newline at end of file diff --git a/kitchen/sqlx-data.json b/kitchen/sqlx-data.json index 15e2ca0..d64b96f 100644 --- a/kitchen/sqlx-data.json +++ b/kitchen/sqlx-data.json @@ -112,6 +112,16 @@ }, "query": "delete from sessions where id = ?" }, + "83824ea638cb64c524f5c8984ef6ef28dfe781f0abf168abc4ae9a51e6e0ae88": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 4 + } + }, + "query": "insert into plan_recipes (user_id, plan_date, recipe_id, count) values (?, ?, ?, ?)\n on conflict (user_id, plan_date, recipe_id) do update set count=excluded.count;" + }, "8490e1bb40879caed62ac1c38cb9af48246f3451b6f7f1e1f33850f1dbe25f58": { "describe": { "columns": [], @@ -214,16 +224,6 @@ }, "query": "with max_date as (\n select user_id, max(date(plan_date)) as plan_date from plan_recipes group by user_id\n)\n\nselect plan_recipes.plan_date as \"plan_date: NaiveDate\", plan_recipes.recipe_id, plan_recipes.count\n from plan_recipes\n inner join max_date on plan_recipes.user_id = max_date.user_id\nwhere\n plan_recipes.user_id = ?\n and plan_recipes.plan_date = max_date.plan_date" }, - "c889e3621cb2977204b847c03930cde394cc16eaa63741f8ca07484a41f1aa87": { - "describe": { - "columns": [], - "nullable": [], - "parameters": { - "Right": 4 - } - }, - "query": "insert into plan_recipes (user_id, plan_date, recipe_id, count) values (?, ?, ?, ?)" - }, "c988364f9f83f4fa8bd0e594bab432ee7c9ec47ca40f4d16e5e2a8763653f377": { "describe": { "columns": [ diff --git a/kitchen/src/web/storage/mod.rs b/kitchen/src/web/storage/mod.rs index 68a9717..c609c14 100644 --- a/kitchen/src/web/storage/mod.rs +++ b/kitchen/src/web/storage/mod.rs @@ -199,7 +199,9 @@ pub struct SqliteStore { impl SqliteStore { pub async fn new>(path: P) -> sqlx::Result { let url = format!("sqlite://{}/store.db", path.as_ref().to_string_lossy()); - let options = SqliteConnectOptions::from_str(&url)?.journal_mode(SqliteJournalMode::Wal); + let options = SqliteConnectOptions::from_str(&url)? + .journal_mode(SqliteJournalMode::Wal) + .create_if_missing(true); info!(?options, "Connecting to sqlite db"); let pool = Arc::new(sqlx::SqlitePool::connect_with(options).await?); Ok(Self { pool, url }) diff --git a/kitchen/src/web/storage/save_meal_plan.sql b/kitchen/src/web/storage/save_meal_plan.sql index a632bd5..cc1ed0b 100644 --- a/kitchen/src/web/storage/save_meal_plan.sql +++ b/kitchen/src/web/storage/save_meal_plan.sql @@ -1 +1,2 @@ -insert into plan_recipes (user_id, plan_date, recipe_id, count) values (?, ?, ?, ?) \ No newline at end of file +insert into plan_recipes (user_id, plan_date, recipe_id, count) values (?, ?, ?, ?) + on conflict (user_id, plan_date, recipe_id) do update set count=excluded.count; \ No newline at end of file