-
-
Save 4l3j4ndr0/5ba4b6af2fcd0a10aa7c1538cabe48b4 to your computer and use it in GitHub Desktop.
Database Schema Extraction
This file contains hidden or 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
| 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