@@ -59,13 +59,47 @@ function _exportDB() {
5959function _copyRows ( src , dst , table ) {
6060 const result = src . exec ( `SELECT * FROM ${ table } ` ) ;
6161 if ( ! result . length || ! result [ 0 ] . values . length ) return ;
62- const cols = result [ 0 ] . columns ;
63- const placeholders = cols . map ( ( ) => "?" ) . join ( "," ) ;
62+ // Shard may have extra columns that the frontend's schema no longer
63+ // defines (e.g. summary_format_version). Only copy columns that
64+ // actually exist in the destination table.
65+ var dstCols = { } ;
66+ try {
67+ var info = dst . exec ( "PRAGMA table_info(" + table + ")" ) ;
68+ if ( info . length && info [ 0 ] . values ) {
69+ for ( var i = 0 ; i < info [ 0 ] . values . length ; i ++ ) {
70+ dstCols [ info [ 0 ] . values [ i ] [ 1 ] ] = true ;
71+ }
72+ }
73+ } catch ( e ) { /* fall through — use all source columns */ }
74+ const srcCols = result [ 0 ] . columns ;
75+ var cols = srcCols ;
76+ var colIndexes = null ;
77+ if ( Object . keys ( dstCols ) . length ) {
78+ cols = [ ] ;
79+ colIndexes = [ ] ;
80+ for ( var j = 0 ; j < srcCols . length ; j ++ ) {
81+ if ( dstCols [ srcCols [ j ] ] ) {
82+ cols . push ( srcCols [ j ] ) ;
83+ colIndexes . push ( j ) ;
84+ }
85+ }
86+ }
87+ if ( ! cols . length ) return ;
88+ const placeholders = cols . map ( function ( ) { return "?" ; } ) . join ( "," ) ;
6489 const stmt = dst . prepare (
65- ` INSERT OR IGNORE INTO ${ table } ( ${ cols . join ( "," ) } ) VALUES (${ placeholders } )`
90+ " INSERT OR IGNORE INTO " + table + " (" + cols . join ( "," ) + " ) VALUES (" + placeholders + ")"
6691 ) ;
67- for ( const row of result [ 0 ] . values ) {
68- stmt . bind ( row ) ;
92+ for ( var k = 0 ; k < result [ 0 ] . values . length ; k ++ ) {
93+ var row = result [ 0 ] . values [ k ] ;
94+ if ( colIndexes ) {
95+ var filtered = [ ] ;
96+ for ( var m = 0 ; m < colIndexes . length ; m ++ ) {
97+ filtered . push ( row [ colIndexes [ m ] ] ) ;
98+ }
99+ stmt . bind ( filtered ) ;
100+ } else {
101+ stmt . bind ( row ) ;
102+ }
69103 stmt . step ( ) ;
70104 stmt . reset ( ) ;
71105 }
0 commit comments