@@ -125,6 +125,109 @@ describe('SQLitePresenter legacy schema bootstrap', () => {
125125 expect ( columnNames . has ( 'is_draft' ) ) . toBe ( true )
126126 expect ( columnNames . has ( 'active_skills' ) ) . toBe ( true )
127127 expect ( columnNames . has ( 'disabled_agent_tools' ) ) . toBe ( true )
128+
129+ const versions = checkDb
130+ . prepare ( 'SELECT version FROM schema_versions ORDER BY version ASC' )
131+ . all ( ) as Array < { version : number } >
132+ expect ( versions . map ( ( row ) => row . version ) ) . toEqual ( expect . arrayContaining ( [ 11 , 15 , 16 ] ) )
133+ checkDb . close ( )
134+ } )
135+
136+ it ( 'recreates new_sessions with applied columns when schema version is already 16' , async ( ) => {
137+ const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'deepchat-sqlite-presenter-' ) )
138+ tempDirs . push ( tempDir )
139+
140+ const dbPath = path . join ( tempDir , 'agent.db' )
141+ const bootstrapDb = new Database ( dbPath )
142+ bootstrapDb . exec ( `
143+ CREATE TABLE IF NOT EXISTS schema_versions (
144+ version INTEGER PRIMARY KEY,
145+ applied_at INTEGER NOT NULL
146+ );
147+ INSERT INTO schema_versions (version, applied_at) VALUES (16, ${ Date . now ( ) } );
148+ ` )
149+ bootstrapDb . close ( )
150+
151+ const presenter = new SQLitePresenter ( dbPath )
152+ presenter . newSessionsTable . create ( 'session-1' , 'agent-1' , 'Recovered session' , null )
153+ presenter . close ( )
154+
155+ const checkDb = new Database ( dbPath )
156+ const newSessionColumns = checkDb . prepare ( 'PRAGMA table_info(new_sessions)' ) . all ( ) as Array < {
157+ name : string
158+ } >
159+ const columnNames = new Set ( newSessionColumns . map ( ( column ) => column . name ) )
160+
161+ expect ( columnNames . has ( 'is_draft' ) ) . toBe ( true )
162+ expect ( columnNames . has ( 'active_skills' ) ) . toBe ( true )
163+ expect ( columnNames . has ( 'disabled_agent_tools' ) ) . toBe ( true )
164+
165+ const row = checkDb
166+ . prepare (
167+ 'SELECT is_draft, active_skills, disabled_agent_tools FROM new_sessions WHERE id = ?'
168+ )
169+ . get ( 'session-1' ) as
170+ | {
171+ is_draft : number
172+ active_skills : string
173+ disabled_agent_tools : string
174+ }
175+ | undefined
176+
177+ expect ( row ) . toEqual ( {
178+ is_draft : 0 ,
179+ active_skills : '[]' ,
180+ disabled_agent_tools : '[]'
181+ } )
182+ checkDb . close ( )
183+ } )
184+
185+ it ( 'recreates deepchat_sessions with applied columns when schema version is already 14' , async ( ) => {
186+ const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'deepchat-sqlite-presenter-' ) )
187+ tempDirs . push ( tempDir )
188+
189+ const dbPath = path . join ( tempDir , 'agent.db' )
190+ const bootstrapDb = new Database ( dbPath )
191+ bootstrapDb . exec ( `
192+ CREATE TABLE IF NOT EXISTS schema_versions (
193+ version INTEGER PRIMARY KEY,
194+ applied_at INTEGER NOT NULL
195+ );
196+ INSERT INTO schema_versions (version, applied_at) VALUES (14, ${ Date . now ( ) } );
197+ ` )
198+ bootstrapDb . close ( )
199+
200+ const presenter = new SQLitePresenter ( dbPath )
201+ presenter . deepchatSessionsTable . create ( 'session-1' , 'openai' , 'gpt-4o' )
202+ presenter . close ( )
203+
204+ const checkDb = new Database ( dbPath )
205+ const deepchatColumns = checkDb . prepare ( 'PRAGMA table_info(deepchat_sessions)' ) . all ( ) as Array < {
206+ name : string
207+ } >
208+ const columnNames = new Set ( deepchatColumns . map ( ( column ) => column . name ) )
209+
210+ expect ( columnNames . has ( 'system_prompt' ) ) . toBe ( true )
211+ expect ( columnNames . has ( 'summary_text' ) ) . toBe ( true )
212+ expect ( columnNames . has ( 'summary_cursor_order_seq' ) ) . toBe ( true )
213+
214+ const row = checkDb
215+ . prepare (
216+ 'SELECT system_prompt, summary_text, summary_cursor_order_seq FROM deepchat_sessions WHERE id = ?'
217+ )
218+ . get ( 'session-1' ) as
219+ | {
220+ system_prompt : string | null
221+ summary_text : string | null
222+ summary_cursor_order_seq : number
223+ }
224+ | undefined
225+
226+ expect ( row ) . toEqual ( {
227+ system_prompt : null ,
228+ summary_text : null ,
229+ summary_cursor_order_seq : 1
230+ } )
128231 checkDb . close ( )
129232 } )
130233} )
0 commit comments