Skip to content

Commit a839126

Browse files
authored
feat: create pg:upgrade:run command (#3276)
2 parents 6691ef8 + ef78317 commit a839126

File tree

6 files changed

+375
-15
lines changed

6 files changed

+375
-15
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {nls} from '../../../nls'
1313
export default class Upgrade extends Command {
1414
static topic = 'pg';
1515
static description = heredoc(`
16-
Were deprecating this command. To upgrade your database's Postgres version, use the new ${color.cmd('pg:upgrade:*')} subcommands. See https://devcenter.heroku.com/changelog-items/3179.
16+
We're deprecating this command. To upgrade your database's Postgres version, use the new ${color.cmd('pg:upgrade:*')} subcommands. See https://devcenter.heroku.com/changelog-items/3179.
1717
18-
For an Essential-* plan, this command upgrades the database's Postgres version. For a Standard-tier and higher plan, this command unfollows the leader database before upgrading the Postgres version.
18+
For an Essential-tier plan, this command upgrades the database's Postgres version. For a Standard-tier and higher plan, this command unfollows the leader database before upgrading the Postgres version.
1919
`)
2020

2121
static flags = {
@@ -46,25 +46,25 @@ export default class Upgrade extends Command {
4646
const origin = databaseNameFromUrl(replica.following, configVars)
4747

4848
await confirmCommand(app, confirm, heredoc(`
49-
Were deprecating this command. To upgrade your database's Postgres version, use the new ${color.cmd('pg:upgrade:*')} subcommands. See https://devcenter.heroku.com/changelog-items/3179.
49+
We're deprecating this command. To upgrade your database's Postgres version, use the new ${color.cmd('pg:upgrade:*')} subcommands. See https://devcenter.heroku.com/changelog-items/3179.
5050
5151
Destructive action
5252
You're upgrading ${color.addon(db.name)} to ${versionPhrase}. The database will stop following ${origin} and become writable.
5353
54-
This cannot be undone.
54+
You can't undo this action.
5555
`))
5656
} else if (essentialNumPlan(db)) {
5757
await confirmCommand(app, confirm, heredoc(`
58-
Were deprecating this command. To upgrade your database's Postgres version, use the new ${color.cmd('pg:upgrade:*')} subcommands. See https://devcenter.heroku.com/changelog-items/3179.
58+
We're deprecating this command. To upgrade your database's Postgres version, use the new ${color.cmd('pg:upgrade:*')} subcommands. See https://devcenter.heroku.com/changelog-items/3179.
5959
6060
Destructive action
6161
You're upgrading ${color.addon(db.name)} to ${versionPhrase}.
6262
63-
This cannot be undone.
63+
You can't undo this action.
6464
`))
6565
} else {
6666
ux.warn(heredoc(`
67-
Were deprecating this command. To upgrade your database's Postgres version, use the new ${color.cmd('pg:upgrade:*')} subcommands. See https://devcenter.heroku.com/changelog-items/3179.`,
67+
We're deprecating this command. To upgrade your database's Postgres version, use the new ${color.cmd('pg:upgrade:*')} subcommands. See https://devcenter.heroku.com/changelog-items/3179.`,
6868
))
6969
ux.error(`You can only use ${color.cmd('heroku pg:upgrade')} on Essential-tier databases and follower databases on Standard-tier and higher plans.`)
7070
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import color from '@heroku-cli/color'
2+
import {Command, flags} from '@heroku-cli/command'
3+
import {Args, ux} from '@oclif/core'
4+
import heredoc from 'tsheredoc'
5+
import {getAddon} from '../../../lib/pg/fetcher'
6+
import pgHost from '../../../lib/pg/host'
7+
import {legacyEssentialPlan, databaseNameFromUrl, essentialNumPlan, formatResponseWithCommands} from '../../../lib/pg/util'
8+
import {PgDatabase, PgUpgradeError, PgUpgradeResponse} from '../../../lib/pg/types'
9+
import * as Heroku from '@heroku-cli/schema'
10+
import confirmCommand from '../../../lib/confirmCommand'
11+
import {nls} from '../../../nls'
12+
13+
export default class Upgrade extends Command {
14+
static topic = 'pg';
15+
static description = heredoc(`
16+
On Essential-tier databases, this command upgrades the database's Postgres version.
17+
18+
On Standard-tier and higher leader databases, this command runs a previously scheduled Postgres version upgrade. You must run ${color.cmd('pg:upgrade:prepare')} before this command to schedule a version upgrade.
19+
20+
On follower databases, this command unfollows the leader database before upgrading the follower's Postgres version.
21+
`)
22+
23+
static examples = [
24+
heredoc`
25+
# Upgrade an Essential-tier database to a specific version
26+
$ heroku pg:upgrade:run postgresql-curved-12345 --version 14 --app myapp
27+
`,
28+
heredoc`
29+
# Upgrade a Standard-tier follower database to the latest supported version
30+
$ heroku pg:upgrade:run HEROKU_POSTGRESQL_BLUE_URL --app myapp
31+
`,
32+
heredoc`
33+
# Run a previously scheduled upgrade on a Standard-tier leader database
34+
$ heroku pg:upgrade:run DATABASE_URL --app myapp
35+
`,
36+
]
37+
38+
static flags = {
39+
confirm: flags.string({char: 'c'}),
40+
version: flags.string({char: 'v', description: 'Postgres version to upgrade to'}),
41+
app: flags.app({required: true}),
42+
remote: flags.remote({char: 'r'}),
43+
}
44+
45+
static args = {
46+
database: Args.string({description: `${nls('pg:database:arg:description')} ${nls('pg:database:arg:description:default:suffix')}`}),
47+
}
48+
49+
public async run(): Promise<void> {
50+
const {flags, args} = await this.parse(Upgrade)
51+
const {app, version, confirm} = flags
52+
const {database} = args
53+
54+
const db = await getAddon(this.heroku, app, database)
55+
if (legacyEssentialPlan(db))
56+
ux.error(`You can only use ${color.cmd('pg:upgrade:*')} commands on Essential-* and higher plans.`)
57+
58+
const versionPhrase = version ? heredoc(`Postgres version ${version}`) : heredoc('the latest supported Postgres version')
59+
const {body: replica} = await this.heroku.get<PgDatabase>(`/client/v11/databases/${db.id}`, {hostname: pgHost()})
60+
61+
if (essentialNumPlan(db)) {
62+
await confirmCommand(app, confirm, heredoc(`
63+
Destructive action
64+
You're upgrading ${color.addon(db.name)} to ${versionPhrase}.
65+
66+
You can't undo this action.
67+
`))
68+
} else if (replica.following) {
69+
const {body: configVars} = await this.heroku.get<Heroku.ConfigVars>(`/apps/${app}/config-vars`)
70+
const origin = databaseNameFromUrl(replica.following, configVars)
71+
72+
await confirmCommand(app, confirm, heredoc(`
73+
Destructive action
74+
You're upgrading ${color.addon(db.name)} to ${versionPhrase}. The database will stop following ${origin} and become writable.
75+
76+
You can't undo this action.
77+
`))
78+
} else {
79+
await confirmCommand(app, confirm, heredoc(`
80+
Destructive action
81+
You're upgrading the Postgres version on ${color.addon(db.name)}. This action also upgrades any followers on the database.
82+
83+
You can't undo this action.
84+
`))
85+
}
86+
87+
try {
88+
const data = {version}
89+
ux.action.start(`Starting upgrade on ${color.addon(db.name)}`)
90+
const response = await this.heroku.post<PgUpgradeResponse>(`/client/v11/databases/${db.id}/upgrade/run`, {hostname: pgHost(), body: data})
91+
ux.action.stop(heredoc(`done\n${formatResponseWithCommands(response.body.message)}`))
92+
} catch (error) {
93+
if (error instanceof Error && 'body' in error) {
94+
const response = error as PgUpgradeError
95+
ux.error(heredoc(`${formatResponseWithCommands(response.body.message)}\n\nError ID: ${response.body.id}`))
96+
} else {
97+
throw error
98+
}
99+
}
100+
}
101+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ export type PgUpgradeResponse = {
172172
}
173173

174174
export type PgUpgradeError = {
175-
status: number,
176175
body: {
177176
id: string,
178177
message: string,

packages/cli/test/acceptance/commands-output.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pg:settings:log-min-duration-statement The duration of each completed st
206206
pg:settings:log-statement log_statement controls which SQL statements are logged.
207207
pg:settings:track-functions track_functions controls tracking of function call counts and time used. Default is none.
208208
pg:unfollow stop a replica from following and make it a writeable database
209-
pg:upgrade Were deprecating this command. To upgrade your database's Postgres version, use the new pg:upgrade:* subcommands. See https://devcenter.heroku.com/changelog-items/3179.
209+
pg:upgrade We're deprecating this command. To upgrade your database's Postgres version, use the new pg:upgrade:* subcommands. See https://devcenter.heroku.com/changelog-items/3179.
210210
pg:vacuum-stats show dead rows and whether an automatic vacuum is expected to be triggered
211211
pg:wait blocks until database is available
212212
pipelines list pipelines you have access to

packages/cli/test/unit/commands/pg/upgrade/index.unit.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ describe('pg:upgrade', function () {
8787
.reply(200, {message: 'Upgrading'})
8888

8989
const message = heredoc(`
90-
Were deprecating this command. To upgrade your database's Postgres version, use the new pg:upgrade:* subcommands. See https://devcenter.heroku.com/changelog-items/3179.
90+
We're deprecating this command. To upgrade your database's Postgres version, use the new pg:upgrade:* subcommands. See https://devcenter.heroku.com/changelog-items/3179.
9191
9292
Destructive action
9393
You're upgrading ${addon.name} to Postgres version 15. The database will stop following DATABASE and become writable.
9494
95-
This cannot be undone.
95+
You can't undo this action.
9696
`)
9797

9898
await runCommand(Cmd, [
@@ -126,12 +126,12 @@ describe('pg:upgrade', function () {
126126
.reply(200, {message: 'Upgrading'})
127127

128128
const message = heredoc(`
129-
Were deprecating this command. To upgrade your database's Postgres version, use the new pg:upgrade:* subcommands. See https://devcenter.heroku.com/changelog-items/3179.
129+
We're deprecating this command. To upgrade your database's Postgres version, use the new pg:upgrade:* subcommands. See https://devcenter.heroku.com/changelog-items/3179.
130130
131131
Destructive action
132132
You're upgrading ${addon.name} to the latest supported Postgres version. The database will stop following DATABASE and become writable.
133133
134-
This cannot be undone.
134+
You can't undo this action.
135135
`)
136136

137137
await runCommand(Cmd, [
@@ -167,12 +167,12 @@ describe('pg:upgrade', function () {
167167
.reply(200, {message: 'Upgrading'})
168168

169169
const message = heredoc(`
170-
Were deprecating this command. To upgrade your database's Postgres version, use the new pg:upgrade:* subcommands. See https://devcenter.heroku.com/changelog-items/3179.
170+
We're deprecating this command. To upgrade your database's Postgres version, use the new pg:upgrade:* subcommands. See https://devcenter.heroku.com/changelog-items/3179.
171171
172172
Destructive action
173173
You're upgrading ${essentialAddon.name} to the latest supported Postgres version.
174174
175-
This cannot be undone.
175+
You can't undo this action.
176176
`)
177177

178178
await runCommand(Cmd, [

0 commit comments

Comments
 (0)