diff --git a/packages/cli/src/lib/pg/fetcher.ts b/packages/cli/src/lib/pg/fetcher.ts index 66f8b8baf7..df0e30c8cf 100644 --- a/packages/cli/src/lib/pg/fetcher.ts +++ b/packages/cli/src/lib/pg/fetcher.ts @@ -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(`/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 { - 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> { - pgDebug(`fetching all DBs on ${app_id}`) - - const attachments = await allAttachments(heroku, app_id) - let addons: Array = 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 { - const {body: attachments} = await heroku.get(`/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(`/apps/${appName}/releases/${id}`) diff --git a/packages/cli/src/oldCommands/pg/backups/schedules.ts b/packages/cli/src/oldCommands/pg/backups/schedules.ts index 0879b28e31..1182fd9071 100644 --- a/packages/cli/src/oldCommands/pg/backups/schedules.ts +++ b/packages/cli/src/oldCommands/pg/backups/schedules.ts @@ -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 { @@ -18,7 +17,8 @@ export default class Schedules extends Command { public async run(): Promise { 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(`/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`) diff --git a/packages/cli/src/oldCommands/pg/backups/unschedule.ts b/packages/cli/src/oldCommands/pg/backups/unschedule.ts index 736f07d7ed..f3c025eec9 100644 --- a/packages/cli/src/oldCommands/pg/backups/unschedule.ts +++ b/packages/cli/src/oldCommands/pg/backups/unschedule.ts @@ -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' @@ -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( `/client/v11/databases/${appDB.id}/transfer-schedules`, {hostname: utils.pg.host()}, diff --git a/packages/cli/src/oldCommands/pg/info.ts b/packages/cli/src/oldCommands/pg/info.ts index 69622dc2df..09f888acba 100644 --- a/packages/cli/src/oldCommands/pg/info.ts +++ b/packages/cli/src/oldCommands/pg/info.ts @@ -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' @@ -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 diff --git a/packages/cli/src/oldCommands/pg/links/index.ts b/packages/cli/src/oldCommands/pg/links/index.ts index 2cb88b853b..ee1c938bf2 100644 --- a/packages/cli/src/oldCommands/pg/links/index.ts +++ b/packages/cli/src/oldCommands/pg/links/index.ts @@ -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' @@ -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>)[number] & {links?: Link[]}> + type all = typeof utils.pg.DatabaseResolver.prototype.getAllLegacyDatabases + + let dbs: Array<(Awaited>)[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 => { diff --git a/packages/cli/src/oldCommands/pg/wait.ts b/packages/cli/src/oldCommands/pg/wait.ts index 6b3fdff05c..577baefd4d 100644 --- a/packages/cli/src/oldCommands/pg/wait.ts +++ b/packages/cli/src/oldCommands/pg/wait.ts @@ -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' @@ -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) {