Doctrinebundle: A way to check whether database exists (extract functionality from `CreateDatabaseDoctrineCommand.php`)

Created on 24 Apr 2017  路  6Comments  路  Source: doctrine/DoctrineBundle

Hello,

Would it be possible to extract the part of CreateDatabaseDoctrineCommand.php, that is responsible for checking, whether the database already exists, please? A separate command (that returns 0 - "it exists", 1 - "it doesn't exist") or a service (Helper::doesDatabaseExist(): bool) would do the trick. The CreateDatabaseDoctrineCommand.php would then use that extracted functionality and I would be able to run my code conditionally on, whether the database exists or not.

Another solution would be to actually change AbstractSchemaManager::listDatabases() to NOT try to connect to any particular database (unless db connection is already established). I'd actually prefer this one, to be honest, so I can use it even outside of Symfony projects.

My use case is, that during my automated build, I need to query the database to see something before I run db migrations. This means, that the database and the schema might not exist at this point. If the database (or the schema) doesn't exist, I just assume, that "that something" is not there and I just carry on. I do NOT want/can create the database at that point (it's a library code.. long story).

In other words, the use case is not very common, but I believe it's also not that uncommon.

Please let me know what you think about all it and whether this could be implemented in the foreseeable future.

Thank you very much,
@d-ph

Won't Fix

Most helpful comment

what a pitty to see this closed, i would also can such a feature. my init script for all developers should check if the legacy database already exist and if not load a fixture sql file in the database. if the database already exists, it should leave the database alone.

something like `php bin/console doctrine:database:import foo.sql --if-not-exists=foo-database

All 6 comments

Of these very specific use cases i think it is better that one try to implemented something there fit the case.

what a pitty to see this closed, i would also can such a feature. my init script for all developers should check if the legacy database already exist and if not load a fixture sql file in the database. if the database already exists, it should leave the database alone.

something like `php bin/console doctrine:database:import foo.sql --if-not-exists=foo-database

You are welcome to try to implement some of your ideas. The reason i closed this was that it was phrased more like a question without a "specific" use case and no implementation.

The CreateDatabaseDoctrineCommand already have an "if-not-exists" switch.

A Specific command for checking if a database exists could maybe be an idea. No promises that it will merged ofc. Depends on implementation and how the PR are received by others.

Helper::doesDatabaseExist(): bool
I would properly say no to making this a public api. Sounds like bugfest and maintainer nightmare waiting to happen. But i could very much be wrong.

AbstractSchemaManager::listDatabases()
That depends on driver, platform and the parameters/options passed. Ex postgres driver will fallback to the "postgres" database for the connection if no database name is passed. So here listDatabases() should work. If this doesn't work for other drivers it is most likely a bug in dbal, the usage of the driver or just not possible with the driver.

php bin/console doctrine:database:import foo.sql --if-not-exists=foo-database
Sounds very specific to usage and some drivers, but i could be mistaken.

We at the doctrine team will not implement new features if we ourself have no need for them (Most of us working on doctrine do so in our spare time). So if you or some else have some ideas which could benefit others and the implementation is sane, there is a chance it could be merged.

@c33s You are welcome to c/p ImportCommand from dbal and implement your application logic yourself. We try to provider a toolbox for a lot of use cases. But sometimes you need to build custom tools for the job.

If we look at dbal import we will notice it can be used more then one time on the same database. So here a --if-not-exists and similar, would be usage specific in my humble opinion :)

@kimhemsoe thanks for your detailed reply. my comment here was only to show that there are still people out there interested in this feature. i found this issue googling for a solution. fixed already :)
still this feature would be helpful.

The way forward is pull requests from the community. :)

Was this page helpful?
0 / 5 - 0 ratings