lundi 9 mars 2020

Run specific select raw queries on write database in order to have fresh data due to replication lag

I use my Laravel 5.7 application a master-slave database replication scheme using postgresql:

'mydb' => [
    'driver' => 'pgsql',

    'read'  => [
        'host' => env('DB_HOST_READ', '192.168.2.3'),
    ],
    'write' => [
        'host' => env('DB_HOST_WRITE', '192.168.2.5'),
    ],
    'port'     => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'mydb'),
    'username' => env('DB_USERNAME', ''),
     'password' => env('DB_PASSWORD', ''),
    'charset'  => 'utf8',
    'prefix'   => '',
    'schema'   => 'public',
    'sslmode'  => 'prefer',
],

And I was provided some raw queries to run (the query was provided as is into my Jira ticket). These queries are reporting queries that need fresh data hence I do not want to tinker them a lot.

The Laravel itself provides ways to do so but either requires to create a model for example:

MyModel::limit(10)->useWritePdo()->get();

Or the query builder:

DB::connection("mydb")->table('user')->select("*")->limit(10)->useWritePdo()->get();

But I want to do the following:

DB::connection("mydb")->useWritePdo()->select("SELECT * from et_user");

Is there a way to do so?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire