Skip to content

Commit f2c6b1a

Browse files
authored
Fix websocket auth race for newly created test users (#344)
1 parent 9b14d4e commit f2c6b1a

6 files changed

+54
-27
lines changed

backend/.sqlx/query-0f706c05ef523de5c112b3b6f788860ee9e6fb9c159895ae83c13d0308cd8cb0.json

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

backend/.sqlx/query-13c6461599e2477b5031c636b335a6226dadce2f6d87bc58853dfade589f1e20.json

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

backend/.sqlx/query-6f7f8dc96696714ffe029a34ef16f500e9113ec87fb7928f0176e08c765d8f3f.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

backend/.sqlx/query-929d183557cb75781216ec056f9c483c6bf9ac0491d94ebc226e6461a9d3c3f9.json

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

backend/.sqlx/query-c14b33f32699bf8773bc195f23d5faead219b685cdf38a466058067e3b795151.json

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

backend/src/db.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ impl DB {
744744
initial_balance: Decimal,
745745
) -> SqlxResult<ValidationResult<EnsureUserCreatedSuccess>> {
746746
let balance = Text(initial_balance);
747+
let mut transaction = self.pool.begin().await?;
747748

748749
// First try to find user by kinde_id
749750
let existing_user = sqlx::query!(
@@ -754,17 +755,19 @@ impl DB {
754755
"#,
755756
kinde_id
756757
)
757-
.fetch_optional(&self.pool)
758+
.fetch_optional(transaction.as_mut())
758759
.await?;
759760

760761
if let Some(user) = existing_user {
762+
transaction.commit().await?;
761763
return Ok(Ok(EnsureUserCreatedSuccess {
762764
id: user.id,
763765
name: None,
764766
}));
765767
}
766768

767769
let Some(requested_name) = requested_name else {
770+
transaction.commit().await?;
768771
return Ok(Err(ValidationFailure::NoNameProvidedForNewUser));
769772
};
770773

@@ -777,7 +780,7 @@ impl DB {
777780
requested_name,
778781
kinde_id
779782
)
780-
.fetch_optional(&self.pool)
783+
.fetch_optional(transaction.as_mut())
781784
.await?;
782785

783786
let final_name = if conflicting_account.is_some() {
@@ -786,18 +789,30 @@ impl DB {
786789
requested_name.to_string()
787790
};
788791

789-
let id = sqlx::query_scalar!(
792+
sqlx::query!(
790793
r#"
791794
INSERT INTO account (kinde_id, name, balance)
792795
VALUES (?, ?, ?)
793-
RETURNING id
794796
"#,
795797
kinde_id,
796798
final_name,
797799
balance,
798800
)
799-
.fetch_one(&self.pool)
801+
.execute(transaction.as_mut())
802+
.await?;
803+
804+
let id = sqlx::query_scalar!(
805+
r#"
806+
SELECT id AS "id!: i64"
807+
FROM account
808+
WHERE kinde_id = ?
809+
"#,
810+
kinde_id
811+
)
812+
.fetch_one(transaction.as_mut())
800813
.await?;
814+
815+
transaction.commit().await?;
801816
Ok(Ok(EnsureUserCreatedSuccess {
802817
id,
803818
name: Some(final_name),

0 commit comments

Comments
 (0)