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:log-min-duration-statement to oclif #2779

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,35 @@
import {Args} from '@oclif/core'
import heredoc from 'tsheredoc'
import {PGSettingsCommand} from '../../../lib/pg/setter'
import type {Setting, SettingKey} from '../../../lib/pg/types'

export default class LogMinDurationStatement extends PGSettingsCommand {
static description = heredoc(`
The duration of each completed statement will be logged if the statement completes after the time specified by VALUE.
VALUE needs to specified as a whole number, in milliseconds.
Setting log_min_duration_statement to zero prints all statement durations and -1 will disable logging statement durations.
`)

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

protected settingKey:SettingKey = 'log_min_duration_statement'

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

protected explain(setting: Setting<unknown>) {
if (setting.value === -1) {
return 'The duration of each completed statement will not be logged.'
}

if (setting.value === 0) {
return 'The duration of each completed statement will be logged.'
}

return `The duration of each completed statement will be logged if the statement ran for at least ${setting.value} milliseconds.`
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/lib/pg/setter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export abstract class PGSettingsCommand extends Command {
protected abstract convertValue(val: string): unknown
protected abstract explain(setting: Setting<unknown>): string

static topic = 'pg'

static flags = {
app: flags.app({required: true}),
remote: flags.remote(),
Expand Down
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/log-min-duration-statement'
import * as fixtures from '../../../../fixtures/addons/fixtures'

describe('pg:settings:log-min-duration-statement', () => {
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, {log_min_duration_statement: {value: 'test_value'}})
await runCommand(Cmd, ['--app', 'myapp', 'test-database'])
expect(stdout.output).to.equal(heredoc(`
log-min-duration-statement is set to test_value for ${addon.name}.
The duration of each completed statement will be logged if the statement ran for at least test_value milliseconds.
`))
})

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, {log_min_duration_statement: {value: -1}})
await runCommand(Cmd, ['--app', 'myapp', 'test-database'])
expect(stdout.output).to.equal(heredoc(`
log-min-duration-statement is set to -1 for ${addon.name}.
The duration of each completed statement will not be logged.
`))
})

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, {log_min_duration_statement: {value: 0}})
await runCommand(Cmd, ['--app', 'myapp', 'test-database'])
expect(stdout.output).to.equal(heredoc(`
log-min-duration-statement is set to 0 for ${addon.name}.
The duration of each completed statement will be logged.
`))
})
})
28 changes: 0 additions & 28 deletions packages/pg-v5/commands/settings/log_min_duration_statement.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 @@ -29,7 +29,6 @@ exports.commands = flatten([
require('./commands/settings/auto_explain_log_nested_statements'),
require('./commands/settings/auto_explain_log_triggers'),
require('./commands/settings/log_lock_waits'),
require('./commands/settings/log_min_duration_statement'),
require('./commands/settings/log_statement'),
require('./commands/settings/track_functions'),
require('./commands/unfollow'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,42 +96,6 @@ describe('pg:settings', () => {
.then(() => expect(cli.stdout).to.equal(`${settingsResultName} is set to ${settingResult.value} for postgres-1.\n${settingResult.values[settingResult.value]}\n`))
})

it('shows settings for log_min_duration_statement with value', () => {
setupSettingsMockData('log_min_duration_statement')
cmd = proxyquire('../../../../commands/settings/log_min_duration_statement', {
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('log-min-duration-statement is set to test_value for postgres-1.\nThe duration of each completed statement will be logged if the statement ran for at least test_value milliseconds.\n'))
})

it('shows settings for log_min_duration_statement with no value', () => {
setupSettingsMockData('log_min_duration_statement', -1)
cmd = proxyquire('../../../../commands/settings/log_min_duration_statement', {
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('log-min-duration-statement is set to -1 for postgres-1.\nThe duration of each completed statement will not be logged.\n'))
})

it('shows settings for log_min_duration_statement with value set to 0', () => {
setupSettingsMockData('log_min_duration_statement', 0)
cmd = proxyquire('../../../../commands/settings/log_min_duration_statement', {
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('log-min-duration-statement is set to 0 for postgres-1.\nThe duration of each completed statement will be logged.\n'))
})

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