Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

106 lignes
3.0KB

  1. <?php
  2. declare(strict_types=1);
  3. require_once __DIR__ . '/vendor/autoload.php';
  4. $command = $argv[1] ?? 'help';
  5. $options = array_slice($argv, 2);
  6. $manager = migration_manager();
  7. try {
  8. switch ($command) {
  9. case 'up':
  10. $ran = $manager->runPending();
  11. if ($ran === []) {
  12. echo "No pending migrations." . PHP_EOL;
  13. exit(0);
  14. }
  15. foreach ($ran as $migration) {
  16. echo "Migrated: {$migration}" . PHP_EOL;
  17. }
  18. echo 'Applied ' . count($ran) . ' migration(s).' . PHP_EOL;
  19. exit(0);
  20. case 'down':
  21. $steps = isset($argv[2]) ? max(1, (int) $argv[2]) : 1;
  22. $rolledBack = $manager->rollback($steps);
  23. if ($rolledBack === []) {
  24. echo "No applied migrations to roll back." . PHP_EOL;
  25. exit(0);
  26. }
  27. foreach ($rolledBack as $migration) {
  28. echo "Rolled back: {$migration}" . PHP_EOL;
  29. }
  30. echo 'Rolled back ' . count($rolledBack) . ' migration(s).' . PHP_EOL;
  31. exit(0);
  32. case 'status':
  33. $status = $manager->status();
  34. if ($status === []) {
  35. echo "No migration files found." . PHP_EOL;
  36. exit(0);
  37. }
  38. foreach ($status as $row) {
  39. $state = $row['ran'] ? 'up' : 'pending';
  40. $ranAt = $row['ran_at'] ?? '-';
  41. echo str_pad($state, 10) . ' ' . $row['migration'] . ' ' . $ranAt . PHP_EOL;
  42. }
  43. exit(0);
  44. case 'make':
  45. case 'create':
  46. $name = $argv[2] ?? '';
  47. if ($name === '') {
  48. throw new InvalidArgumentException('Provide a migration name. Example: php migrate.php make create_projects_table');
  49. }
  50. $path = $manager->make($name);
  51. echo "Created migration: {$path}" . PHP_EOL;
  52. exit(0);
  53. case 'fresh':
  54. $result = $manager->fresh();
  55. foreach ($result['rolled_back'] as $migration) {
  56. echo "Rolled back: {$migration}" . PHP_EOL;
  57. }
  58. foreach ($result['migrated'] as $migration) {
  59. echo "Migrated: {$migration}" . PHP_EOL;
  60. }
  61. if (in_array('--seed', $options, true)) {
  62. require __DIR__ . '/database/seed_employees.php';
  63. seed_employees(1000, true);
  64. }
  65. echo "Fresh migration run complete." . PHP_EOL;
  66. exit(0);
  67. case 'help':
  68. default:
  69. echo "Migration CLI" . PHP_EOL;
  70. echo "Usage:" . PHP_EOL;
  71. echo " php migrate.php up" . PHP_EOL;
  72. echo " php migrate.php down [steps]" . PHP_EOL;
  73. echo " php migrate.php status" . PHP_EOL;
  74. echo " php migrate.php make <name>" . PHP_EOL;
  75. echo " php migrate.php fresh [--seed]" . PHP_EOL;
  76. exit(0);
  77. }
  78. } catch (Throwable $exception) {
  79. fwrite(STDERR, $exception->getMessage() . PHP_EOL);
  80. exit(1);
  81. }

Powered by TurnKey Linux.