Created
February 7, 2021 19:23
-
-
Save boutell/7ec5e2ec180e580d842aec89b99b0218 to your computer and use it in GitHub Desktop.
Example of an Apostrophe migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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