1
0

migrate.php 1.7 KB

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