Skip to content

Commit 4ffdbe8

Browse files
committed
format response
1 parent 90aa38a commit 4ffdbe8

3 files changed

Lines changed: 27 additions & 6 deletions

File tree

packages/cli/src/commands/pg/upgrade/prepare.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {Args, ux} from '@oclif/core'
44
import heredoc from 'tsheredoc'
55
import {getAddon} from '../../../lib/pg/fetcher'
66
import pgHost from '../../../lib/pg/host'
7-
import {legacyEssentialPlan, essentialNumPlan} from '../../../lib/pg/util'
8-
import {PgDatabase} from '../../../lib/pg/types'
7+
import {legacyEssentialPlan, essentialNumPlan, formatResponseWithCommands} from '../../../lib/pg/util'
8+
import {PgDatabase, PgUpgradeError, PgUpgradeResponse} from '../../../lib/pg/types'
99
import confirmCommand from '../../../lib/confirmCommand'
1010
import {nls} from '../../../nls'
1111

@@ -52,9 +52,14 @@ export default class Upgrade extends Command {
5252
This command prepares the upgrade for ${color.addon(db.name)} to the latest supported PostgreSQL version and schedules to upgrade it during the next available maintenance window.
5353
`))
5454

55-
const data = {version}
56-
ux.action.start(`Preparing upgrade on ${color.addon(db.name)}`)
57-
await this.heroku.post(`/client/v11/databases/${db.id}/upgrade/prepare`, {hostname: pgHost(), body: data})
58-
ux.action.stop(`done\nYour database is scheduled for upgrade during your next available maintenance window.\nRun ${color.cmd('heroku pg:upgrade:wait')} to track its status.\nYou can also run this upgrade manually before the maintenance window with ${color.cmd('heroku pg:upgrade:run')}. You can only run the upgrade after it's fully prepared, which can take up to a day.`)
55+
try {
56+
const data = {version}
57+
ux.action.start(`Preparing upgrade on ${color.addon(db.name)}`)
58+
const {body : response} = await this.heroku.post<PgUpgradeResponse>(`/client/v11/databases/${db.id}/upgrade/prepare`, {hostname: pgHost(), body: data})
59+
ux.action.stop('done\n' + formatResponseWithCommands(response.message))
60+
} catch (error) {
61+
const response = error as PgUpgradeError
62+
ux.error(formatResponseWithCommands(response.body.message) + `\n\nError ID: ${response.body.id}`)
63+
}
5964
}
6065
}

packages/cli/src/lib/pg/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ export type PgDatabaseTenant = {
167167

168168
export type PgDatabase = PgDatabaseService & PgDatabaseTenant
169169

170+
export type PgUpgradeResponse = {
171+
message: string
172+
}
173+
174+
export type PgUpgradeError = {
175+
status: number,
176+
body: {
177+
id: string,
178+
message: string,
179+
}
180+
}
181+
170182
export type AddOnWithPlan = Required<Heroku.AddOnAttachment['addon']> & {plan: Required<Heroku.AddOn['plan']>}
171183
export type AddOnAttachmentWithConfigVarsAndPlan = Required<Heroku.AddOnAttachment> & {
172184
config_vars: Heroku.AddOn['config_vars']

packages/cli/src/lib/pg/util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export function getConfigVarNameFromAttachment(attachment: Required<AddOnAttachm
4040
return getConfigVarName(configVars)
4141
}
4242

43+
export function formatResponseWithCommands(response: string): string {
44+
return response.replace(/`(.*?)`/g, (_, word) => color.cmd(word))
45+
}
46+
4347
export function presentCredentialAttachments(app: string, credAttachments: Required<AddOnAttachment>[], credentials: CredentialsInfo, cred: string) {
4448
const isForeignApp = (attOrAddon: Required<AddOnAttachment>) => attOrAddon.app.name === app ? 0 : 1
4549
const comparators = [

0 commit comments

Comments
 (0)