From 0aa31f5eddf4d1c126d38ca4c85195a6bb86a97b Mon Sep 17 00:00:00 2001 From: Wei Zang Date: Thu, 2 Apr 2026 14:35:21 +0100 Subject: [PATCH] Add DB inspection and pg_trgm utility scripts 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. --- app/api/prospects/database/check_chris.py | 35 +++++++++++++++++++ app/api/prospects/database/check_pg_trgm.py | 25 +++++++++++++ app/api/prospects/database/enable_pg_trgm.py | 23 ++++++++++++ .../database/print_prospects_columns.py | 21 +++++++++++ 4 files changed, 104 insertions(+) create mode 100644 app/api/prospects/database/check_chris.py create mode 100644 app/api/prospects/database/check_pg_trgm.py create mode 100644 app/api/prospects/database/enable_pg_trgm.py create mode 100644 app/api/prospects/database/print_prospects_columns.py diff --git a/app/api/prospects/database/check_chris.py b/app/api/prospects/database/check_chris.py new file mode 100644 index 0000000..480479e --- /dev/null +++ b/app/api/prospects/database/check_chris.py @@ -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) + cur = conn.cursor() + try: + cur.execute(""" + SELECT id, first_name, last_name, email, company_name FROM prospects + WHERE ( + LOWER(first_name) LIKE '%chris%' + OR LOWER(last_name) LIKE '%chris%' + OR LOWER(email) LIKE '%chris%' + OR LOWER(company_name) LIKE '%chris%' + ) + AND hide IS NOT TRUE + LIMIT 10; + """) + rows = cur.fetchall() + if rows: + for row in rows: + print(row) + else: + print("No prospects found with 'chris' in first_name, last_name, email, or company_name.") + except Exception as e: + print(f"Error: {e}") + finally: + cur.close() + conn.close() + diff --git a/app/api/prospects/database/check_pg_trgm.py b/app/api/prospects/database/check_pg_trgm.py new file mode 100644 index 0000000..54e4098 --- /dev/null +++ b/app/api/prospects/database/check_pg_trgm.py @@ -0,0 +1,25 @@ +""" +Check if pg_trgm extension is enabled in the PostgreSQL database. +""" +import psycopg2 +from app.utils.db import get_db_connection + +def check_pg_trgm(): + conn_gen = get_db_connection() + conn = next(conn_gen) + cur = conn.cursor() + try: + cur.execute("SELECT extname FROM pg_extension WHERE extname = 'pg_trgm';") + result = cur.fetchone() + if result: + print("pg_trgm extension is ENABLED.") + else: + print("pg_trgm extension is NOT enabled.") + except Exception as e: + print(f"Failed to check pg_trgm: {e}") + finally: + cur.close() + conn.close() + +if __name__ == "__main__": + check_pg_trgm() diff --git a/app/api/prospects/database/enable_pg_trgm.py b/app/api/prospects/database/enable_pg_trgm.py new file mode 100644 index 0000000..8919000 --- /dev/null +++ b/app/api/prospects/database/enable_pg_trgm.py @@ -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 +from app.utils.db import get_db_connection + +def enable_pg_trgm(): + conn_gen = get_db_connection() + conn = next(conn_gen) + cur = conn.cursor() + try: + cur.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm;") + conn.commit() + print("pg_trgm extension enabled successfully.") + except Exception as e: + print(f"Failed to enable pg_trgm: {e}") + finally: + cur.close() + conn.close() + +if __name__ == "__main__": + enable_pg_trgm() diff --git a/app/api/prospects/database/print_prospects_columns.py b/app/api/prospects/database/print_prospects_columns.py new file mode 100644 index 0000000..9eb09af --- /dev/null +++ b/app/api/prospects/database/print_prospects_columns.py @@ -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() + +if __name__ == "__main__": + print_prospects_columns()