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\Controller\AbstractController; |
12
|
|
|
use Joomla\StatsServer\CommandInterface; |
13
|
|
|
use Joomla\StatsServer\Database\Migrations; |
14
|
|
|
use Psr\Log\{ |
15
|
|
|
LoggerAwareInterface, LoggerAwareTrait |
16
|
|
|
}; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* CLI command for migrating the database |
20
|
|
|
* |
21
|
|
|
* @method \Joomla\StatsServer\CliApplication getApplication() Get the application object. |
22
|
|
|
* @property-read \Joomla\StatsServer\CliApplication $app Application object |
23
|
|
|
*/ |
24
|
|
|
class MigrateCommand extends AbstractController implements CommandInterface, LoggerAwareInterface |
25
|
|
|
{ |
26
|
|
|
use LoggerAwareTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Database migrations helper |
30
|
|
|
* |
31
|
|
|
* @var Migrations |
32
|
|
|
*/ |
33
|
|
|
private $migrations; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
* |
38
|
|
|
* @param Migrations $migrations Database migrations helper |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Migrations $migrations) |
41
|
|
|
{ |
42
|
|
|
$this->migrations = $migrations; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Execute the controller. |
47
|
|
|
* |
48
|
|
|
* @return boolean |
49
|
|
|
*/ |
50
|
|
|
public function execute() |
51
|
|
|
{ |
52
|
|
|
$this->getApplication()->outputTitle('Database Migrations: Migrate'); |
53
|
|
|
|
54
|
|
|
// If a version is given, we are only executing that migration |
55
|
|
|
$version = $this->getApplication()->input->getString('version', $this->getApplication()->input->getString('v', '')); |
56
|
|
|
|
57
|
|
|
try |
58
|
|
|
{ |
59
|
|
|
$this->migrations->migrateDatabase($version); |
60
|
|
|
} |
61
|
|
|
catch (\Exception $exception) |
62
|
|
|
{ |
63
|
|
|
$this->logger->critical( |
64
|
|
|
'Error migrating database', |
65
|
|
|
['exception' => $exception] |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$message = sprintf('Error migrating database: %s', $exception->getMessage()); |
69
|
|
|
|
70
|
|
|
$this->getApplication()->out("<error>$message</error>"); |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->logger->info('Database migrated to latest version.'); |
76
|
|
|
|
77
|
|
|
$this->getApplication()->out('<info>Database migrated to latest version.</info>'); |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the command's description |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getDescription() : string |
88
|
|
|
{ |
89
|
|
|
return 'Migrate the database schema to a newer version.'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the command's title |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getTitle() : string |
98
|
|
|
{ |
99
|
|
|
return 'Database Migrations'; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|