Skip to content

[11.x] Add a method to connection for driver and version comparison#49723

Closed
hafezdivandari wants to merge 3 commits intolaravel:masterfrom
hafezdivandari:master-get-server-version
Closed

[11.x] Add a method to connection for driver and version comparison#49723
hafezdivandari wants to merge 3 commits intolaravel:masterfrom
hafezdivandari:master-get-server-version

Conversation

@hafezdivandari
Copy link
Copy Markdown
Contributor

@hafezdivandari hafezdivandari commented Jan 17, 2024

Determining database driver and version to handle different databases' capabilities is tricky, this PR adds a new is method to Connection for convenient driver and version comparison.

Usage

Almost all these syntaxes are currently used on the framework:

DB::is('mysql'); // true|false

DB::is('pgsql', '>=', '9.5'); // true|false

DB::is(['mysql', 'mariadb']); // true|false

DB::is([['mysql', '<', '8.0.3'], ['mariadb', '<', '10.5.2']]); // true|false

DB::is(['sqlite' => ['>', '3.35.0'], 'vitess' => ['>', '19.0']]); // true|false

DB::is(fn ($driver, $version) => $driver === 'pgsql' && version_compare($version, '10.0', '<')); // true|false

DB::is(fn ($driver) => $driver === 'sqlsrv' ? 'datetime' : 'timestamp'); // 'timestamp'|'datetime'

Why?

The best reason is the changed code of this PR. We need this method to check driver and actual version of the database to handle different grammar syntaxes, features and capabilities of each one.

Support

Supported drivers are based on the current usage on the framework, maridb, mysql, sqlite, pgsql, sqlsrv and vitess (i.e PlanetScale).
Other databases (e.g. SingleStoreDB, CockroachDB, Supabase, etc.) may be added later.

@github-actions
Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@hafezdivandari hafezdivandari marked this pull request as ready for review January 17, 2024 17:45
@taylorotwell
Copy link
Copy Markdown
Member

I dunno - I think our current code is pretty fine?

@hafezdivandari
Copy link
Copy Markdown
Contributor Author

hafezdivandari commented Jan 19, 2024

Current code just works, but we need single source of truth for driver and version comparison. Also the package developers (and end-users on some scenarios) gonna need this regarding the changes on 11.x

@hafezdivandari hafezdivandari deleted the master-get-server-version branch January 19, 2024 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants