Spaces:
Sleeping
Sleeping
Download scripts/create_learned_rule_logs_table.py from Coeinterns/bank-statement-api: direct link, hf CLI and curl.
- Browser
- Download file 2.08 kB
-
https://huggingface.co/spaces/Coeinterns/bank-statement-api/resolve/main/scripts/create_learned_rule_logs_table.py
- Command line
-
hf download hf://spaces/Coeinterns/bank-statement-api/scripts/create_learned_rule_logs_table.py
-
curl -L -o create_learned_rule_logs_table.py https://huggingface.co/spaces/Coeinterns/bank-statement-api/resolve/main/scripts/create_learned_rule_logs_table.py
2.08 kB
| #!/usr/bin/env python3 | |
| """Create the learned_rule_logs table in the database. | |
| Run this script once on your DB to add the new table used for auto-learned | |
| rule suggestion logs. | |
| Usage: | |
| python scripts/create_learned_rule_logs_table.py | |
| This uses the project's SQLAlchemy engine configured in `auth.database`. | |
| """ | |
| from __future__ import annotations | |
| import sys | |
| import logging | |
| import os | |
| import sys | |
| # Ensure project root is on sys.path so top-level packages (like `auth`) can be imported | |
| REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) | |
| if REPO_ROOT not in sys.path: | |
| sys.path.insert(0, REPO_ROOT) | |
| from dotenv import load_dotenv | |
| # Load environment from .env (same behaviour as api_server) | |
| load_dotenv() | |
| # Normalize DATABASE_URL (strip accidental leading/trailing whitespace) | |
| if os.getenv("DATABASE_URL"): | |
| os.environ["DATABASE_URL"] = os.getenv("DATABASE_URL").strip() | |
| from auth.database import engine | |
| from sqlalchemy import inspect, text | |
| try: | |
| from auth.rule_models import LearnedRuleLog | |
| except Exception as e: | |
| logging.error("Failed to import LearnedRuleLog model: %s", e) | |
| sys.exit(1) | |
| def main() -> None: | |
| try: | |
| # Create the table if it doesn't already exist | |
| LearnedRuleLog.__table__.create(bind=engine, checkfirst=True) | |
| print("learned_rule_logs table created (or already exists).") | |
| inspector = inspect(engine) | |
| if "learned_rule_logs" in inspector.get_table_names(): | |
| columns = {col["name"] for col in inspector.get_columns("learned_rule_logs")} | |
| if "category_id" not in columns: | |
| with engine.connect() as conn: | |
| conn.execute(text("ALTER TABLE learned_rule_logs ADD COLUMN category_id VARCHAR")) | |
| conn.commit() | |
| print("Added category_id column to learned_rule_logs.") | |
| except Exception as exc: | |
| logging.exception("Failed to create learned_rule_logs table: %s", exc) | |
| sys.exit(2) | |
| if __name__ == "__main__": | |
| main() | |