Skip to main content

max / makenotwork

Fix invalid SQL in custom domain creation: FOR UPDATE with COUNT(*) PostgreSQL does not allow FOR UPDATE with aggregate functions. Use fetch_all with row-level locking instead to enforce the per-user limit. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author: Max J. <87768334+MaxJMath@users.noreply.github.com> · 2026-04-26 23:04 UTC
Commit: bd5a7ab3c311de460913128cf94b46533f525e53
Parent: dec98f8
1 file changed, +5 insertions, -5 deletions
@@ -17,15 +17,15 @@ pub async fn create_custom_domain(
17 17 ) -> Result<DbCustomDomain> {
18 18 let mut tx = pool.begin().await?;
19 19
20 - // Lock the user row to serialize concurrent domain creation attempts
21 - let existing = sqlx::query_scalar::<_, i64>(
22 - "SELECT COUNT(*) FROM custom_domains WHERE user_id = $1 FOR UPDATE",
20 + // Lock existing rows to serialize concurrent domain creation attempts
21 + let existing: Vec<(CustomDomainId,)> = sqlx::query_as(
22 + "SELECT id FROM custom_domains WHERE user_id = $1 FOR UPDATE",
23 23 )
24 24 .bind(user_id)
25 - .fetch_one(&mut *tx)
25 + .fetch_all(&mut *tx)
26 26 .await?;
27 27
28 - if existing > 0 {
28 + if !existing.is_empty() {
29 29 return Err(AppError::BadRequest(
30 30 "You already have a custom domain configured. Remove it first to add a new one.".to_string(),
31 31 ));