Skip to content

Commit

Permalink
Add prompt before applying migration
Browse files Browse the repository at this point in the history
  • Loading branch information
zzooeeyy committed Feb 12, 2025
1 parent 54472ee commit 809e890
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/app/src/cli/models/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {ApplicationURLs} from '../../services/dev/urls.js'
import appHomeSpec from '../extensions/specifications/app_config_app_home.js'
import appProxySpec from '../extensions/specifications/app_config_app_proxy.js'
import {replaceScopesWithRequiredScopesInToml} from '../../services/app/patch-app-configuration-file.js'
import {confirmApplyPendingMigrations} from '../../prompts/deprecation-warnings.js'
import {ZodObjectOf, zod} from '@shopify/cli-kit/node/schema'
import {DotEnvFile} from '@shopify/cli-kit/node/dot-env'
import {getDependencies, PackageManager, readAndParsePackageJson} from '@shopify/cli-kit/node/node-package-manager'
Expand All @@ -25,6 +26,7 @@ import {AbortError} from '@shopify/cli-kit/node/error'
import {normalizeDelimitedString} from '@shopify/cli-kit/common/string'
import {JsonMapType} from '@shopify/cli-kit/node/toml'
import {getArrayRejectingUndefined} from '@shopify/cli-kit/common/array'
import {renderText, renderSuccess} from '@shopify/cli-kit/node/ui'

// Schemas for loading app configuration

Expand Down Expand Up @@ -319,7 +321,8 @@ type AppConstructor<
export class App<
TConfig extends AppConfiguration = AppConfiguration,
TModuleSpec extends ExtensionSpecification = ExtensionSpecification,
> implements AppInterface<TConfig, TModuleSpec> {
> implements AppInterface<TConfig, TModuleSpec>
{
name: string
idEnvironmentVariableName: 'SHOPIFY_API_KEY' = 'SHOPIFY_API_KEY' as const
directory: string
Expand Down Expand Up @@ -424,12 +427,29 @@ export class App<
}

async migratePendingSchemaChanges() {
await this.migrateScopesToRequiredScopes()
await Promise.all([this.realExtensions.map((ext) => ext.migratePendingSchemaChanges())])
const pendingMigrations = this.getPendingMigrationMessages()
if (pendingMigrations.length > 0) {
const shouldMigrate = await confirmApplyPendingMigrations(pendingMigrations)
if (shouldMigrate) {
await this.migrateScopesToRequiredScopes()
await Promise.all([this.realExtensions.map((ext) => ext.migratePendingSchemaChanges())])
renderSuccess({headline: 'Migration completed locally, run `shopify app deploy` to push the changes.'})
}
}
}

getPendingMigrationMessages(): string[] {
const migrationMessages = []
if (isCurrentAppSchema(this.configuration) && this.configuration.access_scopes?.scopes) {
migrationMessages.push('Replace `scopes(string)` with `required_scopes(string array)`.')
}
return migrationMessages
}

async migrateScopesToRequiredScopes() {
if (isCurrentAppSchema(this.configuration) && this.configuration.access_scopes?.scopes) {
renderText({text: 'Migration: Replacing `scopes` with `required_scopes` locally.'})

const accessConfig = this.configuration as {
access_scopes: {scopes?: string; required_scopes?: string[]}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/app/src/cli/prompts/deprecation-warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ export async function showApiKeyDeprecationWarning() {
body: ['The flag', {command: 'api-key'}, 'has been deprecated in favor of', {command: 'client-id'}],
})
}

export async function confirmApplyPendingMigrations(migrations: string[]): Promise<boolean> {
return renderConfirmationPrompt({
message: `There are pending migrations, would you like to apply them now?`,
infoTable: {'': migrations},
confirmationMessage: 'Yes, apply migrations',
cancellationMessage: 'No, apply migrations later',
defaultValue: true,
})
}

0 comments on commit 809e890

Please sign in to comment.