mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Command "orm:validate-schema --skip-sync" still expects a valid DB connection #6287
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @delolmo on GitHub (Aug 21, 2019).
Originally assigned to: @lcobucci on GitHub.
Bug Report
Summary
The command
orm:validate-schema --skip-syncwill throw a PDOException if a valid connection is not given.Current behavior
For a specific use case, we have a private repo with some shared entities and entity repositories which are common to several PHP projects. The only dependency of the private repo is doctrine/orm, version ^2.6.
Because the repo is so specific to Doctrine ORM, we have a Composer script that executes the following command as part of the CI strategy:
...the goal being to check if the entity mappings are valid.
However, due to the way that
Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommandis coded (the EntityManager requires a valid connection as its first parameter), if there is no connection to the database the command will fail, throwing a PDOException.The SchemaValidator requires the EntityManager because it needs to access the metadata factory (
\Doctrine\ORM\Mapping\ClassMetadataFactory).Should a new class be made, i.e. MappingValidator, which receives in its constructor a ClassMetadataFactory as its only argument? Is there any other way to circumvent this error (i.e. passing a dummy database connection)?
I am unsure on how to proceed to try and solve this bug because I don't know the internals of Doctrine very well, but I will be happy to try and help coding something if someone can give me some guidance.
How to reproduce
Execute
orm:validate-schema --skip-syncwithout a valid database connection available.Expected behavior
The command is expected to check either for valid entity mappings, for synchronicity with the database or both. If only the first option is selected, there is no reason to require a database connection to validate the entity mappings.
@lcobucci commented on GitHub (Aug 22, 2019):
@delolmo this is a tricky one due to how the EM is created and the configuration of the CLI tool...
I think we could improve this for
3.xseries but I wouldn't change2.xdrastically to accommodate these needs (it might create more complexity, which is not desired now for2.x).My suggestion to you is to setup the CLI config to use an in-memory SQLite connection, which won't be used at all in your use case. Which would make your
cli-config.phplook like this:It's not a perfect solution but should solve your need, what do you think?
@delolmo commented on GitHub (Aug 23, 2019):
It works perfectly. However, I had to go a little bit further because some developers also do db updates from the repository. The
orm:schema-tool:*commands obviously need a db connection.What I finally did was add a
config/.envfile to the .gitignore. If the file is present, the EM will be created using the environment variables defined in that file. If not, it will create the EM with the in-memory connection you suggested.The approach may not be perfect, but it will allow us to work locally and to use the CI tool.
What do you think?
@delolmo commented on GitHub (Aug 30, 2019):
Closing this issue. Thanks @lcobucci.
Hope to see mapping validation in the 3.x version without requiring a valid database connection.