Best results yet, just use (user, watch) for quest ID.

This commit is contained in:
Joe Ardent 2023-07-06 13:56:55 -07:00
parent 4c44aa12b0
commit 49d405fd10
6 changed files with 20 additions and 24 deletions

View file

@ -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/

View file

@ -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.

View file

@ -119,15 +119,11 @@ async fn add_quests(
let quests: Vec<WatchQuest> = movies
.choose_multiple(rng, num_movies)
.cloned()
.map(|movie| {
let id = DbId::new();
WatchQuest {
id,
.map(|movie| WatchQuest {
user,
watch: movie,
is_public: true,
already_watched: false,
}
})
.collect();

View file

@ -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);

View file

@ -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)

View file

@ -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)
}
}