0005_converge_binary_model.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Generated by hand on 2026-01-01
  2. # Converges machine app for 0.8.6rc0 → 0.9.x migration path
  3. # Drops old InstalledBinary table and ensures Binary table exists
  4. from django.db import migrations, connection
  5. def converge_binary_table(apps, schema_editor):
  6. """
  7. Drop machine_installedbinary if it exists (0.8.6rc0 path).
  8. Create machine_binary if it doesn't exist (needed by Process model).
  9. """
  10. cursor = connection.cursor()
  11. # Check what tables exist
  12. cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name IN ('machine_installedbinary', 'machine_binary')")
  13. existing_tables = {row[0] for row in cursor.fetchall()}
  14. print(f'DEBUG 0005: Existing tables: {existing_tables}')
  15. # Drop old InstalledBinary table if it exists (0.8.6rc0 path)
  16. if 'machine_installedbinary' in existing_tables:
  17. print('✓ Dropping machine_installedbinary table (0.8.6rc0 divergence)')
  18. cursor.execute("DROP TABLE IF EXISTS machine_installedbinary")
  19. # Create Binary table if it doesn't exist
  20. # This handles the case where 0.8.6rc0's 0001_initial didn't create it
  21. if 'machine_binary' not in existing_tables:
  22. print('✓ Creating machine_binary table with correct schema')
  23. cursor.execute("""
  24. CREATE TABLE machine_binary (
  25. id TEXT PRIMARY KEY NOT NULL,
  26. created_at DATETIME NOT NULL,
  27. modified_at DATETIME NOT NULL,
  28. num_uses_succeeded INTEGER NOT NULL DEFAULT 0,
  29. num_uses_failed INTEGER NOT NULL DEFAULT 0,
  30. machine_id TEXT NOT NULL REFERENCES machine_machine(id) ON DELETE CASCADE,
  31. name VARCHAR(63) NOT NULL,
  32. binproviders VARCHAR(255) NOT NULL DEFAULT 'env',
  33. overrides TEXT NOT NULL DEFAULT '{}',
  34. binprovider VARCHAR(63) NOT NULL DEFAULT 'env',
  35. abspath VARCHAR(255) NOT NULL,
  36. version VARCHAR(128) NOT NULL,
  37. sha256 VARCHAR(64) NOT NULL DEFAULT '',
  38. status VARCHAR(16) NOT NULL DEFAULT 'succeeded',
  39. retry_at DATETIME NULL,
  40. output_dir VARCHAR(255) NOT NULL DEFAULT ''
  41. )
  42. """)
  43. # Create indexes
  44. cursor.execute("CREATE INDEX machine_binary_machine_id_idx ON machine_binary(machine_id)")
  45. cursor.execute("CREATE INDEX machine_binary_name_idx ON machine_binary(name)")
  46. cursor.execute("CREATE INDEX machine_binary_abspath_idx ON machine_binary(abspath)")
  47. print('✓ machine_binary table created')
  48. else:
  49. print('✓ machine_binary table already exists')
  50. class Migration(migrations.Migration):
  51. dependencies = [
  52. ('machine', '0001_initial'),
  53. ]
  54. operations = [
  55. migrations.RunPython(
  56. converge_binary_table,
  57. reverse_code=migrations.RunPython.noop,
  58. ),
  59. ]