Skip to content

Instantly share code, notes, and snippets.

@4l3j4ndr0
Created March 23, 2025 17:50
Show Gist options
  • Select an option

  • Save 4l3j4ndr0/5ba4b6af2fcd0a10aa7c1538cabe48b4 to your computer and use it in GitHub Desktop.

Select an option

Save 4l3j4ndr0/5ba4b6af2fcd0a10aa7c1538cabe48b4 to your computer and use it in GitHub Desktop.
Database Schema Extraction
def get_database_schema(self):
"""Get database schema information to provide context to the model"""
schema_info = []
with self.engine.connect() as connection:
# Get all tables
tables_result = connection.execute(text("""
SELECT table_name FROM information_schema.tables
WHERE table_schema = :db_name
"""), {"db_name": self.db_config['db']})
# For each table, get column information
for table in tables_result:
columns_result = connection.execute(text("""
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_schema = :db_name AND table_name = :table_name
"""), {"db_name": self.db_config['db'], "table_name": table[0]})
columns = [f"{row[0]} ({row[1]})" for row in columns_result]
table_info = f"Table: {table[0]}\nColumns: {', '.join(columns)}\n"
schema_info.append(table_info)
return "\n".join(schema_info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment