1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebComplete\core\utils\migration; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use WebComplete\core\utils\container\ContainerInterface; |
7
|
|
|
|
8
|
|
|
class MigrationRegistryMysql extends AbstractMigrationRegistry |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Connection |
12
|
|
|
*/ |
13
|
|
|
protected $db; |
14
|
|
|
protected $migrationTable = '_migrations'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param ContainerInterface $container |
18
|
|
|
* @param Connection $db |
19
|
|
|
*/ |
20
|
|
|
public function __construct(ContainerInterface $container, Connection $db) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($container); |
23
|
|
|
$this->db = $db; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* check or create initial registry |
28
|
|
|
* |
29
|
|
|
* @throws \Doctrine\DBAL\DBALException |
30
|
|
|
*/ |
31
|
|
|
public function initRegistry() |
32
|
|
|
{ |
33
|
|
|
$this->db->executeQuery($this->getInitialSql()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string[] migration classes |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
public function getRegistered(): array |
41
|
|
|
{ |
42
|
|
|
$this->initRegistry(); |
43
|
|
|
$result = []; |
44
|
|
|
$stmt = $this->db->createQueryBuilder() |
45
|
|
|
->select('*')->from($this->migrationTable)->orderBy('id', 'asc')->execute(); |
46
|
|
|
if ($rows = $stmt->fetchAll(\PDO::FETCH_ASSOC)) { |
47
|
|
|
foreach ($rows as $row) { |
48
|
|
|
if (isset($row['class'])) { |
49
|
|
|
$result[] = $row['class']; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $class migration class |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function isRegistered(string $class): bool |
62
|
|
|
{ |
63
|
|
|
return \in_array($class, $this->getRegistered(), true); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $class migration class |
68
|
|
|
* @throws \Exception |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function register(string $class) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$this->initRegistry(); |
73
|
|
|
if (!$this->isRegistered($class)) { |
74
|
|
|
$migration = $this->getMigration($class); |
75
|
|
|
$migration->up(); |
76
|
|
|
$this->db->insert($this->migrationTable, ['class' => $class]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $class migration class |
82
|
|
|
* @throws \Exception |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function unregister(string $class) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$this->initRegistry(); |
87
|
|
|
if ($this->isRegistered($class)) { |
88
|
|
|
$migration = $this->getMigration($class); |
89
|
|
|
$migration->down(); |
90
|
|
|
$this->db->delete($this->migrationTable, ['class' => $class]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
protected function getInitialSql(): string |
98
|
|
|
{ |
99
|
|
|
return "CREATE TABLE IF NOT EXISTS `{$this->migrationTable}` ( |
100
|
|
|
`id` INT(11) NOT NULL AUTO_INCREMENT, |
101
|
|
|
`class` VARCHAR(300) NOT NULL, |
102
|
|
|
`created_on` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
103
|
|
|
PRIMARY KEY(`id`), |
104
|
|
|
UNIQUE(`class`) |
105
|
|
|
)"; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.