Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 => table[columnName].nullable);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this a bit more explicit, maybe the type fo string or text should be checked here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking this too - though we only update if the existing value is '' so we shouldn't pick up any fields like that. Maybe it's better to be explicit tho 👍

if (!columns.length) {
return tablesToUpdate;
}
return tablesToUpdate.concat({
tableName,
columns
});
}, []);

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 = ({connection}) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const replaceEmptyStringWithNull = createReplace(connection, '', 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, '');
Copy link
Contributor Author

@allouis allouis Jan 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kirrg001 I found something interesting, which is that setting the {transaction: true} config on the exports, passed a transacting object to up, but still a connection object to down

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool 👍


return Promise.all(
tablesToUpdate.map(({tableName, columns}) => Promise.all(
columns.map(
columnName => replaceNullWithEmptyString(tableName, columnName)
)
))
);
};