Skip to content

Commit e5376ba

Browse files
committed
daos\*\Database: Add method for getting db schema version
This will be used by import/export tool. Also cleans up the comparisons a bit.
1 parent 04b977b commit e5376ba

File tree

4 files changed

+57
-34
lines changed

4 files changed

+57
-34
lines changed

src/daos/DatabaseInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,11 @@ public function commit();
6363
* @return void
6464
*/
6565
public function optimize();
66+
67+
/**
68+
* Get the current version database schema.
69+
*
70+
* @return int
71+
*/
72+
public function getSchemaVersion();
6673
}

src/daos/mysql/Database.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,9 @@ public function __construct(Configuration $configuration, \DB\SQL $connection, L
129129
}
130130
}
131131

132-
$version = @$this->exec('SELECT version FROM ' . $this->configuration->dbPrefix . 'version ORDER BY version DESC LIMIT 0, 1');
133-
$version = $version[0]['version'];
132+
$version = $this->getSchemaVersion();
134133

135-
if (strnatcmp($version, '3') < 0) {
134+
if ($version < 3) {
136135
$this->logger->debug('Upgrading database schema to version 3');
137136

138137
$this->exec('
@@ -142,7 +141,7 @@ public function __construct(Configuration $configuration, \DB\SQL $connection, L
142141
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (3);
143142
');
144143
}
145-
if (strnatcmp($version, '4') < 0) {
144+
if ($version < 4) {
146145
$this->logger->debug('Upgrading database schema to version 4');
147146

148147
$this->exec('
@@ -166,7 +165,7 @@ public function __construct(Configuration $configuration, \DB\SQL $connection, L
166165
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (4);
167166
');
168167
}
169-
if (strnatcmp($version, '5') < 0) {
168+
if ($version < 5) {
170169
$this->logger->debug('Upgrading database schema to version 5');
171170

172171
$this->exec('
@@ -176,7 +175,7 @@ public function __construct(Configuration $configuration, \DB\SQL $connection, L
176175
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (5);
177176
');
178177
}
179-
if (strnatcmp($version, '6') < 0) {
178+
if ($version < 6) {
180179
$this->logger->debug('Upgrading database schema to version 6');
181180

182181
$this->exec('
@@ -189,7 +188,7 @@ public function __construct(Configuration $configuration, \DB\SQL $connection, L
189188
// Jump straight from v6 to v8 due to bug in previous version of the code
190189
// in \daos\sqlite\Database which
191190
// set the database version to "7" for initial installs.
192-
if (strnatcmp($version, '8') < 0) {
191+
if ($version < 8) {
193192
$this->logger->debug('Upgrading database schema to version 8');
194193

195194
$this->exec('
@@ -199,7 +198,7 @@ public function __construct(Configuration $configuration, \DB\SQL $connection, L
199198
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (8);
200199
');
201200
}
202-
if (strnatcmp($version, '9') < 0) {
201+
if ($version < 9) {
203202
$this->logger->debug('Upgrading database schema to version 9');
204203

205204
$this->exec('
@@ -209,7 +208,7 @@ public function __construct(Configuration $configuration, \DB\SQL $connection, L
209208
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (9);
210209
');
211210
}
212-
if (strnatcmp($version, '10') < 0) {
211+
if ($version < 10) {
213212
$this->logger->debug('Upgrading database schema to version 10');
214213

215214
$this->exec([
@@ -220,7 +219,7 @@ public function __construct(Configuration $configuration, \DB\SQL $connection, L
220219
'INSERT INTO `' . $this->configuration->dbPrefix . 'version` (version) VALUES (10);',
221220
]);
222221
}
223-
if (strnatcmp($version, '11') < 0) {
222+
if ($version < 11) {
224223
$this->logger->debug('Upgrading database schema to version 11');
225224

226225
$this->exec([
@@ -251,7 +250,7 @@ public function __construct(Configuration $configuration, \DB\SQL $connection, L
251250
'INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (11)',
252251
]);
253252
}
254-
if (strnatcmp($version, '12') < 0) {
253+
if ($version < 12) {
255254
$this->logger->debug('Upgrading database schema to version 12');
256255

257256
$this->exec([
@@ -261,7 +260,7 @@ public function __construct(Configuration $configuration, \DB\SQL $connection, L
261260
'INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (12)',
262261
]);
263262
}
264-
if (strnatcmp($version, '13') < 0) {
263+
if ($version < 13) {
265264
$this->logger->debug('Upgrading database schema to version 13');
266265

267266
$this->exec([
@@ -330,4 +329,10 @@ public function commit() {
330329
public function optimize() {
331330
@$this->exec('OPTIMIZE TABLE `' . $this->configuration->dbPrefix . 'sources`, `' . $this->configuration->dbPrefix . 'items`');
332331
}
332+
333+
public function getSchemaVersion() {
334+
$version = @$this->exec('SELECT version FROM ' . $this->configuration->dbPrefix . 'version ORDER BY version DESC LIMIT 1');
335+
336+
return (int) $version[0]['version'];
337+
}
333338
}

src/daos/pgsql/Database.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
134134
');
135135
}
136136
}
137-
$version = @$this->exec('SELECT version FROM version ORDER BY version DESC LIMIT 1');
138-
$version = $version[0]['version'];
139137

140-
if (strnatcmp($version, '3') < 0) {
138+
$version = $this->getSchemaVersion();
139+
140+
if ($version < 3) {
141141
$this->logger->debug('Upgrading database schema to version 3');
142142

143143
$this->exec('
@@ -147,7 +147,7 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
147147
INSERT INTO version (version) VALUES (3);
148148
');
149149
}
150-
if (strnatcmp($version, '4') < 0) {
150+
if ($version < 4) {
151151
$this->logger->debug('Upgrading database schema to version 4');
152152

153153
$this->exec('
@@ -174,15 +174,15 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
174174
INSERT INTO version (version) VALUES (4);
175175
');
176176
}
177-
if (strnatcmp($version, '5') < 0) {
177+
if ($version < 5) {
178178
$this->logger->debug('Upgrading database schema to version 5');
179179

180180
$this->exec([
181181
'ALTER TABLE items ADD author TEXT;',
182182
'INSERT INTO version (version) VALUES (5);',
183183
]);
184184
}
185-
if (strnatcmp($version, '6') < 0) {
185+
if ($version < 6) {
186186
$this->logger->debug('Upgrading database schema to version 6');
187187

188188
$this->exec([
@@ -193,23 +193,23 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
193193
// Jump straight from v6 to v8 due to bug in previous version of the code
194194
// in \daos\sqlite\Database which
195195
// set the database version to "7" for initial installs.
196-
if (strnatcmp($version, '8') < 0) {
196+
if ($version < 8) {
197197
$this->logger->debug('Upgrading database schema to version 8');
198198

199199
$this->exec([
200200
'ALTER TABLE sources ADD lastentry INT;',
201201
'INSERT INTO version (version) VALUES (8);',
202202
]);
203203
}
204-
if (strnatcmp($version, '9') < 0) {
204+
if ($version < 9) {
205205
$this->logger->debug('Upgrading database schema to version 9');
206206

207207
$this->exec([
208208
'ALTER TABLE items ADD shared BOOLEAN;',
209209
'INSERT INTO version (version) VALUES (9);',
210210
]);
211211
}
212-
if (strnatcmp($version, '10') < 0) {
212+
if ($version < 10) {
213213
$this->logger->debug('Upgrading database schema to version 10');
214214

215215
$this->exec([
@@ -218,7 +218,7 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
218218
'INSERT INTO version (version) VALUES (10);',
219219
]);
220220
}
221-
if (strnatcmp($version, '11') < 0) {
221+
if ($version < 11) {
222222
$this->logger->debug('Upgrading database schema to version 11');
223223

224224
$this->exec([
@@ -234,7 +234,7 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
234234
'INSERT INTO version (version) VALUES (11);',
235235
]);
236236
}
237-
if (strnatcmp($version, '12') < 0) {
237+
if ($version < 12) {
238238
$this->logger->debug('Upgrading database schema to version 12');
239239

240240
$this->exec([
@@ -243,7 +243,7 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
243243
'INSERT INTO version (version) VALUES (12)',
244244
]);
245245
}
246-
if (strnatcmp($version, '13') < 0) {
246+
if ($version < 13) {
247247
$this->logger->debug('Upgrading database schema to version 13');
248248

249249
$this->exec([
@@ -314,4 +314,10 @@ public function commit() {
314314
*/
315315
public function optimize() {
316316
}
317+
318+
public function getSchemaVersion() {
319+
$version = @$this->exec('SELECT version FROM version ORDER BY version DESC LIMIT 1');
320+
321+
return (int) $version[0]['version'];
322+
}
317323
}

src/daos/sqlite/Database.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,9 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
124124
}
125125
}
126126

127-
$version = @$this->exec('SELECT version FROM version ORDER BY version DESC LIMIT 0, 1');
128-
$version = $version[0]['version'];
127+
$version = $this->getSchemaVersion();
129128

130-
if (strnatcmp($version, '3') < 0) {
129+
if ($version < 3) {
131130
$this->logger->debug('Upgrading database schema to version 3');
132131

133132
$this->exec('
@@ -137,7 +136,7 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
137136
INSERT INTO version (version) VALUES (3);
138137
');
139138
}
140-
if (strnatcmp($version, '4') < 0) {
139+
if ($version < 4) {
141140
$this->logger->debug('Upgrading database schema to version 4');
142141

143142
$this->exec('
@@ -165,7 +164,7 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
165164
INSERT INTO version (version) VALUES (4);
166165
');
167166
}
168-
if (strnatcmp($version, '5') < 0) {
167+
if ($version < 5) {
169168
$this->logger->debug('Upgrading database schema to version 5');
170169

171170
$this->exec('
@@ -175,7 +174,7 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
175174
INSERT INTO version (version) VALUES (5);
176175
');
177176
}
178-
if (strnatcmp($version, '6') < 0) {
177+
if ($version < 6) {
179178
$this->logger->debug('Upgrading database schema to version 6');
180179

181180
$this->exec('
@@ -188,7 +187,7 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
188187
// Jump straight from v6 to v8 due to bug in previous version of the code
189188
// in \daos\sqlite\Database which
190189
// set the database version to "7" for initial installs.
191-
if (strnatcmp($version, '8') < 0) {
190+
if ($version < 8) {
192191
$this->logger->debug('Upgrading database schema to version 8');
193192

194193
$this->exec('
@@ -200,7 +199,7 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
200199

201200
$this->initLastEntryFieldDuringUpgrade();
202201
}
203-
if (strnatcmp($version, '9') < 0) {
202+
if ($version < 9) {
204203
$this->logger->debug('Upgrading database schema to version 9');
205204

206205
$this->exec('
@@ -210,7 +209,7 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
210209
INSERT INTO version (version) VALUES (9);
211210
');
212211
}
213-
if (strnatcmp($version, '11') < 0) {
212+
if ($version < 11) {
214213
$this->logger->debug('Upgrading database schema to version 11');
215214

216215
$this->exec([
@@ -252,7 +251,7 @@ public function __construct(\DB\SQL $connection, Logger $logger) {
252251
'INSERT INTO version (version) VALUES (11)',
253252
]);
254253
}
255-
if (strnatcmp($version, '13') < 0) {
254+
if ($version < 13) {
256255
$this->logger->debug('Upgrading database schema to version 13');
257256

258257
$this->exec([
@@ -349,4 +348,10 @@ private function initLastEntryFieldDuringUpgrade() {
349348
}
350349
}
351350
}
351+
352+
public function getSchemaVersion() {
353+
$version = @$this->exec('SELECT version FROM version ORDER BY version DESC LIMIT 1');
354+
355+
return (int) $version[0]['version'];
356+
}
352357
}

0 commit comments

Comments
 (0)