mpociot / laravel-test-factory-helper

Generate Laravel test factories from your existing models
935 stars 87 forks source link

PostgreSQL query fix #51

Open Chingoski opened 4 years ago

Chingoski commented 4 years ago

If the application is connected to an postgres DB then the query that existed wouldn't suffice, because postgres has a different syntax. I added a simple if clause to check if the laravel application is connected to an postgres DB, if so the query will be executed with proper postgres syntax else the existing query will be executed. This will remove the SQL error as well as the class not recognized error when running the php artisan command for applications that have postgres DB.

jasonmccreary commented 4 years ago

Thanks. I'd like to leverage Doctrine. I believe it's already a dependency of the project and handles all this "what type of DB" is it logic for free.

I'm going to leave this open for a bit. In the meantime, anyone landing here can feel free to pull this branch.

rmundel commented 4 years ago

What if the user has custom connection names?

I think the best way would be to add a option to the command, to pass the desired connection name if not default:

protected $name = 'generate:model-factory {--connection=?}';

Than, it should query for the connection driver if the option is set:

$connection = $this->option('connection');

$dbDriver = config("database.connections.$connection.driver");

if ($dbDriver === 'pgsql') {

Something like that, give or take.

TheDoctor0 commented 4 years ago

Even in case of custom connection name that probably still will be a default connection. It would be best to just check the connection driver, not a name.

Something like that should work fine:

if (DB::connection()->getDriverName() === 'pgsql') {
AhmadWaleed commented 4 years ago

Any plan on merging this?