-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add organization deletion route - delete scoped partitions and global…
… data
- Loading branch information
Showing
17 changed files
with
328 additions
and
46 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
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
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
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
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
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
61 changes: 61 additions & 0 deletions
61
.../src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/organizations/OrganizationDeleter.scala
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,61 @@ | ||
package ch.epfl.bluebrain.nexus.delta.sdk.organizations | ||
|
||
import cats.effect.IO | ||
import cats.syntax.all._ | ||
import ch.epfl.bluebrain.nexus.delta.kernel.Logger | ||
import ch.epfl.bluebrain.nexus.delta.rdf.IriOrBNode | ||
import ch.epfl.bluebrain.nexus.delta.sdk.acls.Acls | ||
import ch.epfl.bluebrain.nexus.delta.sdk.acls.model.AclAddress | ||
import ch.epfl.bluebrain.nexus.delta.sdk.organizations.model.OrganizationRejection.OrganizationNonEmpty | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{EntityType, Label} | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.{PartitionInit, Transactors} | ||
import doobie.implicits._ | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.implicits._ | ||
import doobie.util.update.Update0 | ||
import doobie.{ConnectionIO, Update} | ||
import org.typelevel.log4cats.{Logger => Log4CatsLogger} | ||
|
||
trait OrganizationDeleter { | ||
def delete(id: Label): IO[Unit] | ||
} | ||
|
||
object OrganizationDeleter { | ||
|
||
def apply(xas: Transactors): OrganizationDeleter = new OrganizationDeleter { | ||
|
||
private val log: Log4CatsLogger[IO] = Logger.cats[OrganizationDeleter] | ||
|
||
def delete(id: Label): IO[Unit] = | ||
for { | ||
canDelete <- orgIsEmpty(id) | ||
_ <- if (canDelete) log.info(s"Deleting empty organization $id") *> deleteAll(id) | ||
else log.error(s"Failed to delete empty organization $id") *> IO.raiseError(OrganizationNonEmpty(id)) | ||
} yield () | ||
|
||
private def deleteAll(id: Label): IO[Unit] = | ||
(for { | ||
_ <- List("scoped_events", "scoped_states").traverse(dropPartition(id, _)) | ||
_ <- List("global_events", "global_states").traverse(deleteGlobal(id, _)) | ||
} yield ()).transact(xas.writeCE).void | ||
|
||
private def dropPartition(id: Label, table: String): ConnectionIO[Unit] = | ||
Update0(s"DROP TABLE IF EXISTS ${PartitionInit.orgPartition(table, id)}", None).run.void | ||
|
||
private def deleteGlobal(id: Label, table: String): ConnectionIO[Unit] = | ||
for { | ||
_ <- deleteGlobalQuery(Organizations.encodeId(id), Organizations.entityType, table) | ||
_ <- deleteGlobalQuery(Acls.encodeId(AclAddress.fromOrg(id)), Acls.entityType, table) | ||
} yield () | ||
|
||
private def deleteGlobalQuery(id: IriOrBNode.Iri, tpe: EntityType, table: String): ConnectionIO[Unit] = | ||
Update[(EntityType, IriOrBNode.Iri)](s"DELETE FROM $table WHERE" ++ " type = ? AND id = ?").run((tpe, id)).void | ||
|
||
private def orgIsEmpty(id: Label): IO[Boolean] = | ||
sql"SELECT type from scoped_events WHERE org = $id LIMIT 1" | ||
.query[Label] | ||
.option | ||
.map(_.isEmpty) | ||
.transact(xas.readCE) | ||
} | ||
|
||
} |
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
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
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
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
Oops, something went wrong.