Skip to content

Commit

Permalink
improve performance by not saving dirty models in generate computed a…
Browse files Browse the repository at this point in the history
…ttributes command
  • Loading branch information
Gregor Vostrak authored and korridor committed Jun 10, 2022
1 parent 71d3b03 commit f549d92
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Console/GenerateComputedAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public function handle(): int
foreach ($attributes as $attribute) {
$modelResult->setComputedAttributeValue($attribute);
}
$modelResult->save();
if ($modelResult->isDirty()) {
$modelResult->save();
}
}
});
}
Expand Down
46 changes: 46 additions & 0 deletions tests/Feature/GenerateComputedAttributesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
use Korridor\LaravelComputedAttributes\Tests\TestCase;
use Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Events\PostSaved;
use Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Events\PostSaving;
use Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Models\Post;
use Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Models\Vote;

Expand Down Expand Up @@ -33,6 +36,7 @@ public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAtt
'complex_calculation' => null,
'sum_of_votes' => 0,
]);
Event::fake();

// Act
$this->artisan('computed-attributes:generate', [
Expand All @@ -50,6 +54,8 @@ public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAtt
'complex_calculation' => 3,
'sum_of_votes' => 4,
]);
Event::assertDispatched(PostSaved::class);
Event::assertDispatched(PostSaving::class);
}

public function testCommandCanOnlyCalculateOneAttributeOfOneModelIfSpecifiedInArgument(): void
Expand Down Expand Up @@ -121,4 +127,44 @@ public function testZeroAsChunkSizeReturnsErrorMessage(): void
->assertExitCode(1)
->execute();
}

public function testGenerateAttributesCommandWillNotDispatchEventsIfNotDirty(): void
{
// Arrange
$post = new Post();
$post->title = 'titleTest';
$post->content = 'Text';
$post->complex_calculation = 3;
$post->save();
Config::set('computed-attributes.model_path', 'Models');
Config::set(
'computed-attributes.model_namespace',
'Korridor\\LaravelComputedAttributes\\Tests\\TestEnvironment\\Models'
);
$this->assertDatabaseHas('posts', [
'id' => $post->id,
'complex_calculation' => 3,
'sum_of_votes' => 0,
]);
Event::fake();

// Act
$this->artisan('computed-attributes:generate', [
'modelsAttributes' => 'Post:complex_calculation',
])
->expectsOutput('Start calculating for following attributes of model '.
'"Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Models\Post":')
->expectsOutput('[complex_calculation]')
->assertExitCode(0)
->execute();

// Assert
$this->assertDatabaseHas('posts', [
'id' => $post->id,
'complex_calculation' => 3,
'sum_of_votes' => 0,
]);
Event::assertNotDispatched(PostSaved::class);
Event::assertNotDispatched(PostSaving::class);
}
}
12 changes: 12 additions & 0 deletions tests/TestEnvironment/Events/PostSaved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class PostSaved
{
use Dispatchable, InteractsWithSockets, SerializesModels;
}
12 changes: 12 additions & 0 deletions tests/TestEnvironment/Events/PostSaving.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class PostSaving
{
use Dispatchable, InteractsWithSockets, SerializesModels;
}
10 changes: 10 additions & 0 deletions tests/TestEnvironment/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Korridor\LaravelComputedAttributes\ComputedAttributes;
use Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Events\PostSaved;
use Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Events\PostSaving;

class Post extends Model
{
Expand All @@ -32,6 +34,14 @@ class Post extends Model
'sum_of_votes' => 'int',
];

/**
* @var array
*/
protected $dispatchesEvents = [
'saved' => PostSaved::class,
'saving' => PostSaving::class,
];

/*
* Computed attributes.
*/
Expand Down

0 comments on commit f549d92

Please sign in to comment.