Skip to content

Commit

Permalink
Merge pull request #6 from envor/main
Browse files Browse the repository at this point in the history
Added verbs
  • Loading branch information
inmanturbo authored Aug 1, 2024
2 parents a0bb363 + 4e8b44a commit 155f0fa
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to `libstream` will be documented in this file.

## v1.0.5 - 2024-07-31

### What's Changed

* Use static instead of self by @inmanturbo in https://github.com/envor/envor-libstream/pull/5

**Full Changelog**: https://github.com/envor/envor-libstream/compare/v1.0.4...v1.0.5

## v1.0.4 - 2024-07-31

### What's Changed
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"require": {
"php": "^8.2",
"hirethunk/verbs": "^0.5.1",
"illuminate/contracts": "^10.0||^11.0",
"spatie/laravel-deleted-models": "^1.0",
"spatie/laravel-event-sourcing": "^7.7",
Expand Down
36 changes: 36 additions & 0 deletions database/migrations/2024_04_16_115559_create_verb_events_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
// If they've already migrated under the previous migration name, just skip
if (Schema::hasTable($this->tableName())) {
return;
}

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

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

$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists($this->tableName());
}

protected function tableName(): string
{
return config('verbs.tables.events', 'verb_events');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

use Glhd\Bits\Contracts\MakesSnowflakes;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Thunk\Verbs\Facades\Id;

return new class extends Migration
{
public function up()
{
// If we migrated before Verbs 0.5.0 we need to do a little extra work
$migrating = Schema::hasTable($this->tableName());

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

Schema::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, 'state_id');

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

$table->snowflake('last_event_id')->nullable();

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

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

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

public function down()
{
Schema::dropIfExists($this->tableName());

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

protected function migrateChunk(Collection $chunk): void
{
$rows = $chunk->map(fn ($row) => [
'id' => app(MakesSnowflakes::class)->makeFromTimestamp(Date::parse($row->created_at))->id(),
'type' => $row->type,
'state_id' => $row->id,
'data' => $row->data,
'last_event_id' => $row->last_event_id,
'expires_at' => null,
'created_at' => $row->created_at,
'updated_at' => $row->updated_at,
]);

DB::table($this->tableName())->insert($rows->toArray());
}

protected function tableName(): string
{
return config('verbs.tables.snapshots', 'verb_snapshots');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

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
{
public function up()
{
// If they've already migrated under the previous migration name, just skip
if (Schema::hasTable($this->tableName())) {
return;
}

Schema::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();

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

$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists($this->tableName());
}

protected function tableName(): string
{
return config('verbs.tables.state_events', 'verb_state_events');
}
};
8 changes: 8 additions & 0 deletions src/Commands/LibstreamMigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function handle(): int

protected function tables(): array
{
$defaultTables = array_merge(
config('libstream.migration_tables', []),
[
config('verbs.tables.events', 'verbs_events'),
config('verbs.tables.snapshots', 'verbs_snapshots'),
config('verbs.tables.state_events', 'verbs_state_events'),
]);

return $this->option('tables') ?: config('libstream.migration_tables', []);
}

Expand Down
8 changes: 8 additions & 0 deletions src/LibstreamServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function configurePackage(Package $package): void
->hasConfigFile()
->hasCommand(Commands\LibstreamMigrateCommand::class)
->hasMigrations([
'2024_04_16_115559_create_verb_events_table',
'2024_04_16_115559_create_verb_snapshots_table',
'2024_04_16_115559_create_verb_state_events_table',
'2024_06_26_194318_create_stored_events_table',
'2024_06_26_194319_create_snapshots_table',
'2024_06_27_165025_change_stored_events_aggregate_uuid_column',
Expand All @@ -27,4 +30,9 @@ public function configurePackage(Package $package): void
])
->runsMigrations();
}

public function packageRegistered()
{
config(['verbs.id_type' => 'ulid']);
}
}

0 comments on commit 155f0fa

Please sign in to comment.