1 | 'use strict'
|
2 |
|
3 | const co = require('co')
|
4 | const cli = require('heroku-cli-util')
|
5 | const host = require('../lib/host')
|
6 |
|
7 | function * run (context, heroku) {
|
8 | const util = require('../lib/util')
|
9 | const fetcher = require('../lib/fetcher')(heroku)
|
10 | const {app, args, flags} = context
|
11 |
|
12 | let db = yield fetcher.addon(app, args.database)
|
13 | let addon = yield heroku.get(`/addons/${encodeURIComponent(db.name)}`)
|
14 | let credential = flags.credential || 'default'
|
15 |
|
16 | if (util.starterPlan(db)) throw new Error('This operation is not supported by Hobby tier databases.')
|
17 |
|
18 | let attachment = yield cli.action(
|
19 | `Enabling Connection Pooling${credential === 'default' ? '' : ' for credential ' + cli.color.addon(credential)} on ${cli.color.addon(addon.name)} to ${cli.color.app(app)}`,
|
20 | heroku.post(`/client/v11/databases/${encodeURIComponent(db.name)}/connection-pooling`, {
|
21 | body: { name: flags.as, credential: credential },
|
22 | host: host(db)
|
23 | })
|
24 | )
|
25 |
|
26 | yield cli.action(
|
27 | `Setting ${cli.color.attachment(attachment.name)} config vars and restarting ${cli.color.app(app)}`,
|
28 | {success: false},
|
29 | co(function * () {
|
30 | let releases = yield heroku.get(`/apps/${app}/releases`, {
|
31 | partial: true,
|
32 | headers: { 'Range': 'version ..; max=1, order=desc' }
|
33 | })
|
34 | cli.action.done(`done, v${releases[0].version}`)
|
35 | })
|
36 | )
|
37 | }
|
38 |
|
39 | module.exports = {
|
40 | topic: 'pg',
|
41 | command: 'connection-pooling:attach',
|
42 | description: 'add an attachment to a database using connection pooling',
|
43 | needsApp: true,
|
44 | needsAuth: true,
|
45 | help: `Example:
|
46 |
|
47 | heroku pg:connection-pooling:attach postgresql-something-12345 --credential cred-name
|
48 | `,
|
49 | args: [{name: 'database', optional: true}],
|
50 | flags: [
|
51 | {name: 'as', description: 'name for add-on attachment', hasValue: true},
|
52 | {name: 'credential', char: 'n', hasValue: true, required: false, description: 'name of the credential within the database'}
|
53 | ],
|
54 | run: cli.command({preauth: true}, co.wrap(run))
|
55 | }
|