Fix for #663.
Unfortunately, the migration 20190726150000_shorten_field_column_names.js corrupted the segments table. There is no automatic fix. If this affected you, you have to either revert the DB or fix the segments manually.
This commit is contained in:
parent
30e03adf0c
commit
69ce80ebfd
1 changed files with 44 additions and 3 deletions
|
@ -11,6 +11,8 @@ const { EntityVals: TriggerEntityVals, EventVals: TriggerEventVals } = require('
|
||||||
const { SubscriptionSource } = require('../../../../shared/lists');
|
const { SubscriptionSource } = require('../../../../shared/lists');
|
||||||
const {DOMParser, XMLSerializer} = require('xmldom');
|
const {DOMParser, XMLSerializer} = require('xmldom');
|
||||||
const log = require('../../../lib/log');
|
const log = require('../../../lib/log');
|
||||||
|
const shortid = require('shortid');
|
||||||
|
const slugify = require('slugify');
|
||||||
|
|
||||||
const entityTypesAddNamespace = ['list', 'custom_form', 'template', 'campaign', 'report', 'report_template', 'user'];
|
const entityTypesAddNamespace = ['list', 'custom_form', 'template', 'campaign', 'report', 'report_template', 'user'];
|
||||||
const shareableEntityTypes = ['list', 'custom_form', 'template', 'campaign', 'report', 'report_template', 'namespace', 'send_configuration', 'mosaico_template'];
|
const shareableEntityTypes = ['list', 'custom_form', 'template', 'campaign', 'report', 'report_template', 'namespace', 'send_configuration', 'mosaico_template'];
|
||||||
|
@ -236,16 +238,54 @@ async function migrateUsers(knex) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function shortenFieldColumnNames(knex, list) {
|
||||||
|
const fields = await knex('custom_fields').whereNotNull('column').where('list', list.id);
|
||||||
|
|
||||||
|
const fieldsMap = 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, '_');
|
||||||
|
|
||||||
|
fieldsMap.set(oldName, newName);
|
||||||
|
|
||||||
|
await knex('custom_fields').where('id', field.id).update('column', newName);
|
||||||
|
|
||||||
|
await knex.schema.table('subscription__' + list.id, table => {
|
||||||
|
table.renameColumn(oldName, newName);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function processRule(rule) {
|
||||||
|
if (rule.type === 'all' || rule.type === 'some' || rule.type === 'none') {
|
||||||
|
for (const childRule of rule.rules) {
|
||||||
|
processRule(childRule);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
rule.column = fieldsMap.get(rule.column) || rule.column /* this is to handle "email" column */;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function migrateSubscriptions(knex) {
|
async function migrateSubscriptions(knex) {
|
||||||
await knex.schema.dropTableIfExists('subscription');
|
await knex.schema.dropTableIfExists('subscription');
|
||||||
|
|
||||||
const lists = await knex('lists');
|
const lists = await knex('lists');
|
||||||
for (const list of lists) {
|
for (const list of lists) {
|
||||||
|
await shortenFieldColumnNames(knex, list);
|
||||||
|
|
||||||
await knex.schema.raw('ALTER TABLE `subscription__' + list.id + '` ADD `unsubscribed` timestamp NULL DEFAULT NULL');
|
await knex.schema.raw('ALTER TABLE `subscription__' + list.id + '` ADD `unsubscribed` timestamp NULL DEFAULT NULL');
|
||||||
await knex.schema.raw('ALTER TABLE `subscription__' + list.id + '` ADD `source_email` int(11) DEFAULT NULL');
|
await knex.schema.raw('ALTER TABLE `subscription__' + list.id + '` ADD `source_email` int(11) DEFAULT NULL');
|
||||||
await knex.schema.raw('ALTER TABLE `subscription__' + list.id + '` ADD `hash_email` varchar(255) CHARACTER SET ascii');
|
await knex.schema.raw('ALTER TABLE `subscription__' + list.id + '` ADD `hash_email` varchar(255) CHARACTER SET ascii');
|
||||||
|
|
||||||
|
|
||||||
const fields = await knex('custom_fields').where('list', list.id);
|
const fields = await knex('custom_fields').where('list', list.id);
|
||||||
const info = await knex('subscription__' + list.id).columnInfo();
|
const info = await knex('subscription__' + list.id).columnInfo();
|
||||||
for (const field of fields) {
|
for (const field of fields) {
|
||||||
|
@ -1251,11 +1291,12 @@ exports.up = (knex, Promise) => (async() => {
|
||||||
await migrateCustomFields(knex);
|
await migrateCustomFields(knex);
|
||||||
log.verbose('Migration', 'Custom fields complete')
|
log.verbose('Migration', 'Custom fields complete')
|
||||||
|
|
||||||
await migrateSubscriptions(knex);
|
|
||||||
|
|
||||||
await migrateSegments(knex);
|
await migrateSegments(knex);
|
||||||
log.verbose('Migration', 'Segments complete')
|
log.verbose('Migration', 'Segments complete')
|
||||||
|
|
||||||
|
await migrateSubscriptions(knex);
|
||||||
|
log.verbose('Migration', 'Subscriptions complete')
|
||||||
|
|
||||||
await migrateReports(knex);
|
await migrateReports(knex);
|
||||||
log.verbose('Migration', 'Reports complete')
|
log.verbose('Migration', 'Reports complete')
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue