Skip to content

Commit

Permalink
fix(store-sync,store-indexer): make last updated block number not null (
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Nov 29, 2023
1 parent 1b5eb0d commit 504e25d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-rockets-return.md
@@ -0,0 +1,5 @@
---
"@latticexyz/store-indexer": patch
---

Records are now ordered by `lastUpdatedBlockNumber` at the Postgres SQL query level
5 changes: 5 additions & 0 deletions .changeset/cyan-vans-pay.md
@@ -0,0 +1,5 @@
---
"@latticexyz/store-sync": major
---

`lastUpdatedBlockNumber` columns in Postgres storage adapters are no longer nullable
5 changes: 3 additions & 2 deletions packages/store-indexer/src/postgres/getLogs.ts
Expand Up @@ -2,7 +2,7 @@ import { PgDatabase } from "drizzle-orm/pg-core";
import { Hex } from "viem";
import { StorageAdapterLog, SyncFilter } from "@latticexyz/store-sync";
import { tables } from "@latticexyz/store-sync/postgres";
import { and, eq, or } from "drizzle-orm";
import { and, asc, eq, or } from "drizzle-orm";
import { decodeDynamicField } from "@latticexyz/protocol-parser";
import { bigIntMax } from "@latticexyz/common/utils";

Expand Down Expand Up @@ -53,7 +53,8 @@ export async function getLogs(
const records = await database
.select()
.from(tables.recordsTable)
.where(or(...conditions));
.where(or(...conditions))
.orderBy(asc(tables.recordsTable.lastUpdatedBlockNumber));

const blockNumber = records.reduce(
(max, record) => bigIntMax(max, record.lastUpdatedBlockNumber ?? 0n),
Expand Down
7 changes: 7 additions & 0 deletions packages/store-sync/src/postgres-decoded/buildTable.test.ts
Expand Up @@ -21,37 +21,44 @@ describe("buildTable", () => {
name: column.name,
dataType: column.dataType,
sqlName: column.sqlName,
notNull: column.notNull,
}))
).toMatchInlineSnapshot(`
{
"__keyBytes": {
"dataType": "custom",
"name": "__key_bytes",
"notNull": true,
"sqlName": "bytea",
},
"__lastUpdatedBlockNumber": {
"dataType": "custom",
"name": "__last_updated_block_number",
"notNull": false,
"sqlName": "numeric",
},
"addr": {
"dataType": "custom",
"name": "addr",
"notNull": true,
"sqlName": "bytea",
},
"name": {
"dataType": "string",
"name": "name",
"notNull": true,
"sqlName": undefined,
},
"x": {
"dataType": "custom",
"name": "x",
"notNull": true,
"sqlName": "integer",
},
"y": {
"dataType": "custom",
"name": "y",
"notNull": true,
"sqlName": "integer",
},
}
Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/postgres/tables.ts
Expand Up @@ -9,7 +9,7 @@ const schemaName = transformSchemaName("mud");
*/
const chainTable = pgSchema(schemaName).table("chain", {
chainId: asNumber("chain_id", "bigint").notNull().primaryKey(),
lastUpdatedBlockNumber: asBigInt("last_updated_block_number", "numeric"),
lastUpdatedBlockNumber: asBigInt("last_updated_block_number", "numeric").notNull(),
});

const recordsTable = pgSchema(schemaName).table(
Expand All @@ -27,7 +27,7 @@ const recordsTable = pgSchema(schemaName).table(
encodedLengths: asHex("encoded_lengths"),
dynamicData: asHex("dynamic_data"),
isDeleted: boolean("is_deleted"),
lastUpdatedBlockNumber: asBigInt("last_updated_block_number", "numeric"),
lastUpdatedBlockNumber: asBigInt("last_updated_block_number", "numeric").notNull(),
},
(table) => ({
pk: primaryKey(table.address, table.tableId, table.keyBytes),
Expand Down

0 comments on commit 504e25d

Please sign in to comment.