Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.0.1 under development

- Chg #88: Bump supported version of `yiisoft/db` to `^2.0` (@batyrmastyr)
- Chg #91: Change supported PHP versions to `8.1 - 8.4` (@batyrmastyr)

1.0.0 May 09, 2023
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"ext-pdo": "*",
"psr/log": "^2.0|^3.0",
"psr/simple-cache": "^2.0|^3.0",
"yiisoft/db": "^1.0"
"yiisoft/db": "^2.0"
},
"suggest": {
"yiisoft/log": "^2.0"
Expand All @@ -47,7 +47,8 @@
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.22 || ^6.13.1",
"yiisoft/cache": "^3.0",
"yiisoft/log": "^2.0"
"yiisoft/log": "^2.0",
"yiisoft/dummy-provider": "^1.0"
},
"provide": {
"psr/simple-cache-implementation": "1.0.0"
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
ensureOverrideAttribute="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
6 changes: 3 additions & 3 deletions src/DbCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Psr\SimpleCache\InvalidArgumentException;
use Throwable;
use Traversable;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Expression\Value\Param;
use Yiisoft\Db\Query\Query;

use function array_fill_keys;
Expand Down Expand Up @@ -174,7 +174,7 @@ public function setMultiple(iterable $values, DateInterval|int|string|null $ttl
if (!empty($rows) && !$this->isExpiredTtl($ttl)) {
$this->db
->createCommand()
->batchInsert($this->table, ['id', 'expire', 'data'], $rows)
->insertBatch($this->table, $rows, ['id', 'expire', 'data'])
->execute();
}

Expand Down Expand Up @@ -228,7 +228,7 @@ public function setLoggerMessageUpdate(string $value): void
* Gets the cache data from the database.
*
* @param array|string $id One or more IDs for deleting data.
* @param array $fields Selectable fields.
* @param string[] $fields Selectable fields.
* @param string $method Method of the returned data ("all", "scalar", "exists").
*
* @return mixed The cache data.
Expand Down
22 changes: 9 additions & 13 deletions src/DbSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Schema\SchemaInterface;

/**
* Manages the cache table schema in the database.
Expand All @@ -32,19 +31,17 @@
*/
public function ensureTable(string $table = '{{%yii_cache}}'): void
{
$schema = $this->db->getSchema();
$tableRawName = $schema->getRawTableName($table);

if ($this->hasTable($table)) {
$tableRawName = $this->db->getQuoter()->getRawTableName($table);
if ($this->hasTable($tableRawName)) {
return;
}

$this->db->createCommand()->createTable(
$table,
[
'id' => $schema->createColumn(SchemaInterface::TYPE_STRING, 128)->notNull(),
'data' => $schema->createColumn(SchemaInterface::TYPE_BINARY),
'expire' => $schema->createColumn(SchemaInterface::TYPE_INTEGER),
'id' => $this->db->getColumnBuilderClass()::string(128)->notNull(),
'data' => $this->db->getColumnBuilderClass()::binary(),
'expire' => $this->db->getColumnBuilderClass()::integer(),
"CONSTRAINT [[PK_$tableRawName]] PRIMARY KEY ([[id]])",
],
)->execute();
Expand All @@ -61,10 +58,9 @@
*/
public function ensureNoTable(string $table = '{{%yii_cache}}'): void
{
$tableRawName = $this->db->getSchema()->getRawTableName($table);

if ($this->hasTable($table)) {
$this->db->createCommand()->dropTable($tableRawName)->execute();
$rawTableName = $this->db->getQuoter()->getRawTableName($table);
if ($this->hasTable($rawTableName)) {
$this->db->createCommand()->dropTable($rawTableName)->execute();
}
}

Expand All @@ -77,6 +73,6 @@
*/
private function hasTable(string $table): bool
{
return $this->db->getTableSchema($table, true) !== null;
return $this->db->getSchema()->hasTable($table, refresh: true);

Check warning on line 76 in src/DbSchemaManager.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": @@ @@ */ private function hasTable(string $table): bool { - return $this->db->getSchema()->hasTable($table, refresh: true); + return $this->db->getSchema()->hasTable($table, refresh: false); } }
}
}
23 changes: 12 additions & 11 deletions tests/Common/AbstractDbSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
use Yiisoft\Cache\Db\DbCache;
use Yiisoft\Cache\Db\DbSchemaManager;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Constraint\IndexConstraint;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Schema\SchemaInterface;

/**
* @group Mssql
Expand Down Expand Up @@ -70,6 +69,8 @@ public function testEnsureTableAndDropTable(string $table): void
*/
public function testEnsureTableExist(string $table): void
{
$this->assertNull($this->db->getTableSchema($table, true));

$this->dbSchemaManager->ensureTable($table);

$this->assertNotNull($this->db->getTableSchema($table, true));
Expand Down Expand Up @@ -99,12 +100,12 @@ public function testVerifyTableIndexes(string $table): void

$schema = $this->db->getSchema();

/** @psalm-var IndexConstraint[] $indexes */
$indexes = $schema->getTableIndexes($dbCache->getTable(), true);
$index = $schema->getTablePrimaryKey($dbCache->getTable(), true);

$this->assertSame(['id'], $indexes[0]->getColumnNames());
$this->assertTrue($indexes[0]->isUnique());
$this->assertTrue($indexes[0]->isPrimary());
$this->assertNotNull($index);
$this->assertSame(['id'], $index->columnNames);
$this->assertTrue($index->isUnique);
$this->assertTrue($index->isPrimaryKey);

$this->dbSchemaManager->ensureNoTable($dbCache->getTable());

Expand All @@ -126,17 +127,17 @@ public function testVerifyTableStructure(string $table): void
$this->dbSchemaManager->ensureTable($dbCache->getTable());

$tableSchema = $this->db->getTableSchema($dbCache->getTable());
$tableRawName = $this->db->getSchema()->getRawTableName($dbCache->getTable());
$tableRawName = $this->db->getQuoter()->getRawTableName($dbCache->getTable());

$this->assertNotNull($tableSchema);

$this->assertSame($tableRawName, $tableSchema->getName());
$this->assertSame(['id'], $tableSchema->getPrimaryKey());
$this->assertSame(['id', 'data', 'expire'], $tableSchema->getColumnNames());
$this->assertSame(SchemaInterface::TYPE_STRING, $tableSchema->getColumn('id')?->getType());
$this->assertSame(ColumnType::STRING, $tableSchema->getColumn('id')?->getType());
$this->assertSame(128, $tableSchema->getColumn('id')?->getSize());
$this->assertSame(SchemaInterface::TYPE_BINARY, $tableSchema->getColumn('data')?->getType());
$this->assertSame(SchemaInterface::TYPE_INTEGER, $tableSchema->getColumn('expire')?->getType());
$this->assertSame(ColumnType::BINARY, $tableSchema->getColumn('data')?->getType());
$this->assertSame(ColumnType::INTEGER, $tableSchema->getColumn('expire')?->getType());

$this->dbSchemaManager->ensureNoTable($dbCache->getTable());

Expand Down
21 changes: 10 additions & 11 deletions tests/Common/AbstractSQLDumpFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
use Throwable;
use Yiisoft\Cache\Db\DbCache;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Constraint\IndexConstraint;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Schema\SchemaInterface;

/**
* @group Mssql
Expand Down Expand Up @@ -71,12 +70,12 @@ public function testVerifyTableIndexes(): void

$schema = $this->db->getSchema();

/** @psalm-var IndexConstraint[] $indexes */
$indexes = $schema->getTableIndexes($dbCache->getTable(), true);
$index = $schema->getTablePrimaryKey($dbCache->getTable(), true);

$this->assertSame(['id'], $indexes[0]->getColumnNames());
$this->assertTrue($indexes[0]->isUnique());
$this->assertTrue($indexes[0]->isPrimary());
$this->assertNotNull($index);
$this->assertSame(['id'], $index->columnNames);
$this->assertTrue($index->isPrimaryKey);
$this->assertTrue($index->isUnique);

$this->loadFromSQLDumpFile(dirname(__DIR__, 2) . "/sql/$this->driverName-down.sql");

Expand All @@ -96,17 +95,17 @@ public function testVerifyTableStructure(): void
$this->loadFromSQLDumpFile(dirname(__DIR__, 2) . "/sql/$this->driverName-up.sql");

$tableSchema = $this->db->getTableSchema($dbCache->getTable());
$tableRawName = $this->db->getSchema()->getRawTableName($dbCache->getTable());
$tableRawName = $this->db->getQuoter()->getRawTableName($dbCache->getTable());

$this->assertNotNull($tableSchema);

$this->assertSame($tableRawName, $tableSchema->getName());
$this->assertSame(['id'], $tableSchema->getPrimaryKey());
$this->assertSame(['id', 'data', 'expire'], $tableSchema->getColumnNames());
$this->assertSame(SchemaInterface::TYPE_STRING, $tableSchema->getColumn('id')?->getType());
$this->assertSame(ColumnType::STRING, $tableSchema->getColumn('id')?->getType());
$this->assertSame(128, $tableSchema->getColumn('id')?->getSize());
$this->assertSame(SchemaInterface::TYPE_BINARY, $tableSchema->getColumn('data')?->getType());
$this->assertSame(SchemaInterface::TYPE_INTEGER, $tableSchema->getColumn('expire')?->getType());
$this->assertSame(ColumnType::BINARY, $tableSchema->getColumn('data')?->getType());
$this->assertSame(ColumnType::INTEGER, $tableSchema->getColumn('expire')?->getType());

$this->loadFromSQLDumpFile(dirname(__DIR__, 2) . "/sql/$this->driverName-down.sql");

Expand Down
2 changes: 1 addition & 1 deletion tests/Support/MssqlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class MssqlFactory extends ConnectionFactory
public function createConnection(): ConnectionInterface
{
$pdoDriver = new Driver(
(new Dsn('sqlsrv', 'localhost', 'yiitest;TrustServerCertificate=1'))->asString(),
new Dsn('sqlsrv', 'localhost', 'yiitest;TrustServerCertificate=1'),
'SA',
'YourStrong!Passw0rd',
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/MysqlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class MysqlFactory extends ConnectionFactory
public function createConnection(): ConnectionInterface
{
$pdoDriver = new Driver(
(new Dsn('mysql', '127.0.0.1', 'yiitest', '3306', ['charset' => 'utf8mb4']))->asString(),
new Dsn('mysql', '127.0.0.1', 'yiitest', '3306', ['charset' => 'utf8mb4']),
'root',
'',
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/OracleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class OracleFactory extends ConnectionFactory
public function createConnection(): ConnectionInterface
{
$pdoDriver = new Driver(
(new Dsn('oci', 'localhost', 'XE', '1521', ['charset' => 'AL32UTF8']))->asString(),
new Dsn('oci', 'localhost', 'XE', '1521', ['charset' => 'AL32UTF8']),
'system',
'root'
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/PgsqlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class PgsqlFactory extends ConnectionFactory
public function createConnection(): ConnectionInterface
{
$pdoDriver = new Driver(
(new Dsn('pgsql', '127.0.0.1', 'yiitest', '5432'))->asString(),
new Dsn('pgsql', '127.0.0.1', 'yiitest', '5432'),
'root',
'root',
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/SqliteFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class SqliteFactory extends ConnectionFactory
{
public function createConnection(): ConnectionInterface
{
$pdoDriver = new Driver((new Dsn('sqlite', __DIR__ . '/runtime/yiitest.sq3'))->asString());
$pdoDriver = new Driver(new Dsn('sqlite', __DIR__ . '/runtime/yiitest.sq3'));

return new Connection($pdoDriver, $this->createSchemaCache());
}
Expand Down