diff --git a/api/app/util/ProcessDeletes.scala b/api/app/util/ProcessDeletes.scala index 0bbd67d64..3c3173973 100644 --- a/api/app/util/ProcessDeletes.scala +++ b/api/app/util/ProcessDeletes.scala @@ -7,7 +7,7 @@ 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", @@ -15,12 +15,17 @@ object ProcessDeletes { "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() ( @@ -36,7 +41,7 @@ class ProcessDeletes @Inject() ( } private[util] def organizations(): Unit = { - OrganizationChildren.foreach { table => + OrganizationSoft.foreach { table => exec( s""" |update $table @@ -46,7 +51,8 @@ class ProcessDeletes @Inject() ( |""".stripMargin ) } - Seq("organization_logs").foreach { table => + + OrganizationHard.foreach { table => exec( s""" |delete from $table @@ -57,7 +63,7 @@ class ProcessDeletes @Inject() ( } private[util] def applications(): Unit = { - ApplicationChildren.foreach { table => + ApplicationSoft.foreach { table => exec( s""" |update $table @@ -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 @@ -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 = { diff --git a/api/test/util/ProcessDeletesSpec.scala b/api/test/util/ProcessDeletesSpec.scala index 57d1bb70f..58e076efe 100644 --- a/api/test/util/ProcessDeletesSpec.scala +++ b/api/test/util/ProcessDeletesSpec.scala @@ -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 ) @@ -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 + } } } }