Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Jun 6, 2024
1 parent 42f64d7 commit 351d090
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
45 changes: 45 additions & 0 deletions api/app/processor/PurgeOldDeletedProcessor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package processor

import cats.data.ValidatedNec
import cats.implicits._
import db.UsersDao
import io.apibuilder.task.v0.models.TaskType
import io.flow.postgresql.Query
import play.api.db.Database

import javax.inject.Inject


class PurgeOldDeletedProcessor @Inject()(
args: TaskProcessorArgs,
db: Database,
usersDao: UsersDao
) extends TaskProcessor(args, TaskType.PurgeOldDeleted) {
import DeleteMetadata._

override def processRecord(id: String): ValidatedNec[String, Unit] = {
delete("versions", "version_guid", VersionSoft)
delete("applications", "application_guid", ApplicationSoft)
delete("organizations", "organization_guid", OrganizationSoft)
().validNec
}


private[processor] def delete(parentTable: String, column: String, tables: Seq[String]): Unit = {
tables.foreach { table =>
exec(
s"delete from $table where $column in (select guid from $parentTable where deleted_at <= now() - interval '6 months')"
)
}
}

private[this] def exec(q: String): Unit = {
db.withConnection { c =>
Query(q)
.bind("deleted_by_guid", usersDao.AdminUser.guid)
.anormSql()
.executeUpdate()(c)
}
()
}
}
57 changes: 57 additions & 0 deletions api/test/processor/PurgeOldDeletedProcessorSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package processor

import anorm.SqlParser
import db.Helpers
import io.apibuilder.api.v0.models.{Application, Organization}
import io.flow.postgresql.Query
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.db.Database

import java.util.UUID

class PurgeOldDeletedProcessorSpec extends PlaySpec with GuiceOneAppPerSuite with Helpers {

private[this] def processor: CleanupDeletionsProcessor = app.injector.instanceOf[CleanupDeletionsProcessor]
private[this] def database: Database = app.injector.instanceOf[Database]

private[this] def isDeleted(table: String, guid: UUID): Boolean = {
database.withConnection { c =>
Query(s"select count(*) from $table")
.equals("guid", guid)
.as(SqlParser.int(1).*)(c)
}.headOption.exists(_ > 0)
}

private[this] def softDelete(table: String, guid: UUID): Unit = {
database.withConnection { c =>
Query(s"update $table set deleted_at = now() - interval '1 year'")
.equals("guid", guid)
.anormSql()
.executeUpdate()(c)
}
()
}

"organization" in {
def isAppDeleted(app: Application): Boolean = isDeleted("applications", app.guid)

def setup(): (Organization, Application) = {
val org = createOrganization()
val app = createApplication(org)
(org, app)
}

val (_, app) = setup()
val (orgDeleted, appDeleted) = setup()
softDelete("organizations", orgDeleted.guid)

isAppDeleted(app) mustBe false
isAppDeleted(appDeleted) mustBe false

processor.organizations()
isAppDeleted(app) mustBe false
isAppDeleted(appDeleted) mustBe true
}

}

0 comments on commit 351d090

Please sign in to comment.