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