Skip to content

Commit d5ffc7a

Browse files
author
Ronald A Richardson
committed
fix: query users from production connection in UserController only
Remove the broad getConnectionName() overrides from User, Company, and CompanyUser models — those would break sandbox relations system-wide. Instead, override queryRecord() in UserController to temporarily restore the production connection for the duration of the user list query only, then restore the sandbox connection immediately after. This is surgical: only the IAM /users list hits production; all other sandbox queries (orders, drivers, etc.) continue to use the sandbox connection normally.
1 parent 684d01c commit d5ffc7a

4 files changed

Lines changed: 34 additions & 34 deletions

File tree

src/Http/Controllers/Internal/v1/UserController.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,40 @@ class UserController extends FleetbaseController
6868
*/
6969
public $updateRequest = UpdateUserRequest::class;
7070

71+
/**
72+
* Query users always against the production database.
73+
*
74+
* Users are authoritative in production. The sandbox database contains
75+
* only a mirrored copy. Temporarily restoring the production connection
76+
* for this query ensures the IAM list is correct regardless of whether
77+
* the console is in sandbox mode, without affecting any other sandbox
78+
* queries in the same request lifecycle.
79+
*
80+
* @return \Illuminate\Http\Response
81+
*/
82+
public function queryRecord(Request $request)
83+
{
84+
$isSandbox = config('fleetbase.connection.db') === 'sandbox';
85+
86+
if ($isSandbox) {
87+
config([
88+
'database.default' => env('DB_CONNECTION', 'mysql'),
89+
'fleetbase.connection.db' => env('DB_CONNECTION', 'mysql'),
90+
]);
91+
}
92+
93+
$response = parent::queryRecord($request);
94+
95+
if ($isSandbox) {
96+
config([
97+
'database.default' => 'sandbox',
98+
'fleetbase.connection.db' => 'sandbox',
99+
]);
100+
}
101+
102+
return $response;
103+
}
104+
71105
/**
72106
* Creates a record with request payload.
73107
*

src/Models/Company.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ class Company extends Model
4242
*/
4343
protected $connection = 'mysql';
4444

45-
/**
46-
* Always use the production database connection.
47-
*
48-
* @return string
49-
*/
50-
public function getConnectionName(): string
51-
{
52-
return env('DB_CONNECTION', 'mysql');
53-
}
54-
5545
/**
5646
* The database table used by the model.
5747
*

src/Models/CompanyUser.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ class CompanyUser extends Model
2323
*/
2424
protected $table = 'company_users';
2525

26-
/**
27-
* Always use the production database connection.
28-
*
29-
* @return string
30-
*/
31-
public function getConnectionName(): string
32-
{
33-
return env('DB_CONNECTION', 'mysql');
34-
}
35-
3626
/**
3727
* The attributes that are mass assignable.
3828
*

src/Models/User.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,6 @@ class User extends Authenticatable
7474
*/
7575
protected $connection = 'mysql';
7676

77-
/**
78-
* Always use the production database connection.
79-
*
80-
* User records are authoritative in production and are only mirrored
81-
* (read-only) to sandbox via sandbox:sync. Reads and writes must always
82-
* target production regardless of whether sandbox mode is active.
83-
*
84-
* @return string
85-
*/
86-
public function getConnectionName(): string
87-
{
88-
return env('DB_CONNECTION', 'mysql');
89-
}
90-
9177
/**
9278
* Override the default primary key.
9379
*

0 commit comments

Comments
 (0)