1 | 'use strict'
|
2 |
|
3 | const cli = require('heroku-cli-util')
|
4 |
|
5 | async function run(context, heroku) {
|
6 | const host = require('../lib/host')
|
7 | const util = require('../lib/util')
|
8 | const fetcher = require('../lib/fetcher')(heroku)
|
9 | let { app, args, flags } = context
|
10 | let db = await fetcher.addon(app, args.database)
|
11 |
|
12 | if (util.starterPlan(db)) throw new Error('pg:upgrade is only available for follower production databases')
|
13 |
|
14 | let [replica, status] = await Promise.all([
|
15 | heroku.get(`/client/v11/databases/${db.id}`, { host: host(db) }),
|
16 | heroku.get(`/client/v11/databases/${db.id}/upgrade_status`, { host: host(db) })
|
17 | ])
|
18 |
|
19 | if (status.error) throw new Error(status.error)
|
20 |
|
21 | if (!replica.following) {
|
22 | throw new Error('pg:upgrade is only available for follower production databases')
|
23 | }
|
24 |
|
25 | let origin = util.databaseNameFromUrl(replica.following, await heroku.get(`/apps/${app}/config-vars`))
|
26 |
|
27 | await cli.confirmApp(app, flags.confirm, `WARNING: Destructive action
|
28 | ${cli.color.addon(db.name)} will be upgraded to a newer PostgreSQL version, stop following ${origin}, and become writable.
|
29 |
|
30 | This cannot be undone.`)
|
31 |
|
32 | let data = { version: flags.version }
|
33 |
|
34 | await cli.action(`Starting upgrade of ${cli.color.addon(db.name)}`, async function () {
|
35 | await heroku.post(`/client/v11/databases/${db.id}/upgrade`, { host: host(db), body: data })
|
36 | cli.action.done(`${cli.color.cmd('heroku pg:wait')} to track status`)
|
37 | }())
|
38 | }
|
39 |
|
40 | module.exports = {
|
41 | topic: 'pg',
|
42 | command: 'upgrade',
|
43 | description: 'unfollow a database and upgrade it to the latest stable PostgreSQL version',
|
44 | help: 'to upgrade to another PostgreSQL version, use pg:copy instead',
|
45 | needsApp: true,
|
46 | needsAuth: true,
|
47 | args: [{ name: 'database', optional: true }],
|
48 | flags: [
|
49 | { name: 'confirm', char: 'c', hasValue: true },
|
50 | { name: 'version', char: 'v', description: 'PostgreSQL version to upgrade to', hasValue: true }
|
51 | ],
|
52 | run: cli.command({ preauth: true }, run)
|
53 | }
|