Skip to content

Commit 19a809f

Browse files
Merge pull request #2509 from HTGAzureX1212/nightly
Discord Frontend: Migrate to New Queries
2 parents 3c26c2c + 8de2323 commit 19a809f

File tree

16 files changed

+182
-223
lines changed

16 files changed

+182
-223
lines changed

discord-frontend/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

discord-frontend/hartex-discord-commands/src/utilities/info/info_emoji.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ use hartex_discord_utils::interaction::embed_response;
4242
use hartex_discord_utils::interaction::ephemeral_error_response;
4343
use hartex_discord_utils::localizable::Localizable;
4444
use hartex_discord_utils::markdown::MarkdownStyle;
45-
use hartex_discord_utils::postgres::PostgresErrorExt;
4645
use hartex_localization_core::Localizer;
4746
use miette::IntoDiagnostic;
4847
use regex::Regex;
49-
use tokio_postgres::error::SqlState;
5048

5149
lazy_static::lazy_static! {
5250
/// The regex for looking for a Discord emoji in the command input.
@@ -109,7 +107,7 @@ pub async fn execute(
109107
let result = CachedEmojiRepository.get(emoji_id).await;
110108
let emoji = match result {
111109
Ok(emoji) => emoji,
112-
Err(CacheError::Postgres(postgres_error)) if postgres_error.is(SqlState::NO_DATA) => {
110+
Err(CacheError::Database(_)) /*if postgres_error.is(SqlState::NO_DATA)*/ => {
113111
interaction_client
114112
.create_response(
115113
interaction.id,

discord-frontend/hartex-discord-entitycache-core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ rust-version = "1.86.0"
1414
hartex_discord_core = { path = "../hartex-discord-core", features = ["discord-model"] }
1515
hartex_discord_entitycache_macros = { path = "../hartex-discord-entitycache-macros", optional = true, default-features = false, features = ["discord_model_git"] }
1616

17+
hartex_database_queries = { path = "../../database/hartex-database-queries" }
18+
1719
bb8 = "0.9.0"
1820
tokio-postgres = "0.7.12"
1921

discord-frontend/hartex-discord-entitycache-core/src/error.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,32 @@ use std::fmt;
2828
use std::fmt::Display;
2929
use std::fmt::Formatter;
3030

31-
use bb8::RunError;
32-
use tokio_postgres::Error as PostgresError;
31+
use hartex_database_queries::result::Error as DatabaseError;
3332

3433
/// A cache error..
3534
#[allow(clippy::module_name_repetitions)]
3635
#[derive(Debug)]
3736
pub enum CacheError {
38-
Bb8(RunError<PostgresError>),
3937
/// Error related to environment variables.
4038
Env(VarError),
4139
/// A postgres error occurred.
42-
Postgres(PostgresError),
40+
Database(DatabaseError),
4341
}
4442

4543
impl Display for CacheError {
4644
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
4745
match self {
48-
Self::Bb8(error) => writeln!(f, "bb8 postgres error: {error}"),
4946
Self::Env(error) => writeln!(f, "env error: {error}"),
50-
Self::Postgres(error) => writeln!(f, "postgres error: {error}"),
47+
Self::Database(error) => writeln!(f, "database error: {error}"),
5148
}
5249
}
5350
}
5451

5552
impl Error for CacheError {}
5653

57-
impl From<RunError<PostgresError>> for CacheError {
58-
fn from(error: RunError<PostgresError>) -> Self {
59-
Self::Bb8(error)
60-
}
61-
}
62-
63-
impl From<PostgresError> for CacheError {
64-
fn from(error: PostgresError) -> Self {
65-
Self::Postgres(error)
54+
impl From<DatabaseError> for CacheError {
55+
fn from(error: DatabaseError) -> Self {
56+
Self::Database(error)
6657
}
6758
}
6859

discord-frontend/hartex-discord-entitycache-entities/src/emoji.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use hartex_discord_entitycache_core::entity;
2525
/// An emoji entity.
2626
#[entity(
2727
from = "twilight_model::guild::Emoji",
28-
assume = ["CachedEmojiSelectByGuildId", "CachedEmojiSelectById"],
28+
assume = ["NightlyCachedEmojis"],
2929
id = ["id"],
3030
include = [
3131
"animated",

discord-frontend/hartex-discord-entitycache-entities/src/guild.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use hartex_discord_entitycache_core::entity;
2828
#[allow(clippy::module_name_repetitions)]
2929
#[entity(
3030
from = "twilight_model::guild::Guild",
31-
assume = ["CachedGuildSelectById"],
31+
assume = ["NightlyCachedGuilds"],
3232
id = ["id"],
3333
include = [
3434
"default_message_notifications",

discord-frontend/hartex-discord-entitycache-entities/src/member.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use hartex_discord_entitycache_core::entity;
2626
#[allow(clippy::module_name_repetitions)]
2727
#[entity(
2828
from = "twilight_model::guild::Member",
29-
assume = ["CachedMemberSelectByGuildId", "CachedMemberSelectByUserIdAndGuildId"],
29+
assume = ["NightlyCachedMembers"],
3030
id = ["guild_id", "user_id"],
3131
include = ["flags", "joined_at", "nick", "roles"],
3232
extra = [

discord-frontend/hartex-discord-entitycache-entities/src/role.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use hartex_discord_entitycache_core::entity;
2626
#[allow(clippy::module_name_repetitions)]
2727
#[entity(
2828
from = "twilight_model::guild::Role",
29-
assume = ["CachedRoleSelectByGuildId", "CachedRoleSelectByIdAndGuildId"],
29+
assume = ["NightlyCachedRoles"],
3030
id = ["guild_id", "id"],
3131
include = ["color", "flags", "hoist", "icon", "managed", "mentionable", "position"],
3232
extra = [

discord-frontend/hartex-discord-entitycache-entities/src/user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use hartex_discord_entitycache_core::entity;
2626
#[allow(clippy::module_name_repetitions)]
2727
#[entity(
2828
from = "twilight_model::user::User",
29-
assume = ["CachedUserSelectById"],
29+
assume = ["NightlyCachedUsers"],
3030
id = ["id"],
3131
include = [
3232
"avatar",

discord-frontend/hartex-discord-entitycache-macros/src/entity.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -368,26 +368,23 @@ pub fn implement_entity(input: &EntityMacroInput, item_struct: &ItemStruct) -> O
368368
};
369369
let ret_type = syn::parse_str::<Type>(hashmap.get(&*entity).unwrap()).unwrap();
370370

371-
let query_function_name = make_query_function_name(first, &element.as_value.value());
371+
let (query_module_name, query_struct_name) =
372+
make_query_function_name(first, &element.as_value.value());
372373
// FIXME: bad assumption of always calling .to_string() here (mostly just that should suffice, but...)
373374
let mut full_query_function_call = quote! {
374-
let data = hartex_database_queries::discord_frontend::queries::#query_function_name::#query_function_name().bind(client, &#param_name.to_string())
375+
let data = hartex_database_queries::queries::discord_frontend::#query_module_name::#query_struct_name::bind(#param_name.to_string()).executor().await?
375376
};
376377

377378
let function = match &*element.unique_or_multiple.to_string() {
378379
"multiple" => {
379380
let ident = Ident::new(&pluralize(first, 2, false), Span::call_site());
380381

381382
full_query_function_call.append_all(quote! {
382-
.all().await?;
383+
.many().await?;
383384
});
384385

385386
quote! {
386387
pub async fn #ident(&self, #param_decl) -> hartex_discord_entitycache_core::error::CacheResult<Vec<#ret_type>> {
387-
let pinned = std::pin::Pin::static_ref(&hartex_discord_utils::DATABASE_POOL).await;
388-
let pooled = pinned.get().await?;
389-
let client = pooled.client();
390-
391388
#full_query_function_call
392389

393390
Ok(data.into_iter().map(|thing| #ret_type::from(thing)).collect())
@@ -403,10 +400,6 @@ pub fn implement_entity(input: &EntityMacroInput, item_struct: &ItemStruct) -> O
403400

404401
quote! {
405402
pub async fn #ident(&self, #param_decl) -> hartex_discord_entitycache_core::error::CacheResult<#ret_type> {
406-
let pinned = std::pin::Pin::static_ref(&hartex_discord_utils::DATABASE_POOL).await;
407-
let pooled = pinned.get().await?;
408-
let client = pooled.client();
409-
410403
#full_query_function_call
411404

412405
Ok(#ret_type::from(data))
@@ -423,10 +416,9 @@ pub fn implement_entity(input: &EntityMacroInput, item_struct: &ItemStruct) -> O
423416
if input.extra_fields_array.elements.is_empty() {
424417
let extra = assumed_extra_impls
425418
.map(|str| {
426-
let ident_snake = Ident::new(&str.to_case(Case::Snake), Span::call_site());
427419
let ident_pascal = Ident::new(&str.to_case(Case::Pascal), Span::call_site());
428420
let full_ident =
429-
quote! {hartex_database_queries::discord_frontend::queries::#ident_snake::#ident_pascal};
421+
quote! {hartex_database_queries::tables::discord_frontend::#ident_pascal};
430422

431423
quote! {
432424
impl From<#full_ident> for #item_struct_name {
@@ -484,10 +476,9 @@ pub fn implement_entity(input: &EntityMacroInput, item_struct: &ItemStruct) -> O
484476
.multiunzip();
485477
let extra = assumed_extra_impls
486478
.map(|str| {
487-
let ident_snake = Ident::new(&str.to_case(Case::Snake), Span::call_site());
488479
let ident_pascal = Ident::new(&str.to_case(Case::Pascal), Span::call_site());
489480
let full_ident =
490-
quote! {hartex_database_queries::discord_frontend::queries::#ident_snake::#ident_pascal};
481+
quote! {hartex_database_queries::tables::discord_frontend::#ident_pascal};
491482

492483
quote! {
493484
impl From<#full_ident> for #item_struct_name {
@@ -568,7 +559,7 @@ fn make_field_decl_and_assignments(
568559
return (
569560
quote! {pub #field_name: #field_type},
570561
quote! {#field_name: model.#field_name},
571-
quote! {#field_name: model.#field_name.parse().unwrap()},
562+
quote! {#field_name: model.#field_name().parse().unwrap()},
572563
);
573564
}
574565

@@ -582,85 +573,100 @@ fn make_field_decl_and_assignments(
582573
(
583574
quote! {pub #field_name: #field_type},
584575
quote! {#field_name: model.#field_name},
585-
quote! {#field_name: #field_type::from(model.#field_name as u8)},
576+
quote! {#field_name: #field_type::from(model.#field_name() as u8)},
586577
)
587578
} else if field_type.is("Id") {
588579
(
589580
quote! {pub #field_name: #field_type},
590581
quote! {#field_name: model.#field_name},
591-
quote! {#field_name: std::str::FromStr::from_str(&model.#field_name).unwrap()},
582+
quote! {#field_name: std::str::FromStr::from_str(model.#field_name()).unwrap()},
592583
)
593584
} else if field_type.is("MemberFlags") {
594585
(
595586
quote! {pub #field_name: #field_type},
596587
quote! {#field_name: model.#field_name},
597-
quote! {#field_name: twilight_model::guild::MemberFlags::from_bits(model.#field_name as u64).unwrap()},
588+
quote! {#field_name: twilight_model::guild::MemberFlags::from_bits(model.#field_name() as u64).unwrap()},
598589
)
599590
} else if field_type.is("RoleFlags") {
600591
(
601592
quote! {pub #field_name: #field_type},
602593
quote! {#field_name: model.#field_name},
603-
quote! {#field_name: twilight_model::guild::RoleFlags::from_bits(model.#field_name as u64).unwrap()},
594+
quote! {#field_name: twilight_model::guild::RoleFlags::from_bits(model.#field_name() as u64).unwrap()},
604595
)
605596
} else if field_type.is_option_of("ImageHash") {
606597
(
607598
quote! {pub #field_name: #field_type},
608599
quote! {#field_name: model.#field_name},
609-
quote! {#field_name: model.#field_name.as_deref().map(|str| std::str::FromStr::from_str(str).unwrap())},
600+
quote! {#field_name: model.#field_name().map(|str| std::str::FromStr::from_str(str).unwrap())},
610601
)
611602
} else if field_type.is_option_of("Timestamp") {
612603
(
613604
quote! {pub #field_name: #field_type},
614605
quote! {#field_name: model.#field_name},
615-
quote! {#field_name: model.#field_name.map(|timestamp| twilight_model::util::Timestamp::from_secs(timestamp.unix_timestamp()).unwrap())},
606+
quote! {#field_name: model.#field_name().map(|timestamp| twilight_model::util::Timestamp::from_secs(timestamp.timestamp()).unwrap())},
607+
)
608+
} else if field_type.is_option_of("String") {
609+
(
610+
quote! {pub #field_name: #field_type},
611+
quote! {#field_name: model.#field_name},
612+
quote! {#field_name: model.#field_name().map(String::from)},
616613
)
617614
} else if field_type.is("i64") {
618615
(
619616
quote! {pub #field_name: #field_type},
620617
quote! {#field_name: model.#field_name},
621-
quote! {#field_name: model.#field_name as i64},
618+
quote! {#field_name: model.#field_name() as i64},
622619
)
623620
} else if field_type.is("u32") {
624621
(
625622
quote! {pub #field_name: #field_type},
626623
quote! {#field_name: model.#field_name},
627-
quote! {#field_name: model.#field_name as u32},
624+
quote! {#field_name: model.#field_name() as u32},
625+
)
626+
} else if field_type.is("String") {
627+
(
628+
quote! {pub #field_name: #field_type},
629+
quote! {#field_name: model.#field_name},
630+
quote! {#field_name: model.#field_name().to_string()},
628631
)
629632
} else if field_type.is_option_of("u64") {
630633
(
631634
quote! {pub #field_name: #field_type},
632635
quote! {#field_name: model.#field_name},
633-
quote! {#field_name: model.#field_name.map(|i| i as u64)},
636+
quote! {#field_name: model.#field_name().map(|i| i as u64)},
634637
)
635638
} else if field_type.is_vec_of("GuildFeature") {
636639
(
637640
quote! {pub #field_name: #field_type},
638641
quote! {#field_name: model.#field_name},
639-
quote! {#field_name: model.#field_name.iter().cloned().map(From::from).collect()},
642+
quote! {#field_name: model.#field_name().iter().cloned().map(From::from).collect()},
640643
)
641644
} else if field_type.is_vec_of("Id") {
642645
(
643646
quote! {pub #field_name: #field_type},
644647
quote! {#field_name: model.#field_name},
645-
quote! {#field_name: model.#field_name.iter().map(|str| std::str::FromStr::from_str(str).unwrap()).collect()},
648+
quote! {#field_name: model.#field_name().iter().map(|str| std::str::FromStr::from_str(str).unwrap()).collect()},
646649
)
647650
} else {
648651
(
649652
quote! {pub #field_name: #field_type},
650653
quote! {#field_name: model.#field_name},
651-
quote! {#field_name: model.#field_name},
654+
quote! {#field_name: model.#field_name()},
652655
)
653656
}
654657
}
655658

656659
// FIXME: may need to generalize for multiple fields
657660
/// Construct an identifier containing the database query function name.
658-
fn make_query_function_name(target_entity: &str, by_field: &str) -> Ident {
661+
fn make_query_function_name(target_entity: &str, by_field: &str) -> (Ident, Ident) {
659662
let name = format!(
660663
"cached_{}_select_by_{}",
661664
target_entity.to_lowercase(),
662665
by_field.to_lowercase()
663666
);
664667

665-
Ident::new(&name, Span::call_site())
668+
(
669+
Ident::new(&name, Span::call_site()),
670+
Ident::new(name.to_case(Case::Pascal).as_str(), Span::call_site()),
671+
)
666672
}

discord-frontend/hartex-discord-entitycache-repositories/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ hartex_discord_entitycache_entities = { path = "../hartex-discord-entitycache-en
1919

2020
hartex_discord_utils = { path = "../../rust-utilities/hartex-discord-utils" }
2121

22+
chrono = "0.4.39"
2223
serde_scan = "0.4.1"
2324
time = "0.3.37"
2425
tokio-postgres = "0.7.12"

0 commit comments

Comments
 (0)