From 858f70e32f01c02113dd8c51e992952c4b6ea4c3 Mon Sep 17 00:00:00 2001 From: Justin Wilaby Date: Wed, 20 Mar 2024 16:35:05 -0700 Subject: [PATCH 1/4] migrate(W-14170373): Upgrade --- .../cli/src/commands/pg/backups/delete.ts | 31 ++++++++++++++ .../unit/commands/backups/delete.unit.test.ts | 40 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 packages/cli/src/commands/pg/backups/delete.ts create mode 100644 packages/cli/test/unit/commands/backups/delete.unit.test.ts diff --git a/packages/cli/src/commands/pg/backups/delete.ts b/packages/cli/src/commands/pg/backups/delete.ts new file mode 100644 index 0000000000..57021dde8b --- /dev/null +++ b/packages/cli/src/commands/pg/backups/delete.ts @@ -0,0 +1,31 @@ +import color from '@heroku-cli/color' +import {Command, flags} from '@heroku-cli/command' +import {Args, ux} from '@oclif/core' +import * as Heroku from '@heroku-cli/schema' +import {host} from '../lib/host' + +export default class Delete extends Command { + static topic = 'pg'; + static description = 'delete a backup'; + static flags = { + confirm: flags.string({char: 'c'}), + app: flags.app({required: true}), + }; + + static args = { + backup_id: Args.string({required: true}), + }; + + public async run(): Promise { + const {flags, argv, args} = await this.parse(Delete) + const pgbackups = require('../../lib/pgbackups')(context, heroku) + const {app, args, flags} = context + await cli.confirmApp(app, flags.confirm) + await ux.action(`Deleting backup ${color.cyan(args.backup_id)} on ${color.magenta(app)}`, (async function () { + let num = await pgbackups.transfer.num(args.backup_id) + if (!num) + throw new Error(`Invalid Backup: ${args.backup_id}`) + await this.heroku.delete(`/client/v11/apps/${app}/transfers/${num}`, {host}) + })()) + } +} diff --git a/packages/cli/test/unit/commands/backups/delete.unit.test.ts b/packages/cli/test/unit/commands/backups/delete.unit.test.ts new file mode 100644 index 0000000000..2758930ea9 --- /dev/null +++ b/packages/cli/test/unit/commands/backups/delete.unit.test.ts @@ -0,0 +1,40 @@ +import {stdout, stderr} from 'stdout-stderr' +import Cmd from 'REPLACE_WITH_PATH_TO_COMMAND' +import runCommand from '../../../helpers/runCommand' +const cli = require('heroku-cli-util') +const {expect} = require('chai') +const nock = require('nock') +const cmd = require('../../../..').commands.find(c => c.topic === 'pg' && c.command === 'backups:delete') +const shouldDelete = function (cmdRun) { + let pg + beforeEach(() => { + pg = nock('https://api.data.heroku.com') + pg.delete('/client/v11/apps/myapp/transfers/3') + .reply(200, { + url: 'https://dburl', + }) + cli.mockConsole() + }) + afterEach(() => { + nock.cleanAll() + pg.done() + }) + it('shows URL', () => { + return runCommand(Cmd, [ + '--app', + 'myapp', + '--confirm', + 'myapp', + 'b003', + ]) + .then(() => expect(stderr.output).to.equal('Deleting backup b003 on myapp... done\n')) + }) +} + +describe('pg:backups:delete', () => { + shouldDelete(args => cmd.run(args)) +}) +describe('pg:backups delete', () => { + shouldDelete(require('./helpers.js') + .dup('delete', cmd)) +}) From 5a7cdad413f523c1cd4e25ec410f2bb7bab369cc Mon Sep 17 00:00:00 2001 From: Justin Wilaby Date: Thu, 21 Mar 2024 08:11:48 -0700 Subject: [PATCH 2/4] migrate(W-14170373): Upgrade pg:backups:delete --- packages/cli/package.json | 1 + .../cli/src/commands/pg/backups/delete.ts | 55 +++++++++++-------- packages/cli/src/lib/pg/backups.ts | 2 +- .../unit/commands/backups/delete.unit.test.ts | 40 -------------- .../commands/pg/backups/delete.unit.test.ts | 33 +++++++++++ packages/pg-v5/commands/backups/delete.js | 30 ---------- packages/pg-v5/index.js | 1 - .../unit/commands/backups/delete.unit.test.js | 37 ------------- yarn.lock | 8 +++ 9 files changed, 76 insertions(+), 131 deletions(-) delete mode 100644 packages/cli/test/unit/commands/backups/delete.unit.test.ts create mode 100644 packages/cli/test/unit/commands/pg/backups/delete.unit.test.ts delete mode 100644 packages/pg-v5/commands/backups/delete.js delete mode 100644 packages/pg-v5/test/unit/commands/backups/delete.unit.test.js diff --git a/packages/cli/package.json b/packages/cli/package.json index f83931dcb5..2880c07310 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -92,6 +92,7 @@ "@heroku-cli/schema": "^1.0.25", "@oclif/test": "^2.3.25", "@types/ansi-styles": "^3.2.1", + "@types/bytes": "^3.1.4", "@types/chai": "^4.1.7", "@types/chai-as-promised": "^7.1.8", "@types/debug": "^4.1.2", diff --git a/packages/cli/src/commands/pg/backups/delete.ts b/packages/cli/src/commands/pg/backups/delete.ts index 57021dde8b..d3e6f0fd58 100644 --- a/packages/cli/src/commands/pg/backups/delete.ts +++ b/packages/cli/src/commands/pg/backups/delete.ts @@ -1,31 +1,42 @@ import color from '@heroku-cli/color' import {Command, flags} from '@heroku-cli/command' import {Args, ux} from '@oclif/core' -import * as Heroku from '@heroku-cli/schema' -import {host} from '../lib/host' +import confirmApp from '../../../lib/apps/confirm-app' +import host from '../../../lib/pg/host' +import backupsFactory from '../../../lib/pg/backups' export default class Delete extends Command { - static topic = 'pg'; - static description = 'delete a backup'; - static flags = { - confirm: flags.string({char: 'c'}), - app: flags.app({required: true}), - }; + static topic = 'pg' + static description = 'delete a backup' + static flags = { + confirm: flags.string({char: 'c'}), + app: flags.app({required: true}), + remote: flags.remote(), + } - static args = { - backup_id: Args.string({required: true}), - }; + static args = { + backup_id: Args.string({required: true}), + } - public async run(): Promise { - const {flags, argv, args} = await this.parse(Delete) - const pgbackups = require('../../lib/pgbackups')(context, heroku) - const {app, args, flags} = context - await cli.confirmApp(app, flags.confirm) - await ux.action(`Deleting backup ${color.cyan(args.backup_id)} on ${color.magenta(app)}`, (async function () { - let num = await pgbackups.transfer.num(args.backup_id) - if (!num) - throw new Error(`Invalid Backup: ${args.backup_id}`) - await this.heroku.delete(`/client/v11/apps/${app}/transfers/${num}`, {host}) - })()) + static examples = [ + '$ heroku pg:backup:delete --app APP_ID BACKUP_ID', + ] + + public async run(): Promise { + const {flags, args} = await this.parse(Delete) + const {app, confirm} = flags + const {backup_id} = args + const pgbackups = backupsFactory(app, this.heroku) + + await confirmApp(app, confirm) + ux.action.start(`Deleting backup ${color.cyan(backup_id)} on ${color.magenta(app)}`) + + const num = await pgbackups.transfer.num(backup_id) + if (!num) { + throw new Error(`Invalid Backup: ${backup_id}`) } + + await this.heroku.delete(`/client/v11/apps/${app}/transfers/${num}`, {hostname: host()}) + ux.action.stop() + } } diff --git a/packages/cli/src/lib/pg/backups.ts b/packages/cli/src/lib/pg/backups.ts index 4d15e66fc8..404fa161de 100644 --- a/packages/cli/src/lib/pg/backups.ts +++ b/packages/cli/src/lib/pg/backups.ts @@ -1,6 +1,6 @@ import {APIClient} from '@heroku-cli/command' import pgHost from './host' -const bytes = require('bytes') +import bytes = require('bytes') export type BackupTransfer = { num: number, diff --git a/packages/cli/test/unit/commands/backups/delete.unit.test.ts b/packages/cli/test/unit/commands/backups/delete.unit.test.ts deleted file mode 100644 index 2758930ea9..0000000000 --- a/packages/cli/test/unit/commands/backups/delete.unit.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import {stdout, stderr} from 'stdout-stderr' -import Cmd from 'REPLACE_WITH_PATH_TO_COMMAND' -import runCommand from '../../../helpers/runCommand' -const cli = require('heroku-cli-util') -const {expect} = require('chai') -const nock = require('nock') -const cmd = require('../../../..').commands.find(c => c.topic === 'pg' && c.command === 'backups:delete') -const shouldDelete = function (cmdRun) { - let pg - beforeEach(() => { - pg = nock('https://api.data.heroku.com') - pg.delete('/client/v11/apps/myapp/transfers/3') - .reply(200, { - url: 'https://dburl', - }) - cli.mockConsole() - }) - afterEach(() => { - nock.cleanAll() - pg.done() - }) - it('shows URL', () => { - return runCommand(Cmd, [ - '--app', - 'myapp', - '--confirm', - 'myapp', - 'b003', - ]) - .then(() => expect(stderr.output).to.equal('Deleting backup b003 on myapp... done\n')) - }) -} - -describe('pg:backups:delete', () => { - shouldDelete(args => cmd.run(args)) -}) -describe('pg:backups delete', () => { - shouldDelete(require('./helpers.js') - .dup('delete', cmd)) -}) diff --git a/packages/cli/test/unit/commands/pg/backups/delete.unit.test.ts b/packages/cli/test/unit/commands/pg/backups/delete.unit.test.ts new file mode 100644 index 0000000000..e5a308f6f0 --- /dev/null +++ b/packages/cli/test/unit/commands/pg/backups/delete.unit.test.ts @@ -0,0 +1,33 @@ +import {stderr} from 'stdout-stderr' +import Cmd from '../../../../../src/commands/pg/backups/delete' +import runCommand from '../../../../helpers/runCommand' +import {expect} from 'chai' +import * as nock from 'nock' + +describe('pg:backups:delete', () => { + let pg: nock.Scope + + beforeEach(() => { + pg = nock('https://api.data.heroku.com') + .delete('/client/v11/apps/myapp/transfers/3') + .reply(200, { + url: 'https://dburl', + }) + }) + + afterEach(() => { + nock.cleanAll() + pg.done() + }) + + it('shows URL', async () => { + await runCommand(Cmd, [ + '--app', + 'myapp', + '--confirm', + 'myapp', + 'b003', + ]) + expect(stderr.output).to.equal('Deleting backup b003 on myapp...\nDeleting backup b003 on myapp... done\n') + }) +}) diff --git a/packages/pg-v5/commands/backups/delete.js b/packages/pg-v5/commands/backups/delete.js deleted file mode 100644 index 8043c01d5e..0000000000 --- a/packages/pg-v5/commands/backups/delete.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict' - -const cli = require('heroku-cli-util') - -async function run(context, heroku) { - const host = require('../../lib/host')() - const pgbackups = require('../../lib/pgbackups')(context, heroku) - - const {app, args, flags} = context - - await cli.confirmApp(app, flags.confirm) - - await cli.action(`Deleting backup ${cli.color.cyan(args.backup_id)} on ${cli.color.app(app)}`, (async function () { - let num = await pgbackups.transfer.num(args.backup_id) - if (!num) throw new Error(`Invalid Backup: ${args.backup_id}`) - - await heroku.delete(`/client/v11/apps/${app}/transfers/${num}`, {host}) - })()) -} - -module.exports = { - topic: 'pg', - command: 'backups:delete', - description: 'delete a backup', - needsApp: true, - needsAuth: true, - args: [{name: 'backup_id'}], - flags: [{name: 'confirm', char: 'c', hasValue: true}], - run: cli.command({preauth: true}, run), -} diff --git a/packages/pg-v5/index.js b/packages/pg-v5/index.js index 8d61c62b79..244e1166d9 100644 --- a/packages/pg-v5/index.js +++ b/packages/pg-v5/index.js @@ -10,7 +10,6 @@ exports.commands = flatten([ require('./commands/backups'), require('./commands/backups/cancel'), require('./commands/backups/capture'), - require('./commands/backups/delete'), require('./commands/backups/download'), require('./commands/backups/info'), require('./commands/backups/restore'), diff --git a/packages/pg-v5/test/unit/commands/backups/delete.unit.test.js b/packages/pg-v5/test/unit/commands/backups/delete.unit.test.js deleted file mode 100644 index faba50298f..0000000000 --- a/packages/pg-v5/test/unit/commands/backups/delete.unit.test.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict' -/* global beforeEach afterEach */ - -const cli = require('heroku-cli-util') -const {expect} = require('chai') -const nock = require('nock') -const cmd = require('../../../..').commands.find(c => c.topic === 'pg' && c.command === 'backups:delete') - -const shouldDelete = function (cmdRun) { - let pg - - beforeEach(() => { - pg = nock('https://api.data.heroku.com') - pg.delete('/client/v11/apps/myapp/transfers/3').reply(200, { - url: 'https://dburl', - }) - cli.mockConsole() - }) - - afterEach(() => { - nock.cleanAll() - pg.done() - }) - - it('shows URL', () => { - return cmd.run({app: 'myapp', args: {backup_id: 'b003'}, flags: {confirm: 'myapp'}}) - .then(() => expect(cli.stderr).to.equal('Deleting backup b003 on myapp... done\n')) - }) -} - -describe('pg:backups:delete', () => { - shouldDelete(args => cmd.run(args)) -}) - -describe('pg:backups delete', () => { - shouldDelete(require('./helpers.js').dup('delete', cmd)) -}) diff --git a/yarn.lock b/yarn.lock index 76b3982a10..708e2b86f6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5037,6 +5037,13 @@ __metadata: languageName: node linkType: hard +"@types/bytes@npm:^3.1.4": + version: 3.1.4 + resolution: "@types/bytes@npm:3.1.4" + checksum: 645732b37404847df9db7ae2af8b4bbdc21c966e9bbf8a5494d7fa95cbaee8bd7b9d65c68bbc2ec4277856d874e2e011b3b2b9479d981cd57d5136b0f41526e9 + languageName: node + linkType: hard + "@types/cacheable-request@npm:^6.0.1": version: 6.0.3 resolution: "@types/cacheable-request@npm:6.0.3" @@ -10971,6 +10978,7 @@ __metadata: "@opentelemetry/sdk-trace-node": ^1.15.1 "@opentelemetry/semantic-conventions": ^1.15.1 "@types/ansi-styles": ^3.2.1 + "@types/bytes": ^3.1.4 "@types/chai": ^4.1.7 "@types/chai-as-promised": ^7.1.8 "@types/debug": ^4.1.2 From ae1692dbcb14597616f9c7be3f8e18d844e2e9e1 Mon Sep 17 00:00:00 2001 From: Justin Wilaby Date: Thu, 21 Mar 2024 11:30:55 -0700 Subject: [PATCH 3/4] updates based on PR feedback --- packages/cli/src/commands/pg/backups/delete.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/commands/pg/backups/delete.ts b/packages/cli/src/commands/pg/backups/delete.ts index d3e6f0fd58..ab8b57f87d 100644 --- a/packages/cli/src/commands/pg/backups/delete.ts +++ b/packages/cli/src/commands/pg/backups/delete.ts @@ -29,7 +29,7 @@ export default class Delete extends Command { const pgbackups = backupsFactory(app, this.heroku) await confirmApp(app, confirm) - ux.action.start(`Deleting backup ${color.cyan(backup_id)} on ${color.magenta(app)}`) + ux.action.start(`Deleting backup ${color.cyan(backup_id)} on ${color.app(app)}`) const num = await pgbackups.transfer.num(backup_id) if (!num) { From 10ebad6700450079d1824b6784f027ab65f48f07 Mon Sep 17 00:00:00 2001 From: Justin Wilaby Date: Thu, 21 Mar 2024 13:37:47 -0700 Subject: [PATCH 4/4] Fix unit test after rebase --- packages/cli/test/unit/commands/pg/backups/delete.unit.test.ts | 2 +- packages/pg-v5/test/init.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/test/unit/commands/pg/backups/delete.unit.test.ts b/packages/cli/test/unit/commands/pg/backups/delete.unit.test.ts index e5a308f6f0..48235c91fc 100644 --- a/packages/cli/test/unit/commands/pg/backups/delete.unit.test.ts +++ b/packages/cli/test/unit/commands/pg/backups/delete.unit.test.ts @@ -28,6 +28,6 @@ describe('pg:backups:delete', () => { 'myapp', 'b003', ]) - expect(stderr.output).to.equal('Deleting backup b003 on myapp...\nDeleting backup b003 on myapp... done\n') + expect(stderr.output).to.equal('Deleting backup b003 on ⬢ myapp...\nDeleting backup b003 on ⬢ myapp... done\n') }) }) diff --git a/packages/pg-v5/test/init.js b/packages/pg-v5/test/init.js index 9a9c85b466..f752981242 100644 --- a/packages/pg-v5/test/init.js +++ b/packages/pg-v5/test/init.js @@ -8,7 +8,7 @@ chai.use(chaiAsPromised) let cli = require('heroku-cli-util') // Load heroku-cli-util helpers cli.raiseErrors = true // Fully raise exceptions process.env.TZ = 'UTC' // Use UTC time always - +process.env.IS_DEV_ENVIRONMENT = 'true' process.stdout.columns = 80 process.stderr.columns = 80