@@ -11,6 +11,31 @@ import { migrateLegacyMediaPersistence } from "./state-migrations.media-persiste
1111
1212const tempDirs : string [ ] = [ ] ;
1313
14+ function createV17AdditiveFixture ( options : { schemaDrift ?: "missing-cache-table" } = { } ) {
15+ const stateDir = makeTempDir ( tempDirs , "media-persistence-v17-additive-" ) ;
16+ const env = { OPENCLAW_STATE_DIR : stateDir } ;
17+ const opened = openOpenClawAgentDatabase ( { agentId : "main" , env } ) ;
18+ const databasePath = opened . path ;
19+ closeOpenClawAgentDatabasesForTest ( ) ;
20+ closeOpenClawStateDatabaseForTest ( ) ;
21+
22+ const { DatabaseSync } = requireNodeSqlite ( ) ;
23+ const database = new DatabaseSync ( databasePath ) ;
24+ database . exec ( `
25+ DROP TABLE session_participants;
26+ DROP TRIGGER session_conversations_route_context_invalidate_after_update;
27+ ALTER TABLE session_conversations DROP COLUMN route_context_json;
28+ DROP INDEX idx_agent_transcript_event_identity_sequence;
29+ PRAGMA user_version = 17;
30+ UPDATE schema_meta SET schema_version = 17;
31+ ` ) ;
32+ if ( options . schemaDrift === "missing-cache-table" ) {
33+ database . exec ( "DROP TABLE cache_entries;" ) ;
34+ }
35+ database . close ( ) ;
36+ return { databasePath, env } ;
37+ }
38+
1439describe ( "legacy media persistence additive schema repair" , ( ) => {
1540 afterEach ( ( ) => {
1641 closeOpenClawAgentDatabasesForTest ( ) ;
@@ -68,4 +93,70 @@ describe("legacy media persistence additive schema repair", () => {
6893 repaired . close ( ) ;
6994 }
7095 } ) ;
96+
97+ it ( "repairs v17 additive session schema before canonical index validation" , async ( ) => {
98+ const { databasePath, env } = createV17AdditiveFixture ( ) ;
99+ const { DatabaseSync } = requireNodeSqlite ( ) ;
100+ const result = await migrateLegacyMediaPersistence ( { env } ) ;
101+ expect ( result . warnings ) . toEqual ( [ ] ) ;
102+ const repaired = new DatabaseSync ( databasePath , { readOnly : true } ) ;
103+ try {
104+ expect ( repaired . prepare ( "PRAGMA user_version" ) . get ( ) ) . toEqual ( {
105+ user_version : OPENCLAW_AGENT_SCHEMA_VERSION ,
106+ } ) ;
107+ expect (
108+ repaired
109+ . prepare (
110+ "SELECT name FROM pragma_table_info('session_conversations') WHERE name = 'route_context_json'" ,
111+ )
112+ . get ( ) ,
113+ ) . toEqual ( { name : "route_context_json" } ) ;
114+ expect (
115+ repaired
116+ . prepare (
117+ "SELECT name FROM sqlite_schema WHERE type = 'trigger' AND name = 'session_conversations_route_context_invalidate_after_update'" ,
118+ )
119+ . get ( ) ,
120+ ) . toEqual ( { name : "session_conversations_route_context_invalidate_after_update" } ) ;
121+ expect (
122+ repaired
123+ . prepare (
124+ "SELECT name FROM sqlite_schema WHERE type = 'index' AND name = 'idx_agent_transcript_event_identity_sequence'" ,
125+ )
126+ . get ( ) ,
127+ ) . toEqual ( { name : "idx_agent_transcript_event_identity_sequence" } ) ;
128+ } finally {
129+ repaired . close ( ) ;
130+ }
131+ } ) ;
132+
133+ it ( "keeps non-additive v17 schema drift rejected during index repair" , async ( ) => {
134+ const { databasePath, env } = createV17AdditiveFixture ( {
135+ schemaDrift : "missing-cache-table" ,
136+ } ) ;
137+ const { DatabaseSync } = requireNodeSqlite ( ) ;
138+ const result = await migrateLegacyMediaPersistence ( { env } ) ;
139+ expect ( result . warnings ) . toHaveLength ( 1 ) ;
140+ expect ( result . warnings [ 0 ] ) . toMatch ( / m i s s i n g t a b l e c a c h e _ e n t r i e s / ) ;
141+ const rejected = new DatabaseSync ( databasePath , { readOnly : true } ) ;
142+ try {
143+ expect ( rejected . prepare ( "PRAGMA user_version" ) . get ( ) ) . toEqual ( { user_version : 17 } ) ;
144+ expect (
145+ rejected
146+ . prepare (
147+ "SELECT name FROM pragma_table_info('session_conversations') WHERE name = 'route_context_json'" ,
148+ )
149+ . get ( ) ,
150+ ) . toBeUndefined ( ) ;
151+ expect (
152+ rejected
153+ . prepare (
154+ "SELECT name FROM sqlite_schema WHERE type = 'index' AND name = 'idx_agent_transcript_event_identity_sequence'" ,
155+ )
156+ . get ( ) ,
157+ ) . toBeUndefined ( ) ;
158+ } finally {
159+ rejected . close ( ) ;
160+ }
161+ } ) ;
71162} ) ;
0 commit comments