Spaces:
Sleeping
Sleeping
Commit ·
6717a70
1
Parent(s): c403a0a
schema feature endpoint added
Browse files
app/api/v1/endpoints/database_connection.py
CHANGED
|
@@ -47,7 +47,6 @@ from pydantic import BaseModel
|
|
| 47 |
from app.services.sql_agent_instance import sql_agent
|
| 48 |
from sqlalchemy.exc import OperationalError, DatabaseError
|
| 49 |
from urllib.parse import urlparse
|
| 50 |
-
|
| 51 |
router = APIRouter()
|
| 52 |
|
| 53 |
class DatabaseConnectionRequest(BaseModel):
|
|
@@ -85,4 +84,6 @@ async def setup_connection(request: DatabaseConnectionRequest):
|
|
| 85 |
raise HTTPException(
|
| 86 |
status_code=500,
|
| 87 |
detail=f"Unexpected error occurred while setting up database connection: {str(e)}"
|
| 88 |
-
)
|
|
|
|
|
|
|
|
|
| 47 |
from app.services.sql_agent_instance import sql_agent
|
| 48 |
from sqlalchemy.exc import OperationalError, DatabaseError
|
| 49 |
from urllib.parse import urlparse
|
|
|
|
| 50 |
router = APIRouter()
|
| 51 |
|
| 52 |
class DatabaseConnectionRequest(BaseModel):
|
|
|
|
| 84 |
raise HTTPException(
|
| 85 |
status_code=500,
|
| 86 |
detail=f"Unexpected error occurred while setting up database connection: {str(e)}"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
|
app/api/v1/endpoints/schema.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException
|
| 2 |
+
from sqlalchemy import inspect
|
| 3 |
+
from app.services.sql_agent_instance import sql_agent
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
router = APIRouter()
|
| 7 |
+
|
| 8 |
+
@router.get("/schema")
|
| 9 |
+
async def get_schema():
|
| 10 |
+
if not sql_agent.db:
|
| 11 |
+
raise HTTPException(
|
| 12 |
+
status_code=400,
|
| 13 |
+
detail="Database connection is not established. Please set up the connection first."
|
| 14 |
+
)
|
| 15 |
+
try:
|
| 16 |
+
inspector = inspect(sql_agent.db._engine)
|
| 17 |
+
schema_data = {}
|
| 18 |
+
for table_name in inspector.get_table_names():
|
| 19 |
+
columns = []
|
| 20 |
+
for col in inspector.get_columns(table_name):
|
| 21 |
+
columns.append({
|
| 22 |
+
"name": col['name'],
|
| 23 |
+
"type": str(col['type']),
|
| 24 |
+
"nullable": col.get('nullable', True)
|
| 25 |
+
})
|
| 26 |
+
fks = []
|
| 27 |
+
for fk in inspector.get_foreign_keys(table_name):
|
| 28 |
+
fks.append({
|
| 29 |
+
"constrained_columns": fk['constrained_columns'],
|
| 30 |
+
"referred_table": fk['referred_table'],
|
| 31 |
+
"referred_columns": fk['referred_columns']
|
| 32 |
+
})
|
| 33 |
+
schema_data[table_name] = {
|
| 34 |
+
"columns": columns,
|
| 35 |
+
"foreign_keys": fks
|
| 36 |
+
}
|
| 37 |
+
return schema_data
|
| 38 |
+
except Exception as e:
|
| 39 |
+
raise HTTPException(
|
| 40 |
+
status_code=500,
|
| 41 |
+
detail="An error occurred while fetching the schema."
|
| 42 |
+
)
|
| 43 |
+
|