Skip to content

Commit 04b977b

Browse files
committed
daos\*\Database: Run all migration steps at the same time
Otherwise, the database will still be outdated in the first request, breaking imports.
1 parent 37a1f10 commit 04b977b

File tree

3 files changed

+369
-370
lines changed

3 files changed

+369
-370
lines changed

src/daos/mysql/Database.php

Lines changed: 131 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -127,147 +127,147 @@ public function __construct(Configuration $configuration, \DB\SQL $connection, L
127127
ALTER TABLE ' . $this->configuration->dbPrefix . 'sources ADD tags TEXT;
128128
');
129129
}
130-
} else {
131-
$version = @$this->exec('SELECT version FROM ' . $this->configuration->dbPrefix . 'version ORDER BY version DESC LIMIT 0, 1');
132-
$version = $version[0]['version'];
130+
}
133131

134-
if (strnatcmp($version, '3') < 0) {
135-
$this->logger->debug('Upgrading database schema to version 3');
132+
$version = @$this->exec('SELECT version FROM ' . $this->configuration->dbPrefix . 'version ORDER BY version DESC LIMIT 0, 1');
133+
$version = $version[0]['version'];
136134

137-
$this->exec('
138-
ALTER TABLE ' . $this->configuration->dbPrefix . 'sources ADD lastupdate INT;
139-
');
140-
$this->exec('
141-
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (3);
142-
');
143-
}
144-
if (strnatcmp($version, '4') < 0) {
145-
$this->logger->debug('Upgrading database schema to version 4');
135+
if (strnatcmp($version, '3') < 0) {
136+
$this->logger->debug('Upgrading database schema to version 3');
146137

147-
$this->exec('
148-
ALTER TABLE ' . $this->configuration->dbPrefix . 'items ADD updatetime DATETIME;
149-
');
150-
$this->exec('
151-
CREATE TRIGGER insert_updatetime_trigger
152-
BEFORE INSERT ON ' . $this->configuration->dbPrefix . 'items FOR EACH ROW
153-
BEGIN
154-
SET NEW.updatetime = NOW();
155-
END;
156-
');
157-
$this->exec('
158-
CREATE TRIGGER update_updatetime_trigger
159-
BEFORE UPDATE ON ' . $this->configuration->dbPrefix . 'items FOR EACH ROW
160-
BEGIN
161-
SET NEW.updatetime = NOW();
162-
END;
163-
');
164-
$this->exec('
165-
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (4);
166-
');
167-
}
168-
if (strnatcmp($version, '5') < 0) {
169-
$this->logger->debug('Upgrading database schema to version 5');
138+
$this->exec('
139+
ALTER TABLE ' . $this->configuration->dbPrefix . 'sources ADD lastupdate INT;
140+
');
141+
$this->exec('
142+
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (3);
143+
');
144+
}
145+
if (strnatcmp($version, '4') < 0) {
146+
$this->logger->debug('Upgrading database schema to version 4');
170147

171-
$this->exec('
172-
ALTER TABLE ' . $this->configuration->dbPrefix . 'items ADD author VARCHAR(255);
173-
');
174-
$this->exec('
175-
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (5);
176-
');
177-
}
178-
if (strnatcmp($version, '6') < 0) {
179-
$this->logger->debug('Upgrading database schema to version 6');
148+
$this->exec('
149+
ALTER TABLE ' . $this->configuration->dbPrefix . 'items ADD updatetime DATETIME;
150+
');
151+
$this->exec('
152+
CREATE TRIGGER insert_updatetime_trigger
153+
BEFORE INSERT ON ' . $this->configuration->dbPrefix . 'items FOR EACH ROW
154+
BEGIN
155+
SET NEW.updatetime = NOW();
156+
END;
157+
');
158+
$this->exec('
159+
CREATE TRIGGER update_updatetime_trigger
160+
BEFORE UPDATE ON ' . $this->configuration->dbPrefix . 'items FOR EACH ROW
161+
BEGIN
162+
SET NEW.updatetime = NOW();
163+
END;
164+
');
165+
$this->exec('
166+
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (4);
167+
');
168+
}
169+
if (strnatcmp($version, '5') < 0) {
170+
$this->logger->debug('Upgrading database schema to version 5');
180171

181-
$this->exec('
182-
ALTER TABLE ' . $this->configuration->dbPrefix . 'sources ADD filter TEXT;
183-
');
184-
$this->exec('
185-
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (6);
186-
');
187-
}
188-
// Jump straight from v6 to v8 due to bug in previous version of the code
189-
// in \daos\sqlite\Database which
190-
// set the database version to "7" for initial installs.
191-
if (strnatcmp($version, '8') < 0) {
192-
$this->logger->debug('Upgrading database schema to version 8');
172+
$this->exec('
173+
ALTER TABLE ' . $this->configuration->dbPrefix . 'items ADD author VARCHAR(255);
174+
');
175+
$this->exec('
176+
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (5);
177+
');
178+
}
179+
if (strnatcmp($version, '6') < 0) {
180+
$this->logger->debug('Upgrading database schema to version 6');
193181

194-
$this->exec('
195-
ALTER TABLE ' . $this->configuration->dbPrefix . 'sources ADD lastentry INT;
196-
');
197-
$this->exec('
198-
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (8);
199-
');
200-
}
201-
if (strnatcmp($version, '9') < 0) {
202-
$this->logger->debug('Upgrading database schema to version 9');
182+
$this->exec('
183+
ALTER TABLE ' . $this->configuration->dbPrefix . 'sources ADD filter TEXT;
184+
');
185+
$this->exec('
186+
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (6);
187+
');
188+
}
189+
// Jump straight from v6 to v8 due to bug in previous version of the code
190+
// in \daos\sqlite\Database which
191+
// set the database version to "7" for initial installs.
192+
if (strnatcmp($version, '8') < 0) {
193+
$this->logger->debug('Upgrading database schema to version 8');
203194

204-
$this->exec('
205-
ALTER TABLE ' . $this->configuration->dbPrefix . 'items ADD shared BOOL;
206-
');
207-
$this->exec('
208-
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (9);
209-
');
210-
}
211-
if (strnatcmp($version, '10') < 0) {
212-
$this->logger->debug('Upgrading database schema to version 10');
213-
214-
$this->exec([
215-
'ALTER TABLE `' . $this->configuration->dbPrefix . 'items` CONVERT TO CHARACTER SET utf8mb4;',
216-
'ALTER TABLE `' . $this->configuration->dbPrefix . 'sources` CONVERT TO CHARACTER SET utf8mb4;',
217-
'ALTER TABLE `' . $this->configuration->dbPrefix . 'tags` CONVERT TO CHARACTER SET utf8mb4;',
218-
'ALTER TABLE `' . $this->configuration->dbPrefix . 'version` CONVERT TO CHARACTER SET utf8mb4;',
219-
'INSERT INTO `' . $this->configuration->dbPrefix . 'version` (version) VALUES (10);',
220-
]);
221-
}
222-
if (strnatcmp($version, '11') < 0) {
223-
$this->logger->debug('Upgrading database schema to version 11');
224-
225-
$this->exec([
226-
'DROP TRIGGER insert_updatetime_trigger',
227-
'DROP TRIGGER update_updatetime_trigger',
228-
'ALTER TABLE ' . $this->configuration->dbPrefix . 'items ADD lastseen DATETIME',
229-
'UPDATE ' . $this->configuration->dbPrefix . 'items SET lastseen = CURRENT_TIMESTAMP',
230-
// Needs to be a trigger since MySQL before 5.6.5 does not support default value for DATETIME.
231-
// https://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-5.html#mysqld-5-6-5-data-types
232-
// Needs to be a single trigger due to MySQL before 5.7.2 not supporting multiple triggers for the same event on the same table.
233-
// https://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-2.html#mysqld-5-7-2-triggers
234-
'CREATE TRIGGER ' . $this->configuration->dbPrefix . 'items_insert_trigger
235-
BEFORE INSERT ON ' . $this->configuration->dbPrefix . 'items FOR EACH ROW
236-
BEGIN
237-
SET NEW.updatetime = NOW();
238-
SET NEW.lastseen = NOW();
239-
END;',
240-
'CREATE TRIGGER ' . $this->configuration->dbPrefix . 'items_update_trigger
241-
BEFORE UPDATE ON ' . $this->configuration->dbPrefix . 'items FOR EACH ROW
195+
$this->exec('
196+
ALTER TABLE ' . $this->configuration->dbPrefix . 'sources ADD lastentry INT;
197+
');
198+
$this->exec('
199+
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (8);
200+
');
201+
}
202+
if (strnatcmp($version, '9') < 0) {
203+
$this->logger->debug('Upgrading database schema to version 9');
204+
205+
$this->exec('
206+
ALTER TABLE ' . $this->configuration->dbPrefix . 'items ADD shared BOOL;
207+
');
208+
$this->exec('
209+
INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (9);
210+
');
211+
}
212+
if (strnatcmp($version, '10') < 0) {
213+
$this->logger->debug('Upgrading database schema to version 10');
214+
215+
$this->exec([
216+
'ALTER TABLE `' . $this->configuration->dbPrefix . 'items` CONVERT TO CHARACTER SET utf8mb4;',
217+
'ALTER TABLE `' . $this->configuration->dbPrefix . 'sources` CONVERT TO CHARACTER SET utf8mb4;',
218+
'ALTER TABLE `' . $this->configuration->dbPrefix . 'tags` CONVERT TO CHARACTER SET utf8mb4;',
219+
'ALTER TABLE `' . $this->configuration->dbPrefix . 'version` CONVERT TO CHARACTER SET utf8mb4;',
220+
'INSERT INTO `' . $this->configuration->dbPrefix . 'version` (version) VALUES (10);',
221+
]);
222+
}
223+
if (strnatcmp($version, '11') < 0) {
224+
$this->logger->debug('Upgrading database schema to version 11');
225+
226+
$this->exec([
227+
'DROP TRIGGER insert_updatetime_trigger',
228+
'DROP TRIGGER update_updatetime_trigger',
229+
'ALTER TABLE ' . $this->configuration->dbPrefix . 'items ADD lastseen DATETIME',
230+
'UPDATE ' . $this->configuration->dbPrefix . 'items SET lastseen = CURRENT_TIMESTAMP',
231+
// Needs to be a trigger since MySQL before 5.6.5 does not support default value for DATETIME.
232+
// https://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-5.html#mysqld-5-6-5-data-types
233+
// Needs to be a single trigger due to MySQL before 5.7.2 not supporting multiple triggers for the same event on the same table.
234+
// https://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-2.html#mysqld-5-7-2-triggers
235+
'CREATE TRIGGER ' . $this->configuration->dbPrefix . 'items_insert_trigger
236+
BEFORE INSERT ON ' . $this->configuration->dbPrefix . 'items FOR EACH ROW
242237
BEGIN
243-
IF (
244-
OLD.unread <> NEW.unread OR
245-
OLD.starred <> NEW.starred
246-
) THEN
247-
SET NEW.updatetime = NOW();
248-
END IF;
238+
SET NEW.updatetime = NOW();
239+
SET NEW.lastseen = NOW();
249240
END;',
250-
'INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (11)',
251-
]);
252-
}
253-
if (strnatcmp($version, '12') < 0) {
254-
$this->logger->debug('Upgrading database schema to version 12');
255-
256-
$this->exec([
257-
'UPDATE ' . $this->configuration->dbPrefix . 'items SET updatetime = datetime WHERE updatetime IS NULL',
258-
'ALTER TABLE ' . $this->configuration->dbPrefix . 'items MODIFY updatetime DATETIME NOT NULL',
259-
'ALTER TABLE ' . $this->configuration->dbPrefix . 'items MODIFY lastseen DATETIME NOT NULL',
260-
'INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (12)',
261-
]);
262-
}
263-
if (strnatcmp($version, '13') < 0) {
264-
$this->logger->debug('Upgrading database schema to version 13');
241+
'CREATE TRIGGER ' . $this->configuration->dbPrefix . 'items_update_trigger
242+
BEFORE UPDATE ON ' . $this->configuration->dbPrefix . 'items FOR EACH ROW
243+
BEGIN
244+
IF (
245+
OLD.unread <> NEW.unread OR
246+
OLD.starred <> NEW.starred
247+
) THEN
248+
SET NEW.updatetime = NOW();
249+
END IF;
250+
END;',
251+
'INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (11)',
252+
]);
253+
}
254+
if (strnatcmp($version, '12') < 0) {
255+
$this->logger->debug('Upgrading database schema to version 12');
256+
257+
$this->exec([
258+
'UPDATE ' . $this->configuration->dbPrefix . 'items SET updatetime = datetime WHERE updatetime IS NULL',
259+
'ALTER TABLE ' . $this->configuration->dbPrefix . 'items MODIFY updatetime DATETIME NOT NULL',
260+
'ALTER TABLE ' . $this->configuration->dbPrefix . 'items MODIFY lastseen DATETIME NOT NULL',
261+
'INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (12)',
262+
]);
263+
}
264+
if (strnatcmp($version, '13') < 0) {
265+
$this->logger->debug('Upgrading database schema to version 13');
265266

266-
$this->exec([
267-
'UPDATE ' . $this->configuration->dbPrefix . "sources SET spout = 'spouts\\\\rss\\\\fulltextrss' WHERE spout = 'spouts\\\\rss\\\\instapaper'",
268-
'INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (13)',
269-
]);
270-
}
267+
$this->exec([
268+
'UPDATE ' . $this->configuration->dbPrefix . "sources SET spout = 'spouts\\\\rss\\\\fulltextrss' WHERE spout = 'spouts\\\\rss\\\\instapaper'",
269+
'INSERT INTO ' . $this->configuration->dbPrefix . 'version (version) VALUES (13)',
270+
]);
271271
}
272272
}
273273

0 commit comments

Comments
 (0)