I'm not that good with Diesel yet am I
continuous-integration/drone the build failed Details

the-beginning
Jef Roosens 2021-06-28 18:37:26 +02:00
parent 4faf19aae5
commit e1282dacb0
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
10 changed files with 87 additions and 3 deletions

11
Cargo.lock generated
View File

@ -187,6 +187,7 @@ dependencies = [
"diesel_derives", "diesel_derives",
"pq-sys", "pq-sys",
"r2d2", "r2d2",
"uuid",
] ]
[[package]] [[package]]
@ -417,6 +418,7 @@ dependencies = [
"diesel_migrations", "diesel_migrations",
"rocket", "rocket",
"rocket_sync_db_pools", "rocket_sync_db_pools",
"uuid",
] ]
[[package]] [[package]]
@ -1379,6 +1381,15 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "uuid"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.15" version = "0.2.15"

View File

@ -22,10 +22,15 @@ doctest = true
[dependencies] [dependencies]
rocket = "0.5.0-rc.1" rocket = "0.5.0-rc.1"
diesel = { version = "1.4.7", features = ["postgres"] } diesel = { version = "1.4.7", features = ["postgres", "uuidv07"] }
diesel_migrations = "1.4.0" diesel_migrations = "1.4.0"
[dependencies.rocket_sync_db_pools] [dependencies.rocket_sync_db_pools]
version = "0.1.0-rc.1" version = "0.1.0-rc.1"
default_features = false default_features = false
features = ["diesel_postgres_pool"] features = ["diesel_postgres_pool"]
[dependencies.uuid]
version = "0.8.2"
default_features = false
features = [ "v4" ]

View File

@ -2,4 +2,4 @@
# see diesel.rs/guides/configuring-diesel-cli # see diesel.rs/guides/configuring-diesel-cli
[print_schema] [print_schema]
file = "src/schema.rs" file = "src/libhilde/schema.rs"

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE distributions;

View File

@ -0,0 +1,12 @@
-- Your SQL goes here
CREATE TABLE distributions (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
name text UNIQUE NOT NULL,
description text,
origin text,
label text,
version text,
suite text,
codename text
);

View File

@ -4,13 +4,15 @@ extern crate rocket;
#[macro_use] #[macro_use]
extern crate diesel_migrations; extern crate diesel_migrations;
mod routes;
use rocket::{fairing::AdHoc, Build, Rocket}; use rocket::{fairing::AdHoc, Build, Rocket};
use rocket_sync_db_pools::{database, diesel}; use rocket_sync_db_pools::{database, diesel};
embed_migrations!(); embed_migrations!();
#[database("postgres_hilde")] #[database("postgres_hilde")]
struct HildeDbConn(diesel::PgConnection); pub struct HildeDbConn(diesel::PgConnection);
async fn run_db_migrations(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>> { async fn run_db_migrations(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>> {
let conn = HildeDbConn::get_one(&rocket) let conn = HildeDbConn::get_one(&rocket)
@ -31,4 +33,5 @@ fn rocket() -> _ {
"Run database migrations", "Run database migrations",
run_db_migrations, run_db_migrations,
)) ))
.mount("/", routes::routes())
} }

View File

@ -0,0 +1,13 @@
use rocket::Route;
use libhilde::packages::generate_release_file;
use crate::HildeDbConn;
pub fn routes() -> Vec<Route> {
routes![get_dist_release]
}
// NOTE: for now, only dists without slashes are supported
#[get("/dists/<dist>/Release")]
async fn get_dist_release(conn: HildeDbConn, dist: String) -> String {
conn.run(|c| generate_release_file(dist, c)).await
}

View File

@ -0,0 +1,5 @@
#[macro_use]
extern crate diesel;
pub mod packages;
pub mod schema;

View File

@ -0,0 +1,21 @@
use diesel::{PgConnection, Queryable, QueryDsl, ExpressionMethods, RunQueryDsl};
use crate::schema::distributions::dsl::*;
#[derive(Queryable)]
struct Distribution {
id: uuid::Uuid,
name: String,
description: Option<String>,
origin: Option<String>,
label: Option<String>,
version: Option<String>,
suite: Option<String>,
codename: Option<String>
}
/// Generate a Release file for a given distribution
pub fn generate_release_file(dist: String, conn: &PgConnection) -> String {
let results = distributions.filter(name.eq(dist)).load::<Distribution>(conn).unwrap();
format!("{}", results.len())
}

View File

@ -0,0 +1,12 @@
table! {
distributions (id) {
id -> Uuid,
name -> Text,
description -> Nullable<Text>,
origin -> Nullable<Text>,
label -> Nullable<Text>,
version -> Nullable<Text>,
suite -> Nullable<Text>,
codename -> Nullable<Text>,
}
}