🏗 Migrated subscribers data to members (#11152)

no issue 

- Populates members table with existing subscribers. Only takes into account columns we know already exist and need to be copied i.e `name`/`email`
This commit is contained in:
Naz Gargol 2019-09-26 10:39:20 +02:00 committed by GitHub
parent 1559d7cb54
commit a562f09c0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,53 @@
const ObjectId = require('bson-objectid');
const _ = require('lodash');
const models = require('../../../../models');
const common = require('../../../../lib/common');
module.exports.config = {
transaction: true,
irreversible: true
};
module.exports.up = (options) => {
const localOptions = _.merge({
context: {internal: true},
migrating: true
}, options);
const memberAttrs = [
'name',
'email',
'created_at',
'created_by',
'updated_at',
'updated_by'
];
return models.Subscribers
.forge()
.fetch(localOptions)
.then(({models: subscribers}) => {
if (subscribers.length > 0) {
common.logging.info(`Adding ${subscribers.length} entries to members`);
let members = _.map(subscribers, (subscriber) => {
let member = memberAttrs.reduce(function (obj, prop) {
return Object.assign(obj, {
[prop]: subscriber.get(prop)
});
}, {});
member.id = ObjectId.generate();
return member;
});
return localOptions.transacting('members').insert(members);
} else {
common.logging.info('Skipping populating members table: found 0 subscribers');
return Promise.resolve();
}
});
};
module.exports.down = () => {
return Promise.reject();
};