diff --git a/migrations/20160430062446-eventlog-add-table.js b/migrations/20160430062446-eventlog-add-table.js index 02af1ecb7..0ad085a6f 100644 --- a/migrations/20160430062446-eventlog-add-table.js +++ b/migrations/20160430062446-eventlog-add-table.js @@ -4,6 +4,7 @@ var type = dbm.dataType; exports.up = function(db, callback) { var cmd = "CREATE TABLE eventlog(" + "id VARCHAR(128) NOT NULL," + + "source JSON," + "creationTime TIMESTAMP," + "action VARCHAR(128) NOT NULL," + "data JSON," + diff --git a/migrations/schema.sql b/migrations/schema.sql index 73e9fbda2..fb633a9bf 100644 --- a/migrations/schema.sql +++ b/migrations/schema.sql @@ -111,6 +111,8 @@ CREATE TABLE IF NOT EXISTS backups( CREATE TABLE IF NOT EXISTS eventlog( id VARCHAR(128) NOT NULL, + username VARCHAR(254) NOT NULL, + ip VARCHAR(32) NOT NULL, creationTime TIMESTAMP, action VARCHAR(128) NOT NULL, dataJson TEXT, diff --git a/src/eventlog.js b/src/eventlog.js index 10c3752e3..f5b534f13 100644 --- a/src/eventlog.js +++ b/src/eventlog.js @@ -50,14 +50,15 @@ function EventLogError(reason, errorOrMessage) { util.inherits(EventLogError, Error); EventLogError.INTERNAL_ERROR = 'Internal error'; -function add(action, data, callback) { +function add(action, source, data, callback) { assert.strictEqual(typeof action, 'string'); + assert.strictEqual(typeof source, 'object'); assert.strictEqual(typeof data, 'object'); assert(!callback || typeof callback === 'function'); callback = callback || NOOP_CALLBACK; - eventlogdb.add(uuid.v4(), action, data, function (error) { + eventlogdb.add(uuid.v4(), action, source, data, function (error) { if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error)); callback(); diff --git a/src/eventlogdb.js b/src/eventlogdb.js index 8842de76d..8fd3eab1f 100644 --- a/src/eventlogdb.js +++ b/src/eventlogdb.js @@ -14,10 +14,11 @@ var assert = require('assert'), DatabaseError = require('./databaseerror'), safe = require('safetydance'); -var EVENTLOGS_FIELDS = [ 'id', 'action', 'data', 'creationTime' ].join(','); +var EVENTLOGS_FIELDS = [ 'id', 'action', 'source', 'data', 'creationTime' ].join(','); // until mysql module supports automatic type coercion function postProcess(eventLog) { + eventLog.source = safe.JSON.parse(eventLog.source); eventLog.data = safe.JSON.parse(eventLog.data); return eventLog; } @@ -48,13 +49,14 @@ function getAllPaged(page, perPage, callback) { }); } -function add(id, action, data, callback) { +function add(id, action, source, data, callback) { assert.strictEqual(typeof id, 'string'); assert.strictEqual(typeof action, 'string'); + assert.strictEqual(typeof source, 'object'); assert.strictEqual(typeof data, 'object'); assert.strictEqual(typeof callback, 'function'); - database.query('INSERT INTO eventlog (id, action, data) VALUES (?, ?, ?)', [ id, action, JSON.stringify(data) ], function (error, result) { + database.query('INSERT INTO eventlog (id, action, source, data) VALUES (?, ?, ?, ?)', [ id, action, JSON.stringify(source), JSON.stringify(data) ], function (error, result) { if (error && error.code === 'ER_DUP_ENTRY') return callback(new DatabaseError(DatabaseError.ALREADY_EXISTS, error)); if (error || result.affectedRows !== 1) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error)); diff --git a/src/test/database-test.js b/src/test/database-test.js index b8b4b97f0..1769450f7 100644 --- a/src/test/database-test.js +++ b/src/test/database-test.js @@ -1073,10 +1073,10 @@ describe('database', function () { }); - describe('evenlog', function () { + describe('eventlog', function () { it('add succeeds', function (done) { - eventlogdb.add('someid', 'some.event', { appId: 'thatapp' }, function (error) { + eventlogdb.add('someid', 'some.event', { ip: '1.2.3.4' }, { appId: 'thatapp' }, function (error) { expect(error).to.be(null); done(); }); @@ -1089,6 +1089,7 @@ describe('database', function () { expect(result.action).to.be('some.event'); expect(result.creationTime).to.be.a(Date); + expect(result.source).to.be.eql({ ip: '1.2.3.4' }); expect(result.data).to.be.eql({ appId: 'thatapp' }); done(); @@ -1112,6 +1113,7 @@ describe('database', function () { expect(results[0].id).to.be('someid'); expect(results[0].action).to.be('some.event'); + expect(results[0].source).to.be.eql({ ip: '1.2.3.4' }); expect(results[0].data).to.be.eql({ appId: 'thatapp' }); done();