mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
💡Reverted "empty-string to NULL" db migration (#10430)
no-issue - revert the migration because migrations that (potentially) touch every row should be kept to major releases where possible - the migration was safe to run and won't cause any problems for anyone who has already upgraded - reversion keeps the migration file but changes the contents to a no-op so that `migrations` table state is the same for all users whether they migrated with the full migration or the no-op version
This commit is contained in:
parent
9f9b58e232
commit
e9c3040786
1 changed files with 2 additions and 78 deletions
|
@ -1,81 +1,5 @@
|
|||
const Promise = require('bluebird');
|
||||
const common = require('../../../../lib/common');
|
||||
const schema = require('../../../schema');
|
||||
|
||||
/*
|
||||
* [{
|
||||
* tableName: 'posts',
|
||||
* columns: ['custom_excerpt', 'description', 'etc...']
|
||||
* }]
|
||||
* */
|
||||
const tablesToUpdate = Object.keys(schema.tables).reduce((tablesToUpdate, tableName) => {
|
||||
const table = schema.tables[tableName];
|
||||
const columns = Object.keys(table).filter((columnName) => {
|
||||
const column = table[columnName];
|
||||
return column.nullable && ['string', 'text'].includes(column.type);
|
||||
});
|
||||
if (!columns.length) {
|
||||
return tablesToUpdate;
|
||||
}
|
||||
return tablesToUpdate.concat({
|
||||
tableName,
|
||||
columns
|
||||
});
|
||||
}, []);
|
||||
module.exports.up = () => Promise.resolve();
|
||||
|
||||
const createReplace = (connection, from, to) => (tableName, columnName) => {
|
||||
return connection.schema.hasTable(tableName)
|
||||
.then((tableExists) => {
|
||||
if (!tableExists) {
|
||||
common.logging.warn(
|
||||
`Table ${tableName} does not exist`
|
||||
);
|
||||
return;
|
||||
}
|
||||
return connection.schema.hasColumn(tableName, columnName)
|
||||
.then((columnExists) => {
|
||||
if (!columnExists) {
|
||||
common.logging.warn(
|
||||
`Table '${tableName}' does not have column '${columnName}'`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
common.logging.info(
|
||||
`Updating ${tableName}, setting '${from}' in ${columnName} to '${to}'`
|
||||
);
|
||||
|
||||
return connection(tableName)
|
||||
.where(columnName, from)
|
||||
.update(columnName, to);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.up = ({transacting}) => {
|
||||
const replaceEmptyStringWithNull = createReplace(transacting, '', null);
|
||||
|
||||
return Promise.all(
|
||||
tablesToUpdate.map(({tableName, columns}) => Promise.all(
|
||||
columns.map(
|
||||
columnName => replaceEmptyStringWithNull(tableName, columnName)
|
||||
)
|
||||
))
|
||||
);
|
||||
};
|
||||
|
||||
module.exports.down = ({connection}) => {
|
||||
const replaceNullWithEmptyString = createReplace(connection, null, '');
|
||||
|
||||
return Promise.all(
|
||||
tablesToUpdate.map(({tableName, columns}) => Promise.all(
|
||||
columns.map(
|
||||
columnName => replaceNullWithEmptyString(tableName, columnName)
|
||||
)
|
||||
))
|
||||
);
|
||||
};
|
||||
|
||||
module.exports.config = {
|
||||
transaction: true
|
||||
};
|
||||
module.exports.down = () => Promise.resolve();
|
||||
|
|
Loading…
Reference in a new issue