From e2d0fe3cd65af6e6e8e9b26d1243cded16bde168 Mon Sep 17 00:00:00 2001 From: paranarimasu <33796518+paranarimasu@users.noreply.github.com> Date: Mon, 15 May 2023 23:53:18 -0500 Subject: [PATCH] feat: allow models to specify database connection (#575) --- .env.example | 10 +++++++++- app/Models/BaseModel.php | 20 ++++++++++++++++++++ config/database.php | 28 ++++++++++++++++++++++++++-- config/eloquent-viewable.php | 2 +- config/queue.php | 2 +- config/telescope.php | 2 +- 6 files changed, 58 insertions(+), 6 deletions(-) diff --git a/.env.example b/.env.example index 842658a9c..841ce8070 100644 --- a/.env.example +++ b/.env.example @@ -48,7 +48,7 @@ DYNAMODB_ENDPOINT= CACHE_PREFIX=animethemes # database -DB_CONNECTION=mysql +DB_CONNECTION=mysql_prod DATABASE_URL= DB_HOST_READ=127.0.0.1 DB_HOST_WRITE=127.0.0.1 @@ -60,6 +60,14 @@ DB_PASSWORD= DB_SOCKET= MYSQL_ATTR_SSL_CA=null +DATABASE_BETA_URL= +DB_BETA_HOST= +DB_BETA_PORT=3306 +DB_BETA_DATABASE=laravel +DB_BETA_USERNAME=root +DB_BETA_PASSWORD= +DB_BETA_SOCKET= + REDIS_CLIENT=predis REDIS_CLUSTER=redis REDIS_URL=null diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index b197207a7..e2818ac5f 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -12,6 +12,8 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Prunable; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Config; +use Illuminate\Support\Str; /** * Class BaseModel. @@ -37,6 +39,24 @@ abstract class BaseModel extends Model implements Nameable final public const ATTRIBUTE_DELETED_AT = 'deleted_at'; final public const ATTRIBUTE_UPDATED_AT = Model::UPDATED_AT; + /** + * Create a new Eloquent model instance. + * + * @param array $attributes + */ + public function __construct(array $attributes = []) + { + parent::__construct($attributes); + + $connectionKey = Str::of('database.models.') + ->append(static::class) + ->__toString(); + + if (Config::has($connectionKey)) { + $this->setConnection(Config::get($connectionKey)); + } + } + /** * Restore a soft-deleted model instance. * diff --git a/config/database.php b/config/database.php index 52095a12f..7256a376c 100644 --- a/config/database.php +++ b/config/database.php @@ -17,7 +17,11 @@ return [ | */ - 'default' => env('DB_CONNECTION', 'mysql'), + 'default' => env('DB_CONNECTION', 'mysql_prod'), + + 'models' => [ + // Override default connection by model class + ], /* |-------------------------------------------------------------------------- @@ -45,7 +49,7 @@ return [ 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), ], - 'mysql' => [ + 'mysql_prod' => [ 'driver' => 'mysql', 'read' => [ 'host' => [ @@ -76,6 +80,26 @@ return [ ]) : [], ], + 'mysql_beta' => [ + 'driver' => 'mysql', + 'url' => env('DATABASE_BETA_URL'), + 'host' => env('DB_BETA_HOST', '127.0.0.1'), + 'port' => env('DB_BETA_PORT', '3306'), + 'database' => env('DB_BETA_DATABASE', 'forge'), + 'username' => env('DB_BETA_USERNAME', 'forge'), + 'password' => env('DB_BETA_PASSWORD', ''), + 'unix_socket' => env('DB_BETA_SOCKET', ''), + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'prefix' => '', + 'prefix_indexes' => true, + 'strict' => true, + 'engine' => null, + 'options' => extension_loaded('pdo_mysql') ? array_filter([ + PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), + ]) : [], + ], + 'pgsql' => [ 'driver' => 'pgsql', 'read' => [ diff --git a/config/eloquent-viewable.php b/config/eloquent-viewable.php index 2509d83cd..4742c53ed 100644 --- a/config/eloquent-viewable.php +++ b/config/eloquent-viewable.php @@ -17,7 +17,7 @@ return [ 'view' => [ 'table_name' => 'views', - 'connection' => env('DB_CONNECTION', 'mysql'), + 'connection' => env('DB_CONNECTION', 'mysql_prod'), ], diff --git a/config/queue.php b/config/queue.php index 10a1d9877..d4a56a5e0 100644 --- a/config/queue.php +++ b/config/queue.php @@ -87,7 +87,7 @@ return [ */ 'batching' => [ - 'database' => env('DB_CONNECTION', 'mysql'), + 'database' => env('DB_CONNECTION', 'mysql_prod'), 'table' => 'job_batches', ], diff --git a/config/telescope.php b/config/telescope.php index 568c4521e..4666dfe72 100644 --- a/config/telescope.php +++ b/config/telescope.php @@ -48,7 +48,7 @@ return [ 'storage' => [ 'database' => [ - 'connection' => env('DB_CONNECTION', 'mysql'), + 'connection' => env('DB_CONNECTION', 'mysql_prod'), 'chunk' => 1000, ], ],