From d2b840d05c297bfdebb3063c4ecbadfdf45fd5bc Mon Sep 17 00:00:00 2001 From: inmanturbo Date: Wed, 31 Jul 2024 20:24:26 +0000 Subject: [PATCH 1/3] Update CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bffe6f..7ff4121 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 5091cd02a4926d34a2db86a50da5a847378223e9 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 1 Aug 2024 11:52:49 -0400 Subject: [PATCH 2/3] work in progress --- composer.json | 1 + ..._04_16_115559_create_verb_events_table.php | 36 +++++++++ ..._16_115559_create_verb_snapshots_table.php | 77 +++++++++++++++++++ ..._115559_create_verb_state_events_table.php | 41 ++++++++++ src/Commands/LibstreamMigrateCommand.php | 8 ++ src/LibstreamServiceProvider.php | 8 ++ 6 files changed, 171 insertions(+) create mode 100644 database/migrations/2024_04_16_115559_create_verb_events_table.php create mode 100644 database/migrations/2024_04_16_115559_create_verb_snapshots_table.php create mode 100644 database/migrations/2024_04_16_115559_create_verb_state_events_table.php diff --git a/composer.json b/composer.json index 7c60e2b..5b783e5 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/database/migrations/2024_04_16_115559_create_verb_events_table.php b/database/migrations/2024_04_16_115559_create_verb_events_table.php new file mode 100644 index 0000000..adeb0f0 --- /dev/null +++ b/database/migrations/2024_04_16_115559_create_verb_events_table.php @@ -0,0 +1,36 @@ +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'); + } +}; diff --git a/database/migrations/2024_04_16_115559_create_verb_snapshots_table.php b/database/migrations/2024_04_16_115559_create_verb_snapshots_table.php new file mode 100644 index 0000000..b43299c --- /dev/null +++ b/database/migrations/2024_04_16_115559_create_verb_snapshots_table.php @@ -0,0 +1,77 @@ +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'); + } +}; diff --git a/database/migrations/2024_04_16_115559_create_verb_state_events_table.php b/database/migrations/2024_04_16_115559_create_verb_state_events_table.php new file mode 100644 index 0000000..ed7c464 --- /dev/null +++ b/database/migrations/2024_04_16_115559_create_verb_state_events_table.php @@ -0,0 +1,41 @@ +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'); + } +}; diff --git a/src/Commands/LibstreamMigrateCommand.php b/src/Commands/LibstreamMigrateCommand.php index 855fc06..5d7db3a 100644 --- a/src/Commands/LibstreamMigrateCommand.php +++ b/src/Commands/LibstreamMigrateCommand.php @@ -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', []); } diff --git a/src/LibstreamServiceProvider.php b/src/LibstreamServiceProvider.php index 2a1fa66..b433de8 100644 --- a/src/LibstreamServiceProvider.php +++ b/src/LibstreamServiceProvider.php @@ -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', @@ -27,4 +30,9 @@ public function configurePackage(Package $package): void ]) ->runsMigrations(); } + + public function packageRegistered() + { + config(['verbs.id_type' => 'ulid']); + } } From 4e8b44a2e81bdfa5e5216184ac0eb7873d28782e Mon Sep 17 00:00:00 2001 From: inmanturbo Date: Thu, 1 Aug 2024 15:53:26 +0000 Subject: [PATCH 3/3] Fix styling --- src/Commands/LibstreamMigrateCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Commands/LibstreamMigrateCommand.php b/src/Commands/LibstreamMigrateCommand.php index 5d7db3a..a66e95d 100644 --- a/src/Commands/LibstreamMigrateCommand.php +++ b/src/Commands/LibstreamMigrateCommand.php @@ -44,11 +44,11 @@ public function handle(): int protected function tables(): array { $defaultTables = array_merge( - config('libstream.migration_tables', []), + config('libstream.migration_tables', []), [ - config('verbs.tables.events', 'verbs_events'), - config('verbs.tables.snapshots', 'verbs_snapshots'), - config('verbs.tables.state_events', 'verbs_state_events') + 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', []);