-
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
- Loading branch information
Showing
13 changed files
with
312 additions
and
36 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
65 changes: 65 additions & 0 deletions
65
.../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,65 @@ | ||
package ch.epfl.bluebrain.nexus.delta.sdk.organizations | ||
|
||
import cats.syntax.all._ | ||
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.{MD5, Transactors} | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.implicits._ | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.model.EntityType | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Label | ||
import com.typesafe.scalalogging.Logger | ||
import doobie.{ConnectionIO, Update} | ||
import doobie.implicits._ | ||
import doobie.util.update.Update0 | ||
import monix.bio.IO | ||
import monix.bio.UIO | ||
|
||
trait OrganizationDeleter { | ||
def delete(id: Label): IO[OrganizationNonEmpty, Unit] | ||
} | ||
|
||
object OrganizationDeleter { | ||
|
||
def apply(xas: Transactors): OrganizationDeleter = new OrganizationDeleter { | ||
|
||
private val logger: Logger = Logger[OrganizationDeleter] | ||
|
||
def delete(id: Label): IO[OrganizationNonEmpty, Unit] = | ||
for { | ||
orgIsEmpty <- orgIsEmpty(id) | ||
_ <- if (orgIsEmpty) log(s"Deleting empty organization $id") *> deleteAll(id) | ||
else IO.raiseError(OrganizationNonEmpty(id)) | ||
} yield () | ||
|
||
private def log(msg: String): UIO[Unit] = UIO.delay(logger.info(msg)) | ||
|
||
private def deleteAll(id: Label): UIO[Unit] = | ||
(for { | ||
_ <- List("scoped_events", "scoped_states").traverse(dropPartition(id, _)).void | ||
_ <- List("global_events", "global_states").traverse(deleteGlobal(id, _)) | ||
} yield ()).transact(xas.write).void.hideErrors | ||
|
||
private def dropPartition(id: Label, table: String): ConnectionIO[Unit] = | ||
Update0(s"DROP TABLE IF EXISTS ${table}_${MD5.hash(id.value)}", 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): UIO[Boolean] = | ||
sql"SELECT type from scoped_events WHERE org = $id LIMIT 1" | ||
.query[Label] | ||
.option | ||
.map(_.isEmpty) | ||
.transact(xas.read) | ||
.hideErrors | ||
} | ||
|
||
} |
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.