Skip to content

Commit

Permalink
refactor(pg-v5): Move command pg:settings:auto-explain:log-min-durati…
Browse files Browse the repository at this point in the history
…on to oclif (#2776)

* Convert log-min-duration to oclif

* remove .only

* Fix settingKey

* undo deletion of log_min_duration_statement tests
  • Loading branch information
eablack authored Apr 5, 2024
1 parent ae460a6 commit f455e4f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {Args} from '@oclif/core'
import heredoc from 'tsheredoc'
import {PGSettingsCommand, type Setting, type SettingKey} from '../../../../lib/pg/setter'

export default class LogMinDuration extends PGSettingsCommand {
static topic = 'pg'
static description = heredoc(`
Sets the minimum execution time in milliseconds for a statement's plan to be logged.
Setting this value to 0 will log all queries. Setting this value to -1 will disable logging entirely.
`)

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

protected settingKey:SettingKey = 'auto_explain.log_min_duration'

protected convertValue(val: number): number {
return val
}

protected explain(setting: Setting) {
if (setting.value === -1) {
return 'Execution plan logging has been disabled.'
}

if (setting.value === 0) {
return 'All queries will have their execution plans logged.'
}

return `All execution plans will be logged for queries taking up to ${setting.value} milliseconds or more.`
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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-min-duration'
import * as fixtures from '../../../../../fixtures/addons/fixtures'

describe('pg:settings:auto-explain:log-min-duration', () => {
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_min_duration': {value: 'test_value'}})
await runCommand(Cmd, ['--app', 'myapp', 'test-database'])
expect(stdout.output).to.equal(heredoc(`
auto-explain.log-min-duration is set to test_value for ${addon.name}.
All execution plans will be logged for queries taking up to test_value milliseconds or more.
`))
})

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_min_duration': {value: -1}})
await runCommand(Cmd, ['--app', 'myapp', 'test-database'])
expect(stdout.output).to.equal(heredoc(`
auto-explain.log-min-duration is set to -1 for ${addon.name}.
Execution plan logging has been disabled.
`))
})

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_min_duration': {value: 0}})
await runCommand(Cmd, ['--app', 'myapp', 'test-database'])
expect(stdout.output).to.equal(heredoc(`
auto-explain.log-min-duration is set to 0 for ${addon.name}.
All queries will have their execution plans logged.
`))
})
})
29 changes: 0 additions & 29 deletions packages/pg-v5/commands/settings/auto_explain_log_min_duration.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_min_duration'),
require('./commands/settings/auto_explain_log_nested_statements'),
require('./commands/settings/auto_explain_log_triggers'),
require('./commands/settings/auto_explain_log_verbose'),
Expand Down
36 changes: 0 additions & 36 deletions packages/pg-v5/test/unit/commands/settings/settings.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,42 +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_min_duration with value', () => {
setupSettingsMockData('auto_explain.log_min_duration')
cmd = proxyquire('../../../../commands/settings/auto_explain_log_min_duration', {
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-min-duration is set to test_value for postgres-1.\nAll execution plans will be logged for queries taking up to test_value milliseconds or more.\n'))
})

it('shows settings for auto_explain_log_min_duration with no value', () => {
setupSettingsMockData('auto_explain.log_min_duration', -1)
cmd = proxyquire('../../../../commands/settings/auto_explain_log_min_duration', {
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-min-duration is set to -1 for postgres-1.\nExecution plan logging has been disabled.\n'))
})

it('shows settings for auto_explain_log_min_duration with value set to 0', () => {
setupSettingsMockData('auto_explain.log_min_duration', 0)
cmd = proxyquire('../../../../commands/settings/auto_explain_log_min_duration', {
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-min-duration is set to 0 for postgres-1.\nAll queries will have their execution plans logged.\n'))
})

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

0 comments on commit f455e4f

Please sign in to comment.