Skip to content

Commit

Permalink
Convert pg:unfollow to oclif
Browse files Browse the repository at this point in the history
  • Loading branch information
eablack committed Apr 19, 2024
1 parent 04a811e commit f8bb80a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 83 deletions.
42 changes: 42 additions & 0 deletions packages/cli/src/commands/pg/unfollow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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 {getAddon} from '../../lib/pg/fetcher'
import pgHost from '../../lib/pg/host'
import {databaseNameFromUrl} from '../../lib/pg/util'
import confirmCommand from '../../lib/confirmCommand'
import {PgDatabase} from '../../lib/pg/types'
import heredoc from 'tsheredoc'

export default class Unfollow extends Command {
static topic = 'pg';
static description = 'stop a replica from following and make it a writeable database';
static flags = {
confirm: flags.string({char: 'c'}),
app: flags.app({required: true}),
remote: flags.remote(),
}

static args = {
database: Args.string({required: true}),
}

public async run(): Promise<void> {
const {flags, args} = await this.parse(Unfollow)
const {app, confirm} = flags
const db = await getAddon(this.heroku, app, args.database)
const {body: replica} = await this.heroku.get<PgDatabase>(`/client/v11/databases/${db.id}`, {hostname: pgHost()})
if (!replica.following)
ux.error(`${color.addon(db.name)} is not a follower`)
const {body: configVars} = await this.heroku.get<Heroku.ConfigVars>(`/apps/${app}/config-vars`)
const origin = databaseNameFromUrl(replica.following as string, configVars)
await confirmCommand(app, confirm, heredoc(`
Destructive action
${color.addon(db.name)} will become writeable and no longer follow ${origin}. This cannot be undone.
`))
ux.action.start(`${color.addon(db.name)} unfollowing`)
await this.heroku.put(`/client/v11/databases/${db.id}/unfollow`, {hostname: pgHost()})
ux.action.stop()
}
}
42 changes: 42 additions & 0 deletions packages/cli/test/unit/commands/pg/unfollow.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {stderr} from 'stdout-stderr'
import * as nock from 'nock'
import Cmd from '../../../../src/commands/pg/unfollow'
import runCommand from '../../../helpers/runCommand'
import expectOutput from '../../../helpers/utils/expectOutput'
import heredoc from 'tsheredoc'
import * as fixtures from '../../../fixtures/addons/fixtures'

describe('pg:unfollow', () => {
const addon = fixtures.addons['dwh-db']
const appName = 'myapp'

afterEach(() => {
nock.cleanAll()
})

it('unfollows db', async () => {
nock('https://api.heroku.com')
.post('/actions/addon-attachments/resolve')
.reply(200, [{addon}])
nock('https://api.heroku.com')
.get(`/apps/${appName}/config-vars`)
.reply(200, {DATABASE_URL: 'postgres://db1'})
nock('https://api.data.heroku.com')
.get(`/client/v11/databases/${addon.id}`)
.reply(200, {following: 'postgres://db1'})
.put(`/client/v11/databases/${addon.id}/unfollow`)
.reply(200)

await runCommand(Cmd, [
'--app',
appName,
'--confirm',
appName,
'DATABASE',
])
expectOutput(stderr.output, heredoc(`
${addon.name} unfollowing...
${addon.name} unfollowing... done
`))
})
})
35 changes: 0 additions & 35 deletions packages/pg-v5/commands/unfollow.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/pg-v5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ exports.commands = flatten([
require('./commands/psql'),
require('./commands/pull'),
require('./commands/repoint'),
require('./commands/unfollow'),
require('./commands/vacuum_stats'),
require('./commands/wait'),
])
Expand Down
47 changes: 0 additions & 47 deletions packages/pg-v5/test/unit/commands/unfollow.unit.test.js

This file was deleted.

0 comments on commit f8bb80a

Please sign in to comment.