Skip to content

Commit 6221597

Browse files
committed
Make finish_sessions_to_replace_device return whether any were finished
1 parent 96eb01f commit 6221597

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

crates/storage-pg/src/app_session.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,14 +487,15 @@ impl AppSessionRepository for PgAppSessionRepository<'_> {
487487
clock: &dyn Clock,
488488
user: &User,
489489
device: &Device,
490-
) -> Result<(), Self::Error> {
490+
) -> Result<bool, Self::Error> {
491+
let mut affected = false;
491492
// TODO need to invoke this from all the oauth2 login sites
492493
let span = tracing::info_span!(
493494
"db.app_session.finish_sessions_to_replace_device.compat_sessions",
494495
{ DB_QUERY_TEXT } = tracing::field::Empty,
495496
);
496497
let finished_at = clock.now();
497-
sqlx::query!(
498+
let compat_affected = sqlx::query!(
498499
"
499500
UPDATE compat_sessions SET finished_at = $3 WHERE user_id = $1 AND device_id = $2 AND finished_at IS NULL
500501
",
@@ -505,7 +506,9 @@ impl AppSessionRepository for PgAppSessionRepository<'_> {
505506
.record(&span)
506507
.execute(&mut *self.conn)
507508
.instrument(span)
508-
.await?;
509+
.await?
510+
.rows_affected();
511+
affected |= compat_affected > 0;
509512

510513
if let Ok([stable_device_as_scope_token, unstable_device_as_scope_token]) =
511514
device.to_scope_token()
@@ -514,7 +517,7 @@ impl AppSessionRepository for PgAppSessionRepository<'_> {
514517
"db.app_session.finish_sessions_to_replace_device.oauth2_sessions",
515518
{ DB_QUERY_TEXT } = tracing::field::Empty,
516519
);
517-
sqlx::query!(
520+
let oauth2_affected = sqlx::query!(
518521
"
519522
UPDATE oauth2_sessions
520523
SET finished_at = $4
@@ -530,10 +533,12 @@ impl AppSessionRepository for PgAppSessionRepository<'_> {
530533
.record(&span)
531534
.execute(&mut *self.conn)
532535
.instrument(span)
533-
.await?;
536+
.await?
537+
.rows_affected();
538+
affected |= oauth2_affected > 0;
534539
}
535540

536-
Ok(())
541+
Ok(affected)
537542
}
538543
}
539544

crates/storage/src/app_session.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,14 @@ pub trait AppSessionRepository: Send + Sync {
196196
/// replacing a device).
197197
///
198198
/// Should be called *before* creating a new session for the device.
199+
///
200+
/// Returns true if a session was finished.
199201
async fn finish_sessions_to_replace_device(
200202
&mut self,
201203
clock: &dyn Clock,
202204
user: &User,
203205
device: &Device,
204-
) -> Result<(), Self::Error>;
206+
) -> Result<bool, Self::Error>;
205207
}
206208

207209
repository_impl!(AppSessionRepository:
@@ -218,5 +220,5 @@ repository_impl!(AppSessionRepository:
218220
clock: &dyn Clock,
219221
user: &User,
220222
device: &Device,
221-
) -> Result<(), Self::Error>;
223+
) -> Result<bool, Self::Error>;
222224
);

0 commit comments

Comments
 (0)