Skip to content

Extract sql module with all custom sql_function!() calls #3935

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

Merged
merged 1 commit into from
Sep 21, 2021
Merged
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
2 changes: 1 addition & 1 deletion src/bin/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn check_stalled_update_downloads(conn: &PgConnection) -> Result<()> {

/// Check for known spam patterns
fn check_spam_attack(conn: &PgConnection) -> Result<()> {
use cargo_registry::models::krate::canon_crate_name;
use cargo_registry::sql::canon_crate_name;
use diesel::dsl::*;

const EVENT_KEY: &str = "spam_attack";
Expand Down
3 changes: 1 addition & 2 deletions src/controllers/krate/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ use crate::controllers::frontend_prelude::*;

use crate::models::{Crate, CrateVersions, Version, VersionDownload};
use crate::schema::version_downloads;
use crate::sql::to_char;
use crate::views::EncodableVersionDownload;

use crate::models::krate::to_char;

/// Handles the `GET /crates/:crate_id/downloads` route.
pub fn downloads(req: &mut dyn RequestExt) -> EndpointResult {
use diesel::dsl::*;
Expand Down
9 changes: 4 additions & 5 deletions src/controllers/krate/search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Endpoint for searching and discovery functionality

use diesel::dsl::*;
use diesel::sql_types::Array;
use diesel_full_text_search::*;
use indexmap::IndexMap;

Expand All @@ -14,7 +15,8 @@ use crate::util::errors::bad_request;
use crate::views::EncodableCrate;

use crate::controllers::helpers::pagination::{Page, Paginated, PaginationOptions};
use crate::models::krate::{canon_crate_name, ALL_COLUMNS};
use crate::models::krate::ALL_COLUMNS;
use crate::sql::{array_agg, canon_crate_name, lower};

/// Handles the `GET /crates` route.
/// Returns a list of crates. Called in a variety of scenarios in the
Expand Down Expand Up @@ -109,9 +111,6 @@ pub fn search(req: &mut dyn RequestExt) -> EndpointResult {
}

if let Some(kws) = params.get("all_keywords") {
use diesel::sql_types::Array;
sql_function!(#[aggregate] fn array_agg<T>(x: T) -> Array<T>);

// Calculating the total number of results with filters is not supported yet.
supports_seek = false;

Expand Down Expand Up @@ -141,7 +140,7 @@ pub fn search(req: &mut dyn RequestExt) -> EndpointResult {
crates_keywords::table
.select(crates_keywords::crate_id)
.inner_join(keywords::table)
.filter(crate::lower(keywords::keyword).eq(crate::lower(kw))),
.filter(lower(keywords::keyword).eq(lower(kw))),
),
);
} else if let Some(letter) = params.get("letter") {
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/user/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ use crate::controllers::frontend_prelude::*;

use crate::models::{CrateOwner, OwnerKind, User};
use crate::schema::{crate_owners, crates, users};
use crate::sql::lower;
use crate::views::EncodablePublicUser;

/// Handles the `GET /users/:user_id` route.
pub fn show(req: &mut dyn RequestExt) -> EndpointResult {
use self::users::dsl::{gh_login, id, users};

let name = crate::lower(&req.params()["user_id"]);
let name = lower(&req.params()["user_id"]);
let conn = req.db_conn()?;
let user: User = users
.filter(crate::lower(gh_login).eq(name))
.filter(lower(gh_login).eq(name))
.order(id.desc())
.first(&*conn)?;

Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub mod middleware;
mod publish_rate_limit;
pub mod render;
pub mod schema;
pub mod sql;
pub mod tasks;
mod test_util;
pub mod uploaders;
Expand Down Expand Up @@ -126,5 +127,3 @@ pub fn env_optional<T: FromStr>(s: &str) -> Option<T> {
.unwrap_or_else(|_| panic!("`{}` was defined but could not be parsed", s))
})
}

sql_function!(fn lower(x: ::diesel::sql_types::Text) -> ::diesel::sql_types::Text);
4 changes: 2 additions & 2 deletions src/models/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Category {
pub created_at: NaiveDateTime,
}

type WithSlug<'a> = diesel::dsl::Eq<categories::slug, crate::lower::HelperType<&'a str>>;
type WithSlug<'a> = diesel::dsl::Eq<categories::slug, crate::sql::lower::HelperType<&'a str>>;
type BySlug<'a> = diesel::dsl::Filter<categories::table, WithSlug<'a>>;
type WithSlugsCaseSensitive<'a> = diesel::dsl::Eq<
categories::slug,
Expand All @@ -40,7 +40,7 @@ pub struct CrateCategory {

impl Category {
pub fn with_slug(slug: &str) -> WithSlug<'_> {
categories::slug.eq(crate::lower(slug))
categories::slug.eq(crate::sql::lower(slug))
}

pub fn by_slug(slug: &str) -> BySlug<'_> {
Expand Down
3 changes: 2 additions & 1 deletion src/models/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use diesel::prelude::*;

use crate::models::Crate;
use crate::schema::*;
use crate::sql::lower;

#[derive(Clone, Identifiable, Queryable, Debug)]
pub struct Keyword {
Expand All @@ -25,7 +26,7 @@ pub struct CrateKeyword {
impl Keyword {
pub fn find_by_keyword(conn: &PgConnection, name: &str) -> QueryResult<Keyword> {
keywords::table
.filter(keywords::keyword.eq(crate::lower(name)))
.filter(keywords::keyword.eq(lower(name)))
.first(&*conn)
}

Expand Down
9 changes: 3 additions & 6 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chrono::NaiveDateTime;
use diesel::associations::Identifiable;
use diesel::pg::Pg;
use diesel::prelude::*;
use diesel::sql_types::Bool;
use diesel::sql_types::{Bool, Text};
use url::Url;

use crate::app::App;
Expand All @@ -17,6 +17,7 @@ use crate::util::errors::{cargo_err, AppResult};
use crate::models::helpers::with_count::*;
use crate::publish_rate_limit::PublishRateLimit;
use crate::schema::*;
use crate::sql::canon_crate_name;

#[derive(Debug, Queryable, Identifiable, Associations, Clone, Copy)]
#[belongs_to(Crate)]
Expand Down Expand Up @@ -72,7 +73,7 @@ pub const ALL_COLUMNS: AllColumns = (

pub const MAX_NAME_LENGTH: usize = 64;

type CanonCrateName<T> = self::canon_crate_name::HelperType<T>;
type CanonCrateName<T> = canon_crate_name::HelperType<T>;
type All = diesel::dsl::Select<crates::table, AllColumns>;
type WithName<'a> = diesel::dsl::Eq<CanonCrateName<crates::name>, CanonCrateName<&'a str>>;
type ByName<'a> = diesel::dsl::Filter<All, WithName<'a>>;
Expand Down Expand Up @@ -440,10 +441,6 @@ impl Crate {
}
}

use diesel::sql_types::{Date, Text};
sql_function!(fn canon_crate_name(x: Text) -> Text);
sql_function!(fn to_char(a: Date, b: Text) -> Text);

#[cfg(test)]
mod tests {
use crate::models::{Crate, NewCrate};
Expand Down
3 changes: 2 additions & 1 deletion src/models/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::util::errors::{cargo_err, AppResult};

use crate::models::{Crate, Team, User};
use crate::schema::{crate_owners, users};
use crate::sql::lower;

#[derive(Insertable, Associations, Identifiable, Debug, Clone, Copy)]
#[belongs_to(Crate)]
Expand Down Expand Up @@ -69,7 +70,7 @@ impl Owner {
)?))
} else {
users::table
.filter(crate::lower(users::gh_login).eq(name.to_lowercase()))
.filter(lower(users::gh_login).eq(name.to_lowercase()))
.filter(users::gh_id.ne(-1))
.order(users::gh_id.desc())
.first(conn)
Expand Down
12 changes: 2 additions & 10 deletions src/publish_rate_limit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use chrono::{NaiveDateTime, Utc};
use diesel::data_types::PgInterval;
use diesel::prelude::*;
use diesel::sql_types::Interval;
use std::time::Duration;

use crate::schema::{publish_limit_buckets, publish_rate_overrides};
use crate::sql::{date_part, floor, greatest, interval_part, least};
use crate::util::errors::{AppResult, TooManyRequests};

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -67,16 +69,6 @@ impl PublishRateLimit {
conn: &PgConnection,
) -> QueryResult<Bucket> {
use self::publish_limit_buckets::dsl::*;
use diesel::sql_types::{Double, Interval, Text, Timestamp};

sql_function!(fn date_part(x: Text, y: Timestamp) -> Double);
sql_function! {
#[sql_name = "date_part"]
fn interval_part(x: Text, y: Interval) -> Double;
}
sql_function!(fn floor(x: Double) -> Integer);
sql_function!(fn greatest<T>(x: T, y: T) -> T);
sql_function!(fn least<T>(x: T, y: T) -> T);

let burst: i32 = publish_rate_overrides::table
.find(uploader)
Expand Down
14 changes: 14 additions & 0 deletions src/sql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use diesel::sql_types::{Array, Date, Double, Interval, Text, Timestamp};

sql_function!(#[aggregate] fn array_agg<T>(x: T) -> Array<T>);
sql_function!(fn canon_crate_name(x: Text) -> Text);
sql_function!(fn to_char(a: Date, b: Text) -> Text);
sql_function!(fn lower(x: Text) -> Text);
sql_function!(fn date_part(x: Text, y: Timestamp) -> Double);
sql_function! {
#[sql_name = "date_part"]
fn interval_part(x: Text, y: Interval) -> Double;
}
sql_function!(fn floor(x: Double) -> Integer);
sql_function!(fn greatest<T>(x: T, y: T) -> T);
sql_function!(fn least<T>(x: T, y: T) -> T);