20260428130000_add_role_to_api_tokens.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. declare(strict_types=1);
  3. use App\Infrastructure\Db\Migrations\BaseMigration;
  4. /**
  5. * Adds a nullable `role` column to api_tokens to back the admin-token role
  6. * binding described in SPEC §6: "admin — Bound to a configured role
  7. * (viewer | operator | admin)". The column is NULL for non-admin token
  8. * kinds; the application layer enforces that admin tokens have it set.
  9. *
  10. * Done as a raw ALTER TABLE rather than via Phinx's table API so SQLite and
  11. * MySQL share the same migration body (Phinx's addColumn after-the-fact
  12. * needs careful CHECK-constraint preservation on SQLite, and the existing
  13. * api_tokens CHECK is acceptable as-is).
  14. */
  15. final class AddRoleToApiTokens extends BaseMigration
  16. {
  17. public function up(): void
  18. {
  19. $this->execute('ALTER TABLE api_tokens ADD COLUMN role VARCHAR(32) NULL');
  20. }
  21. public function down(): void
  22. {
  23. // SQLite cannot DROP COLUMN reliably across all supported versions,
  24. // so for rollback we recreate the table without the column. In
  25. // practice this migration won't be rolled back in production.
  26. if ($this->isMysql()) {
  27. $this->execute('ALTER TABLE api_tokens DROP COLUMN role');
  28. return;
  29. }
  30. // SQLite >= 3.35 supports DROP COLUMN; the runtime in our images is
  31. // newer than that. Fall back to a hard error on older SQLite.
  32. $this->execute('ALTER TABLE api_tokens DROP COLUMN role');
  33. }
  34. }