|
1 | 1 | /* |
2 | 2 | import {APIClient} from '@heroku-cli/command' |
3 | 3 | import * as Heroku from '@heroku-cli/schema' |
4 | | -import {ExtendedAddonAttachment} from '@heroku/heroku-cli-util' |
5 | | -import debug from 'debug' |
6 | | -import {uniqBy} from 'lodash' |
7 | | -
|
8 | | -const pgDebug = debug('pg') |
9 | | -
|
10 | | -export async function arbitraryAppDB(heroku: APIClient, app: string) { |
11 | | - // Since Postgres backups are tied to the app and not the add-on, but |
12 | | - // we require *an* add-on to interact with, make sure that add-on is |
13 | | - // attached to the right app. |
14 | | -
|
15 | | - pgDebug(`fetching arbitrary app db on ${app}`) |
16 | | - const {body: addons} = await heroku.get<Heroku.AddOn[]>(`/apps/${app}/addons`) |
17 | | - const addon = addons.find(a => a?.app?.name === app && a?.plan?.name?.startsWith('heroku-postgresql')) |
18 | | - if (!addon) throw new Error(`No heroku-postgresql databases on ${app}`) |
19 | | - return addon |
20 | | -} |
21 | | -
|
22 | | -function getAttachmentNamesByAddon(attachments: ExtendedAddonAttachment[]): Record<string, string[]> { |
23 | | - return attachments.reduce((results: any, a) => { |
24 | | - results[a.addon.id] = (results[a.addon.id] || []).concat(a.name) |
25 | | - return results |
26 | | - }, {}) |
27 | | -} |
28 | | -
|
29 | | -export async function all(heroku: APIClient, app_id: string): Promise<Array<ExtendedAddonAttachment['addon'] & {attachment_names?: string[]}>> { |
30 | | - pgDebug(`fetching all DBs on ${app_id}`) |
31 | | -
|
32 | | - const attachments = await allAttachments(heroku, app_id) |
33 | | - let addons: Array<ExtendedAddonAttachment['addon'] & {attachment_names?: string[]}> = attachments.map(a => a.addon) |
34 | | -
|
35 | | - // Get the list of attachment names per addon here and add to each addon obj |
36 | | - const attachmentNamesByAddon = getAttachmentNamesByAddon(attachments) |
37 | | - addons = uniqBy(addons, 'id') |
38 | | - addons.forEach(addon => { |
39 | | - addon.attachment_names = attachmentNamesByAddon[addon.id] |
40 | | - }) |
41 | | -
|
42 | | - return addons |
43 | | -} |
44 | | -
|
45 | | -async function allAttachments(heroku: APIClient, app_id: string): Promise<ExtendedAddonAttachment[]> { |
46 | | - const {body: attachments} = await heroku.get<ExtendedAddonAttachment[]>(`/apps/${app_id}/addon-attachments`, { |
47 | | - headers: {'Accept-Inclusion': 'addon:plan,config_vars'}, |
48 | | - }) |
49 | | - return attachments.filter((a: ExtendedAddonAttachment) => a.addon.plan?.name?.startsWith('heroku-postgresql')) |
50 | | -} |
51 | 4 |
|
52 | 5 | export async function getRelease(heroku: APIClient, appName: string, id: string) { |
53 | 6 | const {body: release} = await heroku.get<Heroku.Release>(`/apps/${appName}/releases/${id}`) |
|
0 commit comments