Add visibility column to posts, tags and users

refs #6301, #6165

- visibility is added as a new column on posts, tags and users.
- has a relevant default value for each table
This commit is contained in:
Hannah Wolfe 2016-03-25 20:45:40 +00:00
parent b4ae469c12
commit e7cc18d5fb
4 changed files with 164 additions and 6 deletions

View File

@ -0,0 +1,27 @@
var Promise = require('bluebird'),
commands = require('../../schema').commands,
db = require('../../db'),
tables = ['posts', 'tags', 'users'],
column = 'visibility';
module.exports = function addVisibilityColumnToKeyTables(logger) {
return Promise.mapSeries(tables, function (table) {
var message = 'Adding column: ' + table + '.' + column;
return db.knex.schema.hasTable(table).then(function (exists) {
if (exists) {
return db.knex.schema.hasColumn(table, column).then(function (exists) {
if (!exists) {
logger.info(message);
return commands.addColumn(table, column);
} else {
logger.warn(message);
}
});
} else {
// @TODO: this should probably be an error
logger.warn(message);
}
});
});
};

View File

@ -1,4 +1,6 @@
module.exports = [
// Drop hidden column from tags table
require('./01-drop-hidden-column-from-tags')
require('./01-drop-hidden-column-from-tags'),
// Add visibility column to posts, tags, and users tables
require('./02-add-visibility-column-to-key-tables')
];

View File

@ -11,6 +11,7 @@ module.exports = {
page: {type: 'bool', nullable: false, defaultTo: false, validations: {isIn: [[0, 1, false, true]]}},
status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'draft'},
language: {type: 'string', maxlength: 6, nullable: false, defaultTo: 'en_US'},
visibility: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'public', validations: {isIn: [['public']]}},
meta_title: {type: 'string', maxlength: 150, nullable: true},
meta_description: {type: 'string', maxlength: 200, nullable: true},
author_id: {type: 'integer', nullable: false},
@ -36,6 +37,7 @@ module.exports = {
accessibility: {type: 'text', maxlength: 65535, nullable: true},
status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'active'},
language: {type: 'string', maxlength: 6, nullable: false, defaultTo: 'en_US'},
visibility: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'public', validations: {isIn: [['public']]}},
meta_title: {type: 'string', maxlength: 150, nullable: true},
meta_description: {type: 'string', maxlength: 200, nullable: true},
tour: {type: 'text', maxlength: 65535, nullable: true},
@ -106,6 +108,7 @@ module.exports = {
description: {type: 'string', maxlength: 200, nullable: true},
image: {type: 'text', maxlength: 2000, nullable: true},
parent_id: {type: 'integer', nullable: true},
visibility: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'public', validations: {isIn: [['public', 'internal']]}},
meta_title: {type: 'string', maxlength: 150, nullable: true},
meta_description: {type: 'string', maxlength: 200, nullable: true},
created_at: {type: 'dateTime', nullable: false},

View File

@ -32,7 +32,7 @@ var should = require('should'),
describe('DB version integrity', function () {
// Only these variables should need updating
var currentDbVersion = '005',
currentSchemaHash = 'cc249220eb57b2249fc82f8494ad3912',
currentSchemaHash = '2b823f290d2ffa02ad5a10e31b77dab4',
currentFixturesHash = '77ebb081539f9e0c49f487faf7fd929e';
// If this test is failing, then it is likely a change has been made that requires a DB version bump,
@ -798,7 +798,7 @@ describe('Migrations', function () {
tasksSpy.calledOnce.should.be.true();
tasksSpy.calledWith('005', loggerStub).should.be.true();
tasksSpy.firstCall.returnValue.should.be.an.Array().with.lengthOf(1);
tasksSpy.firstCall.returnValue.should.be.an.Array().with.lengthOf(2);
sequenceStub.calledTwice.should.be.true();
@ -807,8 +807,9 @@ describe('Migrations', function () {
sequenceStub.firstCall.args[0][0].should.be.a.Function().with.property('name', 'runVersionTasks');
sequenceStub.secondCall.calledWith(sinon.match.array, loggerStub).should.be.true();
sequenceStub.secondCall.args[0].should.be.an.Array().with.lengthOf(1);
sequenceStub.secondCall.args[0].should.be.an.Array().with.lengthOf(2);
sequenceStub.secondCall.args[0][0].should.be.a.Function().with.property('name', 'dropHiddenColumnFromTags');
sequenceStub.secondCall.args[0][1].should.be.a.Function().with.property('name', 'addVisibilityColumnToKeyTables');
// Reset sequence
sequenceReset();
@ -817,7 +818,7 @@ describe('Migrations', function () {
});
describe('Tasks:', function () {
var dropColumnStub,
var dropColumnStub, addColumnStub,
knexStub, knexMock;
beforeEach(function () {
@ -830,6 +831,7 @@ describe('Migrations', function () {
knexStub = sinon.stub(db, 'knex', {get: function () { return knexMock; }});
dropColumnStub = sandbox.stub(schema.commands, 'dropColumn');
addColumnStub = sandbox.stub(schema.commands, 'addColumn');
});
afterEach(function () {
@ -838,7 +840,7 @@ describe('Migrations', function () {
it('should have tasks for 005', function () {
should.exist(updates005);
updates005.should.be.an.Array().with.lengthOf(1);
updates005.should.be.an.Array().with.lengthOf(2);
});
describe('01-drop-hidden-column-from-tags', function () {
@ -907,6 +909,130 @@ describe('Migrations', function () {
}).catch(done);
});
});
describe('02-add-visibility-column-to-key-tables', function () {
it('does not try to add new column if the table does not exist', function (done) {
// Setup
knexMock.schema.hasTable.withArgs('posts').returns(Promise.resolve(false));
knexMock.schema.hasTable.withArgs('tags').returns(Promise.resolve(false));
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(false));
// Execute
updates005[1](loggerStub).then(function () {
knexMock.schema.hasTable.calledThrice.should.be.true();
knexMock.schema.hasTable.calledWith('posts').should.be.true();
knexMock.schema.hasTable.calledWith('tags').should.be.true();
knexMock.schema.hasTable.calledWith('users').should.be.true();
knexMock.schema.hasColumn.called.should.be.false();
addColumnStub.called.should.be.false();
loggerStub.info.called.should.be.false();
loggerStub.warn.calledThrice.should.be.true();
done();
}).catch(done);
});
it('does not try to add new columns if the columns already exist', function (done) {
// Setup
knexMock.schema.hasTable.withArgs('posts').returns(Promise.resolve(true));
knexMock.schema.hasTable.withArgs('tags').returns(Promise.resolve(true));
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(true));
knexMock.schema.hasColumn.withArgs('posts', 'visibility').returns(Promise.resolve(true));
knexMock.schema.hasColumn.withArgs('tags', 'visibility').returns(Promise.resolve(true));
knexMock.schema.hasColumn.withArgs('users', 'visibility').returns(Promise.resolve(true));
// Execute
updates005[1](loggerStub).then(function () {
knexMock.schema.hasTable.calledThrice.should.be.true();
knexMock.schema.hasTable.calledWith('posts').should.be.true();
knexMock.schema.hasTable.calledWith('tags').should.be.true();
knexMock.schema.hasTable.calledWith('users').should.be.true();
knexMock.schema.hasColumn.calledThrice.should.be.true();
knexMock.schema.hasColumn.calledWith('posts', 'visibility').should.be.true();
knexMock.schema.hasColumn.calledWith('tags', 'visibility').should.be.true();
knexMock.schema.hasColumn.calledWith('users', 'visibility').should.be.true();
addColumnStub.called.should.be.false();
loggerStub.info.called.should.be.false();
loggerStub.warn.calledThrice.should.be.true();
done();
}).catch(done);
});
it('tries to add new columns if table is present but columns are not', function (done) {
// Setup
knexMock.schema.hasTable.withArgs('posts').returns(Promise.resolve(true));
knexMock.schema.hasTable.withArgs('tags').returns(Promise.resolve(true));
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(true));
knexMock.schema.hasColumn.withArgs('posts', 'visibility').returns(Promise.resolve(false));
knexMock.schema.hasColumn.withArgs('tags', 'visibility').returns(Promise.resolve(false));
knexMock.schema.hasColumn.withArgs('users', 'visibility').returns(Promise.resolve(false));
// Execute
updates005[1](loggerStub).then(function () {
knexMock.schema.hasTable.calledThrice.should.be.true();
knexMock.schema.hasTable.calledWith('posts').should.be.true();
knexMock.schema.hasTable.calledWith('tags').should.be.true();
knexMock.schema.hasTable.calledWith('users').should.be.true();
knexMock.schema.hasColumn.calledThrice.should.be.true();
knexMock.schema.hasColumn.calledWith('posts', 'visibility').should.be.true();
knexMock.schema.hasColumn.calledWith('tags', 'visibility').should.be.true();
knexMock.schema.hasColumn.calledWith('users', 'visibility').should.be.true();
addColumnStub.calledThrice.should.be.true();
addColumnStub.calledWith('posts', 'visibility').should.be.true();
addColumnStub.calledWith('tags', 'visibility').should.be.true();
addColumnStub.calledWith('users', 'visibility').should.be.true();
loggerStub.info.calledThrice.should.be.true();
loggerStub.warn.called.should.be.false();
done();
}).catch(done);
});
it('will only try to add columns that do not exist', function (done) {
// Setup
knexMock.schema.hasTable.withArgs('posts').returns(Promise.resolve(true));
knexMock.schema.hasTable.withArgs('tags').returns(Promise.resolve(true));
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(true));
knexMock.schema.hasColumn.withArgs('posts', 'visibility').returns(Promise.resolve(false));
knexMock.schema.hasColumn.withArgs('tags', 'visibility').returns(Promise.resolve(true));
knexMock.schema.hasColumn.withArgs('users', 'visibility').returns(Promise.resolve(false));
// Execute
updates005[1](loggerStub).then(function () {
knexMock.schema.hasTable.calledThrice.should.be.true();
knexMock.schema.hasTable.calledWith('posts').should.be.true();
knexMock.schema.hasTable.calledWith('tags').should.be.true();
knexMock.schema.hasTable.calledWith('users').should.be.true();
knexMock.schema.hasColumn.calledThrice.should.be.true();
knexMock.schema.hasColumn.calledWith('posts', 'visibility').should.be.true();
knexMock.schema.hasColumn.calledWith('tags', 'visibility').should.be.true();
knexMock.schema.hasColumn.calledWith('users', 'visibility').should.be.true();
addColumnStub.calledTwice.should.be.true();
addColumnStub.calledWith('posts', 'visibility').should.be.true();
addColumnStub.calledWith('tags', 'visibility').should.be.false();
addColumnStub.calledWith('users', 'visibility').should.be.true();
loggerStub.info.calledTwice.should.be.true();
loggerStub.warn.calledOnce.should.be.true();
done();
}).catch(done);
});
});
});
});
});