|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Joomla! Statistics Server |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved. |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Joomla\StatsServer\Commands\Database; |
|
10
|
|
|
|
|
11
|
|
|
use Joomla\Console\Command\AbstractCommand; |
|
12
|
|
|
use Joomla\StatsServer\Database\Migrations; |
|
13
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
14
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* CLI command for migrating the database |
|
22
|
|
|
*/ |
|
23
|
|
|
class MigrateCommand extends AbstractCommand implements LoggerAwareInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use LoggerAwareTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The default command name |
|
29
|
|
|
* |
|
30
|
|
|
* @var string|null |
|
31
|
|
|
*/ |
|
32
|
|
|
protected static $defaultName = 'database:migrate'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Database migrations helper |
|
36
|
|
|
* |
|
37
|
|
|
* @var Migrations |
|
38
|
|
|
*/ |
|
39
|
|
|
private $migrations; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Migrations $migrations Database migrations helper |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function __construct(Migrations $migrations) |
|
47
|
|
|
{ |
|
48
|
3 |
|
$this->migrations = $migrations; |
|
49
|
|
|
|
|
50
|
3 |
|
parent::__construct(); |
|
51
|
3 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Internal function to execute the command. |
|
55
|
|
|
* |
|
56
|
|
|
* @param InputInterface $input The input to inject into the command. |
|
57
|
|
|
* @param OutputInterface $output The output to inject into the command. |
|
58
|
|
|
* |
|
59
|
|
|
* @return integer The command exit code |
|
60
|
|
|
*/ |
|
61
|
3 |
|
protected function doExecute(InputInterface $input, OutputInterface $output): int |
|
62
|
|
|
{ |
|
63
|
3 |
|
$symfonyStyle = new SymfonyStyle($input, $output); |
|
64
|
|
|
|
|
65
|
3 |
|
$symfonyStyle->title('Database Migrations: Migrate'); |
|
66
|
|
|
|
|
67
|
|
|
// If a version is given, we are only executing that migration |
|
68
|
3 |
|
$version = $input->getOption('version'); |
|
69
|
|
|
|
|
70
|
|
|
try |
|
71
|
|
|
{ |
|
72
|
3 |
|
$this->migrations->migrateDatabase($version); |
|
73
|
|
|
} |
|
74
|
1 |
|
catch (\Exception $exception) |
|
75
|
|
|
{ |
|
76
|
1 |
|
$this->logger->critical( |
|
77
|
1 |
|
'Error migrating database', |
|
78
|
1 |
|
['exception' => $exception] |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
1 |
|
$symfonyStyle->error(sprintf('Error migrating database: %s', $exception->getMessage())); |
|
82
|
|
|
|
|
83
|
1 |
|
return 1; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
if ($version) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$message = sprintf('Database migrated to version "%s".', $version); |
|
89
|
|
|
} |
|
90
|
|
|
else |
|
91
|
|
|
{ |
|
92
|
1 |
|
$message = 'Database migrated to latest version.'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
$this->logger->info($message); |
|
96
|
|
|
|
|
97
|
2 |
|
$symfonyStyle->success($message); |
|
98
|
|
|
|
|
99
|
2 |
|
return 0; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Configures the current command. |
|
104
|
|
|
* |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
3 |
|
protected function configure(): void |
|
108
|
|
|
{ |
|
109
|
3 |
|
$this->setDescription('Migrate the database schema to a newer version.'); |
|
110
|
3 |
|
$this->addOption( |
|
111
|
3 |
|
'version', |
|
112
|
3 |
|
null, |
|
113
|
3 |
|
InputOption::VALUE_OPTIONAL, |
|
114
|
3 |
|
'If specified, only the given migration will be executed if necessary.' |
|
115
|
|
|
); |
|
116
|
3 |
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|