Skip to content

Commit defd145

Browse files
authored
Extract crates_io_diesel_helpers crate (#10291)
This makes it possible to compile them concurrently and also makes them potentially usable in other workspace crates.
1 parent 94a4672 commit defd145

File tree

23 files changed

+87
-42
lines changed

23 files changed

+87
-42
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ colored = "=2.2.0"
5858
crates_io_cdn_logs = { path = "crates/crates_io_cdn_logs" }
5959
crates_io_database = { path = "crates/crates_io_database" }
6060
crates_io_database_dump = { path = "crates/crates_io_database_dump" }
61+
crates_io_diesel_helpers = { path = "crates/crates_io_diesel_helpers" }
6162
crates_io_env_vars = { path = "crates/crates_io_env_vars" }
6263
crates_io_github = { path = "crates/crates_io_github" }
6364
crates_io_index = { path = "crates/crates_io_index" }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "crates_io_diesel_helpers"
3+
version = "0.0.0"
4+
license = "MIT OR Apache-2.0"
5+
edition = "2021"
6+
7+
[lints]
8+
workspace = true
9+
10+
[dependencies]
11+
diesel = { version = "=2.2.6", features = ["postgres"] }
12+
semver = "=1.0.24"
13+
serde = { version = "=1.0.217", features = ["derive"] }
14+
15+
[dev-dependencies]
16+
crates_io_database = { path = "../crates_io_database" }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# `crates_io_diesel_helpers`
2+
3+
This package contains a set of helper functions for working with the
4+
[`diesel`](https://crates.io/crates/diesel) database crate.
5+
6+
Specifically, it contains:
7+
8+
- various `define_sql_function!()` calls to define SQL functions to be used in
9+
Diesel queries (e.g. `lower()`, `floor()`, etc.)
10+
11+
- a deserialization helper for `semver::Version`
12+
13+
- a `pg_enum!()` macro to define an enum based on a PostgreSQL integer column
14+
15+
This package was extracted from the main application to avoid repeated
16+
recompilation of these macros when the main application is recompiled.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use diesel::define_sql_function;
2+
use diesel::sql_types::{Date, Double, Integer, Interval, SingleValue, Text, Timestamp};
3+
4+
define_sql_function!(#[aggregate] fn array_agg<T: SingleValue>(x: T) -> Array<T>);
5+
define_sql_function!(fn canon_crate_name(x: Text) -> Text);
6+
define_sql_function!(fn to_char(a: Date, b: Text) -> Text);
7+
define_sql_function!(fn lower(x: Text) -> Text);
8+
define_sql_function!(fn date_part(x: Text, y: Timestamp) -> Double);
9+
define_sql_function! {
10+
#[sql_name = "date_part"]
11+
fn interval_part(x: Text, y: Interval) -> Double;
12+
}
13+
define_sql_function!(fn floor(x: Double) -> Integer);
14+
define_sql_function!(fn greatest<T: SingleValue>(x: T, y: T) -> T);
15+
define_sql_function!(fn least<T: SingleValue>(x: T, y: T) -> T);
16+
define_sql_function!(fn split_part(string: Text, delimiter: Text, n: Integer) -> Text);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![doc = include_str!("../README.md")]
2+
3+
mod fns;
4+
mod pg_enum;
5+
mod semver;
6+
7+
pub use self::fns::*;
8+
pub use self::semver::SemverVersion;
Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
1-
use diesel::sql_types::{Date, Double, Integer, Interval, SingleValue, Text, Timestamp};
2-
3-
mod semver;
4-
5-
pub use semver::SemverVersion;
6-
7-
define_sql_function!(#[aggregate] fn array_agg<T: SingleValue>(x: T) -> Array<T>);
8-
define_sql_function!(fn canon_crate_name(x: Text) -> Text);
9-
define_sql_function!(fn to_char(a: Date, b: Text) -> Text);
10-
define_sql_function!(fn lower(x: Text) -> Text);
11-
define_sql_function!(fn date_part(x: Text, y: Timestamp) -> Double);
12-
define_sql_function! {
13-
#[sql_name = "date_part"]
14-
fn interval_part(x: Text, y: Interval) -> Double;
15-
}
16-
define_sql_function!(fn floor(x: Double) -> Integer);
17-
define_sql_function!(fn greatest<T: SingleValue>(x: T, y: T) -> T);
18-
define_sql_function!(fn least<T: SingleValue>(x: T, y: T) -> T);
19-
define_sql_function!(fn split_part(string: Text, delimiter: Text, n: Integer) -> Text);
20-
1+
#[macro_export]
212
macro_rules! pg_enum {
223
(
234
$vis:vis enum $name:ident {
245
$($item:ident = $int:expr,)*
256
}
267
) => {
27-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, FromSqlRow, AsExpression)]
8+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, diesel::FromSqlRow, diesel::AsExpression)]
289
#[diesel(sql_type = diesel::sql_types::Integer)]
2910
#[serde(rename_all = "snake_case")]
3011
#[repr(i32)]
@@ -55,5 +36,3 @@ macro_rules! pg_enum {
5536
}
5637
}
5738
}
58-
59-
pub(crate) use pg_enum;

src/sql/semver.rs renamed to crates/crates_io_diesel_helpers/src/semver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use diesel::Queryable;
77
/// ## Example
88
///
99
/// ```rust
10-
/// # use crates_io::sql::SemverVersion;
11-
/// # use crates_io::schema::versions;
10+
/// # use crates_io_diesel_helpers::SemverVersion;
11+
/// # use crates_io_database::schema::versions;
1212
/// # use diesel::prelude::*;
1313
/// #
1414
/// #[derive(Clone, Debug, Queryable, Selectable)]

src/bin/monitor.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use anyhow::Result;
88
use crates_io::worker::jobs;
99
use crates_io::{db, schema::*};
10+
use crates_io_diesel_helpers::canon_crate_name;
1011
use crates_io_env_vars::{required_var, var, var_parsed};
1112
use crates_io_pagerduty as pagerduty;
1213
use crates_io_pagerduty::PagerdutyClient;
@@ -131,8 +132,6 @@ async fn check_spam_attack(
131132
conn: &mut AsyncPgConnection,
132133
pagerduty: &PagerdutyClient,
133134
) -> Result<()> {
134-
use crates_io::sql::canon_crate_name;
135-
136135
const EVENT_KEY: &str = "spam_attack";
137136

138137
println!("Checking for crates indicating someone is spamming us");

src/controllers/krate/downloads.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use crate::app::AppState;
77
use crate::controllers::krate::CratePath;
88
use crate::models::{Version, VersionDownload};
99
use crate::schema::{version_downloads, versions};
10-
use crate::sql::to_char;
1110
use crate::util::errors::AppResult;
1211
use crate::views::EncodableVersionDownload;
1312
use axum_extra::json;
1413
use axum_extra::response::ErasedJson;
14+
use crates_io_diesel_helpers::to_char;
1515
use diesel::prelude::*;
1616
use diesel_async::RunQueryDsl;
1717
use std::cmp;

0 commit comments

Comments
 (0)