Skip to content

Commit

Permalink
Allow to change Models database connection
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed May 9, 2020
1 parent ca0e683 commit 555e02c
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 10 deletions.
17 changes: 17 additions & 0 deletions config/passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,21 @@
'secret' => env('PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET'),
],

/*
|--------------------------------------------------------------------------
| Passport Storage Driver
|--------------------------------------------------------------------------
|
| This configuration options determines the storage driver that will
| be used to store Passport's data. In addition, you may set any
| custom options as needed by the particular driver you choose.
|
*/

'storage' => [
'database' => [
'connection' => env('DB_CONNECTION', 'mysql'),
],
],

];
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,41 @@

class CreateOauthAuthCodesTable extends Migration
{
/**
* The database schema.
*
* @var \Illuminate\Database\Schema\Builder
*/
protected $schema;

/**
* Create a new migration instance.
*
* @return void
*/
public function __construct()
{
$this->schema = Schema::connection($this->getConnection());
}

/**
* Get the migration connection name.
*
* @return string|null
*/
public function getConnection()
{
return config('passport.storage.database.connection');
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_auth_codes', function (Blueprint $table) {
$this->schema->create('oauth_auth_codes', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->unsignedBigInteger('user_id')->index();
$table->unsignedBigInteger('client_id');
Expand All @@ -30,6 +57,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('oauth_auth_codes');
$this->schema->dropIfExists('oauth_auth_codes');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,41 @@

class CreateOauthAccessTokensTable extends Migration
{
/**
* The database schema.
*
* @var \Illuminate\Database\Schema\Builder
*/
protected $schema;

/**
* Create a new migration instance.
*
* @return void
*/
public function __construct()
{
$this->schema = Schema::connection($this->getConnection());
}

/**
* Get the migration connection name.
*
* @return string|null
*/
public function getConnection()
{
return config('passport.storage.database.connection');
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_access_tokens', function (Blueprint $table) {
$this->schema->create('oauth_access_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->unsignedBigInteger('client_id');
Expand All @@ -32,6 +59,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('oauth_access_tokens');
$this->schema->dropIfExists('oauth_access_tokens');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,41 @@

class CreateOauthRefreshTokensTable extends Migration
{
/**
* The database schema.
*
* @var \Illuminate\Database\Schema\Builder
*/
protected $schema;

/**
* Create a new migration instance.
*
* @return void
*/
public function __construct()
{
$this->schema = Schema::connection($this->getConnection());
}

/**
* Get the migration connection name.
*
* @return string|null
*/
public function getConnection()
{
return config('passport.storage.database.connection');
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_refresh_tokens', function (Blueprint $table) {
$this->schema->create('oauth_refresh_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->string('access_token_id', 100);
$table->boolean('revoked');
Expand All @@ -28,6 +55,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('oauth_refresh_tokens');
$this->schema->dropIfExists('oauth_refresh_tokens');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,41 @@

class CreateOauthClientsTable extends Migration
{
/**
* The database schema.
*
* @var \Illuminate\Database\Schema\Builder
*/
protected $schema;

/**
* Create a new migration instance.
*
* @return void
*/
public function __construct()
{
$this->schema = Schema::connection($this->getConnection());
}

/**
* Get the migration connection name.
*
* @return string|null
*/
public function getConnection()
{
return config('passport.storage.database.connection');
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_clients', function (Blueprint $table) {
$this->schema->create('oauth_clients', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->string('name');
Expand All @@ -34,6 +61,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('oauth_clients');
$this->schema->dropIfExists('oauth_clients');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,41 @@

class CreateOauthPersonalAccessClientsTable extends Migration
{
/**
* The database schema.
*
* @var \Illuminate\Database\Schema\Builder
*/
protected $schema;

/**
* Create a new migration instance.
*
* @return void
*/
public function __construct()
{
$this->schema = Schema::connection($this->getConnection());
}

/**
* Get the migration connection name.
*
* @return string|null
*/
public function getConnection()
{
return config('passport.storage.database.connection');
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_personal_access_clients', function (Blueprint $table) {
$this->schema->create('oauth_personal_access_clients', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('client_id');
$table->timestamps();
Expand All @@ -27,6 +54,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('oauth_personal_access_clients');
$this->schema->dropIfExists('oauth_personal_access_clients');
}
}
10 changes: 10 additions & 0 deletions src/AuthCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class AuthCode extends Model
*/
protected $keyType = 'string';

/**
* Get the current connection name for the model.
*
* @return string|null
*/
public function getConnectionName()
{
return config('passport.storage.database.connection') ?? $this->connection;
}

/**
* Get the client that owns the authentication code.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ public static function boot()
});
}

/**
* Get the current connection name for the model.
*
* @return string|null
*/
public function getConnectionName()
{
return config('passport.storage.database.connection') ?? $this->connection;
}

/**
* Get the user that the client belongs to.
*
Expand Down
10 changes: 10 additions & 0 deletions src/PersonalAccessClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class PersonalAccessClient extends Model
*/
protected $guarded = [];

/**
* Get the current connection name for the model.
*
* @return string|null
*/
public function getConnectionName()
{
return config('passport.storage.database.connection') ?? $this->connection;
}

/**
* Get all of the authentication codes for the client.
*
Expand Down
10 changes: 10 additions & 0 deletions src/RefreshToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class RefreshToken extends Model
*/
public $timestamps = false;

/**
* Get the current connection name for the model.
*
* @return string|null
*/
public function getConnectionName()
{
return config('passport.storage.database.connection') ?? $this->connection;
}

/**
* Get the access token that the refresh token belongs to.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ class Token extends Model
*/
public $timestamps = false;

/**
* Get the current connection name for the model.
*
* @return string|null
*/
public function getConnectionName()
{
return config('passport.storage.database.connection') ?? $this->connection;
}

/**
* Get the client that the token belongs to.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/PassportTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ protected function getEnvironmentSetUp($app)

$app['config']->set('database.default', 'testbench');

$app['config']->set('passport.storage.database.connection', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
Expand Down

0 comments on commit 555e02c

Please sign in to comment.