Skip to content

Commit 82c360f

Browse files
authoredFeb 27, 2025
feat(migrate): add support for --api-version flag (#8781)
1 parent 5c9f705 commit 82c360f

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const MIGRATIONS_DIRECTORY = 'migrations'
22
export const MIGRATION_SCRIPT_EXTENSIONS = ['mjs', 'js', 'ts', 'cjs']
3+
export const DEFAULT_API_VERSION = 'v2024-01-29'

Diff for: ‎packages/sanity/src/_internal/cli/commands/migration/runMigrationCommand.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import {hideBin} from 'yargs/helpers'
1515
import yargs from 'yargs/yargs'
1616

1717
import {debug} from '../../debug'
18-
import {MIGRATIONS_DIRECTORY} from './constants'
18+
import {DEFAULT_API_VERSION, MIGRATIONS_DIRECTORY} from './constants'
1919
import {resolveMigrations} from './listMigrationsCommand'
2020
import {prettyFormat} from './prettyMutationFormatter'
21+
import {ensureApiVersionFormat} from './utils/ensureApiVersionFormat'
2122
import {isLoadableMigrationScript, resolveMigrationScript} from './utils/resolveMigrationScript'
2223

2324
const helpText = `
@@ -27,6 +28,7 @@ Options
2728
--no-progress Don't output progress. Useful if you want debug your migration script and see the output of console.log() statements.
2829
--dataset <dataset> Dataset to migrate. Defaults to the dataset configured in your Sanity CLI config.
2930
--project <project id> Project ID of the dataset to migrate. Defaults to the projectId configured in your Sanity CLI config.
31+
--api-version <version> API version to use when migrating. Defaults to ${DEFAULT_API_VERSION}.
3032
--no-confirm Skip the confirmation prompt before running the migration. Make sure you know what you're doing before using this flag.
3133
--from-export <export.tar.gz> Use a local dataset export as source for migration instead of calling the Sanity API. Note: this is only supported for dry runs.
3234
@@ -60,6 +62,7 @@ function parseCliFlags(args: {argv?: string[]}) {
6062
.options('dataset', {type: 'string'})
6163
.options('from-export', {type: 'string'})
6264
.options('project', {type: 'string'})
65+
.options('api-version', {type: 'string'})
6366
.options('confirm', {type: 'boolean', default: true}).argv
6467
}
6568

@@ -81,6 +84,7 @@ const runMigrationCommand: CliCommandDefinition<CreateFlags> = {
8184
const dry = flags.dryRun
8285
const dataset = flags.dataset
8386
const project = flags.project
87+
const apiVersion = flags.apiVersion
8488

8589
if ((dataset && !project) || (project && !dataset)) {
8690
throw new Error('If either --dataset or --project is provided, both must be provided')
@@ -179,7 +183,7 @@ const runMigrationCommand: CliCommandDefinition<CreateFlags> = {
179183
projectId: project ?? projectConfig.projectId!,
180184
apiHost: projectConfig.apiHost!,
181185
token: projectConfig.token!,
182-
apiVersion: 'v2024-01-29',
186+
apiVersion: ensureApiVersionFormat(apiVersion ?? DEFAULT_API_VERSION),
183187
} as const
184188
if (dry) {
185189
dryRunHandler()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {type APIConfig} from '@sanity/migrate'
2+
3+
type ApiVersion = APIConfig['apiVersion']
4+
5+
const VERSION_PATTERN = /^v\d+-\d+-\d+$|^vX$/ // Matches version strings like vYYYY-MM-DD or vX
6+
7+
/**
8+
* Ensures that the provided API version string is in the correct format.
9+
* If the version does not start with 'v', it will be prefixed with 'v'.
10+
* If the version does not match the expected pattern, an error will be thrown.
11+
*/
12+
export function ensureApiVersionFormat(version: string): ApiVersion {
13+
const normalizedVersion = version.startsWith('v') ? version : `v${version}`
14+
15+
// Check if the version matches the expected pattern
16+
if (!VERSION_PATTERN.test(normalizedVersion)) {
17+
throw new Error(
18+
`Invalid API version format: ${normalizedVersion}. Expected format: vYYYY-MM-DD or vX`,
19+
)
20+
}
21+
22+
return normalizedVersion as ApiVersion
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.