Skip to content

Commit

Permalink
Move 'pg:settings:auto-explain:log-triggers' to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
sbosio committed Apr 8, 2024
1 parent 2b45a65 commit 53313c5
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 48 deletions.
31 changes: 31 additions & 0 deletions packages/cli/src/commands/pg/settings/auto-explain/log-triggers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {Args} from '@oclif/core'
import heredoc from 'tsheredoc'
import {PGSettingsCommand, booleanConverter, BooleanAsString} from '../../../../lib/pg/setter'
import {SettingKey, Setting} from '../../../../lib/pg/types'

export default class LogTriggers extends PGSettingsCommand {
static topic = 'pg'
static description = heredoc(`
Includes trigger execution statistics in execution plan logs.
This parameter 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 = 'auto_explain.log_triggers' as SettingKey

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

protected explain(setting: Setting<boolean>) {
if (setting.value) {
return 'Trigger execution statistics have been enabled for auto-explain.'
}

return 'Trigger execution statistics have been disabled for auto-explain.'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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-triggers'
import * as fixtures from '../../../../../fixtures/addons/fixtures'

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

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

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

it('updates settings for auto_explain.log_triggers with value', async () => {
const pg = nock('https://api.data.heroku.com')
.patch(`/postgres/v0/databases/${addon.id}/config`).reply(200, {'auto_explain.log_triggers': {value: true}})

await runCommand(Cmd, ['--app', 'myapp', 'test-database', 'true'])

api.done()
pg.done()

expect(stdout.output).to.equal(heredoc(`
auto-explain.log-triggers has been set to true for ${addon.name}.
Trigger execution statistics have been enabled for auto-explain.
`))
})

it('shows settings for auto_explain.log_triggers with no value', async () => {
const pg = nock('https://api.data.heroku.com')
.get(`/postgres/v0/databases/${addon.id}/config`).reply(200, {'auto_explain.log_triggers': {value: false}})

await runCommand(Cmd, ['--app', 'myapp', 'test-database'])

api.done()
pg.done()

expect(stdout.output).to.equal(heredoc(`
auto-explain.log-triggers is set to false for ${addon.name}.
Trigger execution statistics have been disabled for auto-explain.
`))
})
})
23 changes: 0 additions & 23 deletions packages/pg-v5/commands/settings/auto_explain_log_triggers.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_nested_statements'),
require('./commands/settings/auto_explain_log_triggers'),
require('./commands/settings/auto_explain_log_verbose'),
require('./commands/settings/log_lock_waits'),
require('./commands/settings/log_min_duration_statement'),
Expand Down
24 changes: 0 additions & 24 deletions packages/pg-v5/test/unit/commands/settings/settings.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,30 +156,6 @@ describe('pg:settings', () => {
.then(() => expect(cli.stdout).to.equal('auto-explain.log-nested-statements is set to for postgres-1.\nOnly top-level execution plans will be included in logs.\n'))
})

it('shows settings for auto_explain_log_triggers with value', () => {
setupSettingsMockData('auto_explain.log_triggers')
cmd = proxyquire('../../../../commands/settings/auto_explain_log_triggers', {
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-triggers is set to test_value for postgres-1.\nTrigger execution statistics have been enabled for auto_explain.\n'))
})

it('shows settings for auto_explain_log_triggers with no value', () => {
setupSettingsMockData('auto_explain.log_triggers', '')
cmd = proxyquire('../../../../commands/settings/auto_explain_log_triggers', {
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-triggers is set to for postgres-1.\nTrigger execution statistics have been disabled for auto_explain.\n'))
})

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

0 comments on commit 53313c5

Please sign in to comment.