fix: sync user/company to sandbox before creating test API key#203
Merged
roncodes merged 1 commit intodev-v1.6.41from Apr 21, 2026
Merged
fix: sync user/company to sandbox before creating test API key#203roncodes merged 1 commit intodev-v1.6.41from
roncodes merged 1 commit intodev-v1.6.41from
Conversation
When a test/sandbox API key is created the request carries the Access-Console-Sandbox header, which causes SetupFleetbaseSession to switch the default database connection to 'sandbox'. The subsequent INSERT into api_credentials on the sandbox DB references user_uuid and company_uuid values that come from the production session. Because those rows do not necessarily exist in the sandbox database the foreign key constraint api_credentials_user_uuid_foreign (and the companion company_uuid constraint) fires and the insert fails with SQLSTATE[23000] 1452. Fix: override createRecord() in ApiCredentialController to detect the sandbox header and, before delegating to the generic create path, mirror the current user, company, and company_user pivot row from production into the sandbox DB using an on-demand upsert (the same pattern used by the sandbox:sync Artisan command). Foreign key checks are temporarily disabled during the upsert to avoid ordering issues, then re-enabled before the api_credentials insert proceeds.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a user creates a test/sandbox API key (i.e. the request carries the
Access-Console-Sandbox: trueheader), theSetupFleetbaseSessionmiddleware callsAuth::setSandboxSession($request), which switchesdatabase.defaulttosandbox. The subsequentINSERTintoapi_credentialstherefore targets the sandbox database.However, the
user_uuidandcompany_uuidvalues populated byfillSessionAttributes()come from the production session — those rows exist in the productionusersandcompaniestables but are not guaranteed to exist in the sandbox database (unless thesandbox:syncArtisan command has been run manually).The sandbox database enforces the same foreign key constraints as production:
Because the referenced
usersrow is absent in the sandbox DB, MySQL raises:Root Cause
The creation path for
ApiCredentialis entirely generic (viaHasApiControllerBehavior::createRecord→HasApiModelBehavior::createRecordFromRequest→static::create()). There is no sandbox-aware pre-create step that ensures the referencedusers/companiesrows exist in the sandbox DB before the insert is attempted.Fix
Override
createRecord()inApiCredentialControllerto detect the sandbox header and, before delegating to the parent generic create path, mirror the current user, company, andcompany_userspivot row from production into the sandbox DB using an on-demandupdateOrInsertupsert.This is the same approach used by the
sandbox:syncArtisan command (SyncSandbox), applied on-demand and scoped only to the three rows needed to satisfy the FK constraints:users— satisfiesapi_credentials_user_uuid_foreigncompanies— satisfiesapi_credentials_company_uuid_foreigncompany_users— keeps the org-membership pivot consistent in sandboxForeign key checks are temporarily disabled during the upsert (identical to
sandbox:sync) to avoid ordering issues, then re-enabled before theapi_credentialsinsert proceeds.Files Changed
src/Http/Controllers/Internal/v1/ApiCredentialController.phpcreateRecord()override that callssyncCurrentSessionToSandbox()when the sandbox header is present.syncCurrentSessionToSandbox()— fetches the current user, company, and pivot from production and upserts them into sandbox.upsertModelToSandbox()— reusable helper that mirrors a single model record into the sandbox DB (normalises datetimes, JSON-encodes Json-cast columns, upserts byuuid).Testing
sandbox:migrate).user_uuid/company_uuidare not yet in the sandbox DB.SQLSTATE[23000]error.users,companies, andcompany_usersrows now exist in the sandbox DB.