Skip to content

Commit

Permalink
Some fixes on the rebuild cache command.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkbushell committed Nov 29, 2023
1 parent a75c626 commit d19f31e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Behaviours/CacheConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class CacheConfig
{
public function __construct(readonly string $relationName, readonly string $aggregateField, readonly string $sourceField = '') {}
public function __construct(readonly string $relationName, readonly string $aggregateField, readonly string $sourceField = 'id') {}

/**
* Returns the actual Relation object - such as BelongsTo. This method makes a call to the relationship
Expand Down
9 changes: 6 additions & 3 deletions src/Behaviours/Cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,21 @@ public function rebuildCacheRecord(CacheConfig $config, Model $model, $command):
$foreignKey = $config->foreignKeyName($model);
$related = $config->emptyRelatedModel($model);

$updateSql = sprintf('UPDATE %s SET %s = (SELECT %s(%s) FROM %s WHERE %s = %s.%s GROUP BY %s)',
$updateSql = sprintf('UPDATE %s SET %s = COALESCE((SELECT %s(%s) FROM %s WHERE %s = %s.%s), 0)',
$related->getTable(),
$config->aggregateField,
$command,
$config->sourceField,
$model->getTable(),
$foreignKey,
$related->getTable(),
$related->getKeyName(),
$foreignKey
$related->getKeyName()
);

// dd($updateSql);

// echo "\n".$updateSql;

DB::update($updateSql);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/RebuildCaches.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RebuildCaches extends Command
public function handle(): void
{
$path = $this->argument('path') ?? app_path();

$this->allModelsUsingCaches($path)->each(function(string $class) {
$traits = class_uses_recursive($class);

Expand Down
1 change: 1 addition & 0 deletions tests/Acceptance/AcceptanceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Eloquence\EloquenceServiceProvider;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\TestCase;

Expand Down
45 changes: 34 additions & 11 deletions tests/Acceptance/RebuildCachesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,47 @@

namespace Tests\Acceptance;

use Tests\Acceptance\Models\Item;
use Tests\Acceptance\Models\Order;
use Tests\Acceptance\Models\Post;
use Tests\Acceptance\Models\User;

class RebuildCachesCommandTest extends AcceptanceTestCase
{
function test_itCanRebuildCachesOfAllAffectedModels()
{
$result = $this->artisan('eloquence:rebuild-caches '.__DIR__.'/../../tests/Acceptance/Models');

$result->assertExitCode(0);
$order1 = Order::factory()->create(['total_amount' => 0]);
$order2 = Order::factory()->create(['total_amount' => 0]);

Item::factory()->for($order1)->count(10)->create(['amount' => 10]);
Item::factory()->for($order2)->count(5)->create(['amount' => 5]);

$user1 = User::factory()->create();
$user2 = User::factory()->create();

Post::factory()->for($user1)->count(10)->create();
Post::factory()->for($user2)->count(5)->create();

$this->assertDatabaseHas('users', [
'post_count' => 5,
]);
$order1->totalAmount = 0;
$order1->save();

$this->assertDatabaseHas('users', [
'post_count' => 2,
]);
$order2->totalAmount = 0;
$order2->save();

$this->assertDatabaseHas('orders', [
'total_amount' => 30,
]);
$user1->postCount = 0;
$user1->save();

$user2->postCount = 0;
$user2->save();

$result = $this->artisan('eloquence:rebuild-caches '.__DIR__.'/../../tests/Acceptance/Models');

$result->assertExitCode(0);
// $this->assertSame(10, $user1->fresh()->postCount);
$this->assertDatabaseHas('users', ['post_count' => 10]);
$this->assertDatabaseHas('users', ['post_count' => 5]);
$this->assertDatabaseHas('orders', ['total_amount' => 100]);
$this->assertDatabaseHas('orders', ['total_amount' => 25]);
}
}

0 comments on commit d19f31e

Please sign in to comment.