Skip to content

Commit a7294ad

Browse files
author
Ronald A Richardson
committed
fix: pin User, Company, CompanyUser to production connection via getConnectionName()
These models are authoritative in production. Sandbox contains only a read-only mirror synced by sandbox:sync. Using getConnectionName() ensures Eloquent always routes queries for these models to the production DB, regardless of what setSandboxSession() does to database.default or fleetbase.connection.db at runtime. Also reverts the previous SyncSandbox change that added CompanyUser to the syncable list — that was a workaround for the wrong root cause.
1 parent 7c5d1ca commit a7294ad

4 files changed

Lines changed: 35 additions & 4 deletions

File tree

src/Console/Commands/SyncSandbox.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,10 @@ public function handle()
5656
DB::connection('sandbox')
5757
->table('api_credentials')
5858
->truncate();
59-
DB::connection('sandbox')
60-
->table('company_users')
61-
->truncate();
6259
}
6360

6461
// Models that need to be synced from Production to Sandbox
65-
$syncable = [\Fleetbase\Models\User::class, \Fleetbase\Models\Company::class, \Fleetbase\Models\CompanyUser::class, \Fleetbase\Models\ApiCredential::class];
62+
$syncable = [\Fleetbase\Models\User::class, \Fleetbase\Models\Company::class, \Fleetbase\Models\ApiCredential::class];
6663

6764
// Sync each syncable data model
6865
foreach ($syncable as $model) {

src/Models/Company.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ 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+
4555
/**
4656
* The database table used by the model.
4757
*

src/Models/CompanyUser.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ 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+
2636
/**
2737
* The attributes that are mass assignable.
2838
*

src/Models/User.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ 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+
7791
/**
7892
* Override the default primary key.
7993
*

0 commit comments

Comments
 (0)