Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update according to changes in db #368

Merged
merged 3 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,11 @@ public function createTransaction(): TransactionInterface

public function getQueryBuilder(): QueryBuilderInterface
{
if ($this->queryBuilder === null) {
$this->queryBuilder = new QueryBuilder(
$this->getQuoter(),
$this->getSchema(),
);
}

return $this->queryBuilder;
return $this->queryBuilder ??= new QueryBuilder(
$this->getQuoter(),
$this->getSchema(),
$this->getServerInfo(),
);
}

public function getQuoter(): QuoterInterface
Expand Down
22 changes: 12 additions & 10 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Mysql;

use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Mysql\Column\ColumnDefinitionBuilder;
Expand Down Expand Up @@ -46,15 +47,16 @@ final class QueryBuilder extends AbstractQueryBuilder
PseudoType::UUID_PK => 'binary(16) PRIMARY KEY',
];

public function __construct(
QuoterInterface $quoter,
SchemaInterface $schema,
) {
$ddlBuilder = new DDLQueryBuilder($this, $quoter, $schema);
$dmlBuilder = new DMLQueryBuilder($this, $quoter, $schema);
$dqlBuilder = new DQLQueryBuilder($this, $quoter);
$columnDefinitionBuilder = new ColumnDefinitionBuilder($this);

parent::__construct($quoter, $schema, $ddlBuilder, $dmlBuilder, $dqlBuilder, $columnDefinitionBuilder);
public function __construct(QuoterInterface $quoter, SchemaInterface $schema, ServerInfoInterface $serverInfo)
{
parent::__construct(
$quoter,
$schema,
$serverInfo,
new DDLQueryBuilder($this, $quoter, $schema),
new DMLQueryBuilder($this, $quoter, $schema),
new DQLQueryBuilder($this, $quoter),
new ColumnDefinitionBuilder($this),
);
}
}
2 changes: 1 addition & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected function findColumns(TableSchemaInterface $table): bool
}

$jsonColumns = $this->getJsonColumns($table);
$isMariaDb = str_contains($this->db->getServerVersion(), 'MariaDB');
$isMariaDb = str_contains($this->db->getServerInfo()->getVersion(), 'MariaDB');

foreach ($columns as $info) {
$info = array_change_key_case($info);
Expand Down
4 changes: 2 additions & 2 deletions tests/JsonCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testInsertAndSelect(): void

$command = $db->createCommand();
$command->insert('storage', ['data' => ['a' => 1, 'b' => 2]])->execute();
$rowExpected = match (str_contains($db->getServerVersion(), 'MariaDB')) {
$rowExpected = match (str_contains($db->getServerInfo()->getVersion(), 'MariaDB')) {
true => '{"a":1,"b":2}',
default => '{"a": 1, "b": 2}',
};
Expand All @@ -65,7 +65,7 @@ public function testInsertJsonExpressionAndSelect(): void

$command = $db->createCommand();
$command->insert('storage', ['data' => new JsonExpression(['a' => 1, 'b' => 2])])->execute();
$rowExpected = match (str_contains($db->getServerVersion(), 'MariaDB')) {
$rowExpected = match (str_contains($db->getServerInfo()->getVersion(), 'MariaDB')) {
true => '{"a":1,"b":2}',
default => '{"a": 1, "b": 2}',
};
Expand Down
25 changes: 15 additions & 10 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ public function testAddcheck(): void
public function testAddCommentOnColumn(): void
{
$db = $this->getConnection(true);
$serverVersion = $db->getServerInfo()->getVersion();

$qb = $db->getQueryBuilder();
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int NOT NULL AUTO_INCREMENT COMMENT 'Primary key.'
SQL;

if (!str_contains($db->getServerVersion(), 'MariaDB') && version_compare($db->getServerVersion(), '8', '<')) {
if (!str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '8', '<')) {
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key.'
SQL;
} elseif (str_contains($db->getServerVersion(), 'MariaDB')) {
} elseif (str_contains($serverVersion, 'MariaDB')) {
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key.'
SQL;
Expand Down Expand Up @@ -344,17 +345,18 @@ public function testDefaultValues(): void
public function testDropCommentFromColumn(): void
{
$db = $this->getConnection(true);
$serverVersion = $db->getServerInfo()->getVersion();

$qb = $db->getQueryBuilder();
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int NOT NULL AUTO_INCREMENT COMMENT ''
SQL;

if (!str_contains($db->getServerVersion(), 'MariaDB') && version_compare($db->getServerVersion(), '8', '<')) {
if (!str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '8', '<')) {
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ''
SQL;
} elseif (str_contains($db->getServerVersion(), 'MariaDB')) {
} elseif (str_contains($serverVersion, 'MariaDB')) {
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ''
SQL;
Expand Down Expand Up @@ -666,10 +668,11 @@ public function testSelectScalar(array|bool|float|int|string $columns, string $e
public function testJsonOverlapsConditionBuilder(): void
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (str_contains($db->getServerVersion(), 'MariaDB') && version_compare($db->getServerVersion(), '10.9', '<')) {
if (str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '10.9', '<')) {
self::markTestSkipped('MariaDB < 10.9 does not support JSON_OVERLAPS() function.');
} elseif (version_compare($db->getServerVersion(), '8', '<')) {
} elseif (version_compare($serverVersion, '8', '<')) {
self::markTestSkipped('MySQL < 8 does not support JSON_OVERLAPS() function.');
}

Expand All @@ -695,10 +698,11 @@ public function testJsonOverlapsConditionBuilder(): void
public function testJsonOverlapsCondition(iterable|ExpressionInterface $values, int $expectedCount): void
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (str_contains($db->getServerVersion(), 'MariaDB') && version_compare($db->getServerVersion(), '10.9', '<')) {
if (str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '10.9', '<')) {
self::markTestSkipped('MariaDB < 10.9 does not support JSON_OVERLAPS() function.');
} elseif (version_compare($db->getServerVersion(), '8', '<')) {
} elseif (version_compare($serverVersion, '8', '<')) {
self::markTestSkipped('MySQL < 8 does not support JSON_OVERLAPS() function.');
}

Expand All @@ -716,10 +720,11 @@ public function testJsonOverlapsCondition(iterable|ExpressionInterface $values,
public function testJsonOverlapsConditionOperator(iterable|ExpressionInterface $values, int $expectedCount): void
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (str_contains($db->getServerVersion(), 'MariaDB') && version_compare($db->getServerVersion(), '10.9', '<')) {
if (str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '10.9', '<')) {
self::markTestSkipped('MariaDB < 10.9 does not support JSON_OVERLAPS() function.');
} elseif (version_compare($db->getServerVersion(), '8', '<')) {
} elseif (version_compare($serverVersion, '8', '<')) {
self::markTestSkipped('MySQL < 8 does not support JSON_OVERLAPS() function.');
}

Expand Down
10 changes: 6 additions & 4 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ public function testLimitOffsetWithExpression(): void
public function testWithQuery()
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (
!str_contains($db->getServerVersion(), 'MariaDB')
&& version_compare($db->getServerVersion(), '8.0.0', '<')
!str_contains($serverVersion, 'MariaDB')
&& version_compare($serverVersion, '8.0.0', '<')
) {
self::markTestSkipped('CTE not supported in MySQL versions below 8.0.0');
}
Expand All @@ -99,10 +100,11 @@ public function testWithQuery()
public function testWithQueryRecursive()
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (
!str_contains($db->getServerVersion(), 'MariaDB')
&& version_compare($db->getServerVersion(), '8.0.0', '<')
!str_contains($serverVersion, 'MariaDB')
&& version_compare($serverVersion, '8.0.0', '<')
) {
self::markTestSkipped('CTE not supported in MySQL versions below 8.0.0');
}
Expand Down
12 changes: 7 additions & 5 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ final class SchemaTest extends CommonSchemaTest
public function testColumnSchema(array $columns, string $tableName): void
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (
version_compare($db->getServerVersion(), '8.0.17', '>') &&
!str_contains($db->getServerVersion(), 'MariaDB')
version_compare($serverVersion, '8.0.17', '>') &&
!str_contains($serverVersion, 'MariaDB')
) {
if ($tableName === 'type') {
$columns['int_col']['size'] = null;
Expand Down Expand Up @@ -95,10 +96,11 @@ public function testDefaultValueDatetimeColumn(): void
{
$tableName = '{{%datetime_test}}';
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

$oldMySQL = !(
version_compare($db->getServerVersion(), '8.0.0', '>') &&
!str_contains($db->getServerVersion(), 'MariaDB')
version_compare($serverVersion, '8.0.0', '>') &&
!str_contains($serverVersion, 'MariaDB')
);

$columnsData = [
Expand Down Expand Up @@ -280,7 +282,7 @@ public function testGetViewNames(): void
$schema = $db->getSchema();
$views = $schema->getViewNames();

$viewExpected = match (str_contains($db->getServerVersion(), 'MariaDB')) {
$viewExpected = match (str_contains($db->getServerInfo()->getVersion(), 'MariaDB')) {
true => ['animal_view', 'user'],
default => ['animal_view'],
};
Expand Down
Loading