From 49d405fd1000b112ec5b75e0b9def7e38acd8b53 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Thu, 6 Jul 2023 13:56:55 -0700 Subject: [PATCH] Best results yet, just use (user, watch) for quest ID. --- migrations/20230426221940_init.up.sql | 6 +++--- results.txt | 4 ++-- src/bin/import_users.rs | 14 +++++--------- src/import_utils.rs | 7 +++---- src/watches/handlers.rs | 6 +----- src/watches/mod.rs | 7 ++++++- 6 files changed, 20 insertions(+), 24 deletions(-) diff --git a/migrations/20230426221940_init.up.sql b/migrations/20230426221940_init.up.sql index 3b9d91a..9cc1f88 100644 --- a/migrations/20230426221940_init.up.sql +++ b/migrations/20230426221940_init.up.sql @@ -29,7 +29,6 @@ create table if not exists watches ( -- table of what people want to watch create table if not exists witch_watch ( - id blob not null primary key, witch blob not null, watch blob not null, party blob, -- list of witch IDs, but we can also scan for friends that want to watch the same thing @@ -40,8 +39,9 @@ create table if not exists witch_watch ( when_watched int, last_updated int not null default (unixepoch()), foreign key (witch) references witches (id) on delete cascade on update no action, - foreign key (watch) references watches (id) on delete cascade on update no action -); + foreign key (watch) references watches (id) on delete cascade on update no action, + primary key (witch, watch) +) without rowid; -- friend lists; this should really be a graph db, maybe the whole thing should be -- TODO: look into replacing sqlite with https://www.cozodb.org/ diff --git a/results.txt b/results.txt index c8d9d82..1f89676 100644 --- a/results.txt +++ b/results.txt @@ -1,3 +1,3 @@ --rw-r--r-- 1 ardent ardent 17M Jul 6 10:05 /home/ardent/.witch-watch.db +-rw-r--r-- 1 ardent ardent 16M Jul 6 13:51 /home/ardent/.witch-watch.db -6 seconds to add 98,713 watch quests. +6 seconds to add 99,612 watch quests. diff --git a/src/bin/import_users.rs b/src/bin/import_users.rs index 20a374d..e39da34 100644 --- a/src/bin/import_users.rs +++ b/src/bin/import_users.rs @@ -119,15 +119,11 @@ async fn add_quests( let quests: Vec = movies .choose_multiple(rng, num_movies) .cloned() - .map(|movie| { - let id = DbId::new(); - WatchQuest { - id, - user, - watch: movie, - is_public: true, - already_watched: false, - } + .map(|movie| WatchQuest { + user, + watch: movie, + is_public: true, + already_watched: false, }) .collect(); diff --git a/src/import_utils.rs b/src/import_utils.rs index 30a7b2c..5fd5ed2 100644 --- a/src/import_utils.rs +++ b/src/import_utils.rs @@ -72,17 +72,16 @@ pub async fn add_watch_quest(db_pool: &SqlitePool, quest: WatchQuest) -> Result< if add_watch_quest_impl(db_pool, &quest).await.is_ok() { Ok(()) } else { - eprintln!("failed to add {}", quest.id); + eprintln!("failed to add {:?}", quest.id()); Err(()) } } pub async fn add_watch_quests(pool: &SqlitePool, quests: &[WatchQuest]) -> Result<(), ()> { let mut builder = - sqlx::QueryBuilder::new("insert into witch_watch (id, witch, watch, public, watched) "); + sqlx::QueryBuilder::new("insert into witch_watch (witch, watch, public, watched) "); builder.push_values(quests.iter(), |mut b, quest| { - b.push_bind(quest.id) - .push_bind(quest.user) + b.push_bind(quest.user) .push_bind(quest.watch) .push_bind(quest.is_public) .push_bind(quest.already_watched); diff --git a/src/watches/handlers.rs b/src/watches/handlers.rs index 28a38ef..c252bb9 100644 --- a/src/watches/handlers.rs +++ b/src/watches/handlers.rs @@ -24,7 +24,7 @@ const GET_WATCH_QUERY: &str = "select * from watches where id = $1"; const ADD_WATCH_QUERY: &str = "insert into watches (id, title, kind, release_date, metadata_url, added_by) values ($1, $2, $3, $4, $5, $6)"; const ADD_WITCH_WATCH_QUERY: &str = - "insert into witch_watch (id, witch, watch, public, watched) values ($1, $2, $3, $4, $5)"; + "insert into witch_watch (witch, watch, public, watched) values ($1, $2, $3, $4)"; const EMPTY_SEARCH_QUERY_STRUCT: SearchQuery = SearchQuery { title: None, @@ -119,7 +119,6 @@ pub async fn post_add_new_watch( if let Some(user) = auth.current_user { { let watch_id = DbId::new(); - let witch_watch_id = DbId::new(); let release_date = year_to_epoch(form.year.as_deref()); let watch = Watch { id: watch_id, @@ -131,7 +130,6 @@ pub async fn post_add_new_watch( added_by: user.id, }; let quest = WatchQuest { - id: witch_watch_id, user: user.id, watch: watch_id, is_public: !form.private, @@ -173,7 +171,6 @@ pub(crate) async fn add_new_watch_impl( if let Some(quest) = quest { query(ADD_WITCH_WATCH_QUERY) - .bind(quest.id) .bind(quest.user) .bind(quest.watch) .bind(quest.is_public) @@ -204,7 +201,6 @@ pub async fn post_add_watch_quest( pub async fn add_watch_quest_impl(pool: &SqlitePool, quest: &WatchQuest) -> Result<(), ()> { query(ADD_WITCH_WATCH_QUERY) - .bind(quest.id) .bind(quest.user) .bind(quest.watch) .bind(quest.is_public) diff --git a/src/watches/mod.rs b/src/watches/mod.rs index c4a2a9a..8d5ee3b 100644 --- a/src/watches/mod.rs +++ b/src/watches/mod.rs @@ -95,9 +95,14 @@ impl Watch { //-************************************************************************ #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct WatchQuest { - pub id: DbId, pub user: DbId, pub watch: DbId, pub is_public: bool, pub already_watched: bool, } + +impl WatchQuest { + pub fn id(&self) -> (DbId, DbId) { + (self.user, self.watch) + } +}