|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Migrations\Tools\Console\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Migrations\Version; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use function getcwd; |
|
13
|
|
|
use function is_bool; |
|
14
|
|
|
|
|
15
|
|
|
class ExecuteCommand extends AbstractCommand |
|
16
|
|
|
{ |
|
17
|
13 |
|
protected function configure() : void |
|
18
|
|
|
{ |
|
19
|
|
|
$this |
|
20
|
13 |
|
->setName('migrations:execute') |
|
21
|
13 |
|
->setDescription( |
|
22
|
13 |
|
'Execute a single migration version up or down manually.' |
|
23
|
|
|
) |
|
24
|
13 |
|
->addArgument( |
|
25
|
13 |
|
'version', |
|
26
|
13 |
|
InputArgument::REQUIRED, |
|
27
|
13 |
|
'The version to execute.', |
|
28
|
13 |
|
null |
|
29
|
|
|
) |
|
30
|
13 |
|
->addOption( |
|
31
|
13 |
|
'write-sql', |
|
32
|
13 |
|
null, |
|
33
|
13 |
|
InputOption::VALUE_NONE, |
|
34
|
13 |
|
'The path to output the migration SQL file instead of executing it.' |
|
35
|
|
|
) |
|
36
|
13 |
|
->addOption( |
|
37
|
13 |
|
'dry-run', |
|
38
|
13 |
|
null, |
|
39
|
13 |
|
InputOption::VALUE_NONE, |
|
40
|
13 |
|
'Execute the migration as a dry run.' |
|
41
|
|
|
) |
|
42
|
13 |
|
->addOption( |
|
43
|
13 |
|
'up', |
|
44
|
13 |
|
null, |
|
45
|
13 |
|
InputOption::VALUE_NONE, |
|
46
|
13 |
|
'Execute the migration up.' |
|
47
|
|
|
) |
|
48
|
13 |
|
->addOption( |
|
49
|
13 |
|
'down', |
|
50
|
13 |
|
null, |
|
51
|
13 |
|
InputOption::VALUE_NONE, |
|
52
|
13 |
|
'Execute the migration down.' |
|
53
|
|
|
) |
|
54
|
13 |
|
->addOption( |
|
55
|
13 |
|
'query-time', |
|
56
|
13 |
|
null, |
|
57
|
13 |
|
InputOption::VALUE_NONE, |
|
58
|
13 |
|
'Time all the queries individually.' |
|
59
|
|
|
) |
|
60
|
13 |
|
->setHelp(<<<EOT |
|
61
|
13 |
|
The <info>%command.name%</info> command executes a single migration version up or down manually: |
|
62
|
|
|
|
|
63
|
|
|
<info>%command.full_name% YYYYMMDDHHMMSS</info> |
|
64
|
|
|
|
|
65
|
|
|
If no <comment>--up</comment> or <comment>--down</comment> option is specified it defaults to up: |
|
66
|
|
|
|
|
67
|
|
|
<info>%command.full_name% YYYYMMDDHHMMSS --down</info> |
|
68
|
|
|
|
|
69
|
|
|
You can also execute the migration as a <comment>--dry-run</comment>: |
|
70
|
|
|
|
|
71
|
|
|
<info>%command.full_name% YYYYMMDDHHMMSS --dry-run</info> |
|
72
|
|
|
|
|
73
|
|
|
You can output the would be executed SQL statements to a file with <comment>--write-sql</comment>: |
|
74
|
|
|
|
|
75
|
|
|
<info>%command.full_name% YYYYMMDDHHMMSS --write-sql</info> |
|
76
|
|
|
|
|
77
|
|
|
Or you can also execute the migration without a warning message which you need to interact with: |
|
78
|
|
|
|
|
79
|
|
|
<info>%command.full_name% YYYYMMDDHHMMSS --no-interaction</info> |
|
80
|
|
|
EOT |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
13 |
|
parent::configure(); |
|
84
|
13 |
|
} |
|
85
|
|
|
|
|
86
|
5 |
|
public function execute(InputInterface $input, OutputInterface $output) : void |
|
87
|
|
|
{ |
|
88
|
5 |
|
$version = $input->getArgument('version'); |
|
89
|
5 |
|
$direction = $input->getOption('down') |
|
90
|
1 |
|
? Version::DIRECTION_DOWN |
|
91
|
5 |
|
: Version::DIRECTION_UP; |
|
92
|
|
|
|
|
93
|
5 |
|
$configuration = $this->getMigrationConfiguration($input, $output); |
|
94
|
|
|
|
|
95
|
5 |
|
$version = $configuration->getVersion($version); |
|
96
|
|
|
|
|
97
|
5 |
|
$timeAllqueries = $input->getOption('query-time'); |
|
98
|
|
|
|
|
99
|
5 |
|
$path = $input->getOption('write-sql'); |
|
100
|
|
|
|
|
101
|
5 |
|
if ($path) { |
|
102
|
2 |
|
$path = is_bool($path) ? getcwd() : $path; |
|
103
|
2 |
|
$version->writeSqlFile($path, $direction); |
|
104
|
|
|
} else { |
|
105
|
3 |
|
if ($input->isInteractive()) { |
|
106
|
2 |
|
$question = 'WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)'; |
|
107
|
2 |
|
$execute = $this->askConfirmation($question, $input, $output); |
|
108
|
|
|
} else { |
|
109
|
1 |
|
$execute = true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
3 |
|
if ($execute) { |
|
113
|
2 |
|
$version->execute($direction, (bool) $input->getOption('dry-run'), $timeAllqueries); |
|
114
|
|
|
} else { |
|
115
|
1 |
|
$output->writeln('<error>Migration cancelled!</error>'); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
5 |
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|