migrate_db.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import sqlite3
  2. import os
  3. DB_PATH = "madf.db"
  4. def migrate():
  5. if not os.path.exists(DB_PATH):
  6. print(f"Database {DB_PATH} not found. Nothing to migrate.")
  7. return
  8. print(f"Migrating database: {DB_PATH}")
  9. conn = sqlite3.connect(DB_PATH)
  10. cursor = conn.cursor()
  11. try:
  12. # Check messages table columns
  13. cursor.execute("PRAGMA table_info(messages)")
  14. columns = [info[1] for info in cursor.fetchall()]
  15. print(f"Current columns in messages: {columns}")
  16. if 'thoughts' in columns:
  17. print("Found 'thoughts' column. Renaming to 'thought'...")
  18. cursor.execute("ALTER TABLE messages RENAME COLUMN thoughts TO thought")
  19. print("Renamed 'thoughts' to 'thought'.")
  20. elif 'thought' not in columns:
  21. print("'thought' column missing. Adding it...")
  22. cursor.execute("ALTER TABLE messages ADD COLUMN thought TEXT")
  23. print("Added 'thought' column.")
  24. else:
  25. print("'thought' column already exists.")
  26. conn.commit()
  27. print("Migration completed successfully.")
  28. except Exception as e:
  29. print(f"Migration failed: {e}")
  30. conn.rollback()
  31. finally:
  32. conn.close()
  33. if __name__ == "__main__":
  34. migrate()