Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions packages/cli/src/lib/pg/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,6 @@
/*
import {APIClient} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {ExtendedAddonAttachment} from '@heroku/heroku-cli-util'
import debug from 'debug'
import {uniqBy} from 'lodash'

const pgDebug = debug('pg')

export async function arbitraryAppDB(heroku: APIClient, app: string) {
// Since Postgres backups are tied to the app and not the add-on, but
// we require *an* add-on to interact with, make sure that add-on is
// attached to the right app.

pgDebug(`fetching arbitrary app db on ${app}`)
const {body: addons} = await heroku.get<Heroku.AddOn[]>(`/apps/${app}/addons`)
const addon = addons.find(a => a?.app?.name === app && a?.plan?.name?.startsWith('heroku-postgresql'))
if (!addon) throw new Error(`No heroku-postgresql databases on ${app}`)
return addon
}

function getAttachmentNamesByAddon(attachments: ExtendedAddonAttachment[]): Record<string, string[]> {
return attachments.reduce((results: any, a) => {
results[a.addon.id] = (results[a.addon.id] || []).concat(a.name)
return results
}, {})
}

export async function all(heroku: APIClient, app_id: string): Promise<Array<ExtendedAddonAttachment['addon'] & {attachment_names?: string[]}>> {
pgDebug(`fetching all DBs on ${app_id}`)

const attachments = await allAttachments(heroku, app_id)
let addons: Array<ExtendedAddonAttachment['addon'] & {attachment_names?: string[]}> = attachments.map(a => a.addon)

// Get the list of attachment names per addon here and add to each addon obj
const attachmentNamesByAddon = getAttachmentNamesByAddon(attachments)
addons = uniqBy(addons, 'id')
addons.forEach(addon => {
addon.attachment_names = attachmentNamesByAddon[addon.id]
})

return addons
}

async function allAttachments(heroku: APIClient, app_id: string): Promise<ExtendedAddonAttachment[]> {
const {body: attachments} = await heroku.get<ExtendedAddonAttachment[]>(`/apps/${app_id}/addon-attachments`, {
headers: {'Accept-Inclusion': 'addon:plan,config_vars'},
})
return attachments.filter((a: ExtendedAddonAttachment) => a.addon.plan?.name?.startsWith('heroku-postgresql'))
}

export async function getRelease(heroku: APIClient, appName: string, id: string) {
const {body: release} = await heroku.get<Heroku.Release>(`/apps/${appName}/releases/${id}`)
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/oldCommands/pg/backups/schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Command, flags} from '@heroku-cli/command'
import {ux} from '@oclif/core'
import {hux} from '@heroku/heroku-cli-util'
import {utils} from '@heroku/heroku-cli-util'
import {arbitraryAppDB} from '../../../lib/pg/fetcher'
import type {TransferSchedule} from '../../../lib/pg/types'

export default class Schedules extends Command {
Expand All @@ -18,7 +17,8 @@ export default class Schedules extends Command {
public async run(): Promise<void> {
const {flags} = await this.parse(Schedules)
const {app} = flags
const db = await arbitraryAppDB(this.heroku, app)
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
const db = await dbResolver.getArbitraryLegacyDB(app)
const {body: schedules} = await this.heroku.get<TransferSchedule[]>(`/client/v11/databases/${db.id}/transfer-schedules`, {hostname: utils.pg.host()})
if (schedules.length === 0) {
ux.warn(`No backup schedules found on ${color.app(app)}\nUse ${color.cyan.bold('heroku pg:backups:schedule')} to set one up`)
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/oldCommands/pg/backups/unschedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import {arbitraryAppDB} from '../../../lib/pg/fetcher'
import {utils} from '@heroku/heroku-cli-util'
import {TransferSchedule} from '../../../lib/pg/types'
import {nls} from '../../../nls'
Expand All @@ -25,7 +24,8 @@ export default class Unschedule extends Command {
const {database} = args
let db = database
if (!db) {
const appDB = await arbitraryAppDB(this.heroku, app)
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
const appDB = await dbResolver.getArbitraryLegacyDB(app)
const {body: schedules} = await this.heroku.get<TransferSchedule[]>(
`/client/v11/databases/${appDB.id}/transfer-schedules`,
{hostname: utils.pg.host()},
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/oldCommands/pg/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import {ExtendedAddonAttachment, hux} from '@heroku/heroku-cli-util'
import * as Heroku from '@heroku-cli/schema'
import {all} from '../../lib/pg/fetcher'
import {configVarNamesFromValue, databaseNameFromUrl} from '../../lib/pg/util'
import {PgDatabaseTenant} from '../../lib/pg/types'
import {nls} from '../../nls'
Expand Down Expand Up @@ -75,7 +74,8 @@ export default class Info extends Command {
const {addon} = await dbResolver.getAttachment(app, db)
addons = [addon]
} else {
addons = await all(this.heroku, app)
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
addons = await dbResolver.getAllLegacyDatabases(app)
if (addons.length === 0) {
ux.log(`${color.magenta(app)} has no heroku-postgresql databases.`)
return
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/oldCommands/pg/links/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import {hux, utils} from '@heroku/heroku-cli-util'
import {all} from '../../../lib/pg/fetcher'
import type {Link} from '../../../lib/pg/types'
import {nls} from '../../../nls'

Expand All @@ -23,13 +22,18 @@ export default class Index extends Command {
const {flags, args} = await this.parse(Index)
const {app} = flags
const {database} = args
let dbs: Array<(Awaited<ReturnType<typeof all>>)[number] & {links?: Link[]}>
type all = typeof utils.pg.DatabaseResolver.prototype.getAllLegacyDatabases

let dbs: Array<(Awaited<ReturnType<all>>)[number] & {links?: Link[]}>
if (database) {
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
const {addon} = await dbResolver.getAttachment(app, database)
dbs = [addon]
} else
dbs = await all(this.heroku, app)
} else {
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
dbs = await dbResolver.getAllLegacyDatabases(app)
}

if (dbs.length === 0)
throw new Error(`No databases on ${color.app(app)}`)
dbs = await Promise.all(dbs.map(async db => {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/oldCommands/pg/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import debug from 'debug'
import {ExtendedAddonAttachment, utils} from '@heroku/heroku-cli-util'
import {all} from '../../lib/pg/fetcher'
import notify from '../../lib/notify'
import {PgStatus} from '../../lib/pg/types'
import {HTTPError} from '@heroku/http-call'
Expand Down Expand Up @@ -86,7 +85,8 @@ export default class Wait extends Command {
const {addon} = await dbResolver.getAttachment(app, dbName)
dbs = [addon]
} else {
dbs = await all(this.heroku, app)
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
dbs = await dbResolver.getAllLegacyDatabases(app)
}

for (const db of dbs) {
Expand Down
Loading