mirror of
https://github.com/sanity-io/agent-toolkit.git
synced 2026-09-14 18:39:20 +08:00
Update migration rules to use defineMigration
- schema-deprecation-pattern.md: Replace raw client.patch with defineMigration, at, setIfMissing, unset from sanity/migrate - migration-html-import.md: Add defineMigration wrapper example for reproducible HTML imports
This commit is contained in:
@@ -104,4 +104,36 @@ async function uploadImage(client, imageUrl) {
|
||||
}
|
||||
```
|
||||
|
||||
Reference: [Content Migration Cheatsheet](https://www.sanity.io/docs/content-lake/content-migration-cheatsheet)
|
||||
### Using in a Migration
|
||||
|
||||
Wrap this in `defineMigration` for reproducible imports:
|
||||
|
||||
```typescript
|
||||
// migrations/import-wordpress-posts/index.ts
|
||||
import {defineMigration, createOrReplace} from 'sanity/migrate'
|
||||
import {htmlToBlocks} from '@portabletext/block-tools'
|
||||
|
||||
export default defineMigration({
|
||||
title: 'Import WordPress posts',
|
||||
async *migrate(documents, context) {
|
||||
const posts = await fetchWordPressPosts() // Your import source
|
||||
|
||||
for (const post of posts) {
|
||||
const blocks = htmlToBlocks(post.content, blockContentType, {
|
||||
parseHtml: html => new JSDOM(html).window.document,
|
||||
})
|
||||
|
||||
yield createOrReplace({
|
||||
_id: `post-${post.slug}`,
|
||||
_type: 'post',
|
||||
title: post.title,
|
||||
body: blocks,
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Run with: `sanity migration run import-wordpress-posts --no-dry-run`
|
||||
|
||||
Reference: [Schema and Content Migrations](https://www.sanity.io/docs/content-lake/schema-and-content-migrations)
|
||||
|
||||
@@ -49,19 +49,36 @@ defineField({
|
||||
2. Deploy schema changes
|
||||
|
||||
**Phase 2: Migrate**
|
||||
1. Update frontend to use new fields (with fallbacks)
|
||||
2. Run migration script to move data:
|
||||
1. Update frontend to use new fields (with fallbacks using `coalesce()`)
|
||||
2. Create a migration file in `migrations/` folder:
|
||||
|
||||
```typescript
|
||||
// Migration script example
|
||||
const documents = await client.fetch(`*[_type == "article" && defined(oldTitle)]`)
|
||||
// migrations/rename-oldTitle-to-newTitle/index.ts
|
||||
import {defineMigration, at, setIfMissing, unset} from 'sanity/migrate'
|
||||
|
||||
for (const doc of documents) {
|
||||
await client.patch(doc._id)
|
||||
.set({ newTitle: doc.oldTitle })
|
||||
.unset(['oldTitle'])
|
||||
.commit()
|
||||
}
|
||||
export default defineMigration({
|
||||
title: 'Rename oldTitle to newTitle',
|
||||
documentTypes: ['article'],
|
||||
filter: 'defined(oldTitle) && !defined(newTitle)',
|
||||
migrate: {
|
||||
document(doc) {
|
||||
return [
|
||||
at('newTitle', setIfMissing(doc.oldTitle)),
|
||||
at('oldTitle', unset())
|
||||
]
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
3. Run the migration:
|
||||
|
||||
```bash
|
||||
# Dry run first (default)
|
||||
sanity migration run rename-oldTitle-to-newTitle
|
||||
|
||||
# Execute when ready
|
||||
sanity migration run rename-oldTitle-to-newTitle --no-dry-run
|
||||
```
|
||||
|
||||
**Phase 3: Remove**
|
||||
|
||||
Reference in New Issue
Block a user