Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(pg-v5): Move command pg:settings:auto-explain:log-buffers to oclif #2775

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {Args} from '@oclif/core'
import heredoc from 'tsheredoc'
import {PGSettingsCommand, type Setting, type SettingKey, booleanConverter} from '../../../../lib/pg/setter'

export default class LogBuffersWaits extends PGSettingsCommand {
static topic = 'pg'
static description = heredoc(`
Includes buffer usage statistics when execution plans are logged.
This is equivalent to calling EXPLAIN BUFFERS and can only be used in conjunction with pg:settings:auto-explain:log-analyze turned on.
`)

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

protected settingKey: SettingKey = 'auto_explain.log_buffers'

protected convertValue(val: boolean): boolean {
return booleanConverter(val)
}

protected explain(setting: Setting) {
if (setting.value) {
return 'Buffer statistics have been enabled for auto_explain.'
}

return 'Buffer statistics have been disabled for auto_explain.'
}
}
9 changes: 7 additions & 2 deletions packages/cli/src/lib/pg/setter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Command} from '@heroku-cli/command'
import {Command, flags} from '@heroku-cli/command'
import {ux} from '@oclif/core'
import {addonResolver} from '../addons/resolve'
import host from './host'
Expand Down Expand Up @@ -29,14 +29,19 @@ export abstract class PGSettingsCommand extends Command {

protected abstract explain(setting: Setting): string

static flags = {
app: flags.app({required: true}),
remote: flags.remote(),
}

public async run(): Promise<void> {
const {flags, args} = await this.parse()
const {app} = flags
const {value, database} = args

const db = await addonResolver(this.heroku, app, database || '')

if (essentialPlan(db)) throw new Error('You can’t perform this operation on Essential-tier databases.')
if (essentialPlan(db)) ux.error('You can’t perform this operation on Essential-tier databases.')

if (value) {
const {body: settings} = await this.heroku.patch<Record<SettingKey, Setting>>(`/postgres/v0/databases/${db.id}/config`, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {expect} from '@oclif/test'
import * as nock from 'nock'
import {stdout} from 'stdout-stderr'
import heredoc from 'tsheredoc'
import runCommand from '../../../../../helpers/runCommand'
import Cmd from '../../../../../../src/commands/pg/settings/auto-explain/log-buffers'
import * as fixtures from '../../../../../fixtures/addons/fixtures'

describe('pg:settings:auto-explain:log-buffers', () => {
const addon = fixtures.addons['dwh-db']

beforeEach(() => {
nock('https://api.heroku.com')
.post('/actions/addons/resolve', {
app: 'myapp',
addon: 'test-database',
}).reply(200, [addon])
})

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

it('shows settings for auto_explain with value', async () => {
nock('https://api.data.heroku.com')
.get(`/postgres/v0/databases/${addon.id}/config`).reply(200, {'auto_explain.log_buffers': {value: 'test_value'}})
await runCommand(Cmd, ['--app', 'myapp', 'test-database'])
expect(stdout.output).to.equal(heredoc(`
auto-explain.log-buffers is set to test_value for ${addon.name}.
Buffer statistics have been enabled for auto_explain.
`))
})

it('shows settings for auto_explain with no value', async () => {
nock('https://api.data.heroku.com')
.get(`/postgres/v0/databases/${addon.id}/config`).reply(200, {'auto_explain.log_buffers': {value: ''}})
await runCommand(Cmd, ['--app', 'myapp', 'test-database'])
expect(stdout.output).to.equal(heredoc(`
auto-explain.log-buffers is set to for ${addon.name}.
Buffer statistics have been disabled for auto_explain.
`))
})
})
23 changes: 0 additions & 23 deletions packages/pg-v5/commands/settings/auto_explain_log_buffers.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 @@ -28,7 +28,6 @@ exports.commands = flatten([
require('./commands/reset'),
require('./commands/settings'),
require('./commands/settings/auto_explain_log_analyze'),
require('./commands/settings/auto_explain_log_buffers'),
require('./commands/settings/auto_explain_log_min_duration'),
require('./commands/settings/auto_explain_log_nested_statements'),
require('./commands/settings/auto_explain_log_triggers'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,6 @@ describe('pg:settings', () => {
.then(() => expect(cli.stdout).to.equal('auto-explain.log-analyze is set to for postgres-1.\nEXPLAIN ANALYZE execution plans will not be logged.\n'))
})

it('shows settings for auto_explain_log_buffers with value', () => {
setupSettingsMockData('auto_explain.log_buffers')
cmd = proxyquire('../../../../commands/settings/auto_explain_log_buffers', {
settings: proxyquire.noCallThru().load('../../../../lib/setter', {
'./fetcher': fetcher,
}),
})
pg.get('/postgres/v0/databases/1/config').reply(200, settingsResult)
return cmd.run({args: {database: 'test-database', value: ''}, flags: {}})
.then(() => expect(cli.stdout).to.equal('auto-explain.log-buffers is set to test_value for postgres-1.\nBuffer statistics have been enabled for auto_explain.\n'))
})

it('shows settings for auto_explain_log_buffers with no value', () => {
setupSettingsMockData('auto_explain.log_buffers', '')
cmd = proxyquire('../../../../commands/settings/auto_explain_log_buffers', {
settings: proxyquire.noCallThru().load('../../../../lib/setter', {
'./fetcher': fetcher,
}),
})
pg.get('/postgres/v0/databases/1/config').reply(200, settingsResult)
return cmd.run({args: {database: 'test-database', value: ''}, flags: {}})
.then(() => expect(cli.stdout).to.equal('auto-explain.log-buffers is set to for postgres-1.\nBuffer statistics have been disabled for auto_explain.\n'))
})

it('shows settings for auto_explain_log_min_duration with value', () => {
setupSettingsMockData('auto_explain.log_min_duration')
cmd = proxyquire('../../../../commands/settings/auto_explain_log_min_duration', {
Expand Down
Loading