Skip to content

Commit

Permalink
Fix inconsistencies in database migrations
Browse files Browse the repository at this point in the history
In 14442c0, `updatetime` field was
introduced to the `items` table. Unfortunately, unlike the
initialization, the migration did not flag the field `NOT NULL`.
Additionally, the `lastseen` column in MySQL database was added as
`NULL`able in 83794f4. These
inconsistencies can lead to incorrect assumptions and cause bugs like #954.

This patch adds the `NOT NULL` constraint for `updatetime` and `lastseen`
fields, making the database consistent again. SQLite consistency was
already rectified in 83794f4 by
re-creating the table.
  • Loading branch information
jtojnar committed Jun 23, 2017
1 parent 3a7bcc1 commit 337e121
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions daos/mysql/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ public function __construct() {
'INSERT INTO ' . \F3::get('db_prefix') . 'version (version) VALUES (11)'
]);
}
if (strnatcmp($version, '12') < 0) {
\F3::get('db')->exec([
'UPDATE ' . \F3::get('db_prefix') . 'items SET updatetime = datetime WHERE updatetime IS NULL',
'ALTER TABLE ' . \F3::get('db_prefix') . 'items MODIFY updatetime DATETIME NOT NULL',
'ALTER TABLE ' . \F3::get('db_prefix') . 'items MODIFY lastseen DATETIME NOT NULL',
'INSERT INTO ' . \F3::get('db_prefix') . 'version (version) VALUES (12)'
]);
}
}

// just initialize once
Expand Down
7 changes: 7 additions & 0 deletions daos/pgsql/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ public function __construct() {
'INSERT INTO version (version) VALUES (11);'
]);
}
if (strnatcmp($version, '12') < 0) {
\F3::get('db')->exec([
'UPDATE items SET updatetime = datetime WHERE updatetime IS NULL',
'ALTER TABLE items ALTER COLUMN updatetime SET NOT NULL',
'INSERT INTO version (version) VALUES (12)'
]);
}
}

// just initialize once
Expand Down

0 comments on commit 337e121

Please sign in to comment.