Skip to content

Commit

Permalink
rename create into setup and drop into teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Jan 3, 2024
1 parent 2d46c17 commit 51533f9
Show file tree
Hide file tree
Showing 25 changed files with 134 additions and 136 deletions.
8 changes: 4 additions & 4 deletions docs/pages/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ Each projector is then responsible for a specific projection and version.

```php
use Doctrine\DBAL\Connection;
use Patchlevel\EventSourcing\Attribute\Create;
use Patchlevel\EventSourcing\Attribute\Drop;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\EventBus\Message;
Expand Down Expand Up @@ -215,13 +215,13 @@ final class HotelProjector
);
}

#[Create]
#[Setup]
public function create(): void
{
$this->db->executeStatement("CREATE TABLE IF NOT EXISTS ${this->table()} (id VARCHAR PRIMARY KEY, name VARCHAR, guests INTEGER);");
}

#[Drop]
#[Teardown]
public function drop(): void
{
$this->db->executeStatement("DROP TABLE IF EXISTS ${this->table()};");
Expand Down
16 changes: 8 additions & 8 deletions docs/pages/projection.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ In this example we always create a new data set in a relational database when a

```php
use Doctrine\DBAL\Connection;
use Patchlevel\EventSourcing\Attribute\Create;
use Patchlevel\EventSourcing\Attribute\Drop;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\EventBus\Message;
Expand All @@ -40,15 +40,15 @@ final class ProfileProjector
return $this->connection->fetchAllAssociative("SELECT id, name FROM ${this->table()};");
}

#[Create]
#[Setup]
public function create(): void
{
$this->connection->executeStatement(
"CREATE TABLE IF NOT EXISTS ${this->table()} (id VARCHAR PRIMARY KEY, name VARCHAR NOT NULL);"
);
}

#[Drop]
#[Teardown]
public function drop(): void
{
$this->connection->executeStatement("DROP TABLE IF EXISTS ${this->table()};");
Expand Down Expand Up @@ -87,8 +87,8 @@ So that there is no problems with existing projection,
both the name of the projection and the version should be part of the table/collection name.
In our example, we build a `table` helper method, what creates the following string: "projection_profile_0".

Projectors can have one `create` and `drop` method that is executed when the projection is created or deleted.
For this there are the attributes `Create` and `Drop`. The method name itself doesn't matter.
Projectors can have one `setup` and `teardown` method that is executed when the projection is created or deleted.
For this there are the attributes `Setup` and `Teardown`. The method name itself doesn't matter.
In some cases it may be that no schema has to be created for the projection,
as the target does it automatically, so you can skip this.

Expand Down Expand Up @@ -118,8 +118,8 @@ To do this, you have to change the version in the `Projector` attribute.

```php
use Doctrine\DBAL\Connection;
use Patchlevel\EventSourcing\Attribute\Create;
use Patchlevel\EventSourcing\Attribute\Drop;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\Attribute\Handle;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\EventBus\Message;
Expand Down
2 changes: 1 addition & 1 deletion src/Attribute/Drop.php → src/Attribute/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
use Attribute;

#[Attribute(Attribute::TARGET_METHOD)]
final class Drop
final class Setup
{
}
2 changes: 1 addition & 1 deletion src/Attribute/Create.php → src/Attribute/Teardown.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
use Attribute;

#[Attribute(Attribute::TARGET_METHOD)]
final class Create
final class Teardown
{
}
12 changes: 6 additions & 6 deletions src/Metadata/Projector/AttributeProjectorMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Patchlevel\EventSourcing\Metadata\Projector;

use Patchlevel\EventSourcing\Attribute\Create;
use Patchlevel\EventSourcing\Attribute\Drop;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Teardown;
use ReflectionClass;

use function array_key_exists;
Expand Down Expand Up @@ -59,9 +59,9 @@ public function metadata(string $projector): ProjectorMetadata
$subscribeMethods[$eventClass] = $method->getName();
}

if ($method->getAttributes(Create::class)) {
if ($method->getAttributes(Setup::class)) {
if ($createMethod) {
throw new DuplicateCreateMethod(
throw new DuplicateSetupMethod(
$projector,
$createMethod,
$method->getName(),
Expand All @@ -71,12 +71,12 @@ public function metadata(string $projector): ProjectorMetadata
$createMethod = $method->getName();
}

if (!$method->getAttributes(Drop::class)) {
if (!$method->getAttributes(Teardown::class)) {
continue;
}

if ($dropMethod) {
throw new DuplicateDropMethod(
throw new DuplicateTeardownMethod(
$projector,
$dropMethod,
$method->getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

use function sprintf;

final class DuplicateDropMethod extends MetadataException
final class DuplicateSetupMethod extends MetadataException
{
/** @param class-string $projector */
public function __construct(string $projector, string $fistMethod, string $secondMethod)
{
parent::__construct(
sprintf(
'Two methods "%s" and "%s" on the projector "%s" have been marked as "create" methods. Only one method can be defined like this.',
'Two methods "%s" and "%s" on the projector "%s" have been marked as "setup" methods. Only one method can be defined like this.',
$fistMethod,
$secondMethod,
$projector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

use function sprintf;

final class DuplicateCreateMethod extends MetadataException
final class DuplicateTeardownMethod extends MetadataException
{
/** @param class-string $projector */
public function __construct(string $projector, string $fistMethod, string $secondMethod)
{
parent::__construct(
sprintf(
'Two methods "%s" and "%s" on the projector "%s" have been marked as "create" methods. Only one method can be defined like this.',
'Two methods "%s" and "%s" on the projector "%s" have been marked as "teardown" methods. Only one method can be defined like this.',
$fistMethod,
$secondMethod,
$projector,
Expand Down
4 changes: 2 additions & 2 deletions src/Metadata/Projector/ProjectorMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public function __construct(
public readonly int $version,
/** @var array<class-string, string> */
public readonly array $subscribeMethods = [],
public readonly string|null $createMethod = null,
public readonly string|null $dropMethod = null,
public readonly string|null $setupMethod = null,
public readonly string|null $teardownMethod = null,
) {
}
}
22 changes: 11 additions & 11 deletions src/Projection/Projectionist/DefaultProjectionist.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public function boot(
$projection->id()->toString(),
));

$createMethod = $this->projectorResolver->resolveCreateMethod($projector);
$setupMethod = $this->projectorResolver->resolveSetupMethod($projector);

if (!$createMethod) {
if (!$setupMethod) {
$this->logger?->info(sprintf(
'projector "%s" for "%s" has no create method',
'projector "%s" for "%s" has no "setup" method',
$projector::class,
$projection->id()->toString(),
));
Expand All @@ -76,7 +76,7 @@ public function boot(
}

try {
$createMethod();
$setupMethod();
$this->logger?->info(sprintf(
'projector "%s" for "%s" prepared',
$projector::class,
Expand Down Expand Up @@ -242,11 +242,11 @@ public function teardown(ProjectionCriteria $criteria = new ProjectionCriteria()
continue;
}

$dropMethod = $this->projectorResolver->resolveDropMethod($projector);
$teardownMethod = $this->projectorResolver->resolveTeardownMethod($projector);

if ($dropMethod) {
if ($teardownMethod) {
try {
$dropMethod();
$teardownMethod();
} catch (Throwable $e) {
$this->logger?->error(
sprintf('projection for "%s" could not be removed, skipped', $projection->id()->toString()),
Expand Down Expand Up @@ -281,9 +281,9 @@ public function remove(ProjectionCriteria $criteria = new ProjectionCriteria()):
continue;
}

$dropMethod = $this->projectorResolver->resolveDropMethod($projector);
$teardownMethod = $this->projectorResolver->resolveTeardownMethod($projector);

if (!$dropMethod) {
if (!$teardownMethod) {
$this->projectionStore->remove($projection->id());

$this->logger?->info(
Expand All @@ -294,11 +294,11 @@ public function remove(ProjectionCriteria $criteria = new ProjectionCriteria()):
}

try {
$dropMethod();
$teardownMethod();
} catch (Throwable $e) {
$this->logger?->error(
sprintf(
'projector "%s" drop method could not be executed:',
'projector "%s" teardown method could not be executed:',
$projector::class,
),
);
Expand Down
8 changes: 4 additions & 4 deletions src/Projection/Projector/MetadataProjectorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public function __construct(
) {
}

public function resolveCreateMethod(object $projector): Closure|null
public function resolveSetupMethod(object $projector): Closure|null
{
$metadata = $this->metadataFactory->metadata($projector::class);
$method = $metadata->createMethod;
$method = $metadata->setupMethod;

if (!$method) {
return null;
Expand All @@ -31,10 +31,10 @@ public function resolveCreateMethod(object $projector): Closure|null
return $projector->$method(...);
}

public function resolveDropMethod(object $projector): Closure|null
public function resolveTeardownMethod(object $projector): Closure|null
{
$metadata = $this->metadataFactory->metadata($projector::class);
$method = $metadata->dropMethod;
$method = $metadata->teardownMethod;

if (!$method) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Projection/Projector/ProjectorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

interface ProjectorResolver
{
public function resolveCreateMethod(object $projector): Closure|null;
public function resolveSetupMethod(object $projector): Closure|null;

public function resolveDropMethod(object $projector): Closure|null;
public function resolveTeardownMethod(object $projector): Closure|null;

public function resolveSubscribeMethod(object $projector, Message $message): Closure|null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Projection;

use Doctrine\DBAL\Connection;
use Patchlevel\EventSourcing\Attribute\Create;
use Patchlevel\EventSourcing\Attribute\Drop;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\ProfileCreated;

Expand All @@ -22,13 +22,13 @@ public function __construct(
) {
}

#[Create]
#[Setup]
public function create(): void
{
$this->connection->executeStatement('CREATE TABLE IF NOT EXISTS projection_profile (id VARCHAR PRIMARY KEY, name VARCHAR);');
}

#[Drop]
#[Teardown]
public function drop(): void
{
$this->connection->executeStatement('DROP TABLE IF EXISTS projection_profile;');
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/BankAccountSplitStream/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BalanceAdded;
use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BankAccountCreated;
use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\MonthPassed;
use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Projection\BankAccountProjection;
use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Projection\BankAccountProjector;
use Patchlevel\EventSourcing\Tests\Integration\DbalManager;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Lock\LockFactory;
Expand Down Expand Up @@ -55,8 +55,8 @@ public function testSuccessful(): void
'eventstore',
);

$bankAccountProjection = new BankAccountProjection($this->connection);
$projectionRepository = new InMemoryProjectorRepository([$bankAccountProjection]);
$bankAccountProjector = new BankAccountProjector($this->connection);
$projectionRepository = new InMemoryProjectorRepository([$bankAccountProjector]);

$projectionist = new DefaultProjectionist(
$store,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Table;
use Patchlevel\EventSourcing\Attribute\Create;
use Patchlevel\EventSourcing\Attribute\Drop;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BalanceAdded;
use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BankAccountCreated;

use function assert;

#[Projector('dummy', 1)]
final class BankAccountProjection
final class BankAccountProjector
{
public function __construct(
private Connection $connection,
) {
}

#[Create]
#[Setup]
public function create(): void
{
$table = new Table('projection_bank_account');
Expand All @@ -34,7 +36,7 @@ public function create(): void
$this->connection->createSchemaManager()->createTable($table);
}

#[Drop]
#[Teardown]
public function drop(): void
{
$this->connection->createSchemaManager()->dropTable('projection_bank_account');
Expand All @@ -45,6 +47,8 @@ public function handleBankAccountCreated(Message $message): void
{
$event = $message->event();

assert($event instanceof BankAccountCreated);

$this->connection->executeStatement(
'INSERT INTO projection_bank_account (id, name, balance_in_cents) VALUES(:id, :name, 0);',
[
Expand All @@ -59,6 +63,8 @@ public function handleBalanceAdded(Message $message): void
{
$event = $message->event();

assert($event instanceof BalanceAdded);

$this->connection->executeStatement(
'UPDATE projection_bank_account SET balance_in_cents = balance_in_cents + :balance WHERE id = :id;',
[
Expand Down
Loading

0 comments on commit 51533f9

Please sign in to comment.