1
0

migrate.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. /**
  5. * R01-N22: deploy-time migration runner.
  6. *
  7. * Called by the Docker entrypoint before Apache starts, and by operators
  8. * directly when applying a new release outside Docker. The web request path
  9. * (`public/index.php`) only checks `Migrator::pendingFiles()` and refuses
  10. * to serve when there is anything pending — it does NOT apply SQL itself.
  11. *
  12. * Exits 0 on success (including the no-op "already current" case) and 1 on
  13. * any failure, with a one-line message on stderr. Stdout reports what was
  14. * applied so the entrypoint log shows it.
  15. */
  16. use App\Db\Connection;
  17. use App\Db\Migrator;
  18. $root = dirname(__DIR__);
  19. require $root . '/vendor/autoload.php';
  20. if (is_file($root . '/.env')) {
  21. Dotenv\Dotenv::createImmutable($root)->safeLoad();
  22. }
  23. try {
  24. $pdo = Connection::pdo();
  25. $migrator = new Migrator($pdo);
  26. $applied = $migrator->migrate();
  27. } catch (Throwable $e) {
  28. fwrite(STDERR, 'migrate: ' . $e->getMessage() . PHP_EOL);
  29. exit(1);
  30. }
  31. if ($applied === []) {
  32. fwrite(STDOUT, 'migrate: schema already current (version ' . $migrator->currentVersion() . ')' . PHP_EOL);
  33. } else {
  34. fwrite(STDOUT, 'migrate: applied ' . count($applied) . ' migration(s):' . PHP_EOL);
  35. foreach ($applied as $f) {
  36. fwrite(STDOUT, ' - ' . $f . PHP_EOL);
  37. }
  38. fwrite(STDOUT, 'migrate: schema now at version ' . $migrator->currentVersion() . PHP_EOL);
  39. }
  40. exit(0);