UNPKG

1.95 kBJavaScriptView Raw
1'use strict'
2
3const cli = require('heroku-cli-util')
4
5async function run(context, heroku) {
6 cli.warn('This is a beta command and is not considered reliable or complete. Use with caution.')
7 const host = require('../lib/host')
8 const util = require('../lib/util')
9 const fetcher = require('../lib/fetcher')(heroku)
10 let { app, args, flags } = context
11 let db = await fetcher.addon(app, args.database)
12
13 if (util.starterPlan(db)) throw new Error('pg:repoint is only available for follower production databases')
14
15 let replica = await heroku.get(`/client/v11/databases/${db.id}`, { host: host(db) })
16
17 if (!replica.following) {
18 throw new Error('pg:repoint is only available for follower production databases')
19 }
20
21 let origin = util.databaseNameFromUrl(replica.following, await heroku.get(`/apps/${app}/config-vars`))
22
23 let newLeader = await fetcher.addon(app, flags.follow)
24
25 await cli.confirmApp(app, flags.confirm, `WARNING: Destructive action
26${cli.color.addon(db.name)} will be repointed to follow ${newLeader.name}, and stop following ${origin}.
27
28This cannot be undone.`)
29
30 let data = { follow: newLeader.id }
31 await cli.action(`Starting repoint of ${cli.color.addon(db.name)}`, async function () {
32 await heroku.post(`/client/v11/databases/${db.id}/repoint`, { host: host(db), body: data })
33 cli.action.done(`${cli.color.cmd('heroku pg:wait')} to track status`)
34 }())
35}
36
37module.exports = {
38 hidden: true,
39 topic: 'pg',
40 command: 'repoint',
41 description: 'changes which leader a follower is following',
42 help: `Example:
43
44 heroku pg:repoint postgresql-transparent-56874 --follow postgresql-lucid-59103 -a woodstock-production
45`,
46 needsApp: true,
47 needsAuth: true,
48 args: [{ name: 'database', optional: true }],
49 flags: [
50 { name: 'confirm', char: 'c', hasValue: true },
51 { name: 'follow', description: 'leader database to follow', hasValue: true }
52 ],
53 run: cli.command({ preauth: true }, run)
54}