From 1d17f2aa91dadf1d694656d2c589198787157e4d Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 27 Sep 2018 14:51:46 +0700 Subject: [PATCH] Created session table migration (#9908) refs #9865 - This table will be used for storing user sessions in --- .../versions/2.2/1-add-sessions-table.js | 35 +++++++++++++++++++ core/server/data/schema/schema.js | 8 +++++ core/test/functional/routes/api/db_spec.js | 4 +-- core/test/unit/data/schema/integrity_spec.js | 4 +-- 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 core/server/data/migrations/versions/2.2/1-add-sessions-table.js diff --git a/core/server/data/migrations/versions/2.2/1-add-sessions-table.js b/core/server/data/migrations/versions/2.2/1-add-sessions-table.js new file mode 100644 index 000000000000..6814a4fe5258 --- /dev/null +++ b/core/server/data/migrations/versions/2.2/1-add-sessions-table.js @@ -0,0 +1,35 @@ +const common = require('../../../../lib/common'); +const commands = require('../../../schema').commands; +const table = 'sessions'; +const message1 = 'Adding table: ' + table; +const message2 = 'Dropping table: ' + table; + +module.exports.up = (options) => { + const connection = options.connection; + + return connection.schema.hasTable(table) + .then(function (exists) { + if (exists) { + common.logging.warn(message1); + return; + } + + common.logging.info(message1); + return commands.createTable(table, connection); + }); +}; + +module.exports.down = (options) => { + const connection = options.connection; + + return connection.schema.hasTable(table) + .then(function (exists) { + if (!exists) { + common.logging.warn(message2); + return; + } + + common.logging.info(message2); + return commands.deleteTable(table, connection); + }); +}; diff --git a/core/server/data/schema/schema.js b/core/server/data/schema/schema.js index 61b013326888..7d214c3d703a 100644 --- a/core/server/data/schema/schema.js +++ b/core/server/data/schema/schema.js @@ -300,5 +300,13 @@ module.exports = { created_by: {type: 'string', maxlength: 24, nullable: false}, updated_at: {type: 'dateTime', nullable: true}, updated_by: {type: 'string', maxlength: 24, nullable: true} + }, + sessions: { + id: {type: 'string', maxlength: 24, nullable: false, primary: true}, + session_id: {type: 'string', maxlength: 32, nullable: false, unique: true}, + user_id: {type: 'string', maxlength: 24, nullable: false}, + session_data: {type: 'string', maxlength: 2000, nullable: false}, + created_at: {type: 'dateTime', nullable: false}, + updated_at: {type: 'dateTime', nullable: true} } }; diff --git a/core/test/functional/routes/api/db_spec.js b/core/test/functional/routes/api/db_spec.js index 8798b1537ad8..c1d6523da664 100644 --- a/core/test/functional/routes/api/db_spec.js +++ b/core/test/functional/routes/api/db_spec.js @@ -72,7 +72,7 @@ describe('DB API', function () { var jsonResponse = res.body; should.exist(jsonResponse.db); jsonResponse.db.should.have.length(1); - Object.keys(jsonResponse.db[0].data).length.should.eql(21); + Object.keys(jsonResponse.db[0].data).length.should.eql(22); done(); }); }); @@ -90,7 +90,7 @@ describe('DB API', function () { const jsonResponse = res.body; should.exist(jsonResponse.db); jsonResponse.db.should.have.length(1); - Object.keys(jsonResponse.db[0].data).length.should.eql(23); + Object.keys(jsonResponse.db[0].data).length.should.eql(24); done(); }); }); diff --git a/core/test/unit/data/schema/integrity_spec.js b/core/test/unit/data/schema/integrity_spec.js index 625cfabb1a77..a43a2c4d82b1 100644 --- a/core/test/unit/data/schema/integrity_spec.js +++ b/core/test/unit/data/schema/integrity_spec.js @@ -19,8 +19,8 @@ var should = require('should'), */ describe('DB version integrity', function () { // Only these variables should need updating - var currentSchemaHash = '22d24b1de23d118b90e9547fefae5ad7', - currentFixturesHash = 'eab42b1e9cd754e76600f1c57c4a7af8'; + const currentSchemaHash = 'be8d6ff382ae07b238c60d5b453a2944'; + const currentFixturesHash = 'eab42b1e9cd754e76600f1c57c4a7af8'; // If this test is failing, then it is likely a change has been made that requires a DB version bump, // and the values above will need updating as confirmation