Skip to content

Commit

Permalink
Raw query purge (#42)
Browse files Browse the repository at this point in the history
* stop token price purge for now

* purge tokens using raw query
  • Loading branch information
franzns authored Jan 18, 2024
1 parent bf17d89 commit 27637b3
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 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,11 @@ 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) },
},
});
}
// DATE(to_timestamp(timestamp)) will return the midnight timestamp. We'll delete all prices that are not midnight timestamps AND are older than 100 days.
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 27637b3

Please sign in to comment.