Skip to content

Commit

Permalink
Merge pull request #11900 from craftcms/feature/dev-993-allow-overrid…
Browse files Browse the repository at this point in the history
…ing-the-db-server-version

Allow overriding the db server version
  • Loading branch information
brandonkelly authored Sep 8, 2022
2 parents 851bb0f + da5bd7b commit 8d21429
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
- Added the `|integer` Twig filter. ([#11792](https://github.com/craftcms/cms/pull/11792))
- Added the `|string` Twig filter. ([#11792](https://github.com/craftcms/cms/pull/11792))
- Added support for the `CRAFT_DOTENV_PATH` PHP constant. ([#11894](https://github.com/craftcms/cms/discussions/11894))
- Added the `version` database connection setting. ([#11900](https://github.com/craftcms/cms/pull/11900))
- Added `craft\db\ActiveQuery::collect()`. ([#11842](https://github.com/craftcms/cms/pull/11842))
- Added `craft\db\SchemaTrait`.
- Added `craft\elements\actions\Restore::$restorableElementsOnly`.
- Added `craft\enums\DateRangeType`.
- Added `craft\events\AuthorizationCheckEvent::$element`.
Expand Down
38 changes: 38 additions & 0 deletions src/config/DbConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ class DbConfig extends BaseConfig
*/
public ?string $dsn = null;

/**
* @var string|null The database server version in use.
*
* This should only be set if `PDO::ATTR_SERVER_VERSION` is returning the incorrect version.
*
* ::: code
* ```php Static Config
* 'version' => '8.0.31',
* ```
* ```shell Environment Override
* CRAFT_DB_VERSION=8.0.31
* ```
* :::
*
* @since 4.3.0
*/
public ?string $version = null;

/**
* @var string The database password to connect with.
*
Expand Down Expand Up @@ -458,6 +476,26 @@ public function dsn(?string $value): self
return $this;
}

/**
* The database server version in use.
*
* This should only be set if `PDO::ATTR_SERVER_VERSION` is returning the incorrect version.
*
* ```php
* ->version('8.0.31')
* ```
*
* @param string $value
* @return self
* @see $version
* @since 4.3.0
*/
public function version(string $value): self
{
$this->version = $value;
return $this;
}

/**
* The database password to connect with.
*
Expand Down
48 changes: 48 additions & 0 deletions src/db/SchemaTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\db;

use craft\db\mysql\Schema as MysqlSchema;
use craft\db\pgsql\Schema as PgsqlSchema;

/**
* SchemaTrait
*
* @mixin MysqlSchema
* @mixin PgsqlSchema
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.3.0
*/
trait SchemaTrait
{
/**
* @see getServerVersion()
* @see setServerVersion()
*/
private ?string $version = null;

/**
* Returns the database server version.
*
* @return string
*/
public function getServerVersion(): string
{
return $this->version ?? parent::getServerVersion();
}

/**
* Sets the database server version.
*
* @param string $version
*/
public function setServerVersion(string $version): void
{
$this->version = $version;
}
}
3 changes: 3 additions & 0 deletions src/db/mysql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Composer\Util\Platform;
use Craft;
use craft\db\Connection;
use craft\db\SchemaTrait;
use craft\db\TableSchema;
use craft\helpers\App;
use craft\helpers\Db;
Expand All @@ -30,6 +31,8 @@
*/
class Schema extends \yii\db\mysql\Schema
{
use SchemaTrait;

public const TYPE_TINYTEXT = 'tinytext';
public const TYPE_MEDIUMTEXT = 'mediumtext';
public const TYPE_LONGTEXT = 'longtext';
Expand Down
3 changes: 3 additions & 0 deletions src/db/pgsql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Composer\Util\Platform;
use Craft;
use craft\db\Connection;
use craft\db\SchemaTrait;
use craft\db\TableSchema;
use yii\db\Exception;

Expand All @@ -22,6 +23,8 @@
*/
class Schema extends \yii\db\pgsql\Schema
{
use SchemaTrait;

/**
* @var int The maximum length that objects' names can be.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,10 @@ public static function dbConfig(?DbConfig $dbConfig = null): array
];
}

if ($dbConfig->version) {
$schemaConfig['serverVersion'] = $dbConfig->version;
}

$config = [
'class' => Connection::class,
'driverName' => $driver,
Expand Down

0 comments on commit 8d21429

Please sign in to comment.