Skip to content

Commit

Permalink
Refactor deletions to skip over manual deletes done start of june
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Jun 9, 2024
1 parent a3a64c5 commit bf10f5d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
7 changes: 2 additions & 5 deletions api/app/invariants/Invariants.scala
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 3 additions & 6 deletions api/app/invariants/PurgeInvariants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
)
}
}
35 changes: 17 additions & 18 deletions api/app/processor/PurgeOldDeletedProcessor.scala
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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)
}
()
}
Expand Down

0 comments on commit bf10f5d

Please sign in to comment.