Skip to content

Commit

Permalink
Add RuntimeException, if table exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed May 3, 2023
1 parent d5f3e87 commit 143ad05
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
23 changes: 14 additions & 9 deletions src/DbHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Translator\Message\Db;

use RuntimeException;
use Throwable;
use Yiisoft\Db\Command\CommandInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
Expand Down Expand Up @@ -31,11 +32,10 @@ public static function ensureTables(
$tableRawNameSourceMessage = $schema->getRawTableName($tableSourceMessage);
$tableRawNameMessage = $schema->getRawTableName($tableMessage);

if (
$schema->getTableSchema($tableSourceMessage, true) !== null &&
$schema->getTableSchema($tableMessage, true) !== null
) {
return;
if (self::hasTable($db, $tableSourceMessage) && self::hasTable($db, $tableMessage)) {
throw new RuntimeException(
"Table '{$tableRawNameSourceMessage}' and '{$tableRawNameMessage}' already exists."
);
}

if ($driverName === 'sqlite') {
Expand Down Expand Up @@ -75,7 +75,7 @@ public static function dropTables(
$tableRawNameMessage = $schema->getRawTableName($tableMessage);

// drop sequence for table `source_message` and `message`.
if ($db->getTableSchema($tableMessage, true) !== null) {
if (self::hasTable($db, $tableMessage)) {
// drop foreign key for table `message`.
if ($driverName !== 'sqlite' && $schema->getTableForeignKeys($tableMessage, true) !== []) {
$command->dropForeignKey(
Expand All @@ -85,7 +85,7 @@ public static function dropTables(
}

// drop table `message`.
$command->dropTable($tableMessage)->execute();
$command->dropTable($tableRawNameMessage)->execute();

if ($driverName === 'oci') {
$command->setSql(
Expand All @@ -96,9 +96,9 @@ public static function dropTables(
}
}

if ($db->getTableSchema($tableSourceMessage, true) !== null) {
if (self::hasTable($db, $tableSourceMessage)) {
// drop table `source_message`.
$command->dropTable($tableSourceMessage)->execute();
$command->dropTable($tableRawNameSourceMessage)->execute();

if ($driverName === 'oci') {
$command->setSql(
Expand Down Expand Up @@ -332,4 +332,9 @@ private static function createSchemaSqlite(
// create index for table `source_message` and `message`.
self::createIndexForMigration($command, $tableRawNameSourceMessage, $tableRawNameMessage);
}

private static function hasTable(ConnectionInterface $db, string $table): bool
{
return $db->getTableSchema($table, true) !== null;
}
}
49 changes: 27 additions & 22 deletions tests/Common/AbstractDbHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Translator\Message\Db\Tests\Common;

use PHPUnit\Framework\TestCase;
use RuntimeException;
use Throwable;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Exception\Exception;
Expand Down Expand Up @@ -99,20 +100,22 @@ public function testEnsureTableWithCustomTableName(): void
*/
public function testEnsureTableExist(): void
{
DbHelper::ensureTables($this->db);
$prefix = $this->db->getTablePrefix();

$this->assertNotNull($this->db->getTableSchema('{{%source_message}}', true));
$this->assertNotNull($this->db->getTableSchema('{{%message}}', true));
try {
DbHelper::ensureTables($this->db);
DbHelper::ensureTables($this->db);
} catch (RuntimeException $e) {
$this->assertSame(
"Table '{$prefix}source_message' and '{$prefix}message' already exists.",
$e->getMessage()
);

DbHelper::ensureTables($this->db);
DbHelper::dropTables($this->db);

$this->assertNotNull($this->db->getTableSchema('{{%source_message}}', true));
$this->assertNotNull($this->db->getTableSchema('{{%message}}', true));

DbHelper::dropTables($this->db);

$this->assertNull($this->db->getTableSchema('{{%source_message}}', true));
$this->assertNull($this->db->getTableSchema('{{%message}}', true));
$this->assertNull($this->db->getTableSchema('{{%source_message}}', true));
$this->assertNull($this->db->getTableSchema('{{%message}}', true));
}
}

/**
Expand All @@ -122,20 +125,22 @@ public function testEnsureTableExist(): void
*/
public function testEnsureTableExistWithCustomTableName(): void
{
DbHelper::ensureTables($this->db, '{{%test_source_message}}', '{{%test_message}}');
$prefix = $this->db->getTablePrefix();

$this->assertNotNull($this->db->getTableSchema('{{%test_source_message}}', true));
$this->assertNotNull($this->db->getTableSchema('{{%test_message}}', true));
try {
DbHelper::ensureTables($this->db, '{{%test_source_message}}', '{{%test_message}}');
DbHelper::ensureTables($this->db, '{{%test_source_message}}', '{{%test_message}}');
} catch (RuntimeException $e) {
$this->assertSame(
"Table '{$prefix}test_source_message' and '{$prefix}test_message' already exists.",
$e->getMessage()
);

DbHelper::ensureTables($this->db, '{{%test_source_message}}', '{{%test_message}}');
DbHelper::dropTables($this->db, '{{%test_source_message}}', '{{%test_message}}');

$this->assertNotNull($this->db->getTableSchema('{{%test_source_message}}', true));
$this->assertNotNull($this->db->getTableSchema('{{%test_message}}', true));

DbHelper::dropTables($this->db, '{{%test_source_message}}', '{{%test_message}}');

$this->assertNull($this->db->getTableSchema('{{%test_source_message}}', true));
$this->assertNull($this->db->getTableSchema('{{%test_message}}', true));
$this->assertNull($this->db->getTableSchema('{{%test_source_message}}', true));
$this->assertNull($this->db->getTableSchema('{{%test_message}}', true));
}
}

/**
Expand Down

0 comments on commit 143ad05

Please sign in to comment.