@@ -9,6 +9,14 @@ import type { LogbookCardDraft } from "./types.js";
99
1010const DAY = "2026-07-03" ;
1111
12+ function queryPlanDetails ( database : DatabaseSync , sql : string ) : string [ ] {
13+ return (
14+ database . prepare ( `EXPLAIN QUERY PLAN ${ sql } ` ) . all ( ) as Array < {
15+ detail : string ;
16+ } >
17+ ) . map ( ( row ) => row . detail ) ;
18+ }
19+
1220function draft ( overrides : Partial < LogbookCardDraft > = { } ) : LogbookCardDraft {
1321 const base = new Date ( `${ DAY } T10:00:00` ) . getTime ( ) ;
1422 return {
@@ -102,6 +110,107 @@ describe("LogbookStore", () => {
102110 }
103111 } ) ;
104112
113+ it . each ( [
114+ {
115+ operation : "batch frame reads" ,
116+ sql : "SELECT id FROM frames WHERE batch_id = 1 ORDER BY captured_at_ms ASC" ,
117+ expectedIndex : "idx_logbook_frames_batch" ,
118+ scannedTable : "frames" ,
119+ ordered : true ,
120+ } ,
121+ {
122+ operation : "observation replacement" ,
123+ sql : "DELETE FROM observations WHERE batch_id = 1" ,
124+ expectedIndex : "idx_logbook_observations_batch" ,
125+ scannedTable : "observations" ,
126+ ordered : false ,
127+ } ,
128+ {
129+ operation : "frame pruning foreign-key maintenance" ,
130+ sql : "DELETE FROM frames WHERE id = 1" ,
131+ expectedIndex : "idx_logbook_cards_keyframe" ,
132+ scannedTable : "cards" ,
133+ ordered : false ,
134+ } ,
135+ {
136+ operation : "latest frame reads" ,
137+ sql : "SELECT id FROM frames ORDER BY captured_at_ms DESC LIMIT 1" ,
138+ expectedIndex : "idx_logbook_frames_captured_at" ,
139+ scannedTable : "frames" ,
140+ ordered : true ,
141+ } ,
142+ {
143+ operation : "frame range reads" ,
144+ sql : "SELECT id FROM frames WHERE captured_at_ms >= 1 AND captured_at_ms < 2 ORDER BY captured_at_ms ASC" ,
145+ expectedIndex : "idx_logbook_frames_captured_at" ,
146+ scannedTable : "frames" ,
147+ ordered : true ,
148+ } ,
149+ ] ) (
150+ "uses the supporting index for $operation" ,
151+ ( { sql, expectedIndex, scannedTable, ordered } ) => {
152+ const database = new DatabaseSync ( path . join ( dir , "logbook.sqlite" ) , { readOnly : true } ) ;
153+ try {
154+ const plan = queryPlanDetails ( database , sql ) ;
155+ expect ( plan ) . toEqual ( expect . arrayContaining ( [ expect . stringContaining ( expectedIndex ) ] ) ) ;
156+ expect (
157+ plan . some (
158+ ( detail ) => detail . startsWith ( `SCAN ${ scannedTable } ` ) && ! detail . includes ( " USING " ) ,
159+ ) ,
160+ ) . toBe ( false ) ;
161+ if ( ordered ) {
162+ expect ( plan ) . not . toEqual (
163+ expect . arrayContaining ( [ expect . stringContaining ( "USE TEMP B-TREE FOR ORDER BY" ) ] ) ,
164+ ) ;
165+ }
166+ } finally {
167+ database . close ( ) ;
168+ }
169+ } ,
170+ ) ;
171+
172+ it ( "restores missing schema-1 indexes on reopen without changing the version" , ( ) => {
173+ store . close ( ) ;
174+ const databasePath = path . join ( dir , "logbook.sqlite" ) ;
175+ const database = new DatabaseSync ( databasePath ) ;
176+ database . exec ( `
177+ DROP INDEX IF EXISTS idx_logbook_frames_captured_at;
178+ DROP INDEX IF EXISTS idx_logbook_frames_batch;
179+ DROP INDEX IF EXISTS idx_logbook_observations_batch;
180+ DROP INDEX IF EXISTS idx_logbook_cards_keyframe;
181+ ` ) ;
182+ expect ( database . prepare ( "PRAGMA user_version" ) . get ( ) ) . toEqual ( { user_version : 1 } ) ;
183+ database . close ( ) ;
184+
185+ store = new LogbookStore ( dir ) ;
186+
187+ const reopened = new DatabaseSync ( databasePath , { readOnly : true } ) ;
188+ try {
189+ const indexes = reopened
190+ . prepare (
191+ `SELECT name FROM sqlite_schema
192+ WHERE type = 'index'
193+ AND name IN (
194+ 'idx_logbook_frames_batch',
195+ 'idx_logbook_frames_captured_at',
196+ 'idx_logbook_observations_batch',
197+ 'idx_logbook_cards_keyframe'
198+ )
199+ ORDER BY name` ,
200+ )
201+ . all ( ) ;
202+ expect ( indexes ) . toEqual ( [
203+ { name : "idx_logbook_cards_keyframe" } ,
204+ { name : "idx_logbook_frames_batch" } ,
205+ { name : "idx_logbook_frames_captured_at" } ,
206+ { name : "idx_logbook_observations_batch" } ,
207+ ] ) ;
208+ expect ( reopened . prepare ( "PRAGMA user_version" ) . get ( ) ) . toEqual ( { user_version : 1 } ) ;
209+ } finally {
210+ reopened . close ( ) ;
211+ }
212+ } ) ;
213+
105214 it ( "rejects values that violate STRICT column types" , ( ) => {
106215 const database = new DatabaseSync ( path . join ( dir , "logbook.sqlite" ) ) ;
107216 try {
0 commit comments