0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Adding drop-column migration handling

refs #6301, #6165

- Adds detection and processing for column deletions
This commit is contained in:
Hannah Wolfe 2016-02-07 16:23:24 +00:00
parent 2fba311605
commit 4ba0a745df
5 changed files with 29 additions and 1 deletions

View file

@ -11,6 +11,7 @@ var _ = require('lodash'),
getDeleteCommands,
getAddCommands,
addColumnCommands,
dropColumnCommands,
modifyUniqueCommands;
logInfo = function logInfo(message) {
@ -26,6 +27,7 @@ getDeleteCommands = function getDeleteCommands(oldTables, newTables) {
};
});
};
getAddCommands = function getAddCommands(oldTables, newTables) {
var addTables = _.difference(newTables, oldTables);
return _.map(addTables, function (table) {
@ -35,6 +37,7 @@ getAddCommands = function getAddCommands(oldTables, newTables) {
};
});
};
addColumnCommands = function addColumnCommands(table, columns) {
var columnKeys = _.keys(schema[table]),
addColumns = _.difference(columnKeys, columns);
@ -46,6 +49,19 @@ addColumnCommands = function addColumnCommands(table, columns) {
};
});
};
dropColumnCommands = function dropColumnCommands(table, columns) {
var columnKeys = _.keys(schema[table]),
dropColumns = _.difference(columns, columnKeys);
return _.map(dropColumns, function (column) {
return function () {
logInfo(i18n.t('notices.data.migration.commands.droppingColumn', {table: table, column: column}));
return commands.dropColumn(table, column);
};
});
};
modifyUniqueCommands = function modifyUniqueCommands(table, indexes) {
var columnKeys = _.keys(schema[table]);
return _.map(columnKeys, function (column) {
@ -71,5 +87,6 @@ module.exports = {
getDeleteCommands: getDeleteCommands,
getAddCommands: getAddCommands,
addColumnCommands: addColumnCommands,
dropColumnCommands: dropColumnCommands,
modifyUniqueCommands: modifyUniqueCommands
};

View file

@ -182,6 +182,7 @@ migrateUp = function (fromVersion, toVersion) {
return Promise.all(
_.map(oldTables, function (table) {
return commands.getColumns(table).then(function (columns) {
migrateOps = migrateOps.concat(builder.dropColumnCommands(table, columns));
migrateOps = migrateOps.concat(builder.addColumnCommands(table, columns));
});
})

View file

@ -50,6 +50,14 @@ function addColumn(table, column) {
});
}
function dropColumn(table, column) {
dbConfig = dbConfig || config.database;
return dbConfig.knex.schema.table(table, function (table) {
table.dropColumn(column);
});
}
function addUnique(table, column) {
dbConfig = dbConfig || config.database;
return dbConfig.knex.schema.table(table, function (table) {
@ -130,5 +138,6 @@ module.exports = {
addUnique: addUnique,
dropUnique: dropUnique,
addColumn: addColumn,
dropColumn: dropColumn,
getColumns: getColumns
};

View file

@ -553,6 +553,7 @@
"deletingTable": "Deleting table: {table}",
"creatingTable": "Creating table: {table}",
"addingColumn": "Adding column: {table}.{column}",
"droppingColumn": "Dropping column: {table}.{column}",
"addingUnique": "Adding unique on: {table}.{column}",
"droppingUnique": "Dropping unique on: {table}.{column}"
},

View file

@ -13,10 +13,10 @@ var should = require('should'),
should.equal(true, true);
describe('Migrations', function () {
// Check version integrity
// These tests exist to ensure that developers are not able to modify the database schema, or permissions fixtures
// without knowing that they also need to update the default database version,
// both of which are required for migrations to work properly.
describe('DB version integrity', function () {
// Only these variables should need updating
var currentDbVersion = '004',