Skip to content
Permalink
Browse files

Merge pull request #568 from leMaur/master

Add database connection
  • Loading branch information...
Gummibeer committed Jul 23, 2019
2 parents 62f5e23 + f41a854 commit 5dfff99102794ba899c27e8431d62c0ac44ae1d5
@@ -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'),
];
@@ -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');
@@ -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'));
}
}
@@ -19,7 +19,4 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<env name="DB_CONNECTION" value="testing" />
</php>
</phpunit>
@@ -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'));
}
@@ -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');
}
}
@@ -0,0 +1,10 @@
<?php
namespace Spatie\Activitylog\Test\Models;
use Spatie\Activitylog\Models\Activity;
class CustomDatabaseConnectionOnActivityModel extends Activity
{
protected $connection = 'custom_connection_name';
}
@@ -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'])

0 comments on commit 5dfff99

Please sign in to comment.
You can’t perform that action at this time.