From 94fb3974c0e1f3873cd7dcba76b446bd00f51183 Mon Sep 17 00:00:00 2001 From: Rafael Cardenas Date: Tue, 28 Jan 2025 09:19:25 -0600 Subject: [PATCH] fix: queries for new pg schema --- src/pg/counts/counts-pg-store.ts | 2 +- src/pg/pg-store.ts | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/pg/counts/counts-pg-store.ts b/src/pg/counts/counts-pg-store.ts index 9788a59..69cb986 100644 --- a/src/pg/counts/counts-pg-store.ts +++ b/src/pg/counts/counts-pg-store.ts @@ -126,7 +126,7 @@ export class CountsPgStore extends BasePgStoreModule { const result = await this.sql<{ count: number }[]>` SELECT COALESCE(SUM(count), 0)::int AS count FROM counts_by_sat_rarity - WHERE sat_rarity IN ${this.sql(satRarity)} + WHERE rarity IN ${this.sql(satRarity)} `; return result[0].count; } diff --git a/src/pg/pg-store.ts b/src/pg/pg-store.ts index b487b7c..f5f5ea5 100644 --- a/src/pg/pg-store.ts +++ b/src/pg/pg-store.ts @@ -75,7 +75,7 @@ export class PgStore extends BasePgStore { async getInscriptionsIndexETag(): Promise { const result = await this.sql<{ etag: string }[]>` - SELECT date_part('epoch', MAX(updated_at))::text AS etag FROM inscriptions + SELECT MAX(timestamp)::text AS etag FROM locations `; return result[0].etag; } @@ -118,12 +118,17 @@ export class PgStore extends BasePgStore { async getInscriptionETag(args: InscriptionIdentifier): Promise { const result = await this.sql<{ etag: string }[]>` - SELECT date_part('epoch', updated_at)::text AS etag - FROM inscriptions + SELECT l.timestamp::text AS etag + FROM inscriptions AS i + INNER JOIN current_locations AS c ON i.ordinal_number = c.ordinal_number + INNER JOIN locations AS l ON + l.ordinal_number = c.ordinal_number AND + l.block_height = c.block_height AND + l.tx_index = c.tx_index WHERE ${ 'genesis_id' in args - ? this.sql`genesis_id = ${args.genesis_id}` - : this.sql`number = ${args.number}` + ? this.sql`i.inscription_id = ${args.genesis_id}` + : this.sql`i.number = ${args.number}` } `; if (result.count > 0) { @@ -225,12 +230,12 @@ export class PgStore extends BasePgStore { } ${ filters?.from_genesis_timestamp - ? sql`AND i.timestamp >= to_timestamp(${filters.from_genesis_timestamp})` + ? sql`AND i.timestamp >= ${filters.from_genesis_timestamp}` : sql`` } ${ filters?.to_genesis_timestamp - ? sql`AND i.timestamp <= to_timestamp(${filters.to_genesis_timestamp})` + ? sql`AND i.timestamp <= ${filters.to_genesis_timestamp}` : sql`` } ${ @@ -300,7 +305,7 @@ export class PgStore extends BasePgStore { WHERE ${ 'number' in args ? this.sql`i.number = ${args.number}` - : this.sql`i.genesis_id = ${args.genesis_id}` + : this.sql`i.inscription_id = ${args.genesis_id}` } AND ( (l.block_height > i.block_height)