Spaces:
Runtime error
Runtime error
Download database/update_table.py from ajoy0071998/multilingual_rag: direct link, hf CLI and curl.
- Browser
- Download file 2.04 kB
-
https://huggingface.co/spaces/ajoy0071998/multilingual_rag/resolve/main/database/update_table.py
- Command line
-
hf download hf://spaces/ajoy0071998/multilingual_rag/database/update_table.py
-
curl -L -o update_table.py https://huggingface.co/spaces/ajoy0071998/multilingual_rag/resolve/main/database/update_table.py
2.04 kB
| import asyncio | |
| from sqlalchemy import text | |
| from database.database import db_manager | |
| from logger.logger import get_logger | |
| from database.database_models import Base | |
| logger = get_logger(__name__) | |
| async def clean_all_data(): | |
| """ | |
| Clean all data from tables without dropping schema | |
| """ | |
| await db_manager.initialize() | |
| async with db_manager.engine.begin() as conn: | |
| logger.info("Cleaning all tables...") | |
| # Truncate tables in correct order to handle foreign key constraints | |
| # Using CASCADE will automatically handle dependent tables | |
| await conn.execute(text("TRUNCATE TABLE messages CASCADE;")) | |
| await conn.execute(text("TRUNCATE TABLE conversations CASCADE;")) | |
| await conn.execute(text("TRUNCATE TABLE documents CASCADE;")) | |
| await conn.execute(text("TRUNCATE TABLE users CASCADE;")) | |
| # Reset sequences | |
| await conn.execute(text(""" | |
| SELECT setval(pg_get_serial_sequence('users', 'id'), | |
| COALESCE((SELECT MAX(id) FROM users), 1), false); | |
| """)) | |
| await conn.execute(text(""" | |
| SELECT setval(pg_get_serial_sequence('documents', 'id'), | |
| COALESCE((SELECT MAX(id) FROM documents), 1), false); | |
| """)) | |
| logger.info("All tables cleaned and sequences reset!") | |
| async def drop_all_tables(): | |
| """ | |
| Drop all database tables. | |
| """ | |
| await db_manager.initialize() | |
| async with db_manager.engine.begin() as conn: | |
| logger.info("Dropping all tables...") | |
| await conn.run_sync(Base.metadata.drop_all) | |
| logger.info("All tables dropped successfully!") | |
| async def create_all_tables(): | |
| await db_manager.initialize() | |
| async with db_manager.engine.begin() as conn: | |
| logger.info("Creating tables...") | |
| await conn.run_sync(Base.metadata.create_all) | |
| logger.info("All tables created successfully!") | |
| if __name__ == "__main__": | |
| asyncio.run(create_all_tables()) |