Skip to content

Commit

Permalink
fix(migrate): throw if attempting to iterate over documents producer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Feb 16, 2024
1 parent 6caf803 commit 928f04d
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion packages/@sanity/migrate/src/runner/collectMigrationMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,37 @@ import {type SanityDocument} from '@sanity/types'
import {type Migration, type MigrationContext} from '../types'
import {normalizeMigrateDefinition} from './normalizeMigrateDefinition'

async function* empty() {}

function wrapDocumentsIteratorProducer(factory: () => AsyncIterableIterator<SanityDocument>) {
function documents() {
return factory()
}

;(documents as any)[Symbol.asyncIterator] = () => {
throw new Error(
`The migration is attempting to iterate over the "documents" function, please call the function instead:
// BAD:
for await (const document of documents) {
// ...
}
// GOOD: 👇 This is a function and has to be called
for await (const document of documents()) {
// ...
}
`,
)
}
return documents
}

export function collectMigrationMutations(
migration: Migration,
documents: () => AsyncIterableIterator<SanityDocument>,
context: MigrationContext,
) {
const migrate = normalizeMigrateDefinition(migration)
return migrate(documents, context)
return migrate(wrapDocumentsIteratorProducer(documents), context)
}

0 comments on commit 928f04d

Please sign in to comment.