Skip to content

Commit

Permalink
[hammerfest_store] Add touch_theme_page test
Browse files Browse the repository at this point in the history
This commit uses a `sqlx` fork to use the fix from launchbadge/sqlx#1112.
  • Loading branch information
demurgos committed Mar 16, 2021
1 parent 240a156 commit 79e620e
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 57 deletions.
12 changes: 4 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ members = [
"xtask",
"packages/native/native",
]

[patch.crates-io]
# Use domain-type fix from https://github.com/launchbadge/sqlx/pull/1112
sqlx = { git = "https://github.com/demurgos/sqlx", rev = "93b90be9f7dd316889aa01f3e6066062a133180c" }
11 changes: 6 additions & 5 deletions crates/core/src/hammerfest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ pub struct HammerfestDate {
pub month: u8,
// TODO: limit 1 <= d <= 31
pub day: u8,
/// Day of the week from 1 (Monday) to 7 (Sunday)
// TODO: limit 1 <= w <= 7
pub weekday: u8,
}
Expand All @@ -279,7 +280,7 @@ impl sqlx::Type<Postgres> for HammerfestDate {
}

fn compatible(ty: &postgres::PgTypeInfo) -> bool {
*ty == Self::type_info() || <&[u8] as sqlx::Type<Postgres>>::compatible(ty)
*ty == Self::type_info()
}
}

Expand All @@ -305,9 +306,9 @@ impl<'r> sqlx::Decode<'r, Postgres> for HammerfestDate {
impl<'q> sqlx::Encode<'q, Postgres> for HammerfestDate {
fn encode_by_ref(&self, buf: &mut postgres::PgArgumentBuffer) -> sqlx::encode::IsNull {
let mut encoder = postgres::types::PgRecordEncoder::new(buf);
encoder.encode(i32::from(self.month));
encoder.encode(i32::from(self.day));
encoder.encode(i32::from(self.weekday));
encoder.encode(crate::pg_num::PgU8::from(self.month));
encoder.encode(crate::pg_num::PgU8::from(self.day));
encoder.encode(crate::pg_num::PgU8::from(self.weekday));
encoder.finish();
sqlx::encode::IsNull::No
}
Expand All @@ -331,7 +332,7 @@ impl sqlx::Type<Postgres> for HammerfestDateTime {
}

fn compatible(ty: &postgres::PgTypeInfo) -> bool {
*ty == Self::type_info() || <&[u8] as sqlx::Type<Postgres>>::compatible(ty)
*ty == Self::type_info()
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[macro_use]
pub mod types;

#[cfg(feature = "sqlx")]
mod pg_num;

pub mod api;
pub mod auth;
pub mod clock;
Expand Down
46 changes: 46 additions & 0 deletions crates/core/src/pg_num.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use sqlx::{postgres, Postgres};
use std::convert::TryInto;
use std::error::Error;

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub(crate) struct PgU8(u8);

impl From<u8> for PgU8 {
fn from(v: u8) -> Self {
Self(v)
}
}

impl From<PgU8> for u8 {
fn from(v: PgU8) -> Self {
v.0
}
}

#[cfg(feature = "sqlx")]
impl sqlx::Type<Postgres> for PgU8 {
fn type_info() -> postgres::PgTypeInfo {
postgres::PgTypeInfo::with_name("u8")
}

fn compatible(ty: &postgres::PgTypeInfo) -> bool {
*ty == Self::type_info()
}
}

#[cfg(feature = "sqlx")]
impl<'r> sqlx::Decode<'r, Postgres> for PgU8 {
fn decode(value: postgres::PgValueRef<'r>) -> Result<Self, Box<dyn Error + 'static + Send + Sync>> {
let v: i16 = <i16 as sqlx::Decode<Postgres>>::decode(value)?;
let v: u8 = v.try_into().expect("invalid Postgres U8 value");
Ok(v.into())
}
}

#[cfg(feature = "sqlx")]
impl<'q> sqlx::Encode<'q, Postgres> for PgU8 {
fn encode_by_ref(&self, buf: &mut postgres::PgArgumentBuffer) -> sqlx::encode::IsNull {
let v: i16 = u8::from(*self).into();
v.encode(buf)
}
}
40 changes: 20 additions & 20 deletions crates/hammerfest_store/src/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ async fn touch_hammerfest_forum_theme(
INSERT
INTO hammerfest_forum_themes(hammerfest_server, hammerfest_theme_id, archived_at, title, description, is_public)
VALUES (
$1::HAMMERFEST_SERVER, $2::HAMMERFEST_THEME_ID, $3::INSTANT, $4::HAMMERFEST_THEME_TITLE, $5::HAMMERFEST_THEME_DESCRIPTION, $6::BOOLEAN
$1::HAMMERFEST_SERVER, $2::HAMMERFEST_FORUM_THEME_ID, $3::INSTANT, $4::HAMMERFEST_FORUM_THEME_TITLE, $5::HAMMERFEST_FORUM_THEME_DESCRIPTION, $6::BOOLEAN
)
ON CONFLICT (hammerfest_server, hammerfest_theme_id)
DO UPDATE SET
title = $4::HAMMERFEST_THEME_TITLE,
description = COALESCE(EXCLUDED.description, $5::HAMMERFEST_THEME_DESCRIPTION),
title = $4::HAMMERFEST_FORUM_THEME_TITLE,
description = COALESCE(EXCLUDED.description, $5::HAMMERFEST_FORUM_THEME_DESCRIPTION),
is_public = $6::BOOLEAN;
",
)
Expand All @@ -133,7 +133,7 @@ async fn touch_hammerfest_forum_theme_page_count(
let res: PgQueryResult = sqlx::query(upsert_archive_query!(
hammerfest_forum_theme_page_counts(
time($1 period, retrieved_at),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_theme_id::HAMMERFEST_THEME_ID),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_theme_id::HAMMERFEST_FORUM_THEME_ID),
data($4 page_count::U32),
)
))
Expand Down Expand Up @@ -161,9 +161,9 @@ async fn touch_hammerfest_forum_thread(
INSERT
INTO hammerfest_forum_threads(hammerfest_server, hammerfest_thread_id, archived_at)
VALUES (
$1::HAMMERFEST_SERVER, $2::HAMMERFEST_THREAD_ID, $3::INSTANT
$1::HAMMERFEST_SERVER, $2::HAMMERFEST_FORUM_THREAD_ID, $3::INSTANT
)
ON CONFLICT (hammerfest_server, hammerfest_theme_id) DO NOTHING;
ON CONFLICT (hammerfest_server, hammerfest_thread_id) DO NOTHING;
",
)
.bind(thread.server)
Expand All @@ -187,8 +187,8 @@ async fn touch_hammerfest_forum_thread_shared_meta(
let res: PgQueryResult = sqlx::query(upsert_archive_query!(
hammerfest_forum_thread_shared_meta(
time($1 period, retrieved_at),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_thread_id::HAMMERFEST_THREAD_ID),
data($4 title::HAMMERFEST_FORUM_THREAD_TITLE, $5 hammerfest_theme_id::HAMMERFEST_THEME_ID, $6 is_closed::BOOLEAN, $7 page_count::U32),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_thread_id::HAMMERFEST_FORUM_THREAD_ID),
data($4 hammerfest_theme_id::HAMMERFEST_FORUM_THEME_ID, $5 title::HAMMERFEST_FORUM_THREAD_TITLE, $6 is_closed::BOOLEAN, $7 page_count::U32),
)
))
.bind(now)
Expand All @@ -215,24 +215,24 @@ async fn touch_hammerfest_forum_thread_list_meta(
thread: HammerfestForumThreadIdRef,
page: NonZeroU16,
is_sticky: bool,
last_message_date: Option<HammerfestDate>,
latest_message_at: Option<HammerfestDate>,
author: HammerfestUserId,
reply_count: u16,
) -> Result<(), Box<dyn Error>> {
assert_eq!(is_sticky, last_message_date.is_none());
assert_eq!(is_sticky, latest_message_at.is_none());
let res: PgQueryResult = sqlx::query(upsert_archive_query!(
hammerfest_forum_threads_history(
hammerfest_forum_thread_list_meta(
time($1 period, retrieved_at),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_thread_id::HAMMERFEST_THREAD_ID),
data($4 page::U16, $5 is_sticky::BOOLEAN, $6 last_message_date::HAMMERFEST_DATE, $7 author::HAMMERFEST_USER_ID, $8 reply_count::U16),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_thread_id::HAMMERFEST_FORUM_THREAD_ID),
data($4 page::U16, $5 is_sticky::BOOLEAN, $6 latest_message_at::HAMMERFEST_DATE, $7 author::HAMMERFEST_USER_ID, $8 reply_count::U16),
)
))
.bind(now)
.bind(thread.server)
.bind(thread.id)
.bind(i64::from(page.get()))
.bind(is_sticky)
.bind(last_message_date)
.bind(latest_message_at)
.bind(author)
.bind(i64::from(reply_count))
.execute(&mut *tx)
Expand All @@ -259,7 +259,7 @@ async fn touch_hammerfest_forum_message(
let res: PgQueryResult = sqlx::query(upsert_archive_query!(
hammerfest_forum_messages_history(
time($1 period, retrieved_at),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_thread_id::HAMMERFEST_THREAD_ID, $4 page::U16, $5 offset_in_page::U8),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_thread_id::HAMMERFEST_FORUM_THREAD_ID, $4 page::U16, $5 offset_in_page::U8),
data($6 author::HAMMERFEST_USER_ID, $7 posted_at::HAMMERFEST_DATE_TIME, $8 remote_html_body::TEXT),
)
))
Expand Down Expand Up @@ -292,7 +292,7 @@ async fn touch_hammerfest_forum_message_id(
let res: PgQueryResult = sqlx::query(upsert_archive_query!(
hammerfest_forum_messages_history(
time($1 period, retrieved_at),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_thread_id::HAMMERFEST_THREAD_ID, $4 page::U16, $5 offset_in_page::U8),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_thread_id::HAMMERFEST_FORUM_THREAD_ID, $4 page::U16, $5 offset_in_page::U8),
data($6 hammerfest_message_id::HAMMERFEST_FORUM_MESSAGE_ID),
unique(mid(hammerfest_server, hammerfest_message_id)),
)
Expand Down Expand Up @@ -680,16 +680,16 @@ async fn touch_hammerfest_best_season_rank(
season_rank: Option<u32>,
) -> Result<(), Box<dyn Error>> {
let res: PgQueryResult = sqlx::query(upsert_archive_query!(
hammerfest_user_ranks(
hammerfest_best_season_rank(
time($1 period, retrieved_at),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_user_id::HAMMERFEST_USER_ID),
data($4 season_rank::U32?),
data($4 best_season_rank::U32?),
)
))
.bind(now)
.bind(user.server)
.bind(user.id)
.bind(season_rank.map(|x| i32::try_from(x).unwrap()))
.bind(season_rank.map(i64::from))
.execute(&mut *tx)
.await?;
// Affected row counts:
Expand All @@ -708,7 +708,7 @@ async fn touch_hammerfest_forum_role(
role: HammerfestForumRole,
) -> Result<(), Box<dyn Error>> {
let res: PgQueryResult = sqlx::query(upsert_archive_query!(
hammerfest_user_ranks(
hammerfest_forum_roles(
time($1 period, retrieved_at),
primary($2 hammerfest_server::HAMMERFEST_SERVER, $3 hammerfest_user_id::HAMMERFEST_USER_ID),
data($4 role::HAMMERFEST_FORUM_ROLE),
Expand Down
Loading

0 comments on commit 79e620e

Please sign in to comment.