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

fix(pg:settings): Backward compatibility for Data command #3004

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions packages/cli/src/commands/pg/settings/auto-explain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export default class AutoExplain extends PGSettingsCommand {
}

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

static strict = false

protected settingKey: SettingKey = 'auto_explain'

protected invariant = this.booleanInvariant
protected convertValue(val: BooleanAsString): boolean {
return booleanConverter(val)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export default class LogAnalyze extends PGSettingsCommand {
`)

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

protected settingKey = 'auto_explain.log_analyze' as SettingKey

protected invariant = this.booleanInvariant
protected convertValue(val: BooleanAsString): boolean {
return booleanConverter(val)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export default class LogBuffersWaits extends PGSettingsCommand {
`)

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

protected settingKey: SettingKey = 'auto_explain.log_buffers'

protected invariant = this.booleanInvariant
protected convertValue(val: BooleanAsString): boolean {
return booleanConverter(val)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export default class LogMinDuration extends PGSettingsCommand {
`)

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

protected settingKey: SettingKey = 'auto_explain.log_min_duration'

protected invariant = this.numberInvariant
protected convertValue(val: string): number {
return numericConverter(val)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export default class LogNestedStatements extends PGSettingsCommand {
static description = "Nested statements are included in the execution plan's log."

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

protected settingKey: SettingKey = 'auto_explain.log_nested_statements'

protected invariant = this.booleanInvariant
protected convertValue(val: unknown): boolean {
return booleanConverter(val as BooleanAsString)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export default class LogTriggers extends PGSettingsCommand {
`)

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

protected settingKey = 'auto_explain.log_triggers' as SettingKey

protected invariant = this.booleanInvariant
protected convertValue(val: BooleanAsString): boolean {
return booleanConverter(val)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export default class AutoExplainLogVerbose extends PGSettingsCommand {
}

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

protected settingKey: SettingKey = 'auto_explain.log_verbose'
Expand All @@ -30,6 +30,7 @@ export default class AutoExplainLogVerbose extends PGSettingsCommand {
return 'Verbose execution plan logging has been disabled for auto_explain.'
}

protected invariant = this.booleanInvariant
protected convertValue(val: unknown): boolean {
return booleanConverter(val as BooleanAsString)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/pg/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class Index extends Command {
}

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

public async run(): Promise<void> {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/pg/settings/log-connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export default class LogConnections extends PGSettingsCommand {
}

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

protected settingKey: SettingKey = 'log_connections'

protected invariant = this.booleanInvariant
protected convertValue(val: unknown): unknown {
return booleanConverter(val as BooleanAsString)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/pg/settings/log-lock-waits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export default class LogLockWaits extends PGSettingsCommand {
`)

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

protected settingKey: SettingKey = 'log_lock_waits'

protected invariant = this.booleanInvariant
protected convertValue(val: unknown): boolean {
return booleanConverter(val as BooleanAsString)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export default class LogMinDurationStatement extends PGSettingsCommand {
`)

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

protected settingKey:SettingKey = 'log_min_duration_statement'

protected invariant = this.numberInvariant
protected convertValue(val: unknown): number {
return val as number
}
Expand Down
17 changes: 14 additions & 3 deletions packages/cli/src/commands/pg/settings/log-statement.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {Args} from '@oclif/core'
import heredoc from 'tsheredoc'
import {type Setting, type SettingKey} from '../../../lib/pg/types'
import {PGSettingsCommand} from '../../../lib/pg/setter'
import {type ArgTypes, PGSettingsCommand} from '../../../lib/pg/setter'

const values = new Set(['none', 'ddl', 'mod', 'all'])
export default class LogStatement extends PGSettingsCommand {
static description = heredoc(`
log_statement controls which SQL statements are logged.
Expand All @@ -13,12 +15,21 @@ export default class LogStatement extends PGSettingsCommand {
`)

static args = {
database: Args.string(),
value: Args.string({options: ['none', 'ddl', 'mod', 'all']}),
database: Args.string({optional: true}),
value: Args.string({optional: true}),
}

protected settingKey: SettingKey = 'log_statement'

protected invariant(args: ArgTypes): ArgTypes {
const {value, database} = args
if (values.has(database ?? '')) {
return {value: database, database: value}
}

return args
}

protected convertValue(val: string): string {
return val
}
Expand Down
16 changes: 13 additions & 3 deletions packages/cli/src/commands/pg/settings/track-functions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {Args} from '@oclif/core'
import heredoc from 'tsheredoc'
import {PGSettingsCommand} from '../../../lib/pg/setter'
import {type ArgTypes, PGSettingsCommand} from '../../../lib/pg/setter'
import type {Setting, SettingKey} from '../../../lib/pg/types'

const values = new Set(['none', 'pl', 'all'])
// ref: https://www.postgresql.org/docs/current/runtime-config-statistics.html#GUC-TRACK-FUNCTIONS
export default class TrackFunctions extends PGSettingsCommand {
static description = heredoc(`
Expand All @@ -13,12 +14,21 @@ export default class TrackFunctions extends PGSettingsCommand {
all - All functions, including SQL and C language functions, are tracked. Simple SQL-language that are inlined are not tracked`)

static args = {
database: Args.string(),
value: Args.string({options: ['none', 'pl', 'all']}),
database: Args.string({optional: true}),
value: Args.string({optional: true}),
}

protected settingKey: SettingKey = 'track_functions'

protected invariant(args: ArgTypes): ArgTypes {
const {value, database} = args
if (values.has(database ?? '')) {
return {value: database, database: value}
}

return args
}

protected convertValue(val: unknown): unknown {
return val
}
Expand Down
26 changes: 25 additions & 1 deletion packages/cli/src/lib/pg/setter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import host from './host'
import {essentialPlan} from './util'
import {SettingKey, Setting, SettingsResponse} from './types'

export type ArgTypes = {value: string | undefined, database: string | undefined};

export abstract class PGSettingsCommand extends Command {
protected abstract settingKey: SettingKey
protected abstract convertValue(val: string): unknown
Expand All @@ -20,7 +22,7 @@ export abstract class PGSettingsCommand extends Command {
public async run(): Promise<void> {
const {flags, args} = await this.parse()
const {app} = flags
const {value, database} = args as {value: string | undefined, database: string | undefined}
const {value, database} = this.invariant(args as ArgTypes)

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

Expand All @@ -41,6 +43,28 @@ export abstract class PGSettingsCommand extends Command {
ux.log(this.explain(setting))
}
}

protected abstract invariant(args: ArgTypes): ArgTypes

protected booleanInvariant(args: ArgTypes): ArgTypes {
const {value, database} = args
try {
booleanConverter(database as BooleanAsString)
return {value: database, database: value}
} catch {
return args
}
}

protected numberInvariant(args: ArgTypes): ArgTypes {
const {database, value} = args

if (Number.isFinite(Number(database))) {
return {value: database, database: value}
}

return args
}
}

export type BooleanAsString = 'on' | 'ON' | 'true' | 'TRUE' | 'off' | 'OFF' | 'false' | 'FALSE'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ describe('pg:settings:track-functions', function () {
No function calls will be tracked.
`))
})

it('shows settings for track_functions with database and value arg reversed', async function () {
pg.patch('/postgres/v0/databases/1/config').reply(200, {
track_functions: {
value: 'test_value',
values: {test_value: 'No function calls will be tracked.'},
},
})
await runCommand(Cmd, ['--app', 'myapp', 'pl', 'test-database'])
expect(stdout.output).to.equal(heredoc(`
track-functions has been set to test_value for postgres-1.
No function calls will be tracked.
`))
})
})
Loading