@@ -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+
586604describe ( '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' , / v a l u e / ] ,
662+ [ 'WITH example AS (SELECT 1 AS value) SELECT value FROM example' , / v a l u e / ] ,
663+ [ 'PRAGMA database_list' , / m a i n / ] ,
664+ [ 'EXPLAIN QUERY PLAN SELECT 1' , / S C A N | C O N S T A N T R O W / i] ,
665+ [ "INSERT INTO items (name) VALUES ('first') RETURNING id, name" , / f i r s t / ] ,
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 ( / i t e m s _ t o u c h / ) ;
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 ( / U N I Q U E c o n s t r a i n t f a i l e d / ) ;
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
630829describe ( 'void db status' , ( ) => {
0 commit comments