-
Notifications
You must be signed in to change notification settings - Fork 0
Add DB inspection and pg_trgm utility scripts #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Quick script to check if any prospects contain 'chris' in name, email, or company. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| def check_chris(): | ||||||||||||||||||||
|
|
||||||||||||||||||||
| from app.utils.db import get_db_connection | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def check_chris(): | ||||||||||||||||||||
| conn_gen = get_db_connection() | ||||||||||||||||||||
| conn = next(conn_gen) | ||||||||||||||||||||
|
Comment on lines
+6
to
+10
|
||||||||||||||||||||
| from app.utils.db import get_db_connection | |
| def check_chris(): | |
| conn_gen = get_db_connection() | |
| conn = next(conn_gen) | |
| from app.utils.db import get_db_connection_direct | |
| def check_chris(): | |
| conn = get_db_connection_direct() |
Copilot
AI
Apr 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR description mentions these scripts include CLI entry points where appropriate, but this script doesn't currently have an if __name__ == "__main__": ... block to invoke check_chris(). Add a main guard at the bottom so it can be run directly like the other utility scripts.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Check if pg_trgm extension is enabled in the PostgreSQL database. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| import psycopg2 | ||||||||||||||||||||
|
||||||||||||||||||||
| import psycopg2 |
Copilot
AI
Apr 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_db_connection() is a generator intended for FastAPI dependency injection (it closes the connection in the generator's finally). Here the generator is advanced with next() but never closed, so that finally block will not run. Prefer get_db_connection_direct() for scripts, or explicitly close the generator (e.g., in finally) instead of only closing the connection.
| from app.utils.db import get_db_connection | |
| def check_pg_trgm(): | |
| conn_gen = get_db_connection() | |
| conn = next(conn_gen) | |
| from app.utils.db import get_db_connection_direct | |
| def check_pg_trgm(): | |
| conn = get_db_connection_direct() |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,23 @@ | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Migration script to enable pg_trgm extension for fuzzy search support. | ||||||||||||||||||||
| Run this script once to enable the extension in your PostgreSQL database. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| import psycopg2 | ||||||||||||||||||||
|
||||||||||||||||||||
| import psycopg2 |
Copilot
AI
Apr 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_db_connection() is a generator meant to be used as a FastAPI dependency; advancing it with next() without later closing the generator skips its finally cleanup path. For standalone scripts, prefer get_db_connection_direct(), or ensure the generator is properly closed in finally (rather than only calling conn.close()).
| from app.utils.db import get_db_connection | |
| def enable_pg_trgm(): | |
| conn_gen = get_db_connection() | |
| conn = next(conn_gen) | |
| from app.utils.db import get_db_connection, get_db_connection_direct | |
| def enable_pg_trgm(): | |
| conn = get_db_connection_direct() |
Copilot
AI
Apr 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On exception, the connection is left in a failed transaction state but never rolled back. Add conn.rollback() in the except block so the connection is cleanly reset before closing (and to match error-handling patterns used in other DB scripts/endpoints).
| except Exception as e: | |
| except Exception as e: | |
| conn.rollback() |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,21 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Script to print the column names of the prospects table for debugging. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from app.utils.db import get_db_connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| def print_prospects_columns(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| conn_gen = get_db_connection() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| conn = next(conn_gen) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| cur = conn.cursor() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| cur.execute("SELECT * FROM prospects LIMIT 1;") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| columns = [desc[0] for desc in cur.description] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| print("Columns in prospects table:", columns) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"Error: {e}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| finally: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| cur.close() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| conn.close() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+8
to
+18
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| conn = next(conn_gen) | |
| cur = conn.cursor() | |
| try: | |
| cur.execute("SELECT * FROM prospects LIMIT 1;") | |
| columns = [desc[0] for desc in cur.description] | |
| print("Columns in prospects table:", columns) | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| finally: | |
| cur.close() | |
| conn.close() | |
| try: | |
| conn = next(conn_gen) | |
| cur = conn.cursor() | |
| try: | |
| cur.execute("SELECT * FROM prospects LIMIT 1;") | |
| columns = [desc[0] for desc in cur.description] | |
| print("Columns in prospects table:", columns) | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| finally: | |
| cur.close() | |
| conn.close() | |
| finally: | |
| conn_gen.close() |
Copilot
AI
Apr 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the SELECT fails (e.g., missing table/permissions), psycopg2 leaves the session in an aborted transaction state. Consider calling conn.rollback() in the except block to keep error handling consistent with the other DB utility scripts in this directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The module currently has an empty
def check_chris():followed by a seconddef check_chris():and a top-level import after the first function header. As written, this is a syntax/indentation error (function with no body) and the script will fail to run/import. Remove the stray first definition and move imports to the top so there is exactly one validcheck_chrisfunction definition.