How do I migrate docs after changing schema or enabling localization? #434
-
These are common questions and fortunately pretty easy to solve using a migration script. All we have to do is spin up Payload, find all records within a given collection, and then immediately update each. If needed, we can also modify the data before it gets updated.
^ Your specific file path may different. Update this based on your specifications. Also note that Payload may need to know the location of your config, see this page for more detail. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
If you're here needing to regenerate media after changing image sizes, check out this discussion: #1834 |
Beta Was this translation helpful? Give feedback.
-
To clear things out in my head, and in case it helps anyone else, could somebody confirm the modified code would be the correct way to migrate a nested field Initial:
Interim:
Final:
During the interim stage of the schema, run the following script: const payload = require('payload');
require('dotenv').config();
const resaveCollection = async () => {
await payload.init({
secret: process.env.PAYLOAD_SECRET,
mongoURL: process.env.MONGO_URL,
local: true,
});
const args = process.argv.slice(2); // nodejs command line args are an array that begin at the third item
const [
collectionSlug,
overrides
] = args || [];
const results = await payload.find({
collection: collectionSlug,
depth: 0,
limit: 700,
});
try {
await Promise.all(results.docs.map(async (result) => {
const { id } = result;
if (collectionSlug) {
try {
await payload.update({
collection: collectionSlug,
id,
data: {
import: {
...result.import,
objId: result.import?.objectId
},
...overrides || {}
},
})
console.log(`Document in '${collectionSlug}' with id '${id}' updated successfully`);
} catch (e) {
payload.logger.error(`Document in '${collectionSlug}' with id '${id}' failed to update`);
payload.logger.error(e);
}
} else {
console.log(`No document found in '${collectionSlug}' with id '${id}'`);
}
}));
} catch (e) {
payload.logger.error('Something went wrong.');
payload.logger.error(e);
}
console.log('Complete');
process.exit(0);
};
resaveCollection(); (The important parts are around line 33 where I added Then we can remove the 'objectId' field in the 'import' group. Is this correct? If so, how can we go about removing the original/stale field data from the database? I tried adding |
Beta Was this translation helpful? Give feedback.
If you're here needing to regenerate media after changing image sizes, check out this discussion: #1834