From bf10f5d2c0b0db6f55b977fa2a5eee40b6971a17 Mon Sep 17 00:00:00 2001 From: Michael Bryzek Date: Sun, 9 Jun 2024 09:18:33 -0400 Subject: [PATCH] Refactor deletions to skip over manual deletes done start of june --- api/app/invariants/Invariants.scala | 7 ++-- api/app/invariants/PurgeInvariants.scala | 9 ++--- .../processor/PurgeOldDeletedProcessor.scala | 35 +++++++++---------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/api/app/invariants/Invariants.scala b/api/app/invariants/Invariants.scala index 9ef277db0..1af5041f2 100644 --- a/api/app/invariants/Invariants.scala +++ b/api/app/invariants/Invariants.scala @@ -1,15 +1,12 @@ package invariants import io.flow.postgresql.Query -import play.api.Environment import javax.inject.Inject case class Invariant(name: String, query: Query) -class Invariants @Inject() ( - env: Environment - ) { +class Invariants @Inject() () { val all: Seq[Invariant] = - TaskInvariants.all ++ PurgeInvariants.all(env) + TaskInvariants.all ++ PurgeInvariants.all } diff --git a/api/app/invariants/PurgeInvariants.scala b/api/app/invariants/PurgeInvariants.scala index 3e31f843a..6af0f4a0e 100644 --- a/api/app/invariants/PurgeInvariants.scala +++ b/api/app/invariants/PurgeInvariants.scala @@ -2,8 +2,6 @@ package invariants import io.flow.postgresql.Query import org.joda.time.DateTime -import play.api.Environment -import processor.PurgeOldDeleted object PurgeInvariants { private[this] case class PurgeTable(name: String, retentionMonths: Int) @@ -13,13 +11,12 @@ object PurgeInvariants { PurgeTable("versions", 6) ) - def all(env: Environment): Seq[Invariant] = Tables.map { t => - val timestamp = Seq(DateTime.now.minusMonths(t.retentionMonths+1), PurgeOldDeleted.cutoff(env)).max + val all: Seq[Invariant] = Tables.map { t => Invariant( s"old_deleted_records_purged_from_${t.name}", Query( - s"select count(*) from ${t.name} where deleted_at < {cutoff}::timestamptz" - ).bind("cutoff", timestamp) + s"select count(*) from ${t.name} where deleted_at < {deleted_at}::timestamptz" + ).bind("deleted_at", DateTime.now.minusMonths(t.retentionMonths+1)) ) } } diff --git a/api/app/processor/PurgeOldDeletedProcessor.scala b/api/app/processor/PurgeOldDeletedProcessor.scala index a98c40296..313dcccdb 100644 --- a/api/app/processor/PurgeOldDeletedProcessor.scala +++ b/api/app/processor/PurgeOldDeletedProcessor.scala @@ -1,26 +1,17 @@ package processor -import cats.implicits._ import cats.data.ValidatedNec +import cats.implicits._ import db.{Authorization, OrganizationsDao, UsersDao} import io.apibuilder.task.v0.models.TaskType import io.flow.postgresql.Query import org.joda.time.DateTime -import play.api.{Environment, Mode} +import play.api.Environment import play.api.db.Database import javax.inject.Inject -import scala.sys.env import scala.util.{Failure, Success, Try} -object PurgeOldDeleted { - private[this] val cutoffProd = DateTime.parse("2024-06-05T12:00:00.0+00") - def cutoff(env: Environment): DateTime = env.mode match { - case Mode.Prod => cutoffProd - case _ => cutoffProd.minusYears(10) - } - -} class PurgeOldDeletedProcessor @Inject()( args: TaskProcessorArgs, @@ -30,7 +21,6 @@ class PurgeOldDeletedProcessor @Inject()( organizationsDao: OrganizationsDao ) extends TaskProcessor(args, TaskType.PurgeOldDeleted) { import DeleteMetadata._ - private[this] val cutoff: DateTime = PurgeOldDeleted.cutoff(env) override def processRecord(id: String): ValidatedNec[String, Unit] = { delete("versions", "version_guid", VersionSoft) @@ -42,7 +32,7 @@ class PurgeOldDeletedProcessor @Inject()( private[this] def purgeOldOrganizations(): ValidatedNec[String, Unit] = { organizationsDao.findAll( Authorization.All, - deletedAtBefore = Some(Seq(DateTime.now.minusYears(1), cutoff).max), + deletedAtBefore = Some(DateTime.now.minusMonths(6)), isDeleted = Some(true), limit = 1000, offset = 0, @@ -57,27 +47,36 @@ class PurgeOldDeletedProcessor @Inject()( } private[processor] def delete(parentTable: String, column: String, tables: Seq[String]): Unit = { + println(s"starting delete $parentTable") tables.foreach { table => + println(s"Delete from child $table") + + exec( + s""" + |update $table + | set deleted_at = deleted_at - interval '45 days' + | where deleted_at >= '2024-06-01' + | and deleted_at < '2024-06-06' + |""".stripMargin) exec( s""" |delete from $table | where deleted_at < now() - interval '45 days' - | and deleted_at >= {cutoff}::timestamptz | and $column in ( - | select guid from $parentTable where deleted_at <= now() - interval '6 months' - |) + | select guid from $parentTable where deleted_at <= now() - interval '6 months' + | ) |""".stripMargin ) } } private[this] def exec(q: String): Unit = { - exec(Query(q).bind("deleted_by_guid", usersDao.AdminUser.guid).bind("cutoff", cutoff)) + exec(Query(q).bind("deleted_by_guid", usersDao.AdminUser.guid)) } private[this] def exec(q: Query): Unit = { db.withConnection { c => - q.anormSql().executeUpdate()(c) + q.withDebugging().anormSql().executeUpdate()(c) } () }