@@ -10,8 +10,11 @@ import {
1010 resolvePairingSetupAccess ,
1111 type PairingSetupAccess ,
1212} from "../shared/device-bootstrap-profile.js" ;
13- import { ensureDevicePairSetupCompletionSchema } from "../state/openclaw-state-db-schema-additive.js" ;
14- import { tableExists } from "../state/openclaw-state-db-schema-helpers.js" ;
13+ import {
14+ ensureDevicePairSetupBootstrapSchema ,
15+ ensureDevicePairSetupCompletionSchema ,
16+ } from "../state/openclaw-state-db-schema-additive.js" ;
17+ import { tableExists , tableHasColumn } from "../state/openclaw-state-db-schema-helpers.js" ;
1518import type {
1619 DB as OpenClawStateKyselyDatabase ,
1720 DevicePairingPaired ,
@@ -46,6 +49,19 @@ type DevicePairingStoreState = {
4649 pairedByDeviceId : Record < string , PairedDevice > ;
4750} ;
4851
52+ const DEVICE_BOOTSTRAP_TOKEN_COLUMNS_WITHOUT_SETUP = [
53+ "device_id" ,
54+ "issued_at_ms" ,
55+ "last_used_at_ms" ,
56+ "pending_profile_json" ,
57+ "profile_json" ,
58+ "public_key" ,
59+ "redeemed_profile_json" ,
60+ "token" ,
61+ "token_key" ,
62+ "ts" ,
63+ ] as const satisfies readonly ( keyof DeviceBootstrapTokens ) [ ] ;
64+
4965type DevicePairingStoreTarget = "pending" | "paired" | "both" ;
5066
5167type DevicePairingStoreValidityToken = {
@@ -515,10 +531,16 @@ export function loadDeviceBootstrapTokenRecords(
515531 const { db } = openOpenClawStateDatabase ( resolveDevicePairingStateDbOptions ( baseDir ) ) ;
516532 const kysely = getNodeSqliteKysely < OpenClawStateKyselyDatabase > ( db ) ;
517533 const state : Record < string , DeviceBootstrapTokenRecord > = { } ;
518- for ( const row of executeSqliteQuerySync (
519- db ,
520- kysely . selectFrom ( "device_bootstrap_tokens" ) . selectAll ( ) ,
521- ) . rows ) {
534+ const hasSetupId = tableHasColumn ( db , "device_bootstrap_tokens" , "setup_id" ) ;
535+ const rows : DeviceBootstrapTokens [ ] = hasSetupId
536+ ? executeSqliteQuerySync ( db , kysely . selectFrom ( "device_bootstrap_tokens" ) . selectAll ( ) ) . rows
537+ : executeSqliteQuerySync (
538+ db ,
539+ kysely
540+ . selectFrom ( "device_bootstrap_tokens" )
541+ . select ( DEVICE_BOOTSTRAP_TOKEN_COLUMNS_WITHOUT_SETUP ) ,
542+ ) . rows . map ( ( row ) => Object . assign ( row , { setup_id : null } ) ) ;
543+ for ( const row of rows ) {
522544 state [ row . token_key ] = fromBootstrapRow ( row ) ;
523545 }
524546 return state ;
@@ -530,13 +552,24 @@ export function persistDeviceBootstrapTokenRecords(
530552 baseDir ?: string ,
531553) : void {
532554 runOpenClawStateWriteTransaction ( ( { db } ) => {
533- const kysely = getNodeSqliteKysely < OpenClawStateKyselyDatabase > ( db ) ;
534- executeSqliteQuerySync ( db , kysely . deleteFrom ( "device_bootstrap_tokens" ) ) ;
535555 const rows = Object . entries ( state ) . map ( ( [ tokenKey , record ] ) =>
536556 toBootstrapRow ( tokenKey , record ) ,
537557 ) ;
558+ if ( rows . some ( ( row ) => row . setup_id !== null ) ) {
559+ ensureDevicePairSetupBootstrapSchema ( db ) ;
560+ }
561+ const kysely = getNodeSqliteKysely < OpenClawStateKyselyDatabase > ( db ) ;
562+ executeSqliteQuerySync ( db , kysely . deleteFrom ( "device_bootstrap_tokens" ) ) ;
538563 if ( rows . length > 0 ) {
539- executeSqliteQuerySync ( db , kysely . insertInto ( "device_bootstrap_tokens" ) . values ( rows ) ) ;
564+ if ( tableHasColumn ( db , "device_bootstrap_tokens" , "setup_id" ) ) {
565+ executeSqliteQuerySync ( db , kysely . insertInto ( "device_bootstrap_tokens" ) . values ( rows ) ) ;
566+ } else {
567+ const rowsWithoutSetup = rows . map ( ( { setup_id : _setupId , ...row } ) => row ) ;
568+ executeSqliteQuerySync (
569+ db ,
570+ kysely . insertInto ( "device_bootstrap_tokens" ) . values ( rowsWithoutSetup ) ,
571+ ) ;
572+ }
540573 }
541574 } , resolveDevicePairingStateDbOptions ( baseDir ) ) ;
542575}
@@ -558,6 +591,7 @@ export function consumeDeviceBootstrapTokenWithSetupCompletionInTransaction(para
558591 return null ;
559592 }
560593 return runOpenClawStateWriteTransaction ( ( { db } ) => {
594+ ensureDevicePairSetupBootstrapSchema ( db ) ;
561595 ensureDevicePairSetupCompletionSchema ( db ) ;
562596 const kysely = getNodeSqliteKysely < OpenClawStateKyselyDatabase > ( db ) ;
563597 // Verification precedes async pairing work, so expiry must be checked again
0 commit comments