Peter Mutwiri commited on
Commit Β·
dcaef01
1
Parent(s): d27ec68
delete exixting schema
Browse files- app/db.py +8 -2
- app/main.py +14 -0
app/db.py
CHANGED
|
@@ -85,7 +85,13 @@ def transactional_conn(org_id: str):
|
|
| 85 |
finally:
|
| 86 |
conn.close()
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
# ==================== SCHEMA EVOLUTION ==================== #
|
| 90 |
def ensure_raw_table(conn: duckdb.DuckDBPyConnection):
|
| 91 |
"""
|
|
@@ -122,7 +128,7 @@ def ensure_schema_versions_table(conn: duckdb.DuckDBPyConnection):
|
|
| 122 |
conn.execute("CREATE SCHEMA IF NOT EXISTS main")
|
| 123 |
conn.execute("""
|
| 124 |
CREATE TABLE IF NOT EXISTS main.schema_versions (
|
| 125 |
-
version_id
|
| 126 |
table_name VARCHAR NOT NULL,
|
| 127 |
schema_json JSON NOT NULL,
|
| 128 |
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
| 85 |
finally:
|
| 86 |
conn.close()
|
| 87 |
|
| 88 |
+
def delete_problematic_schema_versions_table(conn: duckdb.DuckDBPyConnection):
|
| 89 |
+
"""
|
| 90 |
+
π₯ DEV ONLY: Brutally drops schema_versions table to fix corrupted INTEGER PK schema.
|
| 91 |
+
Run ONCE via API endpoint, then REMOVE this function from codebase.
|
| 92 |
+
"""
|
| 93 |
+
conn.execute("DROP TABLE IF EXISTS main.schema_versions")
|
| 94 |
+
print("[delete] π₯ Dropped main.schema_versions (dev wipe)")
|
| 95 |
# ==================== SCHEMA EVOLUTION ==================== #
|
| 96 |
def ensure_raw_table(conn: duckdb.DuckDBPyConnection):
|
| 97 |
"""
|
|
|
|
| 128 |
conn.execute("CREATE SCHEMA IF NOT EXISTS main")
|
| 129 |
conn.execute("""
|
| 130 |
CREATE TABLE IF NOT EXISTS main.schema_versions (
|
| 131 |
+
version_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
| 132 |
table_name VARCHAR NOT NULL,
|
| 133 |
schema_json JSON NOT NULL,
|
| 134 |
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
app/main.py
CHANGED
|
@@ -131,6 +131,20 @@ app = FastAPI(
|
|
| 131 |
}
|
| 132 |
)
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
# βββ Request ID Middleware βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 135 |
@app.middleware("http")
|
| 136 |
async def add_request_tracking(request: Request, call_next):
|
|
|
|
| 131 |
}
|
| 132 |
)
|
| 133 |
|
| 134 |
+
@app.post("/api/v1/admin/wipe-schema")
|
| 135 |
+
def wipe_schema_versions_table(org_id: str = Query(..., description="Organization ID")):
|
| 136 |
+
"""
|
| 137 |
+
π₯ DEV ONLY: Emergency wipe of schema_versions table.
|
| 138 |
+
USE ONCE PER ORG, THEN REMOVE THIS ENDPOINT IMMEDIATELY.
|
| 139 |
+
"""
|
| 140 |
+
try:
|
| 141 |
+
from app.db import get_conn, delete_problematic_schema_versions_table
|
| 142 |
+
conn = get_conn(org_id)
|
| 143 |
+
delete_problematic_schema_versions_table(conn)
|
| 144 |
+
conn.close()
|
| 145 |
+
return {"status": "wiped", "message": "schema_versions deleted. Remove this endpoint now."}
|
| 146 |
+
except Exception as e:
|
| 147 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 148 |
# βββ Request ID Middleware βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 149 |
@app.middleware("http")
|
| 150 |
async def add_request_tracking(request: Request, call_next):
|