Skip to content

Commit

Permalink
feat(migration): add a prompt before runing a real migration (#5552)
Browse files Browse the repository at this point in the history
### Description

<!--
What changes are introduced?
Why are these changes introduced?
What issue(s) does this solve? (with link, if possible)
-->

Adds a prompt before running a real migration with the projectId and dataset so the user is aware and intentionally selects running a real migration

### What to review

<!--
What steps should the reviewer take in order to review?
What parts/flows of the application/packages/tooling is affected?
-->

`sanity migration run <name-of-migration> --dry=false` should show a prompt like "This migration will run on the “test” dataset in “yajkdsl” project. Are you sure?"

### Testing

<!--
Did you add sufficient testing for this change?
If not, please explain how you tested this change and why it was not
possible/practical for writing an automated test
-->

### Notes for release

<!--
A description of the change(s) that should be used in the release notes.
-->

- N/A
  • Loading branch information
binoy14 authored and bjoerge committed Jan 26, 2024
1 parent 8ebba6a commit b60c02d
Showing 1 changed file with 60 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@sanity/migrate'
import {SanityDocument} from '@sanity/types'
import {Mutation} from '@bjoerge/mutiny'
import {debug} from '../../debug'
import {format, formatMutation} from './utils/mutationFormatter'

const helpText = `
Expand Down Expand Up @@ -145,46 +146,66 @@ const createMigrationCommand: CliCommandDefinition<CreateFlags> = {
apiVersion: 'v2024-01-09',
} as const

const spinner = output.spinner('Running migration…').start()
await (dry
? dryRun({api: apiConfig}, migration, context)
: run(
{
api: apiConfig,
concurrency,
onProgress(progress) {
if (progress.done) {
spinner.text = `Migration "${migrationName}" completed.
Project id: ${chalk.bold(projectId)}
Dataset: ${chalk.bold(dataset)}
${progress.documents} documents processed.
${progress.mutations} mutations generated.
${chalk.green(progress.completedTransactions.length)} transactions committed.`
spinner.stopAndPersist({symbol: chalk.green('✔')})
return
}

;['', ...progress.currentMutations].forEach((mutation) => {
spinner.text = `Running migration "${migrationName}"…
Project id: ${chalk.bold(projectId)}
Dataset: ${chalk.bold(dataset)}
Document type: ${chalk.bold(migration.documentType)}
${progress.documents} documents processed…
${progress.mutations} mutations generated…
${chalk.blue(progress.pending)} requests pending…
${chalk.green(progress.completedTransactions.length)} transactions committed.
${mutation && !progress.done ? ${chalk.grey(formatMutation(chalk, mutation as Mutation))}` : ''}`
})
},
if (dry) {
const spinner = output.spinner(`Running migration "${migrationName}" in dry mode`).start()
await dryRun({api: apiConfig}, migration, context)

spinner.stop()
} else {
const response = await prompt.single<boolean>({
message: `This migration will run on the ${chalk.yellow(
chalk.bold(apiConfig.dataset),
)} dataset in ${chalk.yellow(chalk.bold(apiConfig.projectId))} project. Are you sure?`,
type: 'confirm',
})

if (response === false) {
debug('User aborted migration')
return
}

const spinner = output.spinner(`Running migration "${migrationName}"`).start()

await run(
{
api: apiConfig,
concurrency,
onProgress(progress) {
if (progress.done) {
spinner.text = `Migration "${migrationName}" completed.
Project id: ${chalk.bold(projectId)}
Dataset: ${chalk.bold(dataset)}
${progress.documents} documents processed.
${progress.mutations} mutations generated.
${chalk.green(progress.completedTransactions.length)} transactions committed.`
spinner.stopAndPersist({symbol: chalk.green('✔')})
return
}

;['', ...progress.currentMutations].forEach((mutation) => {
spinner.text = `Running migration "${migrationName}"…
Project id: ${chalk.bold(projectId)}
Dataset: ${chalk.bold(dataset)}
Document type: ${chalk.bold(migration.documentType)}
${progress.documents} documents processed…
${progress.mutations} mutations generated…
${chalk.blue(progress.pending)} requests pending…
${chalk.green(progress.completedTransactions.length)} transactions committed.
${
mutation && !progress.done ? ${chalk.grey(formatMutation(chalk, mutation as Mutation))}` : ''
}`
})
},
migration,
))
spinner.stop()
},
migration,
)
spinner.stop()
}
},
}

Expand Down

0 comments on commit b60c02d

Please sign in to comment.