Skip to content

Commit 1e97dc6

Browse files
0froqcpojer
andauthored
fix(cli): print rows for every returning statement in db execute (#864)
Co-authored-by: Christoph Nakazawa <cnakazawa@cloudflare.com>
1 parent 0d75c97 commit 1e97dc6

4 files changed

Lines changed: 233 additions & 9 deletions

File tree

‎docs/reference/cli/database.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void db execute --file <path>
5555
void db execute --remote <sql>
5656
```
5757

58-
Run ad-hoc SQL against the database. Provide SQL inline or from a file. SELECT queries display results as a formatted table; other statements execute silently.
58+
Run ad-hoc SQL against the database. Provide SQL inline or from a file. A statement that returns rows prints them as a formatted table, and statements that change data print nothing. SQL with several statements runs as a script without printed results.
5959

6060
By default, targets the local database. Pass `--remote` to run against the deployed database selected in `.void/project.json`:
6161

‎packages/void/src/cli/db.ts‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
writeJournal,
3131
} from '../migrations/collect.ts';
3232
import type { MySqlClient } from '../migrations/runner-mysql.ts';
33+
import type { SqliteDatabase, SqliteStatement } from '../migrations/runner.ts';
3334
import { assertJournalCoherence, validateMigrations } from '../migrations/validate.ts';
3435
import { detectFramework } from '../plugin-inference.ts';
3536
import { readProjectPaths } from '../project-paths.ts';
@@ -846,6 +847,27 @@ function isSafeStatement(sql: string): boolean {
846847
return false;
847848
}
848849

850+
/**
851+
* Prepare one statement. better-sqlite3 refuses SQL that holds no statement or more than one
852+
* with a `RangeError`, before it runs anything (`src/objects/statement.cpp`). Those are the
853+
* only errors this call can raise that way, so the caller runs the same text through
854+
* `exec()`, which handles a script and reports no rows. Execution errors never come back
855+
* here, so nothing is replayed.
856+
*/
857+
function prepareSingleStatement(
858+
db: SqliteDatabase,
859+
sql: string,
860+
): SqliteStatement<Record<string, unknown>> | undefined {
861+
try {
862+
return db.prepare<Record<string, unknown>>(sql);
863+
} catch (error) {
864+
if (error instanceof RangeError) {
865+
return undefined;
866+
}
867+
throw error;
868+
}
869+
}
870+
849871
/**
850872
* Fetch a project's remote PostgreSQL connection string from the platform.
851873
* Exits with a friendly message when the URL has never been stored (404);
@@ -1096,12 +1118,13 @@ async function runExecute(
10961118

10971119
const db = await openSqliteDatabase(dbPath);
10981120
try {
1099-
const trimmed = sql.trimStart();
1100-
if (/^SELECT\b/i.test(trimmed)) {
1101-
const rows = db.prepare(sql).all() as Array<Record<string, unknown>>;
1102-
if (rows.length === 0) {
1103-
log.info('No rows returned.');
1104-
} else {
1121+
const statement = prepareSingleStatement(db, sql);
1122+
if (statement?.reader) {
1123+
// `reader` is the statement's own answer to "returns rows": SELECT, WITH ... SELECT,
1124+
// PRAGMA reads, EXPLAIN and RETURNING. Writes and DDL run silently, like every other
1125+
// target.
1126+
const rows = statement.all();
1127+
if (rows.length > 0) {
11051128
printTable(rows);
11061129
}
11071130
} else {

‎packages/void/src/migrations/runner.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { hasPackageDependency, readNearestPackageJson } from '../shared/package-
55
import { collectMigrations } from './collect.ts';
66
import { classifyMigration } from './plan.ts';
77

8-
type SqliteStatement<T = unknown> = {
8+
export type SqliteStatement<T = unknown> = {
9+
/** True when the statement returns rows (SELECT, PRAGMA reads, EXPLAIN, RETURNING). */
10+
readonly reader: boolean;
911
get(...params: Array<unknown>): T | undefined;
1012
all(...params: Array<unknown>): Array<T>;
1113
run(...params: Array<unknown>): unknown;
@@ -16,7 +18,7 @@ type SqliteTransaction<T = unknown> = {
1618
immediate(): T;
1719
};
1820

19-
type SqliteDatabase = {
21+
export type SqliteDatabase = {
2022
prepare<T = unknown>(sql: string): SqliteStatement<T>;
2123
exec(sql: string): void;
2224
close(): void;

‎packages/void/test/unit/db-commands.test.ts‎

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,24 @@ describe('void db export', () => {
583583
});
584584
});
585585

586+
/** A project whose local D1 database is a real SQLite file, opened by the unmocked runner. */
587+
async function createLocalSqliteProject(): Promise<string> {
588+
const root = mkdtempSync(join(tmpdir(), 'void-execute-sqlite-'));
589+
rootsToClean.push(root);
590+
writeFileSync(join(root, 'void.json'), JSON.stringify({}));
591+
const dbPath = join(root, '.void', 'local.sqlite');
592+
mkdirSync(join(root, '.void'), { recursive: true });
593+
mockGetMiniflareDatabasePath.mockReturnValue(dbPath);
594+
const { openSqliteDatabase } = await import('../../src/migrations/runner.ts');
595+
const db = await openSqliteDatabase(dbPath);
596+
try {
597+
db.exec('CREATE TABLE items (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)');
598+
} finally {
599+
db.close();
600+
}
601+
return root;
602+
}
603+
586604
describe('void db execute', () => {
587605
let runDbCommand: typeof import('../../src/cli/db.ts').runDbCommand;
588606

@@ -594,11 +612,22 @@ describe('void db execute', () => {
594612

595613
afterEach(() => {
596614
vi.restoreAllMocks();
615+
mockGetMiniflareDatabasePath.mockReset();
616+
mockOpenSqliteDatabase.mockReset();
597617
for (const root of rootsToClean.splice(0, rootsToClean.length)) {
598618
rmSync(root, { recursive: true, force: true });
599619
}
600620
});
601621

622+
// `runDbCommand` opens with a blank line, so only non-empty calls carry a table.
623+
function tableOutput(): string {
624+
return vi
625+
.mocked(console.log)
626+
.mock.calls.map((call) => String(call[0] ?? ''))
627+
.filter((line) => line.length > 0)
628+
.join('\n');
629+
}
630+
602631
it('reads an absolute --file path as given', async () => {
603632
const root = mkdtempSync(join(tmpdir(), 'void-execute-abs-'));
604633
const elsewhere = mkdtempSync(join(tmpdir(), 'void-execute-sql-'));
@@ -625,6 +654,176 @@ describe('void db execute', () => {
625654

626655
expect(mockPgQuery).toHaveBeenCalledWith('UPDATE users SET id = id;');
627656
});
657+
658+
it('SQLite: prints rows for any single statement that returns them', async () => {
659+
const root = await createLocalSqliteProject();
660+
const reads: Array<[string, RegExp]> = [
661+
['-- leading comment\nSELECT 1 AS value', /value/],
662+
['WITH example AS (SELECT 1 AS value) SELECT value FROM example', /value/],
663+
['PRAGMA database_list', /main/],
664+
['EXPLAIN QUERY PLAN SELECT 1', /SCAN|CONSTANT ROW/i],
665+
["INSERT INTO items (name) VALUES ('first') RETURNING id, name", /first/],
666+
];
667+
668+
for (const [sql, pattern] of reads) {
669+
vi.mocked(console.log).mockClear();
670+
await runDbCommand(root, { action: 'execute', command: sql });
671+
expect(tableOutput(), sql).toMatch(pattern);
672+
}
673+
});
674+
675+
it('SQLite: keeps a single statement whose literal holds a semicolon', async () => {
676+
const root = await createLocalSqliteProject();
677+
678+
await runDbCommand(root, { action: 'execute', command: "SELECT 'a;b' AS value" });
679+
680+
expect(tableOutput()).toMatch(/a;b/);
681+
});
682+
683+
it('SQLite: runs a trigger whose body holds semicolons', async () => {
684+
const root = await createLocalSqliteProject();
685+
686+
await runDbCommand(root, {
687+
action: 'execute',
688+
command:
689+
'CREATE TRIGGER items_touch AFTER INSERT ON items BEGIN UPDATE items SET name = name WHERE id = new.id; END;',
690+
});
691+
692+
expect(tableOutput()).toBe('');
693+
await runDbCommand(root, {
694+
action: 'execute',
695+
command: "SELECT name FROM sqlite_master WHERE type = 'trigger'",
696+
});
697+
expect(tableOutput()).toMatch(/items_touch/);
698+
});
699+
700+
it('SQLite: runs a script whose literals hold comment markers', async () => {
701+
const root = await createLocalSqliteProject();
702+
703+
await runDbCommand(root, {
704+
action: 'execute',
705+
command:
706+
"INSERT INTO items (name) VALUES ('--');\nINSERT INTO items (name) VALUES ('plain--');",
707+
});
708+
await runDbCommand(root, {
709+
action: 'execute',
710+
command: "INSERT INTO items (name) VALUES ('/*');\nINSERT INTO items (name) VALUES ('*/');",
711+
});
712+
713+
expect(tableOutput()).toBe('');
714+
await runDbCommand(root, { action: 'execute', command: 'SELECT name FROM items ORDER BY id' });
715+
expect(tableOutput()).toContain('plain--');
716+
expect(tableOutput()).toContain('/*');
717+
const { openSqliteDatabase } = await import('../../src/migrations/runner.ts');
718+
const db = await openSqliteDatabase(join(root, '.void', 'local.sqlite'));
719+
try {
720+
const rows = db.prepare('SELECT name FROM items ORDER BY id').all() as Array<{
721+
name: string;
722+
}>;
723+
expect(rows.map((row) => row.name)).toEqual(['--', 'plain--', '/*', '*/']);
724+
} finally {
725+
db.close();
726+
}
727+
});
728+
729+
it('SQLite: a failing single statement runs once', async () => {
730+
const root = await createLocalSqliteProject();
731+
await runDbCommand(root, {
732+
action: 'execute',
733+
command: 'CREATE TABLE counters (id INTEGER PRIMARY KEY, hits INTEGER, label TEXT UNIQUE)',
734+
});
735+
await runDbCommand(root, {
736+
action: 'execute',
737+
command: "INSERT INTO counters (id, hits, label) VALUES (1, 0, 'one'), (2, 0, 'two')",
738+
});
739+
740+
await expect(
741+
runDbCommand(root, {
742+
action: 'execute',
743+
command: "UPDATE OR FAIL counters SET hits = hits + 1, label = 'done;x' WHERE id IN (1, 2)",
744+
}),
745+
).rejects.toThrow(/UNIQUE constraint failed/);
746+
747+
const { openSqliteDatabase } = await import('../../src/migrations/runner.ts');
748+
const db = await openSqliteDatabase(join(root, '.void', 'local.sqlite'));
749+
try {
750+
const rows = db.prepare('SELECT id, hits, label FROM counters ORDER BY id').all();
751+
// FAIL keeps the row it already updated, and the failed statement is not replayed.
752+
expect(rows).toEqual([
753+
{ id: 1, hits: 1, label: 'done;x' },
754+
{ id: 2, hits: 0, label: 'two' },
755+
]);
756+
} finally {
757+
db.close();
758+
}
759+
});
760+
761+
it('SQLite: incremental vacuum reclaims the requested number of pages', async () => {
762+
const root = await createLocalSqliteProject();
763+
const { openSqliteDatabase } = await import('../../src/migrations/runner.ts');
764+
const db = await openSqliteDatabase(join(root, '.void', 'local.sqlite'));
765+
try {
766+
db.exec(`
767+
PRAGMA page_size = 4096;
768+
PRAGMA auto_vacuum = INCREMENTAL;
769+
VACUUM;
770+
INSERT INTO items (name) VALUES (zeroblob(262144));
771+
DELETE FROM items;
772+
`);
773+
const freelist = db.prepare<{ freelist_count: number }>('PRAGMA freelist_count');
774+
const before = freelist.get()!.freelist_count;
775+
expect(before).toBeGreaterThanOrEqual(20);
776+
777+
await runDbCommand(root, { action: 'execute', command: 'PRAGMA incremental_vacuum(20)' });
778+
779+
expect(freelist.get()!.freelist_count).toBe(before - 20);
780+
} finally {
781+
db.close();
782+
}
783+
});
784+
785+
it('SQLite: SQL without a statement is a no-op', async () => {
786+
const root = await createLocalSqliteProject();
787+
788+
await runDbCommand(root, { action: 'execute', command: '-- nothing to run' });
789+
790+
expect(tableOutput()).toBe('');
791+
expect(mockLog.error).not.toHaveBeenCalled();
792+
});
793+
794+
it('SQLite: prints no table for writes, DDL and empty results', async () => {
795+
const root = await createLocalSqliteProject();
796+
797+
for (const sql of [
798+
"INSERT INTO items (name) VALUES ('second')",
799+
'CREATE TABLE extra (id INTEGER)',
800+
'SELECT * FROM items WHERE id < 0',
801+
]) {
802+
vi.mocked(console.log).mockClear();
803+
mockLog.info.mockClear();
804+
await runDbCommand(root, { action: 'execute', command: sql });
805+
expect(tableOutput(), sql).toBe('');
806+
expect(mockLog.info, sql).not.toHaveBeenCalled();
807+
}
808+
});
809+
810+
it('SQLite: runs a multi-statement script without printing results', async () => {
811+
const root = await createLocalSqliteProject();
812+
813+
await runDbCommand(root, {
814+
action: 'execute',
815+
command: "INSERT INTO items (name) VALUES ('script'); SELECT name FROM items;",
816+
});
817+
818+
expect(tableOutput()).toBe('');
819+
const { openSqliteDatabase } = await import('../../src/migrations/runner.ts');
820+
const db = await openSqliteDatabase(join(root, '.void', 'local.sqlite'));
821+
try {
822+
expect(db.prepare('SELECT name FROM items ORDER BY id').all()).toEqual([{ name: 'script' }]);
823+
} finally {
824+
db.close();
825+
}
826+
});
628827
});
629828

630829
describe('void db status', () => {

0 commit comments

Comments
 (0)