1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-05-07 09:54:03 +00:00

Skip DB deletion when create_database=false, add ignoreFailures (#1394)

Database deletion is now skipped by default if the tenant has the
`create_database` internal attribute set to false, meaning it was likely
created without a database. This skip can be opted out of by changing a
static property.

It also adds an opt-in static property for ignoring any other failures
during database deletion, to allow continuing execution of the delete
pipeline.

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
Thomas 2026-05-01 21:57:19 +02:00 committed by GitHub
parent 41701aff5f
commit 23b18c93a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 101 additions and 2 deletions

View file

@ -22,12 +22,34 @@ class DeleteDatabase implements ShouldQueue
protected TenantWithDatabase&Model $tenant,
) {}
/** Skip database deletion if the create_database internal attribute is false. */
public static bool $skipWhenCreateDatabaseIsFalse = true;
/** Ignore exceptions thrown during database deletion and continue execution. */
public static bool $ignoreFailures = false;
public function handle(): void
{
if (static::$skipWhenCreateDatabaseIsFalse && $this->tenant->getInternal('create_database') === false) {
// If database creation was skipped, we presume deletion should also be skipped.
// To avoid this skip, either unset the `create_database` attribute (or make it true), or
// set the $skipWhenCreateDatabaseIsFalse static property to false.
return;
}
event(new DeletingDatabase($this->tenant));
$this->tenant->database()->manager()->deleteDatabase($this->tenant);
$deleted = false;
event(new DatabaseDeleted($this->tenant));
try {
$this->tenant->database()->manager()->deleteDatabase($this->tenant);
$deleted = true;
} catch (\Throwable $e) {
if (! static::$ignoreFailures) {
throw $e;
}
}
if ($deleted) event(new DatabaseDeleted($this->tenant));
}
}