Skip to content

Commit

Permalink
Merge pull request #568 from leMaur/master
Browse files Browse the repository at this point in the history
Add database connection
  • Loading branch information
Gummibeer authored Jul 23, 2019
2 parents 62f5e23 + f41a854 commit 5dfff99
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
6 changes: 6 additions & 0 deletions config/activitylog.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@
* used by the Activity model shipped with this package.
*/
'table_name' => 'activity_log',

/*
* This is the database connection that will be used by the migration and
* the Activity model shipped with this package.
*/
'database_connection' => env('ACTIVITY_LOGGER_DB_CONNECTION', 'mysql'),
];
4 changes: 2 additions & 2 deletions migrations/create_activity_log_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CreateActivityLogTable extends Migration
*/
public function up()
{
Schema::create(config('activitylog.table_name'), function (Blueprint $table) {
Schema::connection(config('activitylog.database_connection'))->create(config('activitylog.table_name'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('log_name')->nullable();
$table->text('description');
Expand All @@ -31,6 +31,6 @@ class CreateActivityLogTable extends Migration
*/
public function down()
{
Schema::dropIfExists(config('activitylog.table_name'));
Schema::connection(config('activitylog.database_connection'))->dropIfExists(config('activitylog.table_name'));
}
}
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<env name="DB_CONNECTION" value="testing" />
</php>
</phpunit>
4 changes: 4 additions & 0 deletions src/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class Activity extends Model implements ActivityContract

public function __construct(array $attributes = [])
{
if (! isset($this->connection)) {
$this->setConnection(config('activitylog.database_connection'));
}

if (! isset($this->table)) {
$this->setTable(config('activitylog.table_name'));
}
Expand Down
37 changes: 37 additions & 0 deletions tests/CustomDatabaseConnectionActivityModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Spatie\Activitylog\Test;

use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Test\Models\CustomDatabaseConnectionOnActivityModel;

class CustomDatabaseConnectionActivityModelTest extends TestCase
{
/** @test */
public function it_uses_the_database_connection_from_the_configuration()
{
$model = new Activity();

$this->assertEquals($model->getConnectionName(), config('activitylog.database_connection'));
}

/** @test */
public function it_uses_a_custom_database_connection()
{
$model = new Activity();

$model->setConnection('custom_sqlite');

$this->assertNotEquals($model->getConnectionName(), config('activitylog.database_connection'));
$this->assertEquals($model->getConnectionName(), 'custom_sqlite');
}

/** @test */
public function it_uses_the_database_connection_from_model()
{
$model = new CustomDatabaseConnectionOnActivityModel();

$this->assertNotEquals($model->getConnectionName(), config('activitylog.database_connection'));
$this->assertEquals($model->getConnectionName(), 'custom_connection_name');
}
}
10 changes: 10 additions & 0 deletions tests/Models/CustomDatabaseConnectionOnActivityModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Spatie\Activitylog\Test\Models;

use Spatie\Activitylog\Models\Activity;

class CustomDatabaseConnectionOnActivityModel extends Activity
{
protected $connection = 'custom_connection_name';
}
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ protected function getPackageProviders($app)

public function getEnvironmentSetUp($app)
{
$app['config']->set('activitylog.database_connection', 'sqlite');
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
'database' => ':memory:',
]);

$app['config']->set('auth.providers.users.model', User::class);
$app['config']->set('app.key', 'base64:'.base64_encode(
Encrypter::generateKey($app['config']['app.cipher'])
Expand Down

0 comments on commit 5dfff99

Please sign in to comment.