Skip to content

Commit

Permalink
Implement hasTable() method in Connection::class. (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Sep 6, 2024
1 parent 26e124a commit 15d2ebb
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Yii Core version 2 Change Log
- Enh #84: Remove `Oracle-11c` service image versions in build workflow (@terabytesoftw)
- Enh #86: Remove unnecesary `resolveTableNames()` and refactor `resolveTableName()` in `Schema::class` in `MSSQL`, `MYSQL`, `OCI`, `PGSQL` (@terabytesoftw)
- Enh #87: Refactor `TableSchema::class` in `MSSQL` (@terabytesoftw)
- Enh #88: Implement `hasTable()` method in `Connection::class` (@terabytesoftw)

Yii Framework 2 Change Log
==========================
Expand Down
12 changes: 12 additions & 0 deletions src/db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,18 @@ public function getTableSchema(string $name, bool $refresh = false): TableSchema
return $this->getSchema()->getTableSchema($name, $refresh);
}

/**
* Checks if a table exists.
*
* @param string $name table name.
*
* @return bool whether a table exists in the database.
*/
public function hasTable(string $name): bool
{
return $this->getSchema()->getTableSchema($name, true) !== null;
}

/**
* Returns the ID of the last inserted row or sequence value.
*
Expand Down
43 changes: 43 additions & 0 deletions tests/framework/db/connection/AbstractConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace yiiunit\framework\db\connection;

use yii\db\Connection;
use yii\db\Schema;

abstract class AbstractConnection extends \yiiunit\TestCase
{
protected Connection|null $db = null;

protected function tearDown(): void
{
$this->db->close();
$this->db = null;

parent::tearDown();
}

public function testHasTable(): void
{
$tableName = 'T_table';
$this->assertFalse($this->db->hasTable($tableName));

$result = $this->db->createCommand()->createTable(
$tableName,
[
'id' => Schema::TYPE_PK,
'name' => Schema::TYPE_STRING,
],
)->execute();

$this->assertSame(0, $result);

$this->assertTrue($this->db->hasTable($tableName));

$result = $this->db->createCommand()->dropTable($tableName)->execute();

$this->assertSame(0, $result);
}
}
22 changes: 22 additions & 0 deletions tests/framework/db/mssql/connection/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace yiiunit\framework\db\mssql\command;

use yiiunit\support\MssqlConnection;

/**
* @group db
* @group mssql
* @group connection
*/
final class ConnectionTest extends \yiiunit\framework\db\connection\AbstractConnection
{
protected function setUp(): void
{
parent::setUp();

$this->db = MssqlConnection::getConnection();
}
}
22 changes: 22 additions & 0 deletions tests/framework/db/mysql/connection/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace yiiunit\framework\db\mysql\command;

use yiiunit\support\MysqlConnection;

/**
* @group db
* @group mysql
* @group connection
*/
final class ConnectionTest extends \yiiunit\framework\db\connection\AbstractConnection
{
protected function setUp(): void
{
parent::setUp();

$this->db = MysqlConnection::getConnection();
}
}
22 changes: 22 additions & 0 deletions tests/framework/db/oci/connection/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace yiiunit\framework\db\oci\command;

use yiiunit\support\OciConnection;

/**
* @group db
* @group oci
* @group connection
*/
final class ConnectionTest extends \yiiunit\framework\db\connection\AbstractConnection
{
protected function setUp(): void
{
parent::setUp();

$this->db = OciConnection::getConnection();
}
}
22 changes: 22 additions & 0 deletions tests/framework/db/pgsql/connection/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace yiiunit\framework\db\pgsql\command;

use yiiunit\support\PgsqlConnection;

/**
* @group db
* @group pgsql
* @group connection
*/
final class ConnectionTest extends \yiiunit\framework\db\connection\AbstractConnection
{
protected function setUp(): void
{
parent::setUp();

$this->db = PgsqlConnection::getConnection();
}
}
22 changes: 22 additions & 0 deletions tests/framework/db/sqlite/connection/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace yiiunit\framework\db\sqlite\command;

use yiiunit\support\SqliteConnection;

/**
* @group db
* @group sqlite
* @group connection
*/
final class ConnectionTest extends \yiiunit\framework\db\connection\AbstractConnection
{
protected function setUp(): void
{
parent::setUp();

$this->db = SqliteConnection::getConnection();
}
}

0 comments on commit 15d2ebb

Please sign in to comment.