fix(setup): prompt for postgres query history

This commit is contained in:
Andrey Avtomonov 2026-05-13 19:05:36 +02:00
parent 9057c222dc
commit bc23b1a447
2 changed files with 67 additions and 6 deletions

View file

@ -1454,6 +1454,67 @@ describe('setup databases step', () => {
expect(io.stdout()).toContain('pg_stat_statements ready');
});
it('asks interactive Postgres setup whether to enable query history', async () => {
await writeFile(
join(tempDir, 'ktx.yaml'),
[
'project: warehouse',
'connections:',
' warehouse:',
' driver: postgres',
' url: env:DATABASE_URL',
' readonly: true',
'',
].join('\n'),
'utf-8',
);
const io = makeIo();
const prompts = makePromptAdapter({ selectValues: ['yes'] });
const historicSqlProbe = vi.fn(async () => ({ ok: true, lines: [] }));
const result = await runKtxSetupDatabasesStep(
{
projectDir: tempDir,
inputMode: 'auto',
databaseConnectionIds: ['warehouse'],
databaseSchemas: [],
skipDatabases: false,
},
io.io,
{
prompts,
testConnection: vi.fn(async () => 0),
scanConnection: vi.fn(async () => 0),
historicSqlProbe,
},
);
expect(result.status).toBe('ready');
expect(prompts.select).toHaveBeenCalledWith({
message: 'Enable query-history ingest for this PostgreSQL connection?',
options: [
{ value: 'yes', label: 'Enable query history' },
{ value: 'no', label: 'Do not enable query history' },
{ value: 'back', label: 'Back' },
],
});
expect(historicSqlProbe).toHaveBeenCalledWith({
projectDir: tempDir,
connectionId: 'warehouse',
dialect: 'postgres',
});
const config = parseKtxProjectConfig(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8'));
expect(config.connections.warehouse).toMatchObject({
context: {
queryHistory: {
enabled: true,
minExecutions: 5,
filters: { dropTrivialProbes: true },
},
},
});
});
it('writes query history config for supported existing database connections', async () => {
await writeFile(
join(tempDir, 'ktx.yaml'),

View file

@ -842,7 +842,7 @@ async function maybeApplyHistoricSqlConfig(input: {
let enabled = input.args.enableQueryHistory === true;
if (input.args.disableQueryHistory === true) {
enabled = false;
} else if (input.args.inputMode !== 'disabled' && input.args.enableQueryHistory !== true && dialect !== 'postgres') {
} else if (input.args.inputMode !== 'disabled' && input.args.enableQueryHistory !== true) {
const choice = await input.prompts.select({
message: `Enable query-history ingest for this ${driverLabel(input.driver)} connection?`,
options: [
@ -855,10 +855,6 @@ async function maybeApplyHistoricSqlConfig(input: {
enabled = choice === 'yes';
}
if (dialect === 'postgres' && input.args.enableQueryHistory !== true && input.args.disableQueryHistory !== true) {
return input.connection;
}
const existingRecord = queryHistoryConfigRecord(input.connection) ?? historicSqlConfigRecord(input.connection) ?? {};
const { dialect: _dialect, ...existing } = existingRecord;
@ -1474,7 +1470,11 @@ async function applyHistoricSqlConfigToExistingConnection(input: {
args: KtxSetupDatabasesArgs;
prompts: KtxSetupDatabasesPromptAdapter;
}): Promise<'back' | void> {
if (input.args.enableQueryHistory !== true && input.args.disableQueryHistory !== true) {
if (
input.args.inputMode === 'disabled' &&
input.args.enableQueryHistory !== true &&
input.args.disableQueryHistory !== true
) {
return;
}