migration_6.py 908 B

1234567891011121314151617181920212223242526272829303132
  1. # Migration to drop primary key constraint from token_usage table
  2. from datetime import datetime
  3. def migration(conn):
  4. with conn.cursor() as cursor:
  5. cursor.execute(
  6. """
  7. CREATE TABLE token_usage_temp (
  8. message_id TEXT NOT NULL,
  9. room_id TEXT NOT NULL,
  10. api TEXT NOT NULL,
  11. tokens INTEGER NOT NULL,
  12. timestamp TIMESTAMP NOT NULL
  13. )
  14. """
  15. )
  16. cursor.execute(
  17. "INSERT INTO token_usage_temp SELECT message_id, room_id, api, tokens, timestamp FROM token_usage"
  18. )
  19. cursor.execute("DROP TABLE token_usage")
  20. cursor.execute("ALTER TABLE token_usage_temp RENAME TO token_usage")
  21. cursor.execute(
  22. "INSERT INTO migrations (id, timestamp) VALUES (6, ?)",
  23. (datetime.now(),)
  24. )
  25. conn.commit()