-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |