Skip to content

Commit 1aa78b5

Browse files
chore: replace pg/fetcher functions with those provided by heroku-cli-utils (#3442)
2 parents 492b111 + b992d15 commit 1aa78b5

File tree

6 files changed

+16
-59
lines changed

6 files changed

+16
-59
lines changed

packages/cli/src/lib/pg/fetcher.ts

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,6 @@
11
/*
22
import {APIClient} from '@heroku-cli/command'
33
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-
}
514
525
export async function getRelease(heroku: APIClient, appName: string, id: string) {
536
const {body: release} = await heroku.get<Heroku.Release>(`/apps/${appName}/releases/${id}`)

packages/cli/src/oldCommands/pg/backups/schedules.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {Command, flags} from '@heroku-cli/command'
44
import {ux} from '@oclif/core'
55
import {hux} from '@heroku/heroku-cli-util'
66
import {utils} from '@heroku/heroku-cli-util'
7-
import {arbitraryAppDB} from '../../../lib/pg/fetcher'
87
import type {TransferSchedule} from '../../../lib/pg/types'
98
109
export default class Schedules extends Command {
@@ -18,7 +17,8 @@ export default class Schedules extends Command {
1817
public async run(): Promise<void> {
1918
const {flags} = await this.parse(Schedules)
2019
const {app} = flags
21-
const db = await arbitraryAppDB(this.heroku, app)
20+
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
21+
const db = await dbResolver.getArbitraryLegacyDB(app)
2222
const {body: schedules} = await this.heroku.get<TransferSchedule[]>(`/client/v11/databases/${db.id}/transfer-schedules`, {hostname: utils.pg.host()})
2323
if (schedules.length === 0) {
2424
ux.warn(`No backup schedules found on ${color.app(app)}\nUse ${color.cyan.bold('heroku pg:backups:schedule')} to set one up`)

packages/cli/src/oldCommands/pg/backups/unschedule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import color from '@heroku-cli/color'
33
import {Command, flags} from '@heroku-cli/command'
44
import {Args, ux} from '@oclif/core'
5-
import {arbitraryAppDB} from '../../../lib/pg/fetcher'
65
import {utils} from '@heroku/heroku-cli-util'
76
import {TransferSchedule} from '../../../lib/pg/types'
87
import {nls} from '../../../nls'
@@ -25,7 +24,8 @@ export default class Unschedule extends Command {
2524
const {database} = args
2625
let db = database
2726
if (!db) {
28-
const appDB = await arbitraryAppDB(this.heroku, app)
27+
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
28+
const appDB = await dbResolver.getArbitraryLegacyDB(app)
2929
const {body: schedules} = await this.heroku.get<TransferSchedule[]>(
3030
`/client/v11/databases/${appDB.id}/transfer-schedules`,
3131
{hostname: utils.pg.host()},

packages/cli/src/oldCommands/pg/info.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {Command, flags} from '@heroku-cli/command'
44
import {Args, ux} from '@oclif/core'
55
import {ExtendedAddonAttachment, hux} from '@heroku/heroku-cli-util'
66
import * as Heroku from '@heroku-cli/schema'
7-
import {all} from '../../lib/pg/fetcher'
87
import {configVarNamesFromValue, databaseNameFromUrl} from '../../lib/pg/util'
98
import {PgDatabaseTenant} from '../../lib/pg/types'
109
import {nls} from '../../nls'
@@ -75,7 +74,8 @@ export default class Info extends Command {
7574
const {addon} = await dbResolver.getAttachment(app, db)
7675
addons = [addon]
7776
} else {
78-
addons = await all(this.heroku, app)
77+
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
78+
addons = await dbResolver.getAllLegacyDatabases(app)
7979
if (addons.length === 0) {
8080
ux.log(`${color.magenta(app)} has no heroku-postgresql databases.`)
8181
return

packages/cli/src/oldCommands/pg/links/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import color from '@heroku-cli/color'
33
import {Command, flags} from '@heroku-cli/command'
44
import {Args, ux} from '@oclif/core'
55
import {hux, utils} from '@heroku/heroku-cli-util'
6-
import {all} from '../../../lib/pg/fetcher'
76
import type {Link} from '../../../lib/pg/types'
87
import {nls} from '../../../nls'
98
@@ -23,13 +22,18 @@ export default class Index extends Command {
2322
const {flags, args} = await this.parse(Index)
2423
const {app} = flags
2524
const {database} = args
26-
let dbs: Array<(Awaited<ReturnType<typeof all>>)[number] & {links?: Link[]}>
25+
type all = typeof utils.pg.DatabaseResolver.prototype.getAllLegacyDatabases
26+
27+
let dbs: Array<(Awaited<ReturnType<all>>)[number] & {links?: Link[]}>
2728
if (database) {
2829
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
2930
const {addon} = await dbResolver.getAttachment(app, database)
3031
dbs = [addon]
31-
} else
32-
dbs = await all(this.heroku, app)
32+
} else {
33+
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
34+
dbs = await dbResolver.getAllLegacyDatabases(app)
35+
}
36+
3337
if (dbs.length === 0)
3438
throw new Error(`No databases on ${color.app(app)}`)
3539
dbs = await Promise.all(dbs.map(async db => {

packages/cli/src/oldCommands/pg/wait.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {Command, flags} from '@heroku-cli/command'
44
import {Args, ux} from '@oclif/core'
55
import debug from 'debug'
66
import {ExtendedAddonAttachment, utils} from '@heroku/heroku-cli-util'
7-
import {all} from '../../lib/pg/fetcher'
87
import notify from '../../lib/notify'
98
import {PgStatus} from '../../lib/pg/types'
109
import {HTTPError} from '@heroku/http-call'
@@ -86,7 +85,8 @@ export default class Wait extends Command {
8685
const {addon} = await dbResolver.getAttachment(app, dbName)
8786
dbs = [addon]
8887
} else {
89-
dbs = await all(this.heroku, app)
88+
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
89+
dbs = await dbResolver.getAllLegacyDatabases(app)
9090
}
9191
9292
for (const db of dbs) {

0 commit comments

Comments
 (0)