Repository navigation
feat(ModelsCommand): add model_connection_resolver hook for dynamic connection/schema setup - #1788
Open
IlyasMohetna wants to merge 2 commits into
Open
IlyasMohetna wants to merge 2 commits into
IlyasMohetna wants to merge 2 commits into
Conversation
…onnection/schema setup
…onnection/schema setup
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.
Summary
The
ide-helper:modelscommand has no way to configure the database connection before it introspects a model's table. This is a problem for any setup where the active connection or schema depends on runtime context , the most common case being multi-tenant apps using PostgreSQL schemas-per-tenant (e.g. viastancl/tenancywithPostgreSQLSchemaManager).At artisan CLI time, no tenant is bootstrapped. The PostgreSQL connection's
search_pathstays onpublic, sogetPropertiesFromTable()finds no columns and silently produces empty docblocks for every tenant model.The existing
model_hooks/ModelHookInterfacedoesn't solve this. It was designed to add information to the docblock after introspection has already run. By the timerun()is called, the schema lookup has already failed.This PR adds a
model_connection_resolverconfig option that accepts a class implementing the newModelConnectionResolverInterface. The interface has two methods:resolve(Model $model)called beforegetPropertiesFromTable(), so you can set the right connection,search_path, or any other contextafter(Model $model)called aftergetPropertiesFromTable(), so you can revert whatever you changedThe feature is opt-in ; the config key defaults to
nulland the command behaviour is unchanged for anyone who doesn't set it.The resolver is instantiated once per command run (not once per model), so the implementor can cache expensive lookups (like fetching a schema name) internally.
Type of change
Checklist
composer fix-styleCloses #1141
Related: #1552