Skip to content

Add DB inspection and pg_trgm utility scripts#43

Merged
goldlabelapps merged 1 commit into
masterfrom
staging
Apr 2, 2026
Merged

Add DB inspection and pg_trgm utility scripts#43
goldlabelapps merged 1 commit into
masterfrom
staging

Conversation

@goldlabelapps
Copy link
Copy Markdown
Owner

Add four small database utility scripts under app/api/prospects/database:

  • check_chris.py: searches prospects for occurrences of "chris" in first_name, last_name, email, or company_name (limits output).
  • check_pg_trgm.py: checks whether the pg_trgm extension is installed.
  • enable_pg_trgm.py: enables the pg_trgm extension (intended to be run once/migration-like).
  • print_prospects_columns.py: prints column names from the prospects table for debugging.

All scripts use get_db_connection from app.utils.db and include simple error handling and CLI entry points where appropriate.

Add four small database utility scripts under app/api/prospects/database:

- check_chris.py: searches prospects for occurrences of "chris" in first_name, last_name, email, or company_name (limits output).
- check_pg_trgm.py: checks whether the pg_trgm extension is installed.
- enable_pg_trgm.py: enables the pg_trgm extension (intended to be run once/migration-like).
- print_prospects_columns.py: prints column names from the prospects table for debugging.

All scripts use get_db_connection from app.utils.db and include simple error handling and CLI entry points where appropriate.
@goldlabelapps goldlabelapps added this to the MVP milestone Apr 2, 2026
@goldlabelapps goldlabelapps requested a review from Copilot April 2, 2026 13:36
@goldlabelapps goldlabelapps self-assigned this Apr 2, 2026
@github-project-automation github-project-automation Bot moved this to Backlog in Python Apr 2, 2026
@goldlabelapps goldlabelapps merged commit 43be8f6 into master Apr 2, 2026
4 checks passed
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Python Apr 2, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a small set of PostgreSQL/prospects database utility scripts under app/api/prospects/database to help with one-off DB inspection and managing the pg_trgm extension.

Changes:

  • Add a script to print column names for the prospects table.
  • Add scripts to check for and enable the pg_trgm extension.
  • Add a quick script to find prospects containing “chris” in selected fields.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 10 comments.

File Description
app/api/prospects/database/print_prospects_columns.py Prints prospects table column names by querying a sample row.
app/api/prospects/database/enable_pg_trgm.py Enables pg_trgm via CREATE EXTENSION IF NOT EXISTS ....
app/api/prospects/database/check_pg_trgm.py Checks whether pg_trgm is installed via pg_extension.
app/api/prospects/database/check_chris.py Searches for “chris” across selected prospects fields (limited output).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +4 to +7
def check_chris():

from app.utils.db import get_db_connection

Copy link

Copilot AI Apr 2, 2026

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 second def 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 valid check_chris function definition.

Suggested change
def check_chris():
from app.utils.db import get_db_connection
from app.utils.db import get_db_connection

Copilot uses AI. Check for mistakes.
Comment on lines +31 to +35
print(f"Error: {e}")
finally:
cur.close()
conn.close()

Copy link

Copilot AI Apr 2, 2026

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.

Copilot uses AI. Check for mistakes.
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
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psycopg2 is imported but never used in this script. Removing the unused import avoids lint failures and reduces confusion about intended exception handling.

Suggested change
import psycopg2

Copilot uses AI. Check for mistakes.
cur.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm;")
conn.commit()
print("pg_trgm extension enabled successfully.")
except Exception as e:
Copy link

Copilot AI Apr 2, 2026

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).

Suggested change
except Exception as e:
except Exception as e:
conn.rollback()

Copilot uses AI. Check for mistakes.
"""
Check if pg_trgm extension is enabled in the PostgreSQL database.
"""
import psycopg2
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psycopg2 is imported but not used anywhere in this script. Please remove the unused import to avoid lint errors and keep dependencies clear.

Suggested change
import psycopg2

Copilot uses AI. Check for mistakes.
Comment on lines +5 to +9
from app.utils.db import get_db_connection

def check_pg_trgm():
conn_gen = get_db_connection()
conn = next(conn_gen)
Copy link

Copilot AI Apr 2, 2026

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.

Suggested change
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()

Copilot uses AI. Check for mistakes.
Comment on lines +6 to +10
from app.utils.db import get_db_connection

def enable_pg_trgm():
conn_gen = get_db_connection()
conn = next(conn_gen)
Copy link

Copilot AI Apr 2, 2026

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()).

Suggested change
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 uses AI. Check for mistakes.
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()
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_db_connection() is implemented as a generator (FastAPI dependency). This script calls next() on it but never closes the generator, so the generator's finally block is skipped. Use get_db_connection_direct() for one-off scripts, or close the generator in finally to ensure its cleanup logic always runs.

Suggested change
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 uses AI. Check for mistakes.
Comment on lines +14 to +18
except Exception as e:
print(f"Error: {e}")
finally:
cur.close()
conn.close()
Copy link

Copilot AI Apr 2, 2026

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.

Copilot uses AI. Check for mistakes.
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)
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the other scripts: get_db_connection() is a generator with cleanup in its finally, but this script advances it with next() and never closes the generator. Prefer get_db_connection_direct() for this standalone script, or close the generator explicitly in finally to ensure the generator's cleanup path runs.

Suggested change
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 uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants