Merge branch 'importers'

This commit is contained in:
Joe Ardent 2023-07-07 12:01:02 -07:00
commit c5993f57ee
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 -- table of what people want to watch
create table if not exists witch_watch ( create table if not exists witch_watch (
id blob not null primary key,
witch blob not null, witch blob not null,
watch 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 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, when_watched int,
last_updated int not null default (unixepoch()), last_updated int not null default (unixepoch()),
foreign key (witch) references witches (id) on delete cascade on update no action, 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 -- 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/ -- 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 let quests: Vec<WatchQuest> = movies
.choose_multiple(rng, num_movies) .choose_multiple(rng, num_movies)
.cloned() .cloned()
.map(|movie| { .map(|movie| WatchQuest {
let id = DbId::new();
WatchQuest {
id,
user, user,
watch: movie, watch: movie,
is_public: true, is_public: true,
already_watched: false, already_watched: false,
}
}) })
.collect(); .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() { if add_watch_quest_impl(db_pool, &quest).await.is_ok() {
Ok(()) Ok(())
} else { } else {
eprintln!("failed to add {}", quest.id); eprintln!("failed to add {:?}", quest.id());
Err(()) Err(())
} }
} }
pub async fn add_watch_quests(pool: &SqlitePool, quests: &[WatchQuest]) -> Result<(), ()> { pub async fn add_watch_quests(pool: &SqlitePool, quests: &[WatchQuest]) -> Result<(), ()> {
let mut builder = 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| { builder.push_values(quests.iter(), |mut b, quest| {
b.push_bind(quest.id) b.push_bind(quest.user)
.push_bind(quest.user)
.push_bind(quest.watch) .push_bind(quest.watch)
.push_bind(quest.is_public) .push_bind(quest.is_public)
.push_bind(quest.already_watched); .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_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 = 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 { const EMPTY_SEARCH_QUERY_STRUCT: SearchQuery = SearchQuery {
title: None, title: None,
@ -119,7 +119,6 @@ pub async fn post_add_new_watch(
if let Some(user) = auth.current_user { if let Some(user) = auth.current_user {
{ {
let watch_id = DbId::new(); let watch_id = DbId::new();
let witch_watch_id = DbId::new();
let release_date = year_to_epoch(form.year.as_deref()); let release_date = year_to_epoch(form.year.as_deref());
let watch = Watch { let watch = Watch {
id: watch_id, id: watch_id,
@ -131,7 +130,6 @@ pub async fn post_add_new_watch(
added_by: user.id, added_by: user.id,
}; };
let quest = WatchQuest { let quest = WatchQuest {
id: witch_watch_id,
user: user.id, user: user.id,
watch: watch_id, watch: watch_id,
is_public: !form.private, is_public: !form.private,
@ -173,7 +171,6 @@ pub(crate) async fn add_new_watch_impl(
if let Some(quest) = quest { if let Some(quest) = quest {
query(ADD_WITCH_WATCH_QUERY) query(ADD_WITCH_WATCH_QUERY)
.bind(quest.id)
.bind(quest.user) .bind(quest.user)
.bind(quest.watch) .bind(quest.watch)
.bind(quest.is_public) .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<(), ()> { pub async fn add_watch_quest_impl(pool: &SqlitePool, quest: &WatchQuest) -> Result<(), ()> {
query(ADD_WITCH_WATCH_QUERY) query(ADD_WITCH_WATCH_QUERY)
.bind(quest.id)
.bind(quest.user) .bind(quest.user)
.bind(quest.watch) .bind(quest.watch)
.bind(quest.is_public) .bind(quest.is_public)

View file

@ -95,9 +95,14 @@ impl Watch {
//-************************************************************************ //-************************************************************************
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WatchQuest { pub struct WatchQuest {
pub id: DbId,
pub user: DbId, pub user: DbId,
pub watch: DbId, pub watch: DbId,
pub is_public: bool, pub is_public: bool,
pub already_watched: bool, pub already_watched: bool,
} }
impl WatchQuest {
pub fn id(&self) -> (DbId, DbId) {
(self.user, self.watch)
}
}