add some missing auto-deletes if refcount reaches zero

This commit is contained in:
2018-07-21 19:47:44 +02:00
parent a9ffd378a0
commit dc9acbf9d7
5 changed files with 43 additions and 1 deletions
+16
View File
@@ -24,6 +24,22 @@ class Proposal extends Model
{ {
use Reportable; use Reportable;
protected static function boot()
{
parent::boot();
static::deleting(function(Proposal $self) {
$self->reportsOf()->delete();
// update refcounts
$rev = $self->revision;
$rev->decrement('refs');
// Delete the revision if it has no other refs (manually, to update row refs)
if ($rev->refs <= 0) $rev->delete();
});
}
/** Authoring user */ /** Authoring user */
public function author() public function author()
{ {
+2
View File
@@ -32,6 +32,8 @@ class Revision extends Model
static::deleting(function(Revision $self) { static::deleting(function(Revision $self) {
// update refcounts // update refcounts
$self->rows()->decrement('refs'); $self->rows()->decrement('refs');
$self->rows()->where('refs', '<=', 0)->delete();
}); });
} }
+8 -1
View File
@@ -38,8 +38,15 @@ class Table extends Model
parent::boot(); parent::boot();
static::deleting(function(Table $self) { static::deleting(function(Table $self) {
// update refcounts // update revision refcounts
$self->revisions()->decrement('refs'); $self->revisions()->decrement('refs');
$self->reportsOf()->delete();
// delete revisions with zero refs (manually, to properly cascade to rows)
foreach ($self->revisions()->where('refs', '<=', 0)->get() as $rev) {
$rev->delete();
}
}); });
} }
+9
View File
@@ -25,6 +25,15 @@ class TableComment extends Model
{ {
use Reportable; use Reportable;
protected static function boot()
{
parent::boot();
static::deleting(function(TableComment $self) {
$self->reportsOf()->delete();
});
}
/** Context data table */ /** Context data table */
public function table() public function table()
{ {
+8
View File
@@ -58,10 +58,18 @@ class User extends Authenticatable
parent::boot(); parent::boot();
static::deleting(function(User $self) { static::deleting(function(User $self) {
// comments are deleted via cascade
$self->reportsOf()->delete();
// manually delete proposals to ensure row refcounts are updated // manually delete proposals to ensure row refcounts are updated
foreach ($self->proposals as $proposal) { foreach ($self->proposals as $proposal) {
$proposal->delete(); $proposal->delete();
} }
foreach ($self->tables as $table) {
$table->delete();
}
}); });
} }