const seedDb = require('../index') const h = require('../helpers') /** * What TYPE are you migrating? */ const mainType = h.types.post module.exports = { up: async query => { const fromSeedDB = h.db(seedDb) const toCleanDB = h.db(query.sequelize) /** * Our base list of posts matching mainType to migrate */ const matchingPosts = await fromSeedDB.wp_posts.findAll({ where: { post_type: mainType, post_status: h.types.status } }) const postsToWrite = h.getPostData(matchingPosts, mainType) /** * Attachments matching ID's in postsToWrite */ const matchingAttachments = await h.findDescendants( postsToWrite, fromSeedDB.wp_posts, post => ({ post_type: h.types.attachment, post_parent: post.ID }) ) const attachmentsToWrite = h.getPostData(matchingAttachments, h.types.attachment) /** * Attachments are POSTS too, so we combine the two lists * before we can find postmeta keys we care about * * 1. Meta keys are match anything in our h.metakeys list * using opt.in to evaluate h.metakeys.indexOf(post) */ const postmeta = await h.findDescendants( [...matchingPosts, ...matchingAttachments], fromSeedDB.wp_postmeta, post => ({ post_id: post.ID, /* 1 */ meta_key: h.metakeys }) ) const postmetaToWrite = h.getPostData(postmeta, false) /** * INSERT in our new db with some options * 1. First we insert our episode POST and attachments * to the posts table * 2. Then we add our matching POSTMETA entries to * the postmeta table */ const createOptions = { validate: true } /* 1 */ await toCleanDB.wp_posts.bulkCreate([ ...postsToWrite, ...attachmentsToWrite ], createOptions) /* 2 */ await toCleanDB.wp_postmeta.bulkCreate([ ...postmetaToWrite ], createOptions) }, down: async query => { // Do nothing... }, }