-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add organization deletion route #4298
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use the sql interpolator here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not possible when parameterising over the table name since substitutions only work for values. This is maybe a bit overkill but I was curious to see if it was possible 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fragment.const
could also help here ? (not sure)