Skip to content

Commit

Permalink
fix: queries for new pg schema
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Jan 28, 2025
1 parent 81ca05d commit 94fb397
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/pg/counts/counts-pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
21 changes: 13 additions & 8 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class PgStore extends BasePgStore {

async getInscriptionsIndexETag(): Promise<string> {
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;
}
Expand Down Expand Up @@ -118,12 +118,17 @@ export class PgStore extends BasePgStore {

async getInscriptionETag(args: InscriptionIdentifier): Promise<string | undefined> {
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) {
Expand Down Expand Up @@ -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``
}
${
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 94fb397

Please sign in to comment.