Fix for #665 and additional fix for #663.

If your segemnts are broken or Mailtrain complains about missing 20190726150000_shorten_field_column_names.js, run the following in `server/setup/knex/fixes`:

```NODE_ENV=production node fix-20190726150000_shorten_field_column_names.js```
This commit is contained in:
Tomas Bures 2019-08-12 09:26:49 +02:00
parent bb237b3da4
commit ae5faadffa
3 changed files with 72 additions and 2 deletions

View file

@ -0,0 +1,69 @@
'use strict';
const config = require('../config');
const knex = require('../../../lib/knex');
const shortid = require('shortid');
const slugify = require('slugify');
async function run() {
const lists = await knex('lists');
for (const list of lists) {
console.log(`Processing list ${list.id}`);
const fields = await knex('custom_fields').whereNotNull('column').where('list', list.id);
const fieldsMap = new Map();
const prefixesMap = new Map();
for (const field of fields) {
const oldName = field.column;
const newName = ('custom_' + slugify(field.name, '_').substring(0, 32) + '_' + shortid.generate()).toLowerCase().replace(/[^a-z0-9_]/g, '_');
const formerPrefix = ('custom_' + slugify(field.name, '_') + '_').toLowerCase().replace(/[^a-z0-9_]/g, '');
fieldsMap.set(oldName, newName);
prefixesMap.set(formerPrefix, newName);
await knex('custom_fields').where('id', field.id).update('column', newName);
await knex.schema.table('subscription__' + list.id, table => {
table.renameColumn(oldName, newName);
table.renameColumn('source_' + oldName, 'source_' + newName);
});
}
function processRule(rule) {
if (rule.type === 'all' || rule.type === 'some' || rule.type === 'none') {
for (const childRule of rule.rules) {
processRule(childRule);
}
} else {
let newName = fieldsMap.get(rule.column);
if (newName) {
rule.column = newName;
return;
}
for (const [formerPrefix, newName] of prefixesMap.entries()) {
if (rule.column.startsWith(formerPrefix)) {
rule.column = newName;
return;
}
}
}
}
const segments = await knex('segments').where('list', list.id);
for (const segment of segments) {
const settings = JSON.parse(segment.settings);
processRule(settings.rootRule);
await knex('segments').where('id', segment.id).update({settings: JSON.stringify(settings)});
}
}
await knex('knex_migrations').where('name', '20190726150000_shorten_field_column_names.js').del();
console.log('All fixes done');
process.exit();
}
run().catch(err => console.error(err));