Spaces:
Runtime error
Runtime error
Commit ·
8f19c79
1
Parent(s): de2cfbf
Split upsert, it was failing on row access perms
Browse files- 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
|
| 409 |
-
|
| 410 |
supabase.table("prompts")
|
| 411 |
-
.
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
returning="representation", # type: ignore
|
| 415 |
-
ignore_duplicates=False,
|
| 416 |
-
)
|
| 417 |
.execute()
|
| 418 |
)
|
| 419 |
|
| 420 |
-
if
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
)
|
| 427 |
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|