diff --git a/packages/cli/src/commands/pg/settings/log-connections.ts b/packages/cli/src/commands/pg/settings/log-connections.ts new file mode 100644 index 0000000000..639275dd4e --- /dev/null +++ b/packages/cli/src/commands/pg/settings/log-connections.ts @@ -0,0 +1,36 @@ +import {flags} from '@heroku-cli/command' +import {Args} from '@oclif/core' +import heredoc from 'tsheredoc' +import {type BooleanAsString, booleanConverter, PGSettingsCommand} from '../../../lib/pg/setter' +import type {Setting, SettingKey} from '../../../lib/pg/types' + +export default class LogConnections extends PGSettingsCommand { + static topic = 'pg' + static description = heredoc(` + Controls whether a log message is produced when a login attempt is made. Default is true. + Setting log_connections to false stops emitting log messages for all attempts to login to the database.`) + + static flags = { + app: flags.app({required: true}), + remote: flags.remote(), + } + + static args = { + database: Args.string(), + value: Args.string(), + } + + protected settingKey: SettingKey = 'log_connections' + + protected convertValue(val: unknown): unknown { + return booleanConverter(val as BooleanAsString) + } + + protected explain(setting: Setting): string { + if (setting.value) { + return 'When login attempts are made, a log message will be emitted in your application\'s logs.' + } + + return 'When login attempts are made, no log message will be emitted in your application\'s logs.' + } +} diff --git a/packages/cli/test/unit/commands/pg/settings/log-connections.unit.test.ts b/packages/cli/test/unit/commands/pg/settings/log-connections.unit.test.ts new file mode 100644 index 0000000000..280bffddca --- /dev/null +++ b/packages/cli/test/unit/commands/pg/settings/log-connections.unit.test.ts @@ -0,0 +1,52 @@ +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-connections' + +describe('pg:settings:log-connections', () => { + let api: nock.Scope + let pg: nock.Scope + + beforeEach(() => { + const addon = { + id: 1, + name: 'postgres-1', + app: {name: 'myapp'}, + config_vars: ['READONLY_URL', 'DATABASE_URL', 'HEROKU_POSTGRESQL_RED_URL'], + plan: {name: 'heroku-postgresql:standard-0'}, + } + + api = nock('https://api.heroku.com') + api.post('/actions/addons/resolve', { + app: 'myapp', + addon: 'test-database', + }).reply(200, [addon]) + + pg = nock('https://api.data.heroku.com') + }) + + afterEach(() => { + api.done() + pg.done() + }) + + it('shows settings for auto_explain with value', async () => { + pg.get('/postgres/v0/databases/1/config').reply(200, {log_connections: {value: 'test_value'}}) + await runCommand(Cmd, ['--app', 'myapp', 'test-database']) + expect(stdout.output).to.equal(heredoc(` + log-connections is set to test_value for postgres-1. + When login attempts are made, a log message will be emitted in your application's logs. + `)) + }) + + it('shows settings for auto_explain with no value', async () => { + pg.get('/postgres/v0/databases/1/config').reply(200, {log_connections: {value: ''}}) + await runCommand(Cmd, ['--app', 'myapp', 'test-database']) + expect(stdout.output).to.equal(heredoc(` + log-connections is set to for postgres-1. + When login attempts are made, no log message will be emitted in your application's logs. + `)) + }) +}) diff --git a/packages/pg-v5/commands/settings/log_connections.js b/packages/pg-v5/commands/settings/log_connections.js deleted file mode 100644 index b6c6bdffeb..0000000000 --- a/packages/pg-v5/commands/settings/log_connections.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict' - -const cli = require('heroku-cli-util') -const settings = require('../../lib/setter') - -function explain(setting) { - if (setting.value) { - return 'When login attempts are made, a log message will be emitted in your application\'s logs.' - } - - return 'When login attempts are made, no log message will be emitted in your application\'s logs.' -} - -module.exports = { - topic: 'pg', - command: 'settings:log-connections', - description: 'Controls whether a log message is produced when a login attempt is made. Default is true.', - help: 'Setting log_connections to false stops emitting log messages for all attempts to login to the database.', - needsApp: true, - needsAuth: true, - args: [{name: 'value', optional: true}, {name: 'database', optional: true}], - run: cli.command({preauth: true}, settings.generate('log_connections', settings.boolean, explain)), -}