Skip to content

Commit

Permalink
Make Verbs Table Name Configurable (hirethunk#77)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
onairmarc and inxilpro authored Mar 5, 2024
1 parent e907c44 commit c04fc98
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 12 deletions.
15 changes: 15 additions & 0 deletions config/verbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions database/migrations/create_verb_events_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
}
};
9 changes: 7 additions & 2 deletions database/migrations/create_verb_snapshots_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
}
};
9 changes: 7 additions & 2 deletions database/migrations/create_verb_state_events_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
}
};
7 changes: 5 additions & 2 deletions src/Models/VerbEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

class VerbEvent extends Model
{
public $table = 'verb_events';

public $guarded = [];

protected $casts = [
Expand All @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/Models/VerbSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/Models/VerbStateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
65 changes: 65 additions & 0 deletions tests/Unit/ConfigurableTableNamesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

use Thunk\Verbs\Models\VerbEvent;
use Thunk\Verbs\Models\VerbSnapshot;
use Thunk\Verbs\Models\VerbStateEvent;

test('VerbEvent table name can be configured', function () {
$expected_table_name = 'verb_events';

$verb_model = new VerbEvent();
$actual_table_name = $verb_model->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);
});

0 comments on commit c04fc98

Please sign in to comment.