Skip to content

Commit

Permalink
Adds configurable database connections (#167)
Browse files Browse the repository at this point in the history
* Adds configurable database connections

* Updated config docblock

* Update database/migrations/2024_04_16_115559_create_verb_state_events_table.php

Co-authored-by: Chris Morrell <[email protected]>

---------

Co-authored-by: Chris Morrell <[email protected]>
  • Loading branch information
scottzirkel and inxilpro authored Sep 15, 2024
1 parent 46a20a0 commit 738bf58
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 14 deletions.
15 changes: 15 additions & 0 deletions config/verbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@
PropertyNormalizer::NORMALIZE_VISIBILITY => PropertyNormalizer::NORMALIZE_PUBLIC,
],

/*
|--------------------------------------------------------------------------
| Connection Names
|--------------------------------------------------------------------------
|
| By default, Verbs will use your default database connection, However, you may
| wish to customize these connection names to better fit your application.
|
*/
'connections' => [
'events' => env('VERBS_EVENTS_CONNECTION'),
'snapshots' => env('VERBS_SNAPSHOT_CONNECTION'),
'state_events' => env('VERBS_STATE_EVENTS_CONNECTION'),
],

/*
|--------------------------------------------------------------------------
| Table Names
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
public function up()
{
// If they've already migrated under the previous migration name, just skip
if (Schema::hasTable($this->tableName())) {
if (Schema::connection($this->connectionName())->hasTable($this->tableName())) {
return;
}

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

$table->string('type')->index();
Expand All @@ -26,7 +26,12 @@ public function up()

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

protected function connectionName(): ?string
{
return config('verbs.connections.events');
}

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

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

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

// The 'state_id' column needs to be set up differently depending on
Expand All @@ -39,18 +39,19 @@ public function up()
});

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

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

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

Expand All @@ -67,7 +68,12 @@ protected function migrateChunk(Collection $chunk): void
'updated_at' => $row->updated_at,
]);

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

protected function connectionName(): ?string
{
return config('verbs.connections.snapshots');
}

protected function tableName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
public function up()
{
// If they've already migrated under the previous migration name, just skip
if (Schema::hasTable($this->tableName())) {
if (Schema::connection($this->connectionName())->hasTable($this->tableName())) {
return;
}

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

$table->snowflake('event_id')->index();
Expand All @@ -31,7 +31,12 @@ public function up()

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

protected function connectionName(): ?string
{
return config('verbs.connections.state_events');
}

protected function tableName(): string
Expand Down
5 changes: 5 additions & 0 deletions src/Models/VerbEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class VerbEvent extends Model

protected ?Metadata $meta = null;

public function getConnectionName()
{
return $this->connection ?? config('verbs.connections.events');
}

public function getTable()
{
return $this->table ?? config('verbs.tables.events', 'verb_events');
Expand Down
5 changes: 5 additions & 0 deletions src/Models/VerbSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class VerbSnapshot extends Model

protected ?State $state = null;

public function getConnectionName()
{
return $this->connection ?? config('verbs.connections.snapshots');
}

public function getTable()
{
return $this->table ?? config('verbs.tables.snapshots', 'verb_snapshots');
Expand Down
5 changes: 5 additions & 0 deletions src/Models/VerbStateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class VerbStateEvent extends Model
{
public $guarded = [];

public function getConnectionName()
{
return $this->connection ?? config('verbs.connections.state_events');
}

public function getTable()
{
return $this->table ?? config('verbs.tables.state_events', 'verb_state_events');
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/ConfigurableTableNamesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
use Thunk\Verbs\Models\VerbSnapshot;
use Thunk\Verbs\Models\VerbStateEvent;

test('VerbEvent connection name can be configured', function () {
$expected_connection_name = 'events';

config()->set('verbs.connections.events', $expected_connection_name);

$verb_model = new VerbEvent;
$actual_connection_name = $verb_model->getConnectionName();
expect($expected_connection_name)->toBe($actual_connection_name);
});

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

Expand All @@ -24,6 +34,16 @@
expect($expected_table_name)->toBe($actual_table_name);
});

test('VerbSnapshot connection name can be configured', function () {
$expected_connection_name = 'events';

config()->set('verbs.connections.snapshots', $expected_connection_name);

$verb_model = new VerbSnapshot;
$actual_connection_name = $verb_model->getConnectionName();
expect($expected_connection_name)->toBe($actual_connection_name);
});

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

Expand All @@ -44,6 +64,16 @@
expect($expected_table_name)->toBe($actual_table_name);
});

test('VerbStateEvent connection name can be configured', function () {
$expected_connection_name = 'events';

config()->set('verbs.connections.state_events', $expected_connection_name);

$verb_model = new VerbStateEvent;
$actual_connection_name = $verb_model->getConnectionName();
expect($expected_connection_name)->toBe($actual_connection_name);
});

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

Expand Down

0 comments on commit 738bf58

Please sign in to comment.