Skip to content

Commit 5d6d945

Browse files
ahmadbkyCopilot
andauthored
fix: rename SQL function to remove style of names
* Make the `rm_mp_style` SQL function to remove link tags * Add `unstyled` function in the entity crate * Use new `unstyled` function in entity everywhere needed. Refs: #115. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 9693b46 commit 5d6d945

File tree

10 files changed

+73
-30
lines changed

10 files changed

+73
-30
lines changed

crates/entity/src/functions.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use sea_orm::{
2+
IntoSimpleExpr,
3+
sea_query::{Func, SimpleExpr},
4+
};
5+
6+
pub fn unstyled(v: impl IntoSimpleExpr) -> SimpleExpr {
7+
Func::cust("rm_mp_style").arg(v.into_simple_expr()).into()
8+
}

crates/entity/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod entities;
22
pub use entities::*;
33

4+
pub mod functions;
45
pub mod types;
56

67
pub mod current_bans;

crates/graphql-api/src/objects/root.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use async_graphql::{
33
connection::{self, CursorType},
44
};
55
use deadpool_redis::redis::{AsyncCommands, ToRedisArgs};
6-
use entity::{event as event_entity, event_edition, global_records, maps, players, records};
6+
use entity::{
7+
event as event_entity, event_edition, functions, global_records, maps, players, records,
8+
};
79
use records_lib::{
810
Database, RedisConnection, RedisPool, must,
911
opt_event::OptEvent,
@@ -597,7 +599,7 @@ where
597599
};
598600

599601
let mut query = players::Entity::find().expr_as(
600-
Func::cust("rm_mp_style").arg(Expr::col((players::Entity, players::Column::Name))),
602+
functions::unstyled(players::Column::Name),
601603
"unstyled_player_name",
602604
);
603605
let query = SelectStatement::new()
@@ -707,10 +709,8 @@ where
707709
},
708710
};
709711

710-
let mut query = maps::Entity::find().expr_as(
711-
Func::cust("rm_mp_style").arg(Expr::col((maps::Entity, maps::Column::Name))),
712-
"unstyled_map_name",
713-
);
712+
let mut query =
713+
maps::Entity::find().expr_as(functions::unstyled(maps::Column::Name), "unstyled_map_name");
714714
let query = SelectStatement::new()
715715
.column(Asterisk)
716716
.from_subquery(QuerySelect::query(&mut query).take(), "map")
@@ -733,8 +733,7 @@ where
733733
})
734734
.apply_if(filter.player_name, |query, name| {
735735
query.and_where(
736-
Func::cust("rm_mp_style")
737-
.arg(Expr::col(("author", players::Column::Name)))
736+
functions::unstyled(Expr::col(("author", players::Column::Name)))
738737
.like(format!("%{name}%")),
739738
);
740739
});

crates/graphql-api/src/utils/records_filter.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use entity::{global_event_records, global_records, maps, players, records};
1+
use entity::{functions, global_event_records, global_records, maps, players, records};
22
use sea_orm::{
33
ColumnTrait, EntityTrait, JoinType, QueryFilter as _, QuerySelect as _, RelationDef,
4-
RelationTrait as _, Select,
5-
prelude::Expr,
6-
sea_query::{ExprTrait as _, Func},
4+
RelationTrait as _, Select, prelude::Expr,
75
};
86

97
use crate::objects::records_filter::RecordsFilter;
@@ -99,8 +97,7 @@ where
9997
// Apply player name filter
10098
if let Some(name) = &filter.player_name {
10199
query = query.filter(
102-
Func::cust("rm_mp_style")
103-
.arg(Expr::col(("p", players::Column::Name)))
100+
functions::unstyled(Expr::col(("p", players::Column::Name)))
104101
.like(format!("%{name}%")),
105102
);
106103
}
@@ -115,9 +112,7 @@ where
115112
// Apply map name filter
116113
if let Some(name) = &filter.map_name {
117114
query = query.filter(
118-
Func::cust("rm_mp_style")
119-
.arg(Expr::col(("m", maps::Column::Name)))
120-
.like(format!("%{name}%")),
115+
functions::unstyled(Expr::col(("m", maps::Column::Name))).like(format!("%{name}%")),
121116
);
122117
}
123118

@@ -131,8 +126,7 @@ where
131126
// Apply player name filter
132127
if let Some(name) = &filter.player_name {
133128
query = query.filter(
134-
Func::cust("rm_mp_style")
135-
.arg(Expr::col(("p2", players::Column::Name)))
129+
functions::unstyled(Expr::col(("p2", players::Column::Name)))
136130
.like(format!("%{name}%")),
137131
);
138132
}

crates/migration/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod m20250918_222203_add_event_edition_maps_disability;
99
mod m20251002_100827_add_rm_mp_style_func;
1010
mod m20251004_214656_add_maps_medal_times;
1111
mod m20260103_142202_players_maps_score;
12+
mod m20260109_101455_refactor_rm_mp_style;
1213

1314
use sea_orm_migration::prelude::*;
1415

@@ -31,6 +32,7 @@ impl MigratorTrait for Migrator {
3132
Box::new(m20251002_100827_add_rm_mp_style_func::Migration),
3233
Box::new(m20251004_214656_add_maps_medal_times::Migration),
3334
Box::new(m20260103_142202_players_maps_score::Migration),
35+
Box::new(m20260109_101455_refactor_rm_mp_style::Migration),
3436
]
3537
}
3638
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
mod func_management;
2+
3+
use sea_orm_migration::prelude::*;
4+
5+
#[derive(DeriveMigrationName)]
6+
pub struct Migration;
7+
8+
#[async_trait::async_trait]
9+
impl MigrationTrait for Migration {
10+
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
11+
manager
12+
.get_connection()
13+
.execute_unprepared("drop function rm_mp_style")
14+
.await?;
15+
manager
16+
.get_connection()
17+
.execute_unprepared(func_management::CREATE_NEW_FUNCTION_RM_MP_STYLE)
18+
.await?;
19+
Ok(())
20+
}
21+
22+
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
23+
manager
24+
.get_connection()
25+
.execute_unprepared("drop function rm_mp_style")
26+
.await?;
27+
manager
28+
.get_connection()
29+
.execute_unprepared(func_management::CREATE_OLD_FUNCTION_RM_MP_STYLE)
30+
.await?;
31+
Ok(())
32+
}
33+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
create or replace function rm_mp_style(in login text) returns text collate 'utf8mb4_unicode_ci'
2+
begin
3+
return replace(regexp_replace(login, '\\$(([wWnNoOiItTsSgGzZ<>]|([lL](\\[.*?\\])?))|[aAbBcCdDeEfF0-9]([aAbBcCdDeEfF0-9]{2})?)', ''),
4+
'$$', '$');
5+
end;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
create or replace function rm_mp_style(in login text) returns text collate 'utf8mb4_unicode_ci'
2+
begin
3+
return replace(regexp_replace(login, '\\$([wWnNoOiItTsSgGzZ<>]|[aAbBcCdDeEfF0-9]([aAbBcCdDeEfF0-9]{2})?)', ''),
4+
'$$', '$');
5+
end;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub const CREATE_OLD_FUNCTION_RM_MP_STYLE: &str =
2+
include_str!("./create_old_function_rm_mp_style.sql");
3+
pub const CREATE_NEW_FUNCTION_RM_MP_STYLE: &str =
4+
include_str!("./create_new_function_rm_mp_style.sql");

crates/player-map-ranking/src/lib.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use std::{collections::HashMap, hash::Hash};
22

33
use anyhow::Context as _;
4-
use entity::{global_records, maps, players};
4+
use entity::{functions, global_records, maps, players};
55
use sea_orm::{
66
ColumnTrait as _, ConnectionTrait, EntityTrait as _, QueryFilter as _, QueryOrder, QuerySelect,
77
QueryTrait,
8-
prelude::Expr,
9-
sea_query::Func,
108
sqlx::types::chrono::{DateTime, Utc},
119
};
1210

@@ -91,10 +89,7 @@ pub async fn compute_scores<C: ConnectionTrait>(
9189
from: Option<DateTime<Utc>>,
9290
) -> anyhow::Result<Scores> {
9391
let mut maps = maps::Entity::find()
94-
.expr_as(
95-
Func::cust("rm_mp_style").arg(Expr::col((maps::Entity, maps::Column::Name))),
96-
"unstyled_name",
97-
)
92+
.expr_as(functions::unstyled(maps::Column::Name), "unstyled_name")
9893
.into_model::<RawMap>()
9994
.all(conn)
10095
.await
@@ -104,10 +99,7 @@ pub async fn compute_scores<C: ConnectionTrait>(
10499
.collect::<HashMap<_, _>>();
105100

106101
let mut players = players::Entity::find()
107-
.expr_as(
108-
Func::cust("rm_mp_style").arg(Expr::col((players::Entity, players::Column::Name))),
109-
"unstyled_name",
110-
)
102+
.expr_as(functions::unstyled(players::Column::Name), "unstyled_name")
111103
.into_model::<RawPlayer>()
112104
.all(conn)
113105
.await

0 commit comments

Comments
 (0)