Skip to content

Instantly share code, notes, and snippets.

@kmelve
Created November 10, 2021 22:15
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 kmelve/f44e46ea3cfb04aad4eb90ad565fa720 to your computer and use it in GitHub Desktop.
Save kmelve/f44e46ea3cfb04aad4eb90ad565fa720 to your computer and use it in GitHub Desktop.
Simple migration script that can be run with `sanity exec` in a Sanity Studio
// migration.js
/**
* Run:
* sanity exec --with-user-token migration.js
*
*/
import sanityClient from 'part:@sanity/base/client'
const client = sanityClient.withConfig({ apiVersion: '2021-11-10' })
// Patch 1000 at a time
const query = `*[_type == "stream"][0...999]{title, publishedDate, _id, _rev}`
async function migrate() {
const docs = await client.fetch(query)
const patches = docs.map((doc) => ({
id: doc._id,
patch: {
set: {
test: true
},
},
ifRevisionID: doc._rev, // prevent race conditions
}))
const transactions = patches.reduce(
(tx, patch) => tx.patch(patch.id, patch.patch),
client.transaction()
)
const result = await transactions.commit()
console.log('Done!')
console.log(JSON.stringify(result, null, 2))
}
migrate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment