1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\DbMigration\Database; |
4
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\Db\DbDriverInterface; |
6
|
|
|
use ByJG\AnyDataset\Db\Factory; |
7
|
|
|
use ByJG\DbMigration\Exception\DatabaseNotVersionedException; |
8
|
|
|
use ByJG\DbMigration\Exception\OldVersionSchemaException; |
9
|
|
|
use ByJG\DbMigration\Migration; |
10
|
|
|
use Psr\Http\Message\UriInterface; |
11
|
|
|
|
12
|
|
|
abstract class AbstractDatabase implements DatabaseInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var DbDriverInterface |
16
|
|
|
*/ |
17
|
|
|
private $dbDriver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var UriInterface |
21
|
|
|
*/ |
22
|
|
|
private $uri; |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $migrationTable; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Command constructor. |
30
|
|
|
* |
31
|
|
|
* @param UriInterface $uri |
32
|
|
|
* @param string $migrationTable |
33
|
|
|
*/ |
34
|
|
|
public function __construct(UriInterface $uri, $migrationTable = 'migration_version') |
35
|
|
|
{ |
36
|
|
|
$this->uri = $uri; |
37
|
|
|
$this->migrationTable = $migrationTable; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getMigrationTable() |
44
|
|
|
{ |
45
|
|
|
return $this->migrationTable; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return DbDriverInterface |
50
|
|
|
*/ |
51
|
|
|
public function getDbDriver() |
52
|
|
|
{ |
53
|
|
|
if (is_null($this->dbDriver)) { |
54
|
|
|
$this->dbDriver = Factory::getDbRelationalInstance($this->uri->__toString()); |
55
|
|
|
} |
56
|
|
|
return $this->dbDriver; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array |
61
|
|
|
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
62
|
|
|
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
63
|
|
|
*/ |
64
|
|
|
public function getVersion() |
65
|
|
|
{ |
66
|
|
|
$result = []; |
67
|
|
|
try { |
68
|
|
|
$result['version'] = $this->getDbDriver()->getScalar('SELECT version FROM ' . $this->getMigrationTable()); |
69
|
|
|
} catch (\Exception $ex) { |
70
|
|
|
throw new DatabaseNotVersionedException('This database does not have a migration version. Please use "migrate reset" or "migrate install" to create one.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
$result['status'] = $this->getDbDriver()->getScalar('SELECT status FROM ' . $this->getMigrationTable()); |
75
|
|
|
} catch (\Exception $ex) { |
76
|
|
|
throw new OldVersionSchemaException('This database does not have a migration version. Please use "migrate install" for update it.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $version |
84
|
|
|
* @param $status |
85
|
|
|
*/ |
86
|
|
|
public function setVersion($version, $status) |
87
|
|
|
{ |
88
|
|
|
$this->getDbDriver()->execute( |
89
|
|
|
'UPDATE ' . $this->getMigrationTable() . ' SET version = :version, status = :status', |
90
|
|
|
[ |
91
|
|
|
'version' => $version, |
92
|
|
|
'status' => $status, |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
99
|
|
|
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
100
|
|
|
*/ |
101
|
|
|
protected function checkExistsVersion() |
102
|
|
|
{ |
103
|
|
|
// Get the version to check if exists |
104
|
|
|
$versionInfo = $this->getVersion(); |
105
|
|
|
if ($versionInfo['version'] === false) { |
106
|
|
|
$this->getDbDriver()->execute(sprintf( |
107
|
|
|
"insert into %s values(0, '%s')", |
108
|
|
|
$this->getMigrationTable(), |
109
|
|
|
Migration::VERSION_STATUS_UNKNOWN) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* |
116
|
|
|
*/ |
117
|
|
|
public function updateVersionTable() |
118
|
|
|
{ |
119
|
|
|
$currentVersion = $this->getDbDriver()->getScalar(sprintf('select version from %s', $this->getMigrationTable())); |
120
|
|
|
$this->getDbDriver()->execute(sprintf('drop table %s', $this->getMigrationTable())); |
121
|
|
|
$this->createVersion(); |
122
|
|
|
$this->setVersion($currentVersion, Migration::VERSION_STATUS_UNKNOWN); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function isTableExists($schema, $table) |
126
|
|
|
{ |
127
|
|
|
$count = $this->getDbDriver()->getScalar( |
128
|
|
|
'SELECT count(*) FROM information_schema.tables ' . |
129
|
|
|
' WHERE table_schema = [[schema]] ' . |
130
|
|
|
' AND table_name = [[table]] ', |
131
|
|
|
[ |
132
|
|
|
"schema" => $schema, |
133
|
|
|
"table" => $table |
134
|
|
|
] |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
return (intval($count) !== 0); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function isDatabaseVersioned() |
141
|
|
|
{ |
142
|
|
|
return $this->isTableExists(ltrim($this->getDbDriver()->getUri()->getPath(), "/"), $this->getMigrationTable()); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|