Skip to content
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
11 changes: 10 additions & 1 deletion frontend/src/features/sql/hooks/useSqlExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { EditorView } from '@codemirror/view';
import { editorSelection } from '@shared/lib/editor';
import { showError } from '@shared/lib/errors';
import { showToast } from '@shared/lib/toast';
import { type RefObject, useState } from 'react';
import { type RefObject, useRef, useState } from 'react';

import { executeSql, type SqlRow } from '../lib/api';
import { countRecords, executionMessage, isGoBatch } from '../lib/results';
Expand All @@ -25,15 +25,23 @@ export function useSqlExecution(
const [safeMode, setSafeMode] = useState(true);
const [result, setResult] = useState<SqlRow[][] | null>(null);
const [busy, setBusy] = useState(false);
// Guards against a second run while one is in flight — Ctrl+Enter in the
// editor bypasses the disabled Run button. A ref, not [busy], so two calls
// before the next render are still caught.
const running = useRef(false);

async function execute() {
if (running.current) {
return;
}
const selected = editorSelection(editorRef.current);
const toExecute = selected === '' ? sql : selected;
if (!toExecute.trim()) {
showToast('Write some SQL first', true);
return;
}
pushHistory(toExecute);
running.current = true;
setBusy(true);
try {
// Batch mode only when a line actually IS a GO statement — merely
Expand All @@ -53,6 +61,7 @@ export function useSqlExecution(
showError(err);
setResult(null);
} finally {
running.current = false;
setBusy(false);
}
}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/features/sql/pages/Sql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export default function Sql() {
</>
)}
{view === 'sql' && <SqlResults result={execution.result} />}
{(waiting || generator.generating) && <AiWaiter />}
{/* Long-running queries get the same waiter as every other round-trip here. */}
{(waiting || generator.generating || execution.busy) && <AiWaiter />}
</>
);
}
23 changes: 23 additions & 0 deletions frontend/src/features/sql/test/Sql.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@ describe('Sql', () => {
expect(screen.getByText('1 rows')).toBeInTheDocument();
});

it('covers the page with the waiter while a query runs', async () => {
let finish!: (rows: never) => void;
vi.mocked(sqlApi.executeSql).mockReturnValue(
new Promise(resolve => {
finish = resolve;
}),
);
const { container } = renderPage(<Sql />);
await screen.findByText('shop');

fireEvent.change(screen.getByLabelText('code editor text/x-sql'), {
target: { value: 'select * from products' },
});
await userEvent.click(screen.getByRole('button', { name: '▷ Run' }));

expect(container.querySelector('.ai-waiter')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Running…' })).toBeDisabled();

finish([[{ id: 1, title: 'Widget' }]] as never);
expect(await screen.findByText('Widget')).toBeInTheDocument();
expect(container.querySelector('.ai-waiter')).not.toBeInTheDocument();
});

it('switches to the designer and lists the tables', async () => {
await ready();

Expand Down
26 changes: 26 additions & 0 deletions frontend/src/features/sql/test/useSqlExecution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,30 @@ describe('useSqlExecution', () => {
// The failed statement is still in the history.
expect(pushHistory).toHaveBeenCalledTimes(2);
});

it('ignores a second run while one is still in flight', async () => {
let finish!: (rows: never) => void;
vi.mocked(sqlApi.executeSql).mockReturnValue(
new Promise(resolve => {
finish = resolve;
}),
);
const { result } = setup('select 1', { current: null });

let first!: Promise<void>;
act(() => {
first = result.current.execute();
});
expect(result.current.busy).toBe(true);
await act(() => result.current.execute());

expect(sqlApi.executeSql).toHaveBeenCalledTimes(1);
expect(pushHistory).toHaveBeenCalledTimes(1);

await act(async () => {
finish([[{ a: 1 }]] as never);
await first;
});
expect(result.current.busy).toBe(false);
});
});