Skip to content

Commit d1bb87b

Browse files
committed
refactor(database): decode checkpoints, chain heads, delivery summaries and purge failures directly
query_as! converts each overridden column with Into, so these records decode straight from their rows, and the row structs and the code that copied them field by field go.
1 parent 22fdf99 commit d1bb87b

3 files changed

Lines changed: 20 additions & 100 deletions

File tree

‎crates/database/src/repositories/audit/mod.rs‎

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -180,30 +180,6 @@ impl From<SealedRow> for ChainedEvent {
180180
}
181181
}
182182

183-
/// A checkpoint as stored.
184-
struct CheckpointRow {
185-
/// The head's position.
186-
position: Unsigned,
187-
/// The chain hash at that position.
188-
head: Bytes32,
189-
/// When it was signed.
190-
created_at: DateTime<Utc>,
191-
/// The signed note.
192-
note: String,
193-
}
194-
195-
impl From<CheckpointRow> for Checkpoint {
196-
/// Unwraps the decoded columns.
197-
fn from(row: CheckpointRow) -> Self {
198-
Self {
199-
position: row.position.into(),
200-
head: row.head.into(),
201-
created_at: row.created_at,
202-
note: row.note,
203-
}
204-
}
205-
}
206-
207183
impl AuditRepository<'_> {
208184
/// Returns up to `limit` unsealed events in the order to seal them, to be
209185
/// sealed in [`EventContent::NEWEST_FORMAT`]: the safe order of `in_safe_order!`. No
@@ -293,10 +269,11 @@ impl AuditRepository<'_> {
293269
///
294270
/// Returns [`DatabaseError`] if the query fails.
295271
pub async fn head(&mut self) -> Result<Option<ChainLink>, DatabaseError> {
296-
Ok(run!(
272+
run!(
297273
self,
298274
fetch_optional,
299-
sqlx::query!(
275+
sqlx::query_as!(
276+
ChainLink,
300277
r#"SELECT position AS "position!: Unsigned", hash AS "hash!: Bytes32"
301278
FROM (
302279
(SELECT chain_position AS position, chain_hash AS hash
@@ -311,11 +288,7 @@ impl AuditRepository<'_> {
311288
ORDER BY position DESC
312289
LIMIT 1"#
313290
)
314-
)?
315-
.map(|row| ChainLink {
316-
position: row.position.into(),
317-
hash: row.hash.into(),
318-
}))
291+
)
319292
}
320293

321294
/// Gives each event its position, chain hash and format.
@@ -427,11 +400,11 @@ impl AuditRepository<'_> {
427400
before: Option<u64>,
428401
limit: u32,
429402
) -> Result<Vec<Checkpoint>, DatabaseError> {
430-
Ok(run!(
403+
run!(
431404
self,
432405
fetch_all,
433406
sqlx::query_as!(
434-
CheckpointRow,
407+
Checkpoint,
435408
r#"SELECT position AS "position: Unsigned", head AS "head: Bytes32", created_at, note
436409
FROM audit_checkpoints
437410
WHERE $1::bigint IS NULL OR position < $1
@@ -440,10 +413,7 @@ impl AuditRepository<'_> {
440413
before.map(Unsigned::bind).transpose()?,
441414
i64::from(limit),
442415
)
443-
)?
444-
.into_iter()
445-
.map(Checkpoint::from)
446-
.collect())
416+
)
447417
}
448418

449419
/// Returns up to `limit` checkpoints after position `after`, oldest first.
@@ -456,11 +426,11 @@ impl AuditRepository<'_> {
456426
after: u64,
457427
limit: u32,
458428
) -> Result<Vec<Checkpoint>, DatabaseError> {
459-
Ok(run!(
429+
run!(
460430
self,
461431
fetch_all,
462432
sqlx::query_as!(
463-
CheckpointRow,
433+
Checkpoint,
464434
r#"SELECT position AS "position: Unsigned", head AS "head: Bytes32", created_at, note
465435
FROM audit_checkpoints
466436
WHERE position > $1
@@ -469,10 +439,7 @@ impl AuditRepository<'_> {
469439
Unsigned::bind(after)?,
470440
i64::from(limit),
471441
)
472-
)?
473-
.into_iter()
474-
.map(Checkpoint::from)
475-
.collect())
442+
)
476443
}
477444

478445
/// The checkpoint the chain was last pruned through, if it ever was.
@@ -481,17 +448,16 @@ impl AuditRepository<'_> {
481448
///
482449
/// Returns [`DatabaseError`] if the query fails.
483450
pub async fn anchor(&mut self) -> Result<Option<Checkpoint>, DatabaseError> {
484-
Ok(run!(
451+
run!(
485452
self,
486453
fetch_optional,
487454
sqlx::query_as!(
488-
CheckpointRow,
455+
Checkpoint,
489456
r#"SELECT c.position AS "position: Unsigned", c.head AS "head: Bytes32",
490457
c.created_at, c.note
491458
FROM audit_anchor a JOIN audit_checkpoints c USING (position)"#
492459
)
493-
)?
494-
.map(Checkpoint::from))
460+
)
495461
}
496462

497463
/// Makes this transaction see one snapshot of the whole database, read only, so

‎crates/database/src/repositories/deletions/mod.rs‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,18 @@ impl AccountDeletionRepository<'_> {
160160
&mut self,
161161
user_id: UserId,
162162
) -> Result<Option<PurgeFailure>, DatabaseError> {
163-
let row = run!(
163+
run!(
164164
self,
165165
fetch_optional,
166-
sqlx::query!(
166+
sqlx::query_as!(
167+
PurgeFailure,
167168
r#"SELECT attempts::bigint AS "attempts!: Unsigned",
168169
last_error AS "last_error!", next_attempt_at AS "next_attempt_at!"
169170
FROM account_deletions
170171
WHERE user_id = $1 AND attempts > 0"#,
171172
user_id.as_uuid(),
172173
)
173-
)?;
174-
Ok(row.map(|row| PurgeFailure {
175-
attempts: row.attempts.into(),
176-
last_error: row.last_error,
177-
next_attempt_at: row.next_attempt_at,
178-
}))
174+
)
179175
}
180176

181177
/// Counts the accounts whose purge failed at least `attempts` times in a row.

‎crates/database/src/repositories/deliveries/mod.rs‎

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -185,45 +185,6 @@ pub struct DeliveryBacklog {
185185
pub oldest: Option<DateTime<Utc>>,
186186
}
187187

188-
/// A listed delivery as stored.
189-
struct SummaryRow {
190-
/// Delivery identifier.
191-
id: DeliveryId,
192-
/// Channel name.
193-
channel: String,
194-
/// Where it goes.
195-
destination: String,
196-
/// Life-cycle status.
197-
status: DeliveryStatus,
198-
/// Attempts so far.
199-
attempts: Unsigned,
200-
/// Next attempt.
201-
next_attempt_at: DateTime<Utc>,
202-
/// Latest error.
203-
last_error: Option<String>,
204-
/// Creation time.
205-
created_at: DateTime<Utc>,
206-
/// Finish time.
207-
finished_at: Option<DateTime<Utc>>,
208-
}
209-
210-
impl From<SummaryRow> for DeliverySummary {
211-
/// Converts the stored attempt count.
212-
fn from(row: SummaryRow) -> Self {
213-
Self {
214-
id: row.id,
215-
channel: row.channel,
216-
destination: row.destination,
217-
status: row.status,
218-
attempts: row.attempts.into(),
219-
next_attempt_at: row.next_attempt_at,
220-
last_error: row.last_error,
221-
created_at: row.created_at,
222-
finished_at: row.finished_at,
223-
}
224-
}
225-
}
226-
227188
impl DeliveryRepository<'_> {
228189
/// Queues a delivery on channel `C`. Events' deliveries are inserted by
229190
/// [`DatabaseTransaction::record`]; insert directly only deliveries no event
@@ -455,11 +416,11 @@ impl DeliveryRepository<'_> {
455416
channel: Option<&str>,
456417
limit: u32,
457418
) -> Result<Vec<DeliverySummary>, DatabaseError> {
458-
Ok(run!(
419+
run!(
459420
self,
460421
fetch_all,
461422
sqlx::query_as!(
462-
SummaryRow,
423+
DeliverySummary,
463424
r#"SELECT
464425
id AS "id: DeliveryId", channel, destination,
465426
status AS "status: DeliveryStatus", attempts::bigint AS "attempts!: Unsigned",
@@ -473,10 +434,7 @@ impl DeliveryRepository<'_> {
473434
channel,
474435
i64::from(limit),
475436
)
476-
)?
477-
.into_iter()
478-
.map(DeliverySummary::from)
479-
.collect())
437+
)
480438
}
481439

482440
/// How many deliveries of channel `C` are pending, and since when the one

0 commit comments

Comments
 (0)