1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
<?php
/**
* DBSchemaVersion.php - database backed schema versions
*
* Implementation of SchemaVersion interface using a database table.
*
* @author Elmar Ludwig
* @copyright 2007 Elmar Ludwig
* @license GPL2 or any later version
* @package migrations
*/
class DBSchemaVersion implements SchemaVersion
{
/**
* domain name of schema version
*
* @var string
*/
private $domain;
/**
* schema versions
*
* @var array
*/
private $versions = [];
/**
* @var array
*/
private $data = [];
/**
* Initialize a new DBSchemaVersion for a given domain.
* The default domain name is 'studip'.
*
* @param string $domain domain name (optional)
*/
public function __construct($domain = 'studip')
{
$this->domain = $domain;
$this->initSchemaInfo();
}
/**
* Retrieve the domain name of this schema.
*
* @return string domain name
*/
public function getDomain()
{
return $this->domain;
}
/**
* Initialize the current schema version.
*/
private function initSchemaInfo()
{
$this->data = [];
try {
$query = "SELECT version FROM schema_versions WHERE domain = ?";
$statement = DBManager::get()->prepare($query);
$statement->execute([$this->domain]);
$this->versions = $statement->fetchAll(PDO::FETCH_COLUMN);
} catch (PDOException $e) {
$query = "SELECT version FROM schema_version WHERE domain = ?";
$statement = DBManager::get()->prepare($query);
$statement->execute([$this->domain]);
$this->versions = range(1, $statement->fetchColumn());
}
}
/**
* Retrieve the current schema version.
*
* @return int schema version
*/
public function get()
{
return $this->versions ? max($this->versions) : 0;
}
/**
* Returns whether the given version is already present for the given
* domain.
*
* @param int $version Version number
* @return bool
*/
public function contains($version)
{
return in_array($version, $this->versions);
}
/**
* Set the current schema version.
*
* @param int $version new schema version
*/
public function add($version)
{
$version = (int) $version;
try {
$query = "INSERT INTO `schema_versions` (`domain`, `version`)
VALUES (?, ?)";
DBManager::get()->execute($query, [
$this->domain,
$version,
]);
StudipLog::log(
'MIGRATE_UP',
$version,
$this->domain
);
} catch (PDOException $e) {
$query = "UPDATE `schema_version`
SET `version` = ?
WHERE `domain` = ?";;
DBManager::get()->execute($query, [
$version,
$this->domain,
]);
}
NotificationCenter::postNotification(
'SchemaVersionDidUpdate',
$this->domain,
$version
);
}
/**
* Removes a schema version.
*
* @param int $version schema version to remove
*/
public function remove($version)
{
$version = (int) $version;
try {
$query = "DELETE FROM `schema_versions`
WHERE `domain` = ? AND `version` = ?";
DBManager::get()->execute($query, [
$this->domain,
$version
]);
StudipLog::log(
'MIGRATE_DOWN',
$version,
$this->domain
);
} catch (PDOException $e) {
$query = "UPDATE `schema_version`
SET `version` = ?
WHERE `domain` = ?";
DBManager::get()->execute($query, [
$version,
$this->domain,
]);
}
NotificationCenter::postNotification(
'SchemaVersionDidDelete',
$this->domain,
$version
);
}
/**
* @param $domain
* @param $version
* @return string
*/
static public function exists($domain, $version)
{
return (bool)DBManager::get()->fetchColumn(
"SELECT 1 FROM schema_versions WHERE `domain` = ? AND `version` = ?",
[$domain, $version]);
}
}
|