Skip to content
Merged
Prev Previous commit
Next Next commit
address reviewer findings: return full persisted row, add monotonic c…
…lock comment, note test drift risk
  • Loading branch information
Gravirei committed Jul 10, 2026
commit fff3e05b2c7885c82f516027bb2667f99bf538b9
8 changes: 4 additions & 4 deletions crates/gitlawb-node/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub async fn issue_ref_certificate(

let signature = state.node_keypair.sign_b64(&payload_bytes);

let mut cert = RefCertificate {
let cert = RefCertificate {
id: Uuid::new_v4().to_string(),
repo_id: repo_id.to_string(),
ref_name: ref_name.to_string(),
Expand All @@ -52,7 +52,7 @@ pub async fn issue_ref_certificate(
issued_at,
};

let persisted_id = state.db.insert_ref_certificate(&cert).await?;
cert.id = persisted_id;
Ok(cert)
// Persist and return the row as it exists in the database (on a
// conflict the existing row survives when it is newer).
state.db.insert_ref_certificate(&cert).await
}
19 changes: 12 additions & 7 deletions crates/gitlawb-node/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1972,11 +1972,12 @@ impl Db {
impl Db {
/// Insert a ref certificate, or update it if a row for `(repo_id, ref_name)`
/// already exists. The update only applies when the incoming row is newer
/// (compared by `issued_at`), so a late-landing older cert cannot regress a
/// ref's persisted state. Returns the id of the row that is now in the
/// database (the original id on upsert; the passed id on insert).
pub async fn insert_ref_certificate(&self, cert: &RefCertificate) -> Result<String> {
let row: (String,) = sqlx::query_as(
/// (compared by `issued_at`, which assumes a monotonic wall clock), so a
/// late-landing older cert cannot regress a ref's persisted state. Returns
/// the full row as it now exists in the database (the original row on a
/// rejected upsert; the passed row on insert).
pub async fn insert_ref_certificate(&self, cert: &RefCertificate) -> Result<RefCertificate> {
let row = sqlx::query(
"INSERT INTO ref_certificates
(id, repo_id, ref_name, old_sha, new_sha, pusher_did, node_did, signature, issued_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
Expand All @@ -1993,7 +1994,7 @@ impl Db {
THEN EXCLUDED.signature ELSE ref_certificates.signature END,
issued_at = CASE WHEN EXCLUDED.issued_at > ref_certificates.issued_at
THEN EXCLUDED.issued_at ELSE ref_certificates.issued_at END
RETURNING id",
RETURNING id, repo_id, ref_name, old_sha, new_sha, pusher_did, node_did, signature, issued_at",
)
.bind(&cert.id)
.bind(&cert.repo_id)
Expand All @@ -2006,7 +2007,7 @@ impl Db {
.bind(&cert.issued_at)
.fetch_one(&self.pool)
.await?;
Ok(row.0)
Ok(row_to_cert(row))
}

pub async fn list_ref_certificates(
Expand Down Expand Up @@ -4910,6 +4911,10 @@ mod ref_certificate_tests {
assert!(certs.is_empty());
}

/// NOTE: this test hand-copies the migration SQL as string literals and will
/// silently drift if the v10 migration block changes. The load-bearing
/// upgrade-path test is `v10_upgrade_dedup_via_migration`, which fires the
/// real MIGRATIONS[v10] entry via run_migrations().
#[sqlx::test]
async fn v10_dedup_removes_old_duplicates(pool: PgPool) {
let db = db(pool.clone()).await;
Expand Down