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 5a6c57a commit ccbdadf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 25 deletions.
44 changes: 31 additions & 13 deletions api/app/util/ProcessDeletes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ import play.api.db.Database
import javax.inject.Inject

object ProcessDeletes {
val OrganizationChildren: Seq[String] = Seq(
val OrganizationSoft: Seq[String] = Seq(
"public.applications",
"public.membership_requests",
"public.memberships",
"public.organization_attribute_values",
"public.organization_domains",
"public.subscriptions"
)
val ApplicationChildren: Seq[String] = Seq(
val OrganizationHard: Seq[String] = Seq(
"public.organization_logs", "search.items"
)
val ApplicationSoft: Seq[String] = Seq(
"public.changes", "public.versions", "public.watches"
)
val VersionChildren = Seq(
val ApplicationHard: Seq[String] = Seq("search.items")
val VersionSoft: Seq[String] = Seq(
"cache.services", "public.originals"
)
val VersionHard: Seq[String] = Seq("public.migrations")
}

class ProcessDeletes @Inject() (
Expand All @@ -36,7 +41,7 @@ class ProcessDeletes @Inject() (
}

private[util] def organizations(): Unit = {
OrganizationChildren.foreach { table =>
OrganizationSoft.foreach { table =>
exec(
s"""
|update $table
Expand All @@ -46,7 +51,8 @@ class ProcessDeletes @Inject() (
|""".stripMargin
)
}
Seq("organization_logs").foreach { table =>

OrganizationHard.foreach { table =>
exec(
s"""
|delete from $table
Expand All @@ -57,7 +63,7 @@ class ProcessDeletes @Inject() (
}

private[util] def applications(): Unit = {
ApplicationChildren.foreach { table =>
ApplicationSoft.foreach { table =>
exec(
s"""
|update $table
Expand All @@ -67,16 +73,19 @@ class ProcessDeletes @Inject() (
|""".stripMargin
)
}

ApplicationHard.foreach { table =>
exec(
s"""
|delete from $table
| where application_guid in (select guid from applications where deleted_at is not null)
|""".stripMargin
)
}
}

private[util] def versions(): Unit = {
exec("""
|delete from migrations
| where version_guid in (select guid from versions where deleted_at is not null)
|""".stripMargin
)

VersionChildren.foreach { table =>
VersionSoft.foreach { table =>
exec(
s"""
|update $table
Expand All @@ -86,6 +95,15 @@ class ProcessDeletes @Inject() (
|""".stripMargin
)
}

VersionHard.foreach { table =>
exec(
s"""
|delete from $table
| where version_guid in (select guid from versions where deleted_at is not null)
|""".stripMargin
)
}
}

private[this] def exec(q: String): Unit = {
Expand Down
55 changes: 43 additions & 12 deletions api/test/util/ProcessDeletesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ class ProcessDeletesSpec extends PlaySpec with GuiceOneAppPerSuite with Helpers
}

"have all child tables" must {
def getTables(columnName: String): Seq[String] = {
def getTablesSoft(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}
| join information_schema.columns c2 on c2.table_schema = c1.table_schema and c2.table_name = c1.table_name and c2.column_name = 'deleted_by_guid'
| where c1.column_name = {column_name}
| order by 1
|""".stripMargin
)
Expand All @@ -84,18 +83,50 @@ class ProcessDeletesSpec extends PlaySpec with GuiceOneAppPerSuite with Helpers
}
}

"OrganizationChildren" in {
getTables("organization_guid") mustBe ProcessDeletes.OrganizationChildren
def getTablesHard(columnName: String): Seq[String] = {
database.withConnection { c =>
Query(
"""
|select c1.table_schema || '.' || c1.table_name
| from information_schema.columns c1
| left join information_schema.columns c2 on c2.table_schema = c1.table_schema and c2.table_name = c1.table_name and c2.column_name = 'deleted_by_guid'
| where c1.column_name = {column_name}
| and c2.table_name is null
| order by 1
|""".stripMargin
)
.bind("column_name", columnName)
.as(SqlParser.str(1).*)(c)
}
}

"ApplicationChildren" in {
val Ignore = Seq("public.application_moves")
getTables("application_guid")
.filterNot(Ignore.contains) mustBe ProcessDeletes.ApplicationChildren
"Organization" must {
"soft" in {
getTablesSoft("organization_guid") mustBe ProcessDeletes.OrganizationSoft
}
"hard" in {
getTablesHard("organization_guid") mustBe ProcessDeletes.OrganizationHard
}
}

"VersionChildren" in {
getTables("version_guid") mustBe ProcessDeletes.VersionChildren
"Application" must {
"soft" in {
val Ignore = Seq("public.application_moves")
getTablesSoft("application_guid")
.filterNot(Ignore.contains) mustBe ProcessDeletes.ApplicationSoft
}
"hard" in {
getTablesHard("application_guid") mustBe ProcessDeletes.ApplicationHard
}
}

"Version" must {
"soft" in {
getTablesSoft("version_guid") mustBe ProcessDeletes.VersionSoft
}
"hard" in {
getTablesHard("version_guid") mustBe ProcessDeletes.VersionHard
}
}
}
}

0 comments on commit ccbdadf

Please sign in to comment.