diff --git a/modules/token/lib/token-price.service.ts b/modules/token/lib/token-price.service.ts index 9b51277fb..572b450bb 100644 --- a/modules/token/lib/token-price.service.ts +++ b/modules/token/lib/token-price.service.ts @@ -256,28 +256,11 @@ export class TokenPriceService { } public async purgeOldTokenPricesForAllChains(): Promise { - 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() {