Repository: ponder-sh/ponder
Pull request #1544 (2025-02-19 15:02:26)

fix `db.delete`


Patch:
diff --git a/.changeset/fair-teachers-retire.md b/.changeset/fair-teachers-retire.md
new file mode 100644
index 00000000..a9e8e3a9
--- /dev/null
+++ b/.changeset/fair-teachers-retire.md
@@ -0,0 +1,5 @@
+---
+"ponder": patch
+---
+
+Fixed a bug with `db.delete()` sometimes not persisting to the database.
diff --git a/packages/core/src/indexing-store/cache.ts b/packages/core/src/indexing-store/cache.ts
index 3c750536..293ac132 100644
--- a/packages/core/src/indexing-store/cache.ts
+++ b/packages/core/src/indexing-store/cache.ts
@@ -350,43 +350,22 @@ export const createIndexingCache = ({
 
       return row;
     },
-    delete({ table, key, db }) {
-      const inBuffer =
-        insertBuffer.get(table)!.delete(getCacheKey(table, key)) ||
-        updateBuffer.get(table)!.delete(getCacheKey(table, key));
-
-      if (
-        (cache.get(table)!.has(getCacheKey(table, key)) &&
-          cache.get(table)!.get(getCacheKey(table, key))) ||
-        (spillover.get(table)!.has(getCacheKey(table, key)) &&
-          spillover.get(table)!.get(getCacheKey(table, key)))
-      ) {
-        cache.get(table)!.delete(getCacheKey(table, key));
-        spillover.get(table)!.delete(getCacheKey(table, key));
-        isCacheComplete = false;
+    async delete({ table, key, db }) {
+      const ck = getCacheKey(table, key);
 
-        return db
-          .delete(table)
-          .where(getWhereCondition(table, key))
-          .then(() => true);
-      }
+      const inInsertBuffer = insertBuffer.get(table)!.delete(ck);
+      const inUpdateBuffer = updateBuffer.get(table)!.delete(ck);
 
-      if (
-        cache.get(table)!.has(getCacheKey(table, key)) ||
-        spillover.get(table)?.has(getCacheKey(table, key))
-      ) {
-        return inBuffer;
-      }
+      cache.get(table)!.delete(ck);
+      spillover.get(table)!.delete(ck);
 
-      if (isCacheComplete) {
-        return inBuffer;
-      }
-
-      return db
+      const inDb = await db
         .delete(table)
         .where(getWhereCondition(table, key))
         .returning()
         .then((result) => result.length > 0);
+
+      return inInsertBuffer || inUpdateBuffer || inDb;
     },
     async flush({ db }) {
       for (const table of cache.keys()) {


Test patch:
diff --git a/packages/core/src/indexing-store/historical.test.ts b/packages/core/src/indexing-store/historical.test.ts
index 4000bd09..cc337894 100644
--- a/packages/core/src/indexing-store/historical.test.ts
+++ b/packages/core/src/indexing-store/historical.test.ts
@@ -314,14 +314,15 @@ test("update", async (context) => {
 });
 
 test("delete", async (context) => {
-  const { database } = await setupDatabaseServices(context);
-
   const schema = {
     account: onchainTable("account", (p) => ({
       address: p.hex().primaryKey(),
       balance: p.bigint().notNull(),
     })),
   };
+  const { database } = await setupDatabaseServices(context, {
+    schemaBuild: { schema },
+  });
 
   const indexingCache = createIndexingCache({
     common: context.common,
@@ -351,6 +352,9 @@ test("delete", async (context) => {
   await indexingStore
     .insert(schema.account)
     .values({ address: zeroAddress, balance: 12n });
+  await indexingStore
+    .update(schema.account, { address: zeroAddress })
+    .set({ balance: 12n });
 
   deleted = await indexingStore.delete(schema.account, {
     address: zeroAddress,