Skip to content

Instantly share code, notes, and snippets.

@boutell
Created February 7, 2021 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boutell/7ec5e2ec180e580d842aec89b99b0218 to your computer and use it in GitHub Desktop.
Save boutell/7ec5e2ec180e580d842aec89b99b0218 to your computer and use it in GitHub Desktop.
Example of an Apostrophe migration
// in lib/modules/pet-owners/index.js
module.exports = {
extend: 'apostrophe-pieces',
name: 'pet-owner',
addFields: [
{
name: 'cat',
label: 'Cat',
type: 'string'
}
],
construct(self, options) {
self.apos.migrations.add('renameFelineField', async () => {
await self.apos.migrations.eachDoc({
// This is a raw mongo query, we must take care to leave other piece types alone
type: 'pet-owner',
// Don't bother with docs that never had a feline field
feline: { $exists: 1 }
}, async (doc) => {
// Raw mongodb update so we can use $set and $unset and avoid race conditions
await self.apos.docs.db.updateOne({
_id: doc._id
}, {
$set: {
cat: doc.feline
},
$unset: {
feline: 1
}
});
});
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment