Skip to content

Commit

Permalink
Merge branch 'refs/heads/master' into use-new-column-definition-builder
Browse files Browse the repository at this point in the history
# Conflicts:
#	tests/Common/CommonColumnSchemaBuilderTest.php
  • Loading branch information
Tigrov committed Dec 8, 2024
2 parents a56fb1f + 4771a2f commit b60e908
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 65 deletions.
25 changes: 7 additions & 18 deletions .github/workflows/active-record.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,14 @@ name: active-record

jobs:
tests:
name: PHP ${{ matrix.php }}-active-record-${{ matrix.os }}
name: ActiveRecord

env:
COMPOSER_ROOT_VERSION: dev-master
EXTENSIONS: pdo, pdo_mysql, pdo_oci, pdo_pgsql, pdo_sqlite, pdo_sqlsrv-5.12
PHP_VERSION: 8.3
EXTENSIONS: pdo, pdo_mysql, pdo_oci, pdo_pgsql, pdo_sqlite, pdo_sqlsrv

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- ubuntu-latest

php:
- 8.1
- 8.2
- 8.3
runs-on: ubuntu-latest

services:
mysql:
Expand Down Expand Up @@ -83,19 +74,18 @@ jobs:
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
- name: Checkout.
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Create MS SQL Database.
run: docker exec -i mssql /opt/mssql-tools18/bin/sqlcmd -C -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'CREATE DATABASE yiitest'

- name: Install PHP with extensions.
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
php-version: ${{ env.PHP_VERSION }}
extensions: ${{ env.EXTENSIONS }}
ini-values: date.timezone='UTC'
coverage: pcov
tools: composer:v2, pecl

- name: Update composer.
run: composer self-update
Expand Down Expand Up @@ -170,8 +160,7 @@ jobs:
run: vendor/bin/phpunit --testsuite ActiveRecord --coverage-clover=coverage.xml --colors=always --display-warnings --display-deprecations

- name: Upload coverage to Codecov.
if: matrix.php == '8.3'
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
20 changes: 5 additions & 15 deletions .github/workflows/db-migration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,14 @@ name: db-migration

jobs:
tests:
name: PHP ${{ matrix.php }}-db-migration-${{ matrix.os }}
name: DbMigration

env:
COMPOSER_ROOT_VERSION: dev-master
PHP_VERSION: 8.3
EXTENSIONS: pdo, pdo_mysql, pdo_oci, pdo_pgsql, pdo_sqlite, pdo_sqlsrv

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- ubuntu-latest

php:
- 8.1
- 8.2
- 8.3
runs-on: ubuntu-latest

services:
mysql:
Expand Down Expand Up @@ -91,7 +82,7 @@ jobs:
- name: Install PHP with extensions.
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
php-version: ${{ env.PHP_VERSION }}
extensions: ${{ env.EXTENSIONS }}
ini-values: date.timezone='UTC'
coverage: pcov
Expand Down Expand Up @@ -166,8 +157,7 @@ jobs:
run: vendor/bin/phpunit --testsuite=DbMigration --coverage-clover=coverage.xml --colors=always --display-warnings --display-deprecations

- name: Upload coverage to Codecov.
if: matrix.php == '8.3'
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- New #899: Add `ColumnSchemaInterface::hasDefaultValue()` and `ColumnSchemaInterface::null()` methods (@Tigrov)
- New #902: Add `QueryBuilderInterface::prepareParam()` and `QueryBuilderInterface::prepareValue()` methods (@Tigrov)
- Enh #902: Refactor `Quoter::quoteValue()` method (@Tigrov)
- New #906: Add `ServerInfoInterface` and its implementation (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
3 changes: 3 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace
- `QueryBuilderInterface::buildColumnDefinition()` - builds column definition for `CREATE TABLE` statement;
- `QueryBuilderInterface::prepareParam()` - converts a `ParamInterface` object to its SQL representation;
- `QueryBuilderInterface::prepareValue()` - converts a value to its SQL representation;
- `QueryBuilderInterface::getServerInfo()` - returns `ServerInfoInterface` instance which provides server information;
- `ConnectionInterface::getServerInfo()` - returns `ServerInfoInterface` instance which provides server information;

### Remove methods

Expand All @@ -123,6 +125,7 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace
- `Quoter::unquoteParts()`
- `AbstractPdoCommand::logQuery()`
- `ColumnSchemaInterface::phpType()`
- `ConnectionInterface::getServerVersion()`

### Remove deprecated parameters

Expand Down
9 changes: 2 additions & 7 deletions src/Connection/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,9 @@ public function getQuoter(): QuoterInterface;
public function getSchema(): SchemaInterface;

/**
* Returns a server version as a string comparable by {@see \version_compare()}.
*
* @throws Exception
* @throws InvalidConfigException
*
* @return string The server version as a string.
* Returns {@see ServerInfoInterface} instance that provides information about the database server.
*/
public function getServerVersion(): string;
public function getServerInfo(): ServerInfoInterface;

/**
* Return table prefix for current DB connection.
Expand Down
16 changes: 16 additions & 0 deletions src/Connection/ServerInfoInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Connection;

/**
* Should be implemented by a class that provides information about the database server.
*/
interface ServerInfoInterface
{
/**
* Returns a server version as a string comparable by {@see version_compare()}.
*/
public function getVersion(): string;
}
5 changes: 3 additions & 2 deletions src/Debug/ConnectionInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Yiisoft\Db\Command\CommandInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\Query\BatchQueryResultInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
Expand Down Expand Up @@ -82,9 +83,9 @@ public function getSchema(): SchemaInterface
return $this->connection->getSchema();
}

public function getServerVersion(): string
public function getServerInfo(): ServerInfoInterface
{
return $this->connection->getServerVersion();
return $this->connection->getServerInfo();
}

public function getTablePrefix(): string
Expand Down
13 changes: 4 additions & 9 deletions src/Driver/Pdo/AbstractPdoConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Throwable;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Connection\AbstractConnection;
use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidCallException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand Down Expand Up @@ -43,7 +44,7 @@ abstract class AbstractPdoConnection extends AbstractConnection implements PdoCo
use ProfilerAwareTrait;

protected PDO|null $pdo = null;
protected string $serverVersion = '';
protected ServerInfoInterface|null $serverInfo = null;
protected bool|null $emulatePrepare = null;
protected QueryBuilderInterface|null $queryBuilder = null;
protected QuoterInterface|null $quoter = null;
Expand Down Expand Up @@ -169,15 +170,9 @@ public function getDriverName(): string
return $this->driver->getDriverName();
}

public function getServerVersion(): string
public function getServerInfo(): ServerInfoInterface
{
if ($this->serverVersion === '') {
/** @psalm-var mixed $version */
$version = $this->getActivePDO()->getAttribute(PDO::ATTR_SERVER_VERSION);
$this->serverVersion = is_string($version) ? $version : 'Version could not be determined.';
}

return $this->serverVersion;
return $this->serverInfo ??= new PdoServerInfo($this);
}

public function isActive(): bool
Expand Down
27 changes: 27 additions & 0 deletions src/Driver/Pdo/PdoServerInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Driver\Pdo;

use PDO;
use Yiisoft\Db\Connection\ServerInfoInterface;

class PdoServerInfo implements ServerInfoInterface
{
protected string|null $version = null;

public function __construct(protected PdoConnectionInterface $db)
{
}

public function getVersion(): string
{
if ($this->version === null) {
/** @var string */
$this->version = $this->db->getActivePDO()?->getAttribute(PDO::ATTR_SERVER_VERSION) ?? '';
}

return $this->version;
}
}
7 changes: 7 additions & 0 deletions src/QueryBuilder/AbstractQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Yiisoft\Db\Command\DataType;
use Yiisoft\Db\Command\ParamInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\Constant\GettypeResult;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Expression\Expression;
Expand Down Expand Up @@ -67,6 +68,7 @@ abstract class AbstractQueryBuilder implements QueryBuilderInterface
public function __construct(
private QuoterInterface $quoter,
private SchemaInterface $schema,
private ServerInfoInterface $serverInfo,
private AbstractDDLQueryBuilder $ddlBuilder,
private AbstractDMLQueryBuilder $dmlBuilder,
private AbstractDQLQueryBuilder $dqlBuilder,
Expand Down Expand Up @@ -395,6 +397,11 @@ public function getExpressionBuilder(ExpressionInterface $expression): object
return $this->dqlBuilder->getExpressionBuilder($expression);
}

public function getServerInfo(): ServerInfoInterface
{
return $this->serverInfo;
}

public function insert(string $table, QueryInterface|array $columns, array &$params = []): string
{
return $this->dmlBuilder->insert($table, $columns, $params);
Expand Down
6 changes: 6 additions & 0 deletions src/QueryBuilder/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Yiisoft\Db\Command\ParamInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionInterface;
Expand Down Expand Up @@ -114,6 +115,11 @@ public function getColumnDefinitionBuilder(): ColumnDefinitionBuilderInterface;
*/
public function getExpressionBuilder(ExpressionInterface $expression): object;

/**
* Returns {@see ServerInfoInterface} instance that provides information about the database server.
*/
public function getServerInfo(): ServerInfoInterface;

/**
* @return QuoterInterface The quoter instance.
*/
Expand Down
5 changes: 3 additions & 2 deletions tests/AbstractPdoConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Yiisoft\Db\Driver\Pdo\PdoDriverInterface;
use Yiisoft\Db\Driver\Pdo\PdoServerInfo;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Tests\Support\TestTrait;
Expand All @@ -35,11 +36,11 @@ public function testGetDriver(): void
$this->assertInstanceOf(PdoDriverInterface::class, $driver);
}

public function testGetServerVersion(): void
public function testGetServerInfo(): void
{
$db = $this->getConnection();

$this->assertIsString($db->getServerVersion());
$this->assertInstanceOf(PdoServerInfo::class, $db->getServerInfo());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Common/CommonPdoConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public function testTransactionRollbackTransactionOnLevel(): void
'getQueryBuilder',
'getQuoter',
'getSchema',
'getServerVersion',
'getServerInfo',
'isActive',
'open',
'quoteValue',
Expand Down
21 changes: 21 additions & 0 deletions tests/Db/Driver/PDO/PdoServerInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Driver\PDO;

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Tests\Support\TestTrait;

class PdoServerInfoTest extends TestCase
{
use TestTrait;

public function testGetVersion(): void
{
$db = $this->getConnection();
$serverInfo = $db->getServerInfo();

$this->assertIsString($serverInfo->getVersion());
}
}
Loading

0 comments on commit b60e908

Please sign in to comment.