Skip to content

Commit 24b35c2

Browse files
committed
add debug logging
1 parent 45e1daf commit 24b35c2

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

packages/core/bulkload-logging.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
This file contains the logging changes to apply to bulkLoad method.
2+
3+
Add at start of bulkLoad:
4+
- console.log with table name and row count
5+
- console.log with PK columns and LWW columns
6+
- Initialize counters: processedCount, insertedCount, updatedCount, skippedCount
7+
8+
Inside loop:
9+
- Log if row has no system_id
10+
- Log processing each row with system_id
11+
- Log if existing record found
12+
- Log number of fields to update
13+
- Log UPDATE success/failure with details
14+
- Log INSERT success/failure with details
15+
- Log constraint violations with strategy
16+
- Log dirty mark checks
17+
18+
At end:
19+
- Log summary with all counters
20+
- Log stream notification
21+
22+
Also add logging to _insertFromServer and _updateFromServer methods.

packages/core/src/database/declarative-database.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)