migrate_localized.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import mysql.connector
  2. import os
  3. import sys
  4. # Add parent dir to path to import db
  5. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  6. from db import DB_CONFIG
  7. def migrate():
  8. try:
  9. conn = mysql.connector.connect(**DB_CONFIG)
  10. cursor = conn.cursor()
  11. print("Checking materials table...")
  12. cursor.execute("DESCRIBE materials")
  13. columns = [c[0] for c in cursor.fetchall()]
  14. if 'name_key' in columns:
  15. print("Migrating materials table to localized columns...")
  16. cursor.execute("ALTER TABLE materials ADD COLUMN name_en VARCHAR(100) AFTER id")
  17. cursor.execute("ALTER TABLE materials ADD COLUMN name_ru VARCHAR(100) AFTER name_en")
  18. cursor.execute("ALTER TABLE materials ADD COLUMN name_me VARCHAR(100) AFTER name_ru")
  19. cursor.execute("ALTER TABLE materials ADD COLUMN desc_en TEXT AFTER name_me")
  20. cursor.execute("ALTER TABLE materials ADD COLUMN desc_ru TEXT AFTER desc_en")
  21. cursor.execute("ALTER TABLE materials ADD COLUMN desc_me TEXT AFTER desc_ru")
  22. cursor.execute("ALTER TABLE materials DROP COLUMN name_key")
  23. cursor.execute("ALTER TABLE materials DROP COLUMN description_key")
  24. print("Checking services table...")
  25. cursor.execute("DESCRIBE services")
  26. columns = [c[0] for c in cursor.fetchall()]
  27. if 'description_key' in columns:
  28. # Check if it's already text or needs change
  29. pass # We'll just leave it for now or truncate and redo
  30. conn.commit()
  31. print("Migration complete.")
  32. except Exception as e:
  33. print(f"Error: {e}")
  34. finally:
  35. if 'conn' in locals() and conn.is_connected():
  36. cursor.close()
  37. conn.close()
  38. if __name__ == "__main__":
  39. migrate()