-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
85 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# for database connection and initialization | ||
import psycopg2 | ||
import logging | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import logging | ||
from app.db.db import connect_to_db | ||
|
||
def get_latest_instructions(): | ||
conn = connect_to_db() | ||
if conn is None: | ||
logging.error("Failed to connect to the database.") | ||
return "" | ||
|
||
try: | ||
with conn.cursor() as cur: | ||
cur.execute("SELECT content FROM instructions ORDER BY id DESC LIMIT 1") | ||
latest_instructions = cur.fetchone() | ||
return latest_instructions[0] if latest_instructions else "" | ||
except Exception as e: | ||
logging.error(f"Error fetching latest instructions: {e}") | ||
return "" | ||
finally: | ||
conn.close() | ||
|
||
def update_instructions(new_instructions): | ||
conn = connect_to_db() | ||
if conn is None: | ||
logging.error("Failed to connect to the database.") | ||
return | ||
|
||
try: | ||
with conn.cursor() as cur: | ||
cur.execute(""" | ||
INSERT INTO instructions (content) | ||
VALUES (%s) | ||
ON CONFLICT (id) | ||
DO UPDATE SET content = EXCLUDED.content; | ||
""", (new_instructions,)) | ||
conn.commit() | ||
logging.info("Instructions updated successfully.") | ||
except Exception as e: | ||
logging.error(f"Error updating instructions: {e}") | ||
finally: | ||
conn.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters