1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
12
|
|
|
use function count; |
13
|
|
|
use function sprintf; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Command to generate the SQL needed to update the database schema to match |
17
|
|
|
* the current mapping information. |
18
|
|
|
*/ |
19
|
|
|
class UpdateCommand extends AbstractCommand |
20
|
|
|
{ |
21
|
|
|
/** @var string */ |
22
|
|
|
protected $name = 'orm:schema-tool:update'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
$this->setName($this->name) |
30
|
|
|
->setDescription('Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata') |
31
|
|
|
->addOption('complete', null, InputOption::VALUE_NONE, 'If defined, all assets of the database which are not relevant to the current metadata will be dropped.') |
32
|
|
|
->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Dumps the generated SQL statements to the screen (does not execute them).') |
33
|
|
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Causes the generated SQL statements to be physically executed against your database.') |
34
|
|
|
->setHelp(<<<'EOT' |
35
|
|
|
The <info>%command.name%</info> command generates the SQL needed to |
36
|
|
|
synchronize the database schema with the current mapping metadata of the |
37
|
|
|
default entity manager. |
38
|
|
|
|
39
|
|
|
For example, if you add metadata for a new column to an entity, this command |
40
|
|
|
would generate and output the SQL needed to add the new column to the database: |
41
|
|
|
|
42
|
|
|
<info>%command.name% --dump-sql</info> |
43
|
|
|
|
44
|
|
|
Alternatively, you can execute the generated queries: |
45
|
|
|
|
46
|
|
|
<info>%command.name% --force</info> |
47
|
|
|
|
48
|
|
|
If both options are specified, the queries are output and then executed: |
49
|
|
|
|
50
|
|
|
<info>%command.name% --dump-sql --force</info> |
51
|
|
|
|
52
|
|
|
Finally, be aware that if the <info>--complete</info> option is passed, this |
53
|
|
|
task will drop all database assets (e.g. tables, etc) that are *not* described |
54
|
|
|
by the current metadata. In other words, without this option, this task leaves |
55
|
|
|
untouched any "extra" tables that exist in the database, but which aren't |
56
|
|
|
described by any metadata. |
57
|
|
|
|
58
|
|
|
<comment>Hint:</comment> If you have a database with tables that should not be managed |
59
|
|
|
by the ORM, you can use a DBAL functionality to filter the tables and sequences down |
60
|
|
|
on a global level: |
61
|
|
|
|
62
|
|
|
$config->setFilterSchemaAssetsExpression($regexp); |
63
|
|
|
EOT |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui) |
71
|
|
|
{ |
72
|
|
|
// Defining if update is complete or not (--complete not defined means $saveMode = true) |
73
|
|
|
$saveMode = ! $input->getOption('complete'); |
74
|
|
|
|
75
|
|
|
$sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); |
76
|
|
|
|
77
|
|
|
if (empty($sqls)) { |
78
|
|
|
$ui->success('Nothing to update - your database is already in sync with the current entity metadata.'); |
79
|
|
|
|
80
|
|
|
return 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$dumpSql = $input->getOption('dump-sql') === true; |
84
|
|
|
$force = $input->getOption('force') === true; |
85
|
|
|
|
86
|
|
|
if ($dumpSql) { |
87
|
|
|
$ui->text('The following SQL statements will be executed:'); |
88
|
|
|
$ui->newLine(); |
89
|
|
|
|
90
|
|
|
foreach ($sqls as $sql) { |
91
|
|
|
$ui->text(sprintf(' %s;', $sql)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($force) { |
96
|
|
|
if ($dumpSql) { |
97
|
|
|
$ui->newLine(); |
98
|
|
|
} |
99
|
|
|
$ui->text('Updating database schema...'); |
100
|
|
|
$ui->newLine(); |
101
|
|
|
|
102
|
|
|
$schemaTool->updateSchema($metadatas, $saveMode); |
103
|
|
|
|
104
|
|
|
$pluralization = count($sqls) === 1 ? 'query was' : 'queries were'; |
105
|
|
|
|
106
|
|
|
$ui->text(sprintf(' <info>%s</info> %s executed', count($sqls), $pluralization)); |
107
|
|
|
$ui->success('Database schema updated successfully!'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($dumpSql || $force) { |
111
|
|
|
return 0; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$ui->caution( |
115
|
|
|
[ |
116
|
|
|
'This operation should not be executed in a production environment!', |
117
|
|
|
'', |
118
|
|
|
'Use the incremental update to detect changes during development and use', |
119
|
|
|
'the SQL DDL provided to manually update your database in production.', |
120
|
|
|
] |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$ui->text( |
124
|
|
|
[ |
125
|
|
|
sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', count($sqls)), |
126
|
|
|
'', |
127
|
|
|
'Please run the operation by passing one - or both - of the following options:', |
128
|
|
|
'', |
129
|
|
|
sprintf(' <info>%s --force</info> to execute the command', $this->getName()), |
130
|
|
|
sprintf(' <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName()), |
131
|
|
|
] |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
return 1; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|