Skip to content

Commit

Permalink
use raw query to delete old token prices
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco committed Jan 12, 2024
1 parent 1f0ae92 commit 9d8e6c5
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions modules/token/lib/token-price.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,28 +256,10 @@ export class TokenPriceService {
}

public async purgeOldTokenPricesForAllChains(): Promise<number> {
const purgeBeforeTimestamp = moment().startOf('day').subtract(100, 'days').utc().unix();
const oldPrices = await prisma.prismaTokenPrice.findMany({
where: {
timestamp: { lt: purgeBeforeTimestamp },
},
});

// returns all non midnight prices
const tobeDeleted = _.uniq(oldPrices.filter((tokenPrice) => tokenPrice.timestamp % secondsPerDay !== 0));

//apparently prisma has a limitation on delete
const chunks = _.chunk(tobeDeleted, 1000);

for (const chunk of chunks) {
await prisma.prismaTokenPrice.deleteMany({
where: {
timestamp: { in: chunk.map((tokenPrice) => tokenPrice.timestamp) },
},
});
}
const deleted =
await prisma.$executeRaw`DELETE FROM "PrismaTokenPrice" WHERE DATE(to_timestamp(timestamp)) != to_timestamp(timestamp) AND to_timestamp(timestamp) < CURRENT_DATE - INTERVAL '100 days'`;

return tobeDeleted.length;
return deleted;
}

private async updateCandleStickData() {
Expand Down

0 comments on commit 9d8e6c5

Please sign in to comment.