Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Jun 4, 2024
1 parent 19c232c commit 5a6c57a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
26 changes: 22 additions & 4 deletions api/app/util/ProcessDeletes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,28 @@ import play.api.db.Database

import javax.inject.Inject

object ProcessDeletes {
val OrganizationChildren: Seq[String] = Seq(
"public.applications",
"public.membership_requests",
"public.memberships",
"public.organization_attribute_values",
"public.organization_domains",
"public.subscriptions"
)
val ApplicationChildren: Seq[String] = Seq(
"public.changes", "public.versions", "public.watches"
)
val VersionChildren = Seq(
"cache.services", "public.originals"
)
}

class ProcessDeletes @Inject() (
db: Database,
usersDao: UsersDao
) {
import ProcessDeletes._

def all(): Unit = {
organizations()
Expand All @@ -18,7 +36,7 @@ class ProcessDeletes @Inject() (
}

private[util] def organizations(): Unit = {
Seq("applications", "memberships", "membership_requests", "organization_attribute_values", "organization_domains", "subscriptions").foreach { table =>
OrganizationChildren.foreach { table =>
exec(
s"""
|update $table
Expand All @@ -39,7 +57,7 @@ class ProcessDeletes @Inject() (
}

private[util] def applications(): Unit = {
Seq("versions", "watches").foreach { table =>
ApplicationChildren.foreach { table =>
exec(
s"""
|update $table
Expand All @@ -57,7 +75,8 @@ class ProcessDeletes @Inject() (
| where version_guid in (select guid from versions where deleted_at is not null)
|""".stripMargin
)
Seq("originals", "cache.services").foreach { table =>

VersionChildren.foreach { table =>
exec(
s"""
|update $table
Expand All @@ -73,7 +92,6 @@ class ProcessDeletes @Inject() (
db.withConnection { c =>
Query(q)
.bind("deleted_by_guid", usersDao.AdminUser.guid)
.withDebugging()
.anormSql()
.executeUpdate()(c)
}
Expand Down
33 changes: 33 additions & 0 deletions api/test/util/ProcessDeletesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,37 @@ class ProcessDeletesSpec extends PlaySpec with GuiceOneAppPerSuite with Helpers
isVersionDeleted(version) mustBe false
isVersionDeleted(versionDeleted) mustBe true
}

"have all child tables" must {
def getTables(columnName: String): Seq[String] = {
database.withConnection { c =>
Query(
"""
|select c1.table_schema || '.' || c1.table_name
| from information_schema.columns c1
| join information_schema.columns c2 on c2.table_schema = c1.table_schema and c2.table_name = c1.table_name
| where c1.column_name = 'deleted_by_guid'
| and c2.column_name = {column_name}
| order by 1
|""".stripMargin
)
.bind("column_name", columnName)
.as(SqlParser.str(1).*)(c)
}
}

"OrganizationChildren" in {
getTables("organization_guid") mustBe ProcessDeletes.OrganizationChildren
}

"ApplicationChildren" in {
val Ignore = Seq("public.application_moves")
getTables("application_guid")
.filterNot(Ignore.contains) mustBe ProcessDeletes.ApplicationChildren
}

"VersionChildren" in {
getTables("version_guid") mustBe ProcessDeletes.VersionChildren
}
}
}

0 comments on commit 5a6c57a

Please sign in to comment.