Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Thunk\Verbs\Facades\Id;

return new class extends Migration
{
Expand All @@ -14,7 +15,7 @@ public function up()
}

Schema::connection($this->connectionName())->create($this->tableName(), function (Blueprint $table) {
$table->snowflakeId();
Id::createColumnDefinition($table)->primary();

$table->string('type')->index();
$table->json('data');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,20 @@
{
public function up()
{
// If we migrated before Verbs 0.5.0 we need to do a little extra work
$migrating = Schema::connection($this->connectionName())->hasTable($this->tableName());

if ($migrating) {
Schema::connection($this->connectionName())->rename($this->tableName(), '__verbs_snapshots_pre_050');
}

Schema::connection($this->connectionName())->create($this->tableName(), function (Blueprint $table) {
$table->snowflakeId();

// The 'state_id' column needs to be set up differently depending on
// if you're using Snowflakes vs. ULIDs/etc.
Id::createColumnDefinition($table)->primary();
Id::createColumnDefinition($table, 'state_id');

$table->string('type')->index();
$table->json('data');

$table->snowflake('last_event_id')->nullable();
Id::createColumnDefinition($table, 'last_event_id')->nullable();

$table->timestamp('expires_at')->nullable()->index();
$table->timestamps();

$table->index(['state_id', 'type']);
});

if ($migrating) {
DB::connection($this->connectionName())
->table('__verbs_snapshots_pre_050')
->select('*')
->chunkById(100, $this->migrateChunk(...));
}
}

public function down()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ public function up()
}

Schema::connection($this->connectionName())->create($this->tableName(), function (Blueprint $table) {
$table->snowflakeId();

$table->snowflake('event_id')->index();

// The 'state_id' column needs to be set up differently depending
// on if you're using Snowflakes vs. ULIDs/etc.
Id::createColumnDefinition($table, 'state_id')->index();
Id::createColumnDefinition($table)->primary();
Id::createColumnDefinition($table, 'event_id');
Id::createColumnDefinition($table, 'state_id');

$table->string('state_type')->index();

Expand Down
3 changes: 2 additions & 1 deletion src/Attributes/Autodiscovery/AppliesToState.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Str;
use InvalidArgumentException;
use Thunk\Verbs\Event;
use Thunk\Verbs\Facades\Id;
use Thunk\Verbs\Lifecycle\StateManager;
use Thunk\Verbs\SingletonState;
use Thunk\Verbs\State;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function discoverState(Event $event, StateManager $manager): State|array

// If the ID hasn't been set yet, we'll automatically set one
if ($id === null && $this->autofill) {
$id = snowflake_id();
$id = Id::make();
$event->{$property} = $id;

return $manager->make($id, $this->state_type);
Expand Down
3 changes: 2 additions & 1 deletion src/Attributes/Autodiscovery/StateId.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Thunk\Verbs\Event;
use Thunk\Verbs\Facades\Id;
use Thunk\Verbs\Lifecycle\StateManager;
use Thunk\Verbs\State;

Expand Down Expand Up @@ -35,7 +36,7 @@ public function discoverState(Event $event, StateManager $manager): State|array

// If the ID hasn't been set yet, we'll automatically set one
if ($id === null && $this->autofill) {
$id = snowflake_id();
$id = Id::make();
$this->property->setValue($event, $id);

$autofilled = $meta->get('autofilled', []);
Expand Down
5 changes: 4 additions & 1 deletion src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Thunk\Verbs;

use Glhd\Bits\Bits;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use LogicException;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Uid\AbstractUid;
use Throwable;
use Thunk\Verbs\Exceptions\EventNotAuthorized;
use Thunk\Verbs\Exceptions\EventNotValid;
Expand All @@ -21,7 +24,7 @@
*/
abstract class Event
{
public int $id;
public Bits|UuidInterface|AbstractUid|int|string $id;

public static function __callStatic(string $name, array $arguments)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Lifecycle/EventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected function formatRelationshipsForWrite(array $event_objects): array
{
return collect($event_objects)
->flatMap(fn (Event $event) => $event->states()->map(fn ($state) => [
'id' => snowflake_id(),
'id' => Id::make(),
'event_id' => Id::from($event->id),
'state_id' => Id::from($state->id),
'state_type' => $state::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Lifecycle/SnapshotStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected function loadMany(Collection $ids, string $type): StateCollection
protected function formatForWrite(State $state): array
{
return [
'id' => $this->metadata->getEphemeral($state, 'snapshot_id', snowflake_id()),
'id' => $this->metadata->getEphemeral($state, 'snapshot_id', Id::make()),
'state_id' => Id::from($state->id),
'type' => $state::class,
'data' => $this->serializer->serialize($state),
Expand Down
4 changes: 2 additions & 2 deletions src/Lifecycle/StateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(

public function register(State $state): State
{
$state->id ??= snowflake_id();
$state->id ??= Id::make();

return $this->remember($state);
}
Expand Down Expand Up @@ -62,7 +62,7 @@ public function singleton(string $type): State
}

$state = $this->snapshots->loadSingleton($type) ?? new $type;
$state->id ??= snowflake_id();
$state->id ??= Id::make();

// We'll store a reference to it by the type for future singleton access
$this->states->put($type, $state);
Expand Down
6 changes: 6 additions & 0 deletions src/Models/VerbEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Thunk\Verbs\Event;
use Thunk\Verbs\Facades\Id;
use Thunk\Verbs\Lifecycle\MetadataManager;
use Thunk\Verbs\Metadata;
use Thunk\Verbs\State;
Expand Down Expand Up @@ -38,6 +39,11 @@ public function getTable()
return $this->table ?? config('verbs.tables.events', 'verb_events');
}

public function getKeyType()
{
return Id::keyType();
}

public function event(): Event
{
$this->event ??= app(Serializer::class)->deserialize($this->type, $this->data);
Expand Down
6 changes: 6 additions & 0 deletions src/Models/VerbSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\Model;
use Thunk\Verbs\Facades\Id;
use Thunk\Verbs\Lifecycle\MetadataManager;
use Thunk\Verbs\State;
use Thunk\Verbs\Support\Serializer;
Expand Down Expand Up @@ -32,6 +33,11 @@ public function getTable()
return $this->table ?? config('verbs.tables.snapshots', 'verb_snapshots');
}

public function getKeyType()
{
return Id::keyType();
}

public function state(): State
{
$this->state ??= app(Serializer::class)->deserialize($this->type, $this->data);
Expand Down
6 changes: 6 additions & 0 deletions src/Models/VerbStateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Thunk\Verbs\Models;

use Illuminate\Database\Eloquent\Model;
use Thunk\Verbs\Facades\Id;
use Thunk\Verbs\State;

class VerbStateEvent extends Model
Expand All @@ -19,6 +20,11 @@ public function getTable()
return $this->table ?? config('verbs.tables.state_events', 'verb_state_events');
}

public function getKeyType()
{
return Id::keyType();
}

public function event()
{
return $this->belongsTo(VerbEvent::class);
Expand Down
17 changes: 13 additions & 4 deletions src/Support/IdManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Thunk\Verbs\Support;

use Glhd\Bits\Bits;
use Glhd\Bits\Contracts\MakesSnowflakes;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\ColumnDefinition;
use Illuminate\Support\Str;
Expand All @@ -19,7 +20,7 @@ class IdManager
public const TYPE_UUID = 'uuid';

public function __construct(
protected string $id_type
protected string $id_type,
) {
if (! in_array($this->id_type, [self::TYPE_SNOWFLAKE, self::TYPE_ULID, self::TYPE_UUID])) {
throw new InvalidArgumentException("'{$this->id_type}' is not a valid verbs.id_type");
Expand Down Expand Up @@ -50,9 +51,9 @@ public function from(Bits|UuidInterface|AbstractUid|int|string $id): int|string
public function make(): int|string
{
return match ($this->id_type) {
self::TYPE_SNOWFLAKE => snowflake_id(),
self::TYPE_ULID => Str::ulid(),
self::TYPE_UUID => Str::orderedUuid(),
self::TYPE_SNOWFLAKE => app(MakesSnowflakes::class)->make()->id(),
self::TYPE_ULID => (string) Str::ulid(),
self::TYPE_UUID => (string) Str::uuid7(),
};
}

Expand All @@ -64,4 +65,12 @@ public function createColumnDefinition(Blueprint $table, string $name = 'id'): C
self::TYPE_UUID => $table->uuid($name),
};
}

public function keyType(): string
{
return match ($this->id_type) {
self::TYPE_SNOWFLAKE => 'int',
self::TYPE_ULID, self::TYPE_UUID => 'string',
};
}
}
3 changes: 2 additions & 1 deletion src/Support/PendingEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Thunk\Verbs\Contracts\BrokersEvents;
use Thunk\Verbs\Event;
use Thunk\Verbs\Exceptions\EventNotValid;
use Thunk\Verbs\Facades\Id;
use Thunk\Verbs\Lifecycle\MetadataManager;

/**
Expand Down Expand Up @@ -174,7 +175,7 @@ protected function setDefaultExceptionMapper(): void
protected function conditionallySetId(): void
{
if ($this->event instanceof Event) {
$this->event->id ??= snowflake_id();
$this->event->id ??= Id::make();
}
}
}
2 changes: 1 addition & 1 deletion src/VerbsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected function handleEvent($event = null)
{
// Allow for firing events with traditional Laravel dispatcher
if ($event instanceof Event) {
$event->id ??= snowflake_id();
$event->id ??= $this->app->make(IdManager::class)->make();
$this->app->make(BrokersEvents::class)->fire($event);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/SupportUuidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
});

it('supports using uuids as state ids', function () {
$uuid = (string) Str::orderedUuid();
$uuid = (string) Str::uuid7();

$state = UuidState::load($uuid);

Expand All @@ -41,7 +41,7 @@
});

it('loads states correctly using uuids when the snapshots table has been removed', function () {
$uuid = (string) Str::orderedUuid();
$uuid = (string) Str::uuid7();

$state = UuidState::load($uuid);

Expand Down
Loading