Skip to content

Commit

Permalink
Minor tweaks for release 1.0-BETA1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophist-UK committed Dec 3, 2022
1 parent d639177 commit 9ec1b73
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ then you want the model to behave the same as if you had hard deleted it
and still allow you to create a new (non-deleted) record for 'Pete'
alongside the deleted version.
You therefore want the combination of `deleted_at` and `name` to be unique,
so you create a unique index on ['deleted_at', 'name'] expecting it to prevent duplicates,
so you create a unique index on `['deleted_at', 'name']` expecting it to prevent duplicates,
i.e. in your migration...

``` php
Expand Down Expand Up @@ -49,7 +49,7 @@ to have multiple rows [null, 'Pete']!!!

It does it by creating a new column `deleted_at_uniqueable`
which is maintained as a string version of the `deleted_at` column;
using the empty string '' if the `deleted_at` column is null.
using the empty string `''` if the `deleted_at` column is null.

Your code now needs to look as follows:

Expand Down Expand Up @@ -132,7 +132,7 @@ The `softDeletesUnique` and `dropSoftDeletesUnique` methods are macroed into Blu

`$table->softDeletesUnique();` creates a new non-nullable string column
`deleted_at_uniqueable` of up to 24 characters
(format 'YYYYMMDD HH:MM:SS.xxxxxx'),
(format 'YYYY-MM-DD HH:MM:SS.xxxxxx'),
that contains either '' when `deleted_at` is null, or a string representation if it is not null.

The `HasSoftDeletesUnique` trait creates observers on the creating, updating, deleting and restoring Eloquent actions
Expand Down
22 changes: 14 additions & 8 deletions tests/Unit/SoftDeletesUniqueMigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ public function testSoftDeleteUnique_bespokeColumnNames(): void
public function testSoftDeleteUnique_negativePrecision(): void
{
// Negative precision should fail
$this->expectException(\ValueError::class);
$tableName = 'table_softdeleteunique_invalidprecision';
$columns = $this->createTable($tableName, precision: -1);
$e = null;
try {
$columns = $this->createTable($tableName, precision: -1);
} catch (\Exception $e) {}
$this->assertInstanceOf(\ValueError::class, $e);
}

/**
Expand Down Expand Up @@ -122,11 +125,14 @@ public function testSoftDeletesUnique_uniqueIndexes(): void
$this->assertEquals(count($rows), 3);

// Insert of duplicate empty uniqueable should fail
$this->expectException(QueryException::class);
DB::table($tableName)->insert([
'field'=> 'value1',
'deleted_at' => null,
'deleted_at_uniqueable' => ''
]);
$e = null;
try {
DB::table($tableName)->insert([
'field'=> 'value1',
'deleted_at' => null,
'deleted_at_uniqueable' => ''
]);
} catch (\Exception $e) {}
$this->assertInstanceOf(QueryException::class, $e);
}
}
39 changes: 34 additions & 5 deletions tests/Unit/SoftDeletesUniqueModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Tranzakt\SoftDeletesUnique\Tests\Unit;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\QueryException;
use Tranzakt\SoftDeletesUnique\Tests\Models\TestModelSoftDeletesUnique;
use Tranzakt\SoftDeletesUnique\Tests\Testcases\TestCaseSoftDeletesUnique;

Expand Down Expand Up @@ -98,10 +98,39 @@ public function testSoftDeletesUniqueModelUniqueness()
$this->checkRowCounts(1, 1, 0);

// Attempt to create duplicate record
$this->expectException(\Illuminate\Database\QueryException::class);
$model = TestModelSoftDeletesUnique::create([
$e = null;
try {
TestModelSoftDeletesUnique::create([
'field' => 'value1'
]);
} catch (\Exception $e) {}
$this->assertInstanceOf(QueryException::class, $e);
}

/**
* Create identical active and deleted records, then try to restore the deleted one.
*/
public function testSoftDeletesUniqueModelRestoreFail()
{
$this->createTable($this->tableName);

TestModelSoftDeletesUnique::create([
'field' => 'value1'
])->delete();
$this->checkRowCounts(1, 0, 1);
TestModelSoftDeletesUnique::create([
'field' => 'value1'
]);

}
$this->checkRowCounts(2, 1, 1);
$row = TestModelSoftDeletesUnique::onlyTrashed()->first();
$e = null;
try {
$row->restore(); // Should fail
} catch (\Exception $e) {}
$this->assertInstanceOf(QueryException::class, $e);
$this->checkRowCounts(2, 1, 1);
$row = TestModelSoftDeletesUnique::onlyTrashed()->first();
$this->assertNotEquals($row->deleted_at_uniqueable, '');
$this->assertNotNull($row->deleted_at);
}
}

0 comments on commit 9ec1b73

Please sign in to comment.