Spaces:
Sleeping
Sleeping
File size: 2,080 Bytes
d084ead | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #!/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()
|