1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stats\Commands\Database; |
4
|
|
|
|
5
|
|
|
use Joomla\Controller\AbstractController; |
6
|
|
|
use Psr\Log\LoggerAwareInterface; |
7
|
|
|
use Psr\Log\LoggerAwareTrait; |
8
|
|
|
use Stats\CommandInterface; |
9
|
|
|
use Stats\Database\Migrations; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* CLI command for migrating the database |
13
|
|
|
* |
14
|
|
|
* @method \Stats\CliApplication getApplication() Get the application object. |
15
|
|
|
* @property-read \Stats\CliApplication $app Application object |
16
|
|
|
* |
17
|
|
|
* @since 1.0 |
18
|
|
|
*/ |
19
|
|
|
class MigrateCommand extends AbstractController implements CommandInterface, LoggerAwareInterface |
20
|
|
|
{ |
21
|
|
|
use LoggerAwareTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Database migrations helper |
25
|
|
|
* |
26
|
|
|
* @var Migrations |
27
|
|
|
* @since 1.0 |
28
|
|
|
*/ |
29
|
|
|
private $migrations; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor. |
33
|
|
|
* |
34
|
|
|
* @param Migrations $migrations Database migrations helper |
35
|
|
|
* |
36
|
|
|
* @since 1.0 |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Migrations $migrations) |
39
|
|
|
{ |
40
|
|
|
$this->migrations = $migrations; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Execute the command. |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
* |
48
|
|
|
* @since 1.0 |
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
|
|
|
* @since 1.0 |
88
|
|
|
*/ |
89
|
|
|
public function getDescription() |
90
|
|
|
{ |
91
|
|
|
return 'Migrate the database schema to a newer version.'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the command's title |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
* |
99
|
|
|
* @since 1.0 |
100
|
|
|
*/ |
101
|
|
|
public function getTitle() |
102
|
|
|
{ |
103
|
|
|
return 'Database Migrations'; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|