diff --git a/core/server/data/migrations/versions/3.1/4-add-subscribed-flag-to-members.js b/core/server/data/migrations/versions/3.1/4-add-subscribed-flag-to-members.js new file mode 100644 index 0000000000..8588826898 --- /dev/null +++ b/core/server/data/migrations/versions/3.1/4-add-subscribed-flag-to-members.js @@ -0,0 +1,44 @@ +const common = require('../../../../lib/common'); +const commands = require('../../../schema').commands; + +const createLog = type => msg => common.logging[type](msg); + +function createColumnMigration({table, column, dbIsInCorrectState, operation, operationVerb}) { + return function columnMigrations({transacting}) { + return transacting.schema.hasColumn(table, column) + .then(dbIsInCorrectState) + .then((isInCorrectState) => { + const log = createLog(isInCorrectState ? 'warn' : 'info'); + + log(`${operationVerb} ${table}.${column}`); + + if (!isInCorrectState) { + return operation(table, column, transacting); + } + }); + }; +} + +module.exports.up = createColumnMigration({ + table: 'members', + column: 'subscribed', + dbIsInCorrectState(columnExists) { + return columnExists === true; + }, + operation: commands.addColumn, + operationVerb: 'Adding' +}); + +module.exports.down = createColumnMigration({ + table: 'members', + column: 'subscribed', + dbIsInCorrectState(columnExists) { + return columnExists === false; + }, + operation: commands.dropColumn, + operationVerb: 'Removing' +}); + +module.exports.config = { + transaction: true +}; diff --git a/core/server/data/schema/schema.js b/core/server/data/schema/schema.js index d9723bf742..b52cdfd399 100644 --- a/core/server/data/schema/schema.js +++ b/core/server/data/schema/schema.js @@ -326,6 +326,7 @@ module.exports = { email: {type: 'string', maxlength: 191, nullable: false, unique: true, validations: {isEmail: true}}, name: {type: 'string', maxlength: 191, nullable: true}, note: {type: 'string', maxlength: 2000, nullable: true}, + subscribed: {type: 'bool', nullable: true, defaultTo: true}, created_at: {type: 'dateTime', nullable: false}, created_by: {type: 'string', maxlength: 24, nullable: false}, updated_at: {type: 'dateTime', nullable: true}, diff --git a/core/test/unit/data/schema/integrity_spec.js b/core/test/unit/data/schema/integrity_spec.js index 7057a30a7c..fffe265135 100644 --- a/core/test/unit/data/schema/integrity_spec.js +++ b/core/test/unit/data/schema/integrity_spec.js @@ -19,7 +19,7 @@ var should = require('should'), */ describe('DB version integrity', function () { // Only these variables should need updating - const currentSchemaHash = 'b76727048bd7851cb9fbfbefc9b00745'; + const currentSchemaHash = 'd7af2f707b0221871d27d9387e0de2e2'; const currentFixturesHash = '84005b6e8f62231b6fd0fc261ce893db'; // If this test is failing, then it is likely a change has been made that requires a DB version bump,