@@ -10,6 +10,7 @@ import {
1010 BucketStorageAdapter ,
1111 BucketStorageListener ,
1212 Checkpoint ,
13+ PowerSyncControlCommand ,
1314 PSInternalTable ,
1415 SyncLocalDatabaseResult
1516} from './BucketStorageAdapter.js' ;
@@ -99,13 +100,13 @@ export class SqliteBucketStorage extends BaseObserver<BucketStorageListener> imp
99100 return Object . fromEntries ( rows . map ( ( r ) => [ r . name , { atLast : r . count_at_last , sinceLast : r . count_since_last } ] ) ) ;
100101 }
101102
102- async saveSyncData ( batch : SyncDataBatch ) {
103+ async saveSyncData ( batch : SyncDataBatch , fixedKeyFormat : boolean = false ) {
103104 await this . writeTransaction ( async ( tx ) => {
104105 let count = 0 ;
105106 for ( const b of batch . buckets ) {
106107 const result = await tx . execute ( 'INSERT INTO powersync_operations(op, data) VALUES(?, ?)' , [
107108 'save' ,
108- JSON . stringify ( { buckets : [ b . toJSON ( ) ] } )
109+ JSON . stringify ( { buckets : [ b . toJSON ( fixedKeyFormat ) ] } )
109110 ] ) ;
110111 this . logger . debug ( 'saveSyncData' , JSON . stringify ( result ) ) ;
111112 count += b . data . length ;
@@ -413,6 +414,32 @@ export class SqliteBucketStorage extends BaseObserver<BucketStorageListener> imp
413414 async setTargetCheckpoint ( checkpoint : Checkpoint ) {
414415 // No-op for now
415416 }
417+
418+ async control ( op : PowerSyncControlCommand , payload : string | ArrayBuffer | null ) : Promise < string > {
419+ return await this . writeTransaction ( async ( tx ) => {
420+ const [ [ raw ] ] = await tx . executeRaw ( 'SELECT powersync_control(?, ?)' , [ op , payload ] ) ;
421+ return raw ;
422+ } ) ;
423+ }
424+
425+ async hasMigratedSubkeys ( ) : Promise < boolean > {
426+ const { r } = await this . db . get < { r : number } > ( 'SELECT EXISTS(SELECT * FROM ps_kv WHERE key = ?) as r' , [
427+ SqliteBucketStorage . _subkeyMigrationKey
428+ ] ) ;
429+ return r != 0 ;
430+ }
431+
432+ async migrateToFixedSubkeys ( ) : Promise < void > {
433+ await this . writeTransaction ( async ( tx ) => {
434+ await tx . execute ( 'UPDATE ps_oplog SET key = powersync_remove_duplicate_key_encoding(key);' ) ;
435+ await tx . execute ( 'INSERT OR REPLACE INTO ps_kv (key, value) VALUES (?, ?);' , [
436+ SqliteBucketStorage . _subkeyMigrationKey ,
437+ '1'
438+ ] ) ;
439+ } ) ;
440+ }
441+
442+ static _subkeyMigrationKey = 'powersync_js_migrated_subkeys' ;
416443}
417444
418445function hasMatchingPriority ( priority : number , bucket : BucketChecksum ) {
0 commit comments