Spaces:
Sleeping
Sleeping
Download fix_db.py from Sahilalaknur/smartdocs-api: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/Sahilalaknur/smartdocs-api/resolve/main/fix_db.py
- Command line
-
hf download hf://spaces/Sahilalaknur/smartdocs-api/fix_db.py
-
curl -L -o fix_db.py https://huggingface.co/spaces/Sahilalaknur/smartdocs-api/resolve/main/fix_db.py
1.07 kB
| # fix_db.py | |
| import asyncio | |
| from vectorstore.pgvector_client import get_pgvector_client | |
| async def fix_constraints(): | |
| client = get_pgvector_client() | |
| await client.connect() | |
| try: | |
| async with client._pool.acquire() as conn: | |
| # Drop the global constraint | |
| await conn.execute("ALTER TABLE documents DROP CONSTRAINT IF EXISTS documents_doc_hash_key;") | |
| # Add the per-user constraint (ignores errors if it already exists) | |
| await conn.execute(""" | |
| DO $$ | |
| BEGIN | |
| IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'documents_user_hash_key') THEN | |
| ALTER TABLE documents ADD CONSTRAINT documents_user_hash_key UNIQUE (user_id, doc_hash); | |
| END IF; | |
| END $$; | |
| """) | |
| print("✅ Database constraints permanently fixed!") | |
| except Exception as e: | |
| print(f"❌ Error: {e}") | |
| finally: | |
| await client.close() | |
| if __name__ == "__main__": | |
| asyncio.run(fix_constraints()) |