1 | <?php |
||
37 | class MigrateCommand extends AbstractCommand |
||
38 | { |
||
39 | 18 | protected function configure() |
|
40 | { |
||
41 | $this |
||
42 | 18 | ->setName('migrations:migrate') |
|
43 | 18 | ->setDescription('Execute a migration to a specified version or the latest available version.') |
|
44 | 18 | ->addArgument('version', InputArgument::OPTIONAL, 'The version number (YYYYMMDDHHMMSS) or alias (first, prev, next, latest) to migrate to.', 'latest') |
|
45 | 18 | ->addOption('write-sql', null, InputOption::VALUE_NONE, 'The path to output the migration SQL file instead of executing it.') |
|
46 | 18 | ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Execute the migration as a dry run.') |
|
47 | 18 | ->addOption('query-time', null, InputOption::VALUE_NONE, 'Time all the queries individually.') |
|
48 | 18 | ->addOption('allow-no-migration', null, InputOption::VALUE_NONE, 'Don\'t throw an exception if no migration is available (CI).') |
|
49 | 18 | ->setHelp(<<<EOT |
|
50 | The <info>%command.name%</info> command executes a migration to a specified version or the latest available version: |
||
51 | |||
52 | <info>%command.full_name%</info> |
||
53 | |||
54 | You can optionally manually specify the version you wish to migrate to: |
||
55 | |||
56 | <info>%command.full_name% YYYYMMDDHHMMSS</info> |
||
57 | |||
58 | You can specify the version you wish to migrate to using an alias: |
||
59 | |||
60 | <info>%command.full_name% prev</info> |
||
61 | |||
62 | You can also execute the migration as a <comment>--dry-run</comment>: |
||
63 | |||
64 | <info>%command.full_name% YYYYMMDDHHMMSS --dry-run</info> |
||
65 | |||
66 | You can output the would be executed SQL statements to a file with <comment>--write-sql</comment>: |
||
67 | |||
68 | <info>%command.full_name% YYYYMMDDHHMMSS --write-sql</info> |
||
69 | |||
70 | Or you can also execute the migration without a warning message which you need to interact with: |
||
71 | |||
72 | <info>%command.full_name% --no-interaction</info> |
||
73 | |||
74 | You can also time all the different queries if you wanna know which one is taking so long: |
||
75 | |||
76 | 18 | <info>%command.full_name% --query-time</info> |
|
77 | EOT |
||
78 | ); |
||
79 | |||
80 | 18 | parent::configure(); |
|
81 | 18 | } |
|
82 | |||
83 | 11 | public function execute(InputInterface $input, OutputInterface $output) |
|
84 | { |
||
85 | 11 | $configuration = $this->getMigrationConfiguration($input, $output); |
|
86 | 11 | $migration = $this->createMigration($configuration); |
|
87 | |||
88 | 11 | $this->outputHeader($configuration, $output); |
|
89 | |||
90 | 11 | $timeAllqueries = $input->getOption('query-time'); |
|
91 | |||
92 | 11 | $executedMigrations = $configuration->getMigratedVersions(); |
|
93 | 11 | $availableMigrations = $configuration->getAvailableVersions(); |
|
94 | |||
95 | 11 | $version = $this->getVersionNameFromAlias($input->getArgument('version'), $output, $configuration); |
|
96 | 11 | if ($version === false) { |
|
97 | 3 | return 1; |
|
98 | } |
||
99 | |||
100 | 8 | $executedUnavailableMigrations = array_diff($executedMigrations, $availableMigrations); |
|
101 | 8 | if (!empty($executedUnavailableMigrations)) { |
|
102 | 1 | $output->writeln(sprintf( |
|
103 | '<error>WARNING! You have %s previously executed migrations' |
||
104 | 1 | . ' in the database that are not registered migrations.</error>', |
|
105 | count($executedUnavailableMigrations) |
||
106 | )); |
||
107 | |||
108 | 1 | foreach ($executedUnavailableMigrations as $executedUnavailableMigration) { |
|
109 | 1 | $output->writeln(sprintf( |
|
110 | 1 | ' <comment>>></comment> %s (<comment>%s</comment>)', |
|
111 | 1 | $configuration->getDateTime($executedUnavailableMigration), |
|
112 | $executedUnavailableMigration |
||
113 | )); |
||
114 | } |
||
115 | |||
116 | 1 | $question = 'Are you sure you wish to continue? (y/n)'; |
|
117 | 1 | if (! $this->canExecute($question, $input, $output)) { |
|
118 | 1 | $output->writeln('<error>Migration cancelled!</error>'); |
|
119 | |||
120 | 1 | return 1; |
|
121 | } |
||
122 | } |
||
123 | |||
124 | 7 | if ($path = $input->getOption('write-sql')) { |
|
125 | 2 | $path = is_bool($path) ? getcwd() : $path; |
|
126 | 2 | $migration->writeSqlFile($path, $version); |
|
127 | 2 | return 0; |
|
128 | } |
||
129 | |||
130 | 5 | $dryRun = (boolean) $input->getOption('dry-run'); |
|
131 | |||
132 | 5 | $cancelled = false; |
|
133 | 5 | $migration->setNoMigrationException($input->getOption('allow-no-migration')); |
|
134 | 5 | $result = $migration->migrate($version, $dryRun, $timeAllqueries, function () use ($input, $output, &$cancelled) { |
|
|
|||
135 | $question = 'WARNING! You are about to execute a database migration' |
||
136 | . ' that could result in schema changes and data lost.' |
||
137 | 4 | . ' Are you sure you wish to continue? (y/n)'; |
|
138 | 4 | $canContinue = $this->canExecute($question, $input, $output); |
|
139 | 4 | $cancelled = !$canContinue; |
|
140 | |||
141 | 4 | return $canContinue; |
|
142 | 5 | }); |
|
143 | |||
144 | 5 | if ($cancelled) { |
|
145 | 1 | $output->writeln('<error>Migration cancelled!</error>'); |
|
146 | 1 | return 1; |
|
147 | } |
||
148 | 4 | } |
|
149 | |||
150 | /** |
||
151 | * Create a new migration instance to execute the migrations. |
||
152 | * |
||
153 | * @param $configuration The configuration with which the migrations will be executed |
||
154 | * @return Migration a new migration instance |
||
155 | */ |
||
156 | 1 | protected function createMigration(Configuration $configuration) |
|
160 | |||
161 | /** |
||
162 | * @param string $question |
||
163 | * @param InputInterface $input |
||
164 | * @param OutputInterface $output |
||
165 | * @return bool |
||
166 | */ |
||
167 | 5 | private function canExecute($question, InputInterface $input, OutputInterface $output) |
|
175 | |||
176 | /** |
||
177 | * @param string $versionAlias |
||
178 | * @param OutputInterface $output |
||
179 | * @param Configuration $configuration |
||
180 | * @return bool|string |
||
181 | */ |
||
182 | 11 | private function getVersionNameFromAlias($versionAlias, OutputInterface $output, Configuration $configuration) |
|
204 | } |
||
205 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.