diff --git a/migrations/20190206000000-notifications-drop-eventid-constraint.js b/migrations/20190206000000-notifications-drop-eventid-constraint.js new file mode 100644 index 000000000..288405c4a --- /dev/null +++ b/migrations/20190206000000-notifications-drop-eventid-constraint.js @@ -0,0 +1,22 @@ +'use strict'; + +var async = require('async'); + +exports.up = function(db, callback) { + async.series([ + db.runSql.bind(db, 'START TRANSACTION;'), + // WARNING in the future always give constraints proper names to not rely on automatic ones + db.runSql.bind(db, 'ALTER TABLE notifications DROP FOREIGN KEY notifications_ibfk_1'), + db.runSql.bind(db, 'ALTER TABLE notifications MODIFY eventId VARCHAR(128)'), + db.runSql.bind(db, 'COMMIT') + ], callback); +}; + +exports.down = function(db, callback) { + async.series([ + db.runSql.bind(db, 'START TRANSACTION;'), + db.runSql.bind(db, 'ALTER TABLE notifications MODIFY eventId VARCHAR(128) NOT NULL'), + db.runSql.bind(db, 'ALTER TABLE notifications ADD FOREIGN KEY(eventId) REFERENCES eventlog(id)'), + db.runSql.bind(db, 'COMMIT') + ], callback); +}; diff --git a/migrations/schema.sql b/migrations/schema.sql index 4620e89b1..c18a94ceb 100644 --- a/migrations/schema.sql +++ b/migrations/schema.sql @@ -215,14 +215,13 @@ CREATE TABLE IF NOT EXISTS tasks( CREATE TABLE IF NOT EXISTS notifications( id int NOT NULL AUTO_INCREMENT, userId VARCHAR(128) NOT NULL, - eventId VARCHAR(128) NOT NULL, + eventId VARCHAR(128), title VARCHAR(512) NOT NULL, message TEXT, action VARCHAR(512) NOT NULL, acknowledged BOOLEAN DEFAULT false, creationTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (eventId) REFERENCES eventlog(id), PRIMARY KEY (id) ); diff --git a/src/test/database-test.js b/src/test/database-test.js index 0717590aa..9455c6715 100644 --- a/src/test/database-test.js +++ b/src/test/database-test.js @@ -163,6 +163,14 @@ describe('database', function () { action: 'usually a url' }; + var NOTIFICATION_3 = { + userId: USER_0.id, + eventId: null, + title: 'third one', + message: 'some message there', + action: 'usually a url' + }; + before(function (done) { async.series([ userdb.add.bind(null, USER_0.id, USER_0), @@ -286,6 +294,14 @@ describe('database', function () { done(); }); }); + + it('can add notification without eventId', function (done) { + notificationdb.add(NOTIFICATION_3, function (error) { + expect(error).to.equal(null); + + done(); + }); + }); }); describe('domains', function () {