Skip to content

Commit add0509

Browse files
committed
Refactor storage of TreeState in the DB
1 parent 8cd1601 commit add0509

3 files changed

+44
-34
lines changed

.sqlx/query-6d68067c7121438630d77590ed644d4bc654978836fe5ceda2f0041aeba68e64.json .sqlx/query-cc7019b45037e155c4ce8f0bad9df0dcbe339fb59f93aec8bb6d34719ee042d3.json

+5-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/database/mod.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use std::{
44
str::FromStr,
55
};
66

7-
use chrono::{DateTime, Utc};
8-
pub use client::PgDbClient;
9-
use sqlx::error::BoxDynError;
10-
117
use crate::{
128
bors::RollupMode,
139
github::{GithubRepoName, PullRequestNumber},
1410
};
11+
use chrono::{DateTime, Utc};
12+
pub use client::PgDbClient;
13+
use sqlx::error::BoxDynError;
14+
use sqlx::{Database, Postgres};
1515

1616
mod client;
1717
pub(crate) mod operations;
@@ -211,6 +211,30 @@ pub enum TreeState {
211211
},
212212
}
213213

214+
impl sqlx::Type<sqlx::Postgres> for TreeState {
215+
fn type_info() -> sqlx::postgres::PgTypeInfo {
216+
<(Option<i32>, Option<String>) as sqlx::Type<sqlx::Postgres>>::type_info()
217+
}
218+
}
219+
220+
impl sqlx::Decode<'_, sqlx::Postgres> for TreeState {
221+
fn decode(value: <Postgres as Database>::ValueRef<'_>) -> Result<Self, BoxDynError> {
222+
let data = <(Option<i32>, Option<String>) as sqlx::Decode<sqlx::Postgres>>::decode(value)?;
223+
match data {
224+
(Some(priority), Some(source)) => Ok(TreeState::Closed {
225+
priority: priority as u32,
226+
source,
227+
}),
228+
(None, None) => Ok(TreeState::Open),
229+
_ => Err(
230+
"Cannot deserialize TreeState, priority is non-NULL, but source is NULL"
231+
.to_string()
232+
.into(),
233+
),
234+
}
235+
}
236+
}
237+
214238
/// Represents a repository configuration.
215239
pub struct RepoModel {
216240
pub id: PrimaryKey,

src/database/operations.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,17 @@ pub(crate) async fn get_repository(
456456
executor: impl PgExecutor<'_>,
457457
repo: &GithubRepoName,
458458
) -> anyhow::Result<Option<RepoModel>> {
459-
let repo = sqlx::query!(
459+
let repo = sqlx::query_as!(
460+
RepoModel,
460461
r#"
461-
SELECT id, name as "name: GithubRepoName", tree_state, treeclosed_src, created_at
462+
SELECT
463+
id,
464+
name as "name: GithubRepoName",
465+
(
466+
tree_state,
467+
treeclosed_src
468+
) AS "tree_state!: TreeState",
469+
created_at
462470
FROM repository
463471
WHERE name = $1
464472
"#,
@@ -467,23 +475,7 @@ pub(crate) async fn get_repository(
467475
.fetch_optional(executor)
468476
.await?;
469477

470-
Ok(repo.map(|repo| {
471-
let tree_state = match repo.tree_state {
472-
None => TreeState::Open,
473-
Some(priority) => TreeState::Closed {
474-
priority: priority as u32,
475-
source: repo
476-
.treeclosed_src
477-
.expect("treeclosed_src is NULL even though tree_state is non-NULL"),
478-
},
479-
};
480-
RepoModel {
481-
id: repo.id,
482-
name: repo.name,
483-
tree_state,
484-
created_at: repo.created_at,
485-
}
486-
}))
478+
Ok(repo)
487479
}
488480

489481
/// Updates the tree state of a repository.

0 commit comments

Comments
 (0)