Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/indexer/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
} as const;

/** Index names created by the schema-manager migration (#259). */
export const SCHEMA_MANAGER_INDEXES = {

Check failure on line 78 in src/indexer/db.ts

View workflow job for this annotation

GitHub Actions / build

Cannot redeclare block-scoped variable 'SCHEMA_MANAGER_INDEXES'.
monitoredContractsActive: "idx_monitored_contracts_active",
eventsCreatedAt: "idx_events_created_at",
eventsContractTypeLedger: "idx_events_contract_type_ledger",
Expand Down Expand Up @@ -201,7 +201,7 @@
];

/** Index names created by the SQLite schema manager lookup-index migration (#259). */
export const SCHEMA_MANAGER_INDEXES = {

Check failure on line 204 in src/indexer/db.ts

View workflow job for this annotation

GitHub Actions / build

Cannot redeclare block-scoped variable 'SCHEMA_MANAGER_INDEXES'.
monitoredContractsActive: "idx_monitored_contracts_active",
eventsCreatedAt: "idx_events_created_at",
eventsContractTypeLedger: "idx_events_contract_type_ledger",
Expand All @@ -216,7 +216,7 @@
}

/** Index names created by the version-5 migration (#259), for test assertions. */
export const SCHEMA_MANAGER_INDEXES = {

Check failure on line 219 in src/indexer/db.ts

View workflow job for this annotation

GitHub Actions / build

Cannot redeclare block-scoped variable 'SCHEMA_MANAGER_INDEXES'.
monitoredContractsActive: "idx_monitored_contracts_active",
eventsCreatedAt: "idx_events_created_at",
eventsContractTypeLedger: "idx_events_contract_type_ledger",
Expand Down Expand Up @@ -499,7 +499,7 @@
* and tests can assert the exact lookup indexes the schema manager relies on
* without hardcoding names (#259).
*/
export const SCHEMA_MANAGER_INDEXES = [

Check failure on line 502 in src/indexer/db.ts

View workflow job for this annotation

GitHub Actions / build

Cannot redeclare block-scoped variable 'SCHEMA_MANAGER_INDEXES'.
"idx_events_contract_id",
"idx_events_ledger_sequence",
"idx_events_contract_ledger",
Expand Down Expand Up @@ -998,7 +998,7 @@
* Returns the number of rows actually inserted (excludes rows ignored as
* duplicates).
*/
export function insertHistoricalEventBatch(events: EventRow[]): number {

Check failure on line 1001 in src/indexer/db.ts

View workflow job for this annotation

GitHub Actions / build

Duplicate function implementation.

Check failure on line 1001 in src/indexer/db.ts

View workflow job for this annotation

GitHub Actions / build

Cannot redeclare exported variable 'insertHistoricalEventBatch'.
const db = getDb();

const insertStmt = db.prepare(`
Expand Down Expand Up @@ -1149,6 +1149,47 @@
});
}

/**
* Insert a batch of events WITHOUT touching the live indexer_state ledger
* pointer. Used for custom historical event imports (event_type_filter's
* dynamic start/end ledger support) so a backfill over an arbitrary past
* range can never advance or rewind last_ledger_sequence - only the live
* poller (insertEventBatch, driven strictly by lastLedger+1..currentLedger)
* is allowed to move that pointer. Rows still go through INSERT OR IGNORE
* against the same UNIQUE(contract_id, ledger_sequence, event_type)
* constraint, so re-running a historical import is idempotent exactly like
* the live poller.
*
* Returns the number of rows actually inserted (excludes rows ignored as
* duplicates).
*/
export function insertHistoricalEventBatch(events: EventRow[]): number {

Check failure on line 1166 in src/indexer/db.ts

View workflow job for this annotation

GitHub Actions / build

Duplicate function implementation.

Check failure on line 1166 in src/indexer/db.ts

View workflow job for this annotation

GitHub Actions / build

Cannot redeclare exported variable 'insertHistoricalEventBatch'.
const db = getDb();

const insertStmt = db.prepare(`
INSERT OR IGNORE INTO events
(contract_id, event_type, ledger_sequence, timestamp, data_json)
VALUES (?, ?, ?, ?, ?)
`);

const batchTransaction = db.transaction(() => {
let inserted = 0;
for (const ev of events) {
const result = insertStmt.run(
ev.contractId,
ev.eventType,
ev.ledgerSequence,
ev.timestamp,
ev.dataJson
);
if (result.changes > 0) inserted++;
}
return inserted;
});

return batchTransaction();
}

// ---------------------------------------------------------------------------
// Event queries
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -1548,7 +1589,7 @@
* it rejects any event whose ledger_sequence falls outside the declared
* range so a mistyped range can't silently import the wrong window.
*/
export function insertHistoricalEventBatch(

Check failure on line 1592 in src/indexer/db.ts

View workflow job for this annotation

GitHub Actions / build

Cannot redeclare exported variable 'insertHistoricalEventBatch'.
events: EventRow[],
range: { startLedger: unknown; endLedger: unknown },
options: InsertHistoricalEventBatchOptions = {},
Expand Down
Loading