Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused table pair impls #5362

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions crates/db_schema/src/impls/actor_language.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
diesel::JoinOnDsl,
newtypes::{CommunityId, InstanceId, LanguageId, LocalUserId, SiteId},
schema::{local_site, site, site_language},
schema::{community, community_language, local_site, local_user_language, site, site_language},
source::{
actor_language::{
CommunityLanguage,
Expand Down Expand Up @@ -226,22 +226,19 @@ impl CommunityLanguage {
conn: &mut AsyncPgConnection,
for_instance_id: InstanceId,
) -> Result<(), Error> {
use crate::schema::{
community::dsl as c,
community_language::dsl as cl,
site_language::dsl as sl,
};
let community_languages: Vec<LanguageId> = cl::community_language
.left_outer_join(sl::site_language.on(cl::language_id.eq(sl::language_id)))
.inner_join(c::community)
.filter(c::instance_id.eq(for_instance_id))
.filter(sl::language_id.is_null())
.select(cl::language_id)
let community_languages: Vec<LanguageId> = community_language::table
.left_join(
site_language::table.on(community_language::language_id.eq(site_language::language_id)),
)
.inner_join(community::table)
.filter(community::instance_id.eq(for_instance_id))
.filter(site_language::language_id.is_null())
.select(community_language::language_id)
.get_results(conn)
.await?;

for c in community_languages {
delete(cl::community_language.filter(cl::language_id.eq(c)))
delete(community_language::table.filter(community_language::language_id.eq(c)))
.execute(conn)
.await?;
}
Expand Down Expand Up @@ -325,15 +322,17 @@ pub async fn validate_post_language(
community_id: CommunityId,
local_user_id: LocalUserId,
) -> LemmyResult<LanguageId> {
use crate::schema::{community_language::dsl as cl, local_user_language::dsl as ul};
let conn = &mut get_conn(pool).await?;
let language_id = match language_id {
None | Some(LanguageId(0)) => {
let mut intersection = ul::local_user_language
.inner_join(cl::community_language.on(ul::language_id.eq(cl::language_id)))
.filter(ul::local_user_id.eq(local_user_id))
.filter(cl::community_id.eq(community_id))
.select(cl::language_id)
let mut intersection = local_user_language::table
.inner_join(
community_language::table
.on(local_user_language::language_id.eq(community_language::language_id)),
)
.filter(local_user_language::local_user_id.eq(local_user_id))
.filter(community_language::community_id.eq(community_id))
.select(community_language::language_id)
.get_results::<LanguageId>(conn)
.await?;

Expand Down
3 changes: 3 additions & 0 deletions crates/db_schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub mod aliases {
}
pub mod source;
#[cfg(feature = "full")]
#[rustfmt::skip]
mod table_pairs;
#[cfg(feature = "full")]
pub mod traits;
#[cfg(feature = "full")]
pub mod utils;
Expand Down
4 changes: 3 additions & 1 deletion crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,9 @@ diesel::joinable!(site_language -> language (language_id));
diesel::joinable!(site_language -> site (site_id));
diesel::joinable!(tag -> community (community_id));

diesel::allow_tables_to_appear_in_same_query!(
// TODO: patch file
macro_rules! nothing { ($($foo:tt)*) => {} }
nothing!(
admin_allow_instance,
admin_block_instance,
admin_purge_comment,
Expand Down
4 changes: 4 additions & 0 deletions crates/db_schema/src/table_pairs.rs

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions scripts/update_table_pairs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -e
shopt -s globstar

CWD="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"

cd "$CWD/../"

OUT=crates/db_schema/src/table_pairs.rs

echo "// @generated by scripts/update_table_pairs.sh" > $OUT
echo "use crate::schema::*;" >> $OUT
echo "use diesel::allow_tables_to_appear_in_same_query as a;" >> $OUT

for f in crates/**/**.rs src/**/**.rs
do
python3 -c "import re; import itertools; print(''.join(set('a!({}, {});'.format(min(pair), max(pair)) for fn_chunk in open('$f').read().split('fn ') for pair in itertools.combinations(re.findall('[^A-Z]([a-z_]+)::table[^!]', fn_chunk) + re.findall('[^A-Z]([a-z_]+_actions)::', fn_chunk), 2) if pair[0] != 'pg_namespace' and pair[1] != 'pg_namespace' and pair[0] != pair[1])), end = '')" >> $OUT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if we dont have to depend on another language for development, only for this small script. Besides the code is not readable like this.

done