@@ -613,9 +613,13 @@ export class DeclarativeDatabase {
613613 ) : Promise < void > {
614614 this . ensureInitialized ( ) ;
615615
616+ console . log ( `[bulkLoad] Starting bulk load for table: ${ tableName } , rows: ${ rows . length } ` ) ;
617+
616618 const tableDef = this . schema . tables . find ( t => t . name === tableName ) ;
617619 if ( ! tableDef ) {
618- throw new Error ( `Table not found: ${ tableName } ` ) ;
620+ const error = `Table not found: ${ tableName } ` ;
621+ console . error ( `[bulkLoad] ${ error } ` ) ;
622+ throw new Error ( error ) ;
619623 }
620624
621625 const pkColumns = tableDef . keys
@@ -626,16 +630,31 @@ export class DeclarativeDatabase {
626630 . filter ( c => c . lww )
627631 . map ( c => c . name ) ;
628632
633+ console . log ( `[bulkLoad] Table: ${ tableName } , PK columns: [${ pkColumns . join ( ', ' ) } ], LWW columns: [${ lwwColumns . join ( ', ' ) } ]` ) ;
634+
635+ let processedCount = 0 ;
636+ let insertedCount = 0 ;
637+ let updatedCount = 0 ;
638+ let skippedCount = 0 ;
639+
629640 for ( const row of rows ) {
630641 const systemId = row [ 'system_id' ] ;
631- if ( ! systemId ) continue ;
642+ if ( ! systemId ) {
643+ console . warn ( `[bulkLoad] Row ${ processedCount } has no system_id, skipping` ) ;
644+ skippedCount ++ ;
645+ processedCount ++ ;
646+ continue ;
647+ }
648+
649+ console . log ( `[bulkLoad] Processing row ${ processedCount } : system_id=${ systemId } ` ) ;
632650
633651 const existing = await this . queryOne ( tableName , {
634652 where : 'system_id = ?' ,
635653 whereArgs : [ systemId ]
636654 } ) ;
637655
638656 if ( existing ) {
657+ console . log ( `[bulkLoad] Row ${ processedCount } : Found existing record, performing UPDATE` ) ;
639658 // UPDATE logic
640659 const valuesToUpdate : Record < string , any > = { } ;
641660 const now = this . hlc . now ( ) ;
@@ -681,22 +700,30 @@ export class DeclarativeDatabase {
681700 }
682701
683702 if ( Object . keys ( valuesToUpdate ) . length > 0 ) {
703+ console . log ( `[bulkLoad] Row ${ processedCount } : ${ Object . keys ( valuesToUpdate ) . length } fields to update:` , Object . keys ( valuesToUpdate ) ) ;
684704 try {
685705 // We need to update system_version as well
686706 valuesToUpdate [ 'system_version' ] = Hlc . toString ( now ) ;
687707
688708 // Use internal update to avoid marking as dirty
689709 await this . _updateFromServer ( tableName , valuesToUpdate , systemId ) ;
710+ console . log ( `[bulkLoad] Row ${ processedCount } : UPDATE successful` ) ;
711+ updatedCount ++ ;
690712 } catch ( e ) {
713+ console . error ( `[bulkLoad] Row ${ processedCount } : UPDATE failed:` , e ) ;
691714 if ( this . _isConstraintViolation ( e ) ) {
715+ console . warn ( `[bulkLoad] Row ${ processedCount } : Constraint violation detected, strategy: ${ onConstraintViolation } ` ) ;
692716 if ( onConstraintViolation === ConstraintViolationStrategy . ThrowException ) {
693717 throw e ;
694718 }
719+ skippedCount ++ ;
695720 // Skip
696721 } else {
697722 throw e ;
698723 }
699724 }
725+ } else {
726+ console . log ( `[bulkLoad] Row ${ processedCount } : No fields to update (all values match existing or controlled by LWW)` ) ;
700727 }
701728
702729 // Check if we should clear the dirty mark by comparing system_version
@@ -716,26 +743,37 @@ export class DeclarativeDatabase {
716743 }
717744 }
718745 } else {
746+ console . log ( `[bulkLoad] Row ${ processedCount } : No existing record found, performing INSERT` ) ;
719747 // INSERT logic
720748 try {
721749 await this . _insertFromServer ( tableName , row ) ;
750+ console . log ( `[bulkLoad] Row ${ processedCount } : INSERT successful` ) ;
751+ insertedCount ++ ;
722752 } catch ( e ) {
753+ console . error ( `[bulkLoad] Row ${ processedCount } : INSERT failed:` , e ) ;
723754 if ( this . _isConstraintViolation ( e ) ) {
755+ console . warn ( `[bulkLoad] Row ${ processedCount } : Constraint violation detected, strategy: ${ onConstraintViolation } ` ) ;
724756 if ( onConstraintViolation === ConstraintViolationStrategy . ThrowException ) {
725757 throw e ;
726758 }
759+ skippedCount ++ ;
727760 // Skip
728761 } else {
729762 throw e ;
730763 }
731764 }
732765 }
766+
767+ processedCount ++ ;
733768 }
734769
770+ console . log ( `[bulkLoad] Completed: processed=${ processedCount } , inserted=${ insertedCount } , updated=${ updatedCount } , skipped=${ skippedCount } ` ) ;
771+ console . log ( `[bulkLoad] Notifying stream manager for table: ${ tableName } ` ) ;
735772 this . streamManager . notifyTableChanged ( tableName ) ;
736773 }
737774
738775 private async _insertFromServer ( tableName : string , values : Record < string , any > ) : Promise < void > {
776+ console . log ( `[_insertFromServer] Table: ${ tableName } , system_id: ${ values [ 'system_id' ] } ` ) ;
739777 const valuesToInsert = { ...values } ;
740778 const now = this . hlc . now ( ) ;
741779 const nowString = Hlc . toString ( now ) ;
@@ -747,6 +785,7 @@ export class DeclarativeDatabase {
747785
748786 // Mark as server origin
749787 valuesToInsert [ 'system_is_local_origin' ] = 0 ;
788+ console . log ( `[_insertFromServer] Columns to insert: ${ Object . keys ( valuesToInsert ) . length } ` ) ;
750789
751790 // Ensure LWW columns have HLCs if not provided
752791 const tableDef = this . schema . tables . find ( t => t . name === tableName ) ;
@@ -763,8 +802,10 @@ export class DeclarativeDatabase {
763802 const columnList = columns . map ( c => `"${ c } "` ) . join ( ', ' ) ;
764803
765804 const sql = `INSERT INTO "${ tableName } " (${ columnList } ) VALUES (${ placeholders } )` ;
805+ console . log ( `[_insertFromServer] Executing INSERT with ${ columns . length } columns` ) ;
766806 const stmt = this . adapter . prepare ( sql ) ;
767- await stmt . run ( ...Object . values ( valuesToInsert ) ) ;
807+ const result = await stmt . run ( ...Object . values ( valuesToInsert ) ) ;
808+ console . log ( `[_insertFromServer] INSERT completed, changes: ${ result . changes } ` ) ;
768809
769810 // Don't mark as dirty - this came from the server
770811 }
@@ -774,14 +815,17 @@ export class DeclarativeDatabase {
774815 values : Record < string , any > ,
775816 systemId : string
776817 ) : Promise < void > {
818+ console . log ( `[_updateFromServer] Table: ${ tableName } , system_id: ${ systemId } ` ) ;
777819 const columns = Object . keys ( values ) ;
778820 const setClause = columns . map ( c => `"${ c } " = ?` ) . join ( ', ' ) ;
779821
780822 const sql = `UPDATE "${ tableName } " SET ${ setClause } WHERE system_id = ?` ;
781823 const params = [ ...Object . values ( values ) , systemId ] ;
782824
825+ console . log ( `[_updateFromServer] Executing UPDATE with ${ columns . length } columns` ) ;
783826 const stmt = this . adapter . prepare ( sql ) ;
784- await stmt . run ( ...params ) ;
827+ const result = await stmt . run ( ...params ) ;
828+ console . log ( `[_updateFromServer] UPDATE completed, changes: ${ result . changes } ` ) ;
785829
786830 // Don't mark as dirty - this came from the server
787831 }
0 commit comments