Skip to content

Commit 6e4fc5c

Browse files
committed
database: Add GitLabConfig model structs
1 parent 309eb6d commit 6e4fc5c

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

Cargo.lock

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

crates/crates_io_database/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ utoipa = { version = "=5.4.0", features = ["chrono"] }
3131
claims = "=0.8.0"
3232
crates_io_test_db = { path = "../crates_io_test_db" }
3333
googletest = "=0.14.2"
34-
insta = { version = "=1.43.2", features = ["json"] }
34+
insta = { version = "=1.43.2", features = ["filters", "json"] }
3535
tokio = { version = "=1.47.1", features = ["macros", "rt"] }
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use crate::schema::trustpub_configs_gitlab;
2+
use chrono::{DateTime, Utc};
3+
use diesel::prelude::*;
4+
use diesel_async::{AsyncPgConnection, RunQueryDsl};
5+
use serde::Serialize;
6+
7+
#[derive(Debug, Identifiable, Queryable, Selectable, Serialize)]
8+
#[diesel(table_name = trustpub_configs_gitlab, check_for_backend(diesel::pg::Pg))]
9+
pub struct GitLabConfig {
10+
pub id: i32,
11+
pub created_at: DateTime<Utc>,
12+
pub crate_id: i32,
13+
pub namespace: String,
14+
pub namespace_id: Option<String>,
15+
pub project: String,
16+
pub workflow_filepath: String,
17+
pub environment: Option<String>,
18+
}
19+
20+
#[derive(Debug, Insertable)]
21+
#[diesel(table_name = trustpub_configs_gitlab, check_for_backend(diesel::pg::Pg))]
22+
pub struct NewGitLabConfig<'a> {
23+
pub crate_id: i32,
24+
pub namespace: &'a str,
25+
pub project: &'a str,
26+
pub workflow_filepath: &'a str,
27+
pub environment: Option<&'a str>,
28+
}
29+
30+
impl NewGitLabConfig<'_> {
31+
pub async fn insert(&self, conn: &mut AsyncPgConnection) -> QueryResult<GitLabConfig> {
32+
self.insert_into(trustpub_configs_gitlab::table)
33+
.returning(GitLabConfig::as_returning())
34+
.get_result(conn)
35+
.await
36+
}
37+
}
38+
39+
#[cfg(test)]
40+
mod tests {
41+
use super::*;
42+
use crate::models::krate::*;
43+
use crate::schema::crates;
44+
use crates_io_test_db::TestDatabase;
45+
use diesel_async::RunQueryDsl;
46+
use insta::assert_debug_snapshot;
47+
48+
#[tokio::test]
49+
async fn test_gitlab_config_insert_and_retrieve() {
50+
let test_db = TestDatabase::new();
51+
let mut conn = test_db.async_connect().await;
52+
53+
// Create a test crate first
54+
let test_crate = diesel::insert_into(crates::table)
55+
.values((crates::name.eq("test-crate"),))
56+
.returning(Crate::as_returning())
57+
.get_result(&mut conn)
58+
.await
59+
.unwrap();
60+
61+
// Create a new GitLab config
62+
let new_config = NewGitLabConfig {
63+
crate_id: test_crate.id,
64+
namespace: "rust-lang",
65+
project: "cargo",
66+
workflow_filepath: ".gitlab-ci.yml",
67+
environment: Some("production"),
68+
};
69+
70+
// Insert the config
71+
let inserted_config = new_config.insert(&mut conn).await.unwrap();
72+
73+
// Retrieve the config
74+
let retrieved_config = trustpub_configs_gitlab::table
75+
.filter(trustpub_configs_gitlab::id.eq(inserted_config.id))
76+
.first::<GitLabConfig>(&mut conn)
77+
.await
78+
.unwrap();
79+
80+
// Snapshot test the structure
81+
insta::with_settings!({ filters => vec![(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z", "[datetime]")] }, {
82+
assert_debug_snapshot!(retrieved_config, @r#"
83+
GitLabConfig {
84+
id: 1,
85+
created_at: [datetime],
86+
crate_id: 1,
87+
namespace: "rust-lang",
88+
namespace_id: None,
89+
project: "cargo",
90+
workflow_filepath: ".gitlab-ci.yml",
91+
environment: Some(
92+
"production",
93+
),
94+
}
95+
"#);
96+
});
97+
}
98+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
mod data;
22
mod github_config;
3+
mod gitlab_config;
34
mod token;
45
mod used_jti;
56

67
pub use self::data::TrustpubData;
78
pub use self::github_config::{GitHubConfig, NewGitHubConfig};
9+
pub use self::gitlab_config::{GitLabConfig, NewGitLabConfig};
810
pub use self::token::NewToken;
911
pub use self::used_jti::NewUsedJti;

0 commit comments

Comments
 (0)