run_migrations.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import os
  2. import db
  3. import sys
  4. # Ensure backend dir is in path
  5. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  6. MIGRATIONS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "migrations")
  7. def init_migrations_table():
  8. query = """
  9. CREATE TABLE IF NOT EXISTS `schema_migrations` (
  10. `id` int(11) NOT NULL AUTO_INCREMENT,
  11. `migration_name` varchar(255) NOT NULL,
  12. `applied_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  13. PRIMARY KEY (`id`),
  14. UNIQUE KEY `migration_name` (`migration_name`)
  15. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  16. """
  17. db.execute_commit(query)
  18. def get_applied_migrations():
  19. rows = db.execute_query("SELECT migration_name FROM schema_migrations")
  20. return [r['migration_name'] for r in rows]
  21. def run_migrations():
  22. print(f"Checking for migrations in {MIGRATIONS_DIR}...")
  23. init_migrations_table()
  24. applied = get_applied_migrations()
  25. # Get all .sql files from migrations dir
  26. files = [f for f in os.listdir(MIGRATIONS_DIR) if f.endswith(".sql")]
  27. files.sort() # Ensure they run in order (001, 002...)
  28. count = 0
  29. for filename in files:
  30. if filename not in applied:
  31. print(f"Applying migration: {filename}...")
  32. filepath = os.path.join(MIGRATIONS_DIR, filename)
  33. with open(filepath, "r", encoding="utf-8") as f:
  34. sql = f.read()
  35. # Execute the migration (handling potential multiple statements)
  36. # Simple splitter by semicolon (works for basic schemas)
  37. statements = [s.strip() for s in sql.split(";") if s.strip()]
  38. for stmt in statements:
  39. db.execute_commit(stmt)
  40. # Mark as applied
  41. db.execute_commit("INSERT INTO schema_migrations (migration_name) VALUES (%s)", (filename,))
  42. print(f"Successfully applied {filename}")
  43. count += 1
  44. if count == 0:
  45. print("No new migrations to apply.")
  46. else:
  47. print(f"Applied {count} migrations.")
  48. if __name__ == "__main__":
  49. try:
  50. run_migrations()
  51. except Exception as e:
  52. print(f"MIGRATION ERROR: {e}")
  53. sys.exit(1)