|
| 1 | +import {flags as Flags} from '@heroku-cli/command' |
| 2 | +import {color, hux, utils} from '@heroku/heroku-cli-util' |
| 3 | +import {Args, ux} from '@oclif/core' |
| 4 | +import tsheredoc from 'tsheredoc' |
| 5 | + |
| 6 | +import BaseCommand from '../../../../lib/data/baseCommand.js' |
| 7 | +import {InfoResponse, UpgradeResponse} from '../../../../lib/data/types.js' |
| 8 | + |
| 9 | +const heredoc = tsheredoc.default |
| 10 | + |
| 11 | +export default class DataPgUpgradeRun extends BaseCommand { |
| 12 | + static args = { |
| 13 | + database: Args.string({ |
| 14 | + description: 'database name, database attachment name, or related config var on an app', |
| 15 | + required: true, |
| 16 | + }), |
| 17 | + } |
| 18 | + |
| 19 | + static description = 'upgrade the Postgres version on a Postgres Advanced database' |
| 20 | + |
| 21 | + static examples = [ |
| 22 | + heredoc` |
| 23 | + # Upgrade a Postgres Advanced database to version 17 |
| 24 | + ${color.code('<%= config.bin %> <%= command.id %> DATABASE --version 17 --app my-app')} |
| 25 | + `, |
| 26 | + ] |
| 27 | + |
| 28 | + static flags = { |
| 29 | + app: Flags.app({required: true}), |
| 30 | + confirm: Flags.string({char: 'c', description: 'pass in the app name to skip confirmation prompts'}), |
| 31 | + remote: Flags.remote(), |
| 32 | + version: Flags.string({char: 'v', description: 'Postgres version to upgrade to'}), |
| 33 | + } |
| 34 | + |
| 35 | + public async run(): Promise<void> { |
| 36 | + const {args, flags} = await this.parse(DataPgUpgradeRun) |
| 37 | + const {app, confirm, version} = flags |
| 38 | + const {database} = args |
| 39 | + |
| 40 | + const dbResolver = new utils.pg.DatabaseResolver(this.heroku) |
| 41 | + const {addon} = await dbResolver.getAttachment(app, database) |
| 42 | + |
| 43 | + if (!utils.pg.isAdvancedDatabase(addon)) { |
| 44 | + ux.error( |
| 45 | + 'You can only use this command on Advanced-tier databases.\n' |
| 46 | + + `Use ${color.code(`heroku pg:upgrade:run ${addon.name} --app ${app}`)} instead.`, |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + const {body: databaseInfo} = await this.dataApi.get<InfoResponse>(`/data/postgres/v1/${addon.id}/info`) |
| 51 | + const {version: currentVersion} = databaseInfo |
| 52 | + const newVersion = version ?? 'the latest supported Postgres version' |
| 53 | + await hux.confirmCommand({ |
| 54 | + comparison: app, |
| 55 | + confirmation: confirm, |
| 56 | + warningMessage: heredoc(` |
| 57 | + This command immediately upgrades your ${color.datastore(addon.name)} database from ${currentVersion} to ${newVersion}. |
| 58 | + Your database will be unavailable until the upgrade is complete.`), |
| 59 | + }) |
| 60 | + |
| 61 | + try { |
| 62 | + ux.action.start(`Upgrading your ${color.datastore(addon.name)} database from ${currentVersion} to ${newVersion}`) |
| 63 | + await this.dataApi.post<UpgradeResponse>( |
| 64 | + `/data/postgres/v1/${addon.id}/upgrade/run`, |
| 65 | + {body: {version}}, |
| 66 | + ) |
| 67 | + ux.action.stop() |
| 68 | + ux.stderr(`Upgrade started. Use ${color.code(`heroku data:pg:upgrade:wait ${addon.name} -a ${app}`)} to monitor progress.`) |
| 69 | + } catch (error: unknown) { |
| 70 | + ux.action.stop(color.red('!')) |
| 71 | + throw error |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments