update_db_schema.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import sys
  2. import os
  3. # Add parent directory to path so we can import app modules
  4. sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
  5. from sqlalchemy import text
  6. from backend.app.core.database import SessionLocal, engine
  7. def add_is_superuser_column():
  8. print(f"Connecting to database at: {engine.url}")
  9. db = SessionLocal()
  10. try:
  11. # Check if column exists
  12. result = db.execute(text("SHOW COLUMNS FROM users LIKE 'is_superuser'"))
  13. if result.fetchone():
  14. print("Column 'is_superuser' already exists.")
  15. else:
  16. print("Adding 'is_superuser' column...")
  17. db.execute(text("ALTER TABLE users ADD COLUMN is_superuser BOOLEAN DEFAULT FALSE"))
  18. # Make the first user (likely admin) a superuser
  19. db.execute(text("UPDATE users SET is_superuser = TRUE WHERE id = 1"))
  20. db.commit()
  21. print("Column added and admin updated.")
  22. except Exception as e:
  23. print(f"Error: {e}")
  24. db.rollback()
  25. finally:
  26. db.close()
  27. if __name__ == "__main__":
  28. add_is_superuser_column()