add some missing auto-deletes if refcount reaches zero

pull/26/head
Ondřej Hruška 6 years ago
parent a9ffd378a0
commit dc9acbf9d7
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 16
      app/Models/Proposal.php
  2. 2
      app/Models/Revision.php
  3. 9
      app/Models/Table.php
  4. 9
      app/Models/TableComment.php
  5. 8
      app/Models/User.php

@ -24,6 +24,22 @@ class Proposal extends Model
{
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 */
public function author()
{

@ -32,6 +32,8 @@ class Revision extends Model
static::deleting(function(Revision $self) {
// update refcounts
$self->rows()->decrement('refs');
$self->rows()->where('refs', '<=', 0)->delete();
});
}

@ -38,8 +38,15 @@ class Table extends Model
parent::boot();
static::deleting(function(Table $self) {
// update refcounts
// update revision refcounts
$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();
}
});
}

@ -25,6 +25,15 @@ class TableComment extends Model
{
use Reportable;
protected static function boot()
{
parent::boot();
static::deleting(function(TableComment $self) {
$self->reportsOf()->delete();
});
}
/** Context data table */
public function table()
{

@ -58,10 +58,18 @@ class User extends Authenticatable
parent::boot();
static::deleting(function(User $self) {
// comments are deleted via cascade
$self->reportsOf()->delete();
// manually delete proposals to ensure row refcounts are updated
foreach ($self->proposals as $proposal) {
$proposal->delete();
}
foreach ($self->tables as $table) {
$table->delete();
}
});
}

Loading…
Cancel
Save