From c04fc989d346a7b3bdd3e64abd4461d562aad112 Mon Sep 17 00:00:00 2001 From: Marc Beinder <50760632+onairmarc@users.noreply.github.com> Date: Mon, 4 Mar 2024 21:32:54 -0600 Subject: [PATCH] Make Verbs Table Name Configurable (#77) * Make Verbs Table Name Configurable * Change Models/{MODEL} setTable() to Public * Create ConfigurableTableNamesTest * Fixed setTable Implementation * Fixed $expected_table_name * Set getTable() * Added Test Cases for Customized Table Names * Refactored and Simplified setTable in Models and Test * Refactor * Extract out a function --------- Co-authored-by: Chris Morrell --- config/verbs.php | 15 +++++ .../migrations/create_verb_events_table.php | 9 ++- .../create_verb_snapshots_table.php | 9 ++- .../create_verb_state_events_table.php | 9 ++- src/Models/VerbEvent.php | 7 +- src/Models/VerbSnapshot.php | 7 +- src/Models/VerbStateEvent.php | 7 +- tests/Unit/ConfigurableTableNamesTest.php | 65 +++++++++++++++++++ 8 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 tests/Unit/ConfigurableTableNamesTest.php diff --git a/config/verbs.php b/config/verbs.php index 083ff314..151d6f11 100644 --- a/config/verbs.php +++ b/config/verbs.php @@ -51,6 +51,21 @@ PropertyNormalizer::class, ], + /* + |-------------------------------------------------------------------------- + | Table Names + |-------------------------------------------------------------------------- + | + | By default, Verbs prefixes all of its table names with "verb_". However, you + | may wish to customize these table names to better fit your application. + | + */ + 'tables' => [ + 'events' => 'verb_events', + 'snapshots' => 'verb_snapshots', + 'state_events' => 'verb_state_events', + ], + /* |-------------------------------------------------------------------------- | Wormhole diff --git a/database/migrations/create_verb_events_table.php b/database/migrations/create_verb_events_table.php index 10ec7ed9..9ca12d12 100644 --- a/database/migrations/create_verb_events_table.php +++ b/database/migrations/create_verb_events_table.php @@ -8,7 +8,7 @@ { public function up() { - Schema::create('verb_events', function (Blueprint $table) { + Schema::create($this->tableName(), function (Blueprint $table) { $table->snowflakeId(); $table->string('type')->index(); @@ -21,6 +21,11 @@ public function up() public function down() { - Schema::dropIfExists('verb_events'); + Schema::dropIfExists($this->tableName()); + } + + protected function tableName(): string + { + return config('verbs.tables.events', 'verb_events'); } }; diff --git a/database/migrations/create_verb_snapshots_table.php b/database/migrations/create_verb_snapshots_table.php index f7cf1a06..8685af56 100644 --- a/database/migrations/create_verb_snapshots_table.php +++ b/database/migrations/create_verb_snapshots_table.php @@ -9,7 +9,7 @@ { public function up() { - Schema::create('verb_snapshots', function (Blueprint $table) { + Schema::create($this->tableName(), function (Blueprint $table) { // The 'id' column needs to be set up differently depending // on if you're using Snowflakes vs. ULIDs/etc. $idColumn = Id::createColumnDefinition($table)->primary(); @@ -27,6 +27,11 @@ public function up() public function down() { - Schema::dropIfExists('verb_snapshots'); + Schema::dropIfExists($this->tableName()); + } + + protected function tableName(): string + { + return config('verbs.tables.snapshots', 'verb_snapshots'); } }; diff --git a/database/migrations/create_verb_state_events_table.php b/database/migrations/create_verb_state_events_table.php index ca050efc..cb0d4226 100644 --- a/database/migrations/create_verb_state_events_table.php +++ b/database/migrations/create_verb_state_events_table.php @@ -9,7 +9,7 @@ { public function up() { - Schema::create('verb_state_events', function (Blueprint $table) { + Schema::create($this->tableName(), function (Blueprint $table) { $table->snowflakeId(); $table->snowflake('event_id')->index(); @@ -26,6 +26,11 @@ public function up() public function down() { - Schema::dropIfExists('verb_state_events'); + Schema::dropIfExists($this->tableName()); + } + + protected function tableName(): string + { + return config('verbs.tables.state_events', 'verb_state_events'); } }; diff --git a/src/Models/VerbEvent.php b/src/Models/VerbEvent.php index 1f3455bc..a0f26a24 100644 --- a/src/Models/VerbEvent.php +++ b/src/Models/VerbEvent.php @@ -12,8 +12,6 @@ class VerbEvent extends Model { - public $table = 'verb_events'; - public $guarded = []; protected $casts = [ @@ -30,6 +28,11 @@ class VerbEvent extends Model protected ?Metadata $meta = null; + public function getTable() + { + return $this->table ?? config('verbs.tables.events', 'verb_events'); + } + public function event(): Event { $this->event ??= app(Serializer::class)->deserialize($this->type, $this->data); diff --git a/src/Models/VerbSnapshot.php b/src/Models/VerbSnapshot.php index cef989f9..f407551f 100644 --- a/src/Models/VerbSnapshot.php +++ b/src/Models/VerbSnapshot.php @@ -17,12 +17,15 @@ */ class VerbSnapshot extends Model { - public $table = 'verb_snapshots'; - public $guarded = []; protected ?State $state = null; + public function getTable() + { + return $this->table ?? config('verbs.tables.snapshots', 'verb_snapshots'); + } + public function state(): State { $this->state ??= app(Serializer::class)->deserialize($this->type, $this->data); diff --git a/src/Models/VerbStateEvent.php b/src/Models/VerbStateEvent.php index 31fbd2ed..c5fb0338 100644 --- a/src/Models/VerbStateEvent.php +++ b/src/Models/VerbStateEvent.php @@ -7,10 +7,13 @@ class VerbStateEvent extends Model { - public $table = 'verb_state_events'; - public $guarded = []; + public function getTable() + { + return $this->table ?? config('verbs.tables.state_events', 'verb_state_events'); + } + public function event() { return $this->belongsTo(VerbEvent::class); diff --git a/tests/Unit/ConfigurableTableNamesTest.php b/tests/Unit/ConfigurableTableNamesTest.php new file mode 100644 index 00000000..ed0ec8b0 --- /dev/null +++ b/tests/Unit/ConfigurableTableNamesTest.php @@ -0,0 +1,65 @@ +getTable(); + + expect($expected_table_name)->toBe($actual_table_name); +}); + +test('VerbEvent table name can be configured with different table name', function () { + $expected_table_name = 'sys_verb_events'; + + config()->set('verbs.tables.events', $expected_table_name); + + $verb_model = new VerbEvent(); + $actual_table_name = $verb_model->getTable(); + + expect($expected_table_name)->toBe($actual_table_name); +}); + +test('VerbSnapshot table name can be configured', function () { + $expected_table_name = 'verb_snapshots'; + + $verb_model = new VerbSnapshot(); + $actual_table_name = $verb_model->getTable(); + + expect($expected_table_name)->toBe($actual_table_name); +}); + +test('VerbSnapshot table name can be configured with different table name', function () { + $expected_table_name = 'sys_verb_snapshots'; + + config(['verbs.tables.snapshots' => $expected_table_name]); + + $verb_model = new VerbSnapshot(); + $actual_table_name = $verb_model->getTable(); + + expect($expected_table_name)->toBe($actual_table_name); +}); + +test('VerbStateEvent table name can be configured', function () { + $expected_table_name = 'verb_state_events'; + + $verb_model = new VerbStateEvent(); + $actual_table_name = $verb_model->getTable(); + + expect($expected_table_name)->toBe($actual_table_name); +}); + +test('VerbStateEvent table name can be configured with different table name', function () { + $expected_table_name = 'sys_verb_state_events'; + + config(['verbs.tables.state_events' => $expected_table_name]); + + $verb_model = new VerbStateEvent(); + $actual_table_name = $verb_model->getTable(); + + expect($expected_table_name)->toBe($actual_table_name); +});