1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Commands; |
4
|
|
|
|
5
|
|
|
use Yarak\Config\Config; |
6
|
|
|
use Yarak\DB\ConnectionResolver; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
class Migrate extends YarakCommand |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Configure the command. |
15
|
|
|
*/ |
16
|
|
|
protected function configure() |
17
|
|
|
{ |
18
|
|
|
$this->setName('migrate') |
19
|
|
|
->setDescription('Run the database migrations.') |
20
|
|
|
->setHelp('This command allows you to run migrations.') |
21
|
|
|
->addRollback() |
22
|
|
|
->addSteps() |
23
|
|
|
->addReset() |
24
|
|
|
->addRefresh(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Add rollback option. |
29
|
|
|
*/ |
30
|
|
|
protected function addRollback() |
31
|
|
|
{ |
32
|
|
|
return $this->addOption( |
33
|
|
|
'rollback', |
34
|
|
|
null, |
35
|
|
|
InputOption::VALUE_NONE, |
36
|
|
|
'Rollback migrations by given number of steps.' |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Add steps option. |
42
|
|
|
*/ |
43
|
|
|
protected function addSteps() |
44
|
|
|
{ |
45
|
|
|
return $this->addOption( |
46
|
|
|
'steps', |
47
|
|
|
null, |
48
|
|
|
InputOption::VALUE_OPTIONAL, |
49
|
|
|
'Number of steps to rollback.', |
50
|
|
|
1 |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Add reset option. |
56
|
|
|
*/ |
57
|
|
|
protected function addReset() |
58
|
|
|
{ |
59
|
|
|
return $this->addOption( |
60
|
|
|
'reset', |
61
|
|
|
null, |
62
|
|
|
InputOption::VALUE_NONE, |
63
|
|
|
'Rollback all migrations.' |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Add refresh option. |
69
|
|
|
*/ |
70
|
|
|
protected function addRefresh() |
71
|
|
|
{ |
72
|
|
|
return $this->addOption( |
73
|
|
|
'refresh', |
74
|
|
|
null, |
75
|
|
|
InputOption::VALUE_NONE, |
76
|
|
|
'Rollback and re-run all migrations.' |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Execute the command. |
82
|
|
|
* |
83
|
|
|
* @param InputInterface $input |
84
|
|
|
* @param OutputInterface $output |
85
|
|
|
*/ |
86
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
87
|
|
|
{ |
88
|
|
|
$migrator = $this->getMigrator(); |
89
|
|
|
|
90
|
|
|
if ($input->getOption('rollback')) { |
91
|
|
|
$migrator->rollback($input->getOption('steps')); |
92
|
|
|
} elseif ($input->getOption('reset')) { |
93
|
|
|
$migrator->reset(); |
94
|
|
|
} elseif ($input->getOption('refresh')) { |
95
|
|
|
$migrator->refresh(); |
96
|
|
|
} else { |
97
|
|
|
$migrator->run(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
foreach ($migrator->getLog() as $message) { |
101
|
|
|
$output->writeln($message); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get an instance of the migrator. |
107
|
|
|
* |
108
|
|
|
* @return Migrator |
109
|
|
|
*/ |
110
|
|
|
protected function getMigrator() |
111
|
|
|
{ |
112
|
|
|
$config = Config::getInstance($this->configArray); |
113
|
|
|
|
114
|
|
|
$migratorClassName = $this->getMigratorClassName($config); |
115
|
|
|
|
116
|
|
|
return new $migratorClassName( |
117
|
|
|
$config, |
118
|
|
|
new ConnectionResolver(), |
119
|
|
|
$this->getRepository($config) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the name of the migrator class. |
125
|
|
|
* |
126
|
|
|
* @param Config $config |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
protected function getMigratorClassName(Config $config) |
131
|
|
|
{ |
132
|
|
|
$migratorType = ucfirst($config->get('migratorType')); |
133
|
|
|
|
134
|
|
|
return "Yarak\\Migrations\\$migratorType\\". |
135
|
|
|
$migratorType.'Migrator'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get an instance of MigrationRepository. |
140
|
|
|
* |
141
|
|
|
* @param Config $config |
142
|
|
|
* |
143
|
|
|
* @return Yarak\Migrations\MigrationRepository |
144
|
|
|
*/ |
145
|
|
|
protected function getRepository(Config $config) |
146
|
|
|
{ |
147
|
|
|
$repositoryType = ucfirst($config->get('migrationRepository')); |
148
|
|
|
|
149
|
|
|
$repositoryClass = 'Yarak\\Migrations\\Repositories\\'. |
150
|
|
|
$repositoryType.'MigrationRepository'; |
151
|
|
|
|
152
|
|
|
return new $repositoryClass(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|