JasonGross commited on
Commit
8f19c79
·
1 Parent(s): de2cfbf

Split upsert, it was failing on row access perms

Browse files
Files changed (1) hide show
  1. app/main.py +32 -19
app/main.py CHANGED
@@ -405,31 +405,44 @@ async def ask_paul_graham(request: Request, prompt: str = Form(...)):
405
  )
406
  # ------------------------------------- #
407
 
408
- # --- Find or Create Prompt based on short_description --- #
409
- prompt_upsert_result = (
410
  supabase.table("prompts")
411
- .upsert(
412
- {"short_description": short_description, "prompt_text": prompt_text},
413
- on_conflict="prompt_text",
414
- returning="representation", # type: ignore
415
- ignore_duplicates=False,
416
- )
417
  .execute()
418
  )
419
 
420
- if not prompt_upsert_result.data or len(prompt_upsert_result.data) == 0:
421
- logger.error(
422
- f"Failed to upsert prompt for description: {short_description}"
423
- )
424
- raise HTTPException(
425
- status_code=500, detail="Failed to find or create prompt."
 
 
 
 
 
 
 
 
 
426
  )
427
 
428
- prompt_info = prompt_upsert_result.data[0]
429
- prompt_id = prompt_info["prompt_id"]
430
- prompt_created_at = prompt_info[
431
- "created_at"
432
- ] # Example of getting other info if needed
 
 
 
 
 
 
 
433
 
434
  # Determine if the prompt was newly inserted or if it already existed
435
  # This logic might need refinement based on exact upsert behavior / timestamps
 
405
  )
406
  # ------------------------------------- #
407
 
408
+ # --- Find existing prompt based on short_description --- #
409
+ existing_prompt_result = (
410
  supabase.table("prompts")
411
+ .select("prompt_id, created_at")
412
+ .eq("prompt_text", prompt_text)
413
+ .limit(1)
 
 
 
414
  .execute()
415
  )
416
 
417
+ if existing_prompt_result.data and len(existing_prompt_result.data) > 0:
418
+ # Prompt already exists, use the existing one
419
+ prompt_info = existing_prompt_result.data[0]
420
+ prompt_id = prompt_info["prompt_id"]
421
+ prompt_created_at = prompt_info["created_at"]
422
+ logger.info(f"Found existing prompt with ID: {prompt_id}")
423
+ else:
424
+ # Create new prompt
425
+ new_prompt_result = (
426
+ supabase.table("prompts")
427
+ .insert(
428
+ {"short_description": short_description, "prompt_text": prompt_text},
429
+ returning="representation", # type: ignore
430
+ )
431
+ .execute()
432
  )
433
 
434
+ if not new_prompt_result.data or len(new_prompt_result.data) == 0:
435
+ logger.error(
436
+ f"Failed to insert prompt for description: {short_description}"
437
+ )
438
+ raise HTTPException(
439
+ status_code=500, detail="Failed to create prompt."
440
+ )
441
+
442
+ prompt_info = new_prompt_result.data[0]
443
+ prompt_id = prompt_info["prompt_id"]
444
+ prompt_created_at = prompt_info["created_at"]
445
+ logger.info(f"Created new prompt with ID: {prompt_id}")
446
 
447
  # Determine if the prompt was newly inserted or if it already existed
448
  # This logic might need refinement based on exact upsert behavior / timestamps