|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\Commands\Database; |
15 | 15 |
|
16 | | -use CodeIgniter\CLI\BaseCommand; |
| 16 | +use CodeIgniter\CLI\AbstractCommand; |
| 17 | +use CodeIgniter\CLI\Attributes\Command; |
17 | 18 | use CodeIgniter\CLI\CLI; |
| 19 | +use CodeIgniter\CLI\Input\Option; |
18 | 20 | use CodeIgniter\CLI\SignalTrait; |
19 | 21 |
|
20 | 22 | /** |
21 | | - * Does a rollback followed by a latest to refresh the current state |
22 | | - * of the database. |
| 23 | + * Does a rollback followed by a latest to refresh the current state of the database. |
23 | 24 | */ |
24 | | -class MigrateRefresh extends BaseCommand |
| 25 | +#[Command( |
| 26 | + name: 'migrate:refresh', |
| 27 | + description: 'Does a rollback followed by a latest to refresh the current state of the database.', |
| 28 | + group: 'Database', |
| 29 | +)] |
| 30 | +class MigrateRefresh extends AbstractCommand |
25 | 31 | { |
26 | 32 | use SignalTrait; |
27 | 33 |
|
28 | | - /** |
29 | | - * The group the command is lumped under |
30 | | - * when listing commands. |
31 | | - * |
32 | | - * @var string |
33 | | - */ |
34 | | - protected $group = 'Database'; |
35 | | - |
36 | | - /** |
37 | | - * The Command's name |
38 | | - * |
39 | | - * @var string |
40 | | - */ |
41 | | - protected $name = 'migrate:refresh'; |
42 | | - |
43 | | - /** |
44 | | - * the Command's short description |
45 | | - * |
46 | | - * @var string |
47 | | - */ |
48 | | - protected $description = 'Does a rollback followed by a latest to refresh the current state of the database.'; |
49 | | - |
50 | | - /** |
51 | | - * the Command's usage |
52 | | - * |
53 | | - * @var string |
54 | | - */ |
55 | | - protected $usage = 'migrate:refresh [options]'; |
56 | | - |
57 | | - /** |
58 | | - * the Command's Options |
59 | | - * |
60 | | - * @var array<string, string> |
61 | | - */ |
62 | | - protected $options = [ |
63 | | - '-n' => 'Set migration namespace', |
64 | | - '-g' => 'Set database group', |
65 | | - '--all' => 'Set latest for all namespace, will ignore (-n) option', |
66 | | - '-f' => 'Force command - this option allows you to bypass the confirmation question when running this command in a production environment', |
67 | | - ]; |
68 | | - |
69 | | - /** |
70 | | - * Does a rollback followed by a latest to refresh the current state |
71 | | - * of the database. |
72 | | - */ |
73 | | - public function run(array $params) |
| 34 | + protected function configure(): void |
74 | 35 | { |
75 | | - $params['b'] = 0; |
| 36 | + $this |
| 37 | + ->addOption(new Option( |
| 38 | + name: 'namespace', |
| 39 | + shortcut: 'n', |
| 40 | + description: 'Set migration namespace.', |
| 41 | + requiresValue: true, |
| 42 | + default: '', |
| 43 | + )) |
| 44 | + ->addOption(new Option( |
| 45 | + name: 'group', |
| 46 | + shortcut: 'g', |
| 47 | + description: 'Set database group.', |
| 48 | + requiresValue: true, |
| 49 | + default: '', |
| 50 | + )) |
| 51 | + ->addOption(new Option( |
| 52 | + name: 'all', |
| 53 | + description: 'Set latest for all namespaces. This will ignore the `--namespace` option.', |
| 54 | + )) |
| 55 | + ->addOption(new Option( |
| 56 | + name: 'force', |
| 57 | + shortcut: 'f', |
| 58 | + description: 'Bypass the confirmation question when running this command in a production environment.', |
| 59 | + )); |
| 60 | + } |
| 61 | + |
| 62 | + protected function interact(array &$arguments, array &$options): void |
| 63 | + { |
| 64 | + if (! service('environment')->isProduction()) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if ($this->hasUnboundOption('force', $options)) { |
| 69 | + return; |
| 70 | + } |
76 | 71 |
|
77 | | - if (service('environment')->isProduction()) { |
78 | | - // @codeCoverageIgnoreStart |
79 | | - $force = array_key_exists('f', $params) || CLI::getOption('f'); |
| 72 | + if (CLI::prompt(lang('Migrations.refreshConfirm'), ['y', 'n']) === 'y') { |
| 73 | + $options['force'] = null; // simulate the presence of the --force option |
| 74 | + } |
| 75 | + } |
80 | 76 |
|
81 | | - if (! $force && CLI::prompt(lang('Migrations.refreshConfirm'), ['y', 'n']) === 'n') { |
82 | | - return EXIT_ERROR; |
83 | | - } |
| 77 | + protected function execute(array $arguments, array $options): int |
| 78 | + { |
| 79 | + if (service('environment')->isProduction() && $options['force'] === false) { |
| 80 | + return EXIT_ERROR; |
| 81 | + } |
| 82 | + |
| 83 | + $namespace = $options['namespace']; |
| 84 | + assert(is_string($namespace)); |
| 85 | + |
| 86 | + $group = $options['group']; |
| 87 | + assert(is_string($group)); |
| 88 | + |
| 89 | + // A target batch of 0 rolls everything back before re-applying. |
| 90 | + $rollbackOptions = ['batch' => '0']; |
| 91 | + $migrateOptions = []; |
| 92 | + |
| 93 | + if ($options['force'] === true) { |
| 94 | + $rollbackOptions['force'] = null; |
| 95 | + } |
| 96 | + |
| 97 | + if ($namespace !== '') { |
| 98 | + $migrateOptions['namespace'] = $namespace; |
| 99 | + } |
| 100 | + |
| 101 | + if ($group !== '') { |
| 102 | + $migrateOptions['group'] = $group; |
| 103 | + } |
84 | 104 |
|
85 | | - $params['f'] = null; |
86 | | - // @codeCoverageIgnoreEnd |
| 105 | + if ($options['all'] === true) { |
| 106 | + $migrateOptions['all'] = null; |
87 | 107 | } |
88 | 108 |
|
89 | 109 | return $this->withSignalsBlocked( |
90 | | - fn (): int => $this->call('migrate:rollback', $params) | $this->call('migrate', $params), |
| 110 | + fn (): int => $this->call('migrate:rollback', options: $rollbackOptions) |
| 111 | + | $this->call('migrate', options: $migrateOptions), |
91 | 112 | ); |
92 | 113 | } |
93 | 114 | } |
0 commit comments