Skip to content

Commit

Permalink
Added createDatabase and dropDatabaseIfExists methods to `Hyperf\…
Browse files Browse the repository at this point in the history
…Database\Schmea`. (#6825)
  • Loading branch information
zds-s authored Jun 3, 2024
1 parent b804b37 commit 3e69ba9
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,40 @@ public function compileColumns(): string
return 'select `table_schema`, `table_name`, `column_name`, `ordinal_position`, `column_default`, `is_nullable`, `data_type`, `column_comment` from information_schema.columns where `table_schema` = ? order by ORDINAL_POSITION';
}

/**
* Compile a create database command.
*/
public function compileCreateDatabase(string $name, Connection $connection): string
{
$charset = $connection->getConfig('charset');
$collation = $connection->getConfig('collation');

if (! $charset || ! $collation) {
return sprintf(
'create database %s',
$this->wrapValue($name),
);
}

return sprintf(
'create database %s default character set %s default collate %s',
$this->wrapValue($name),
$this->wrapValue($charset),
$this->wrapValue($collation),
);
}

/**
* Compile a drop database if exists command.
*/
public function compileDropDatabaseIfExists(string $name): string
{
return sprintf(
'drop database if exists %s',
$this->wrapValue($name)
);
}

/**
* Compile a create table command.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Schema/MySqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@

class MySqlBuilder extends Builder
{
/**
* Create a database in the schema.
*/
public function createDatabase(string $name): bool
{
return $this->connection->statement(
$this->grammar->compileCreateDatabase($name, $this->connection)
);
}

/**
* Drop a database from the schema if the database exists.
*/
public function dropDatabaseIfExists(string $name): bool
{
return $this->connection->statement(
$this->grammar->compileDropDatabaseIfExists($name)
);
}

/**
* Determine if the given table exists.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

/**
* @method static bool hasTable(string $table)
* @method static bool createDatabase(string $name)
* @method static bool dropDatabaseIfExists(string $name)
* @method static array getColumnListing(string $table)
* @method static array getColumnTypeListing(string $table)
* @method static void dropAllTables()
Expand Down
64 changes: 64 additions & 0 deletions tests/DatabaseMysqlBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace HyperfTest\Database;

use Hyperf\Database\Connection;
use Hyperf\Database\Schema\Grammars\MySqlGrammar;
use Hyperf\Database\Schema\MySqlBuilder;
use Mockery as m;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversNothing
*/
class DatabaseMysqlBuilderTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}

public function testCreateDatabase()
{
$grammar = new MySqlGrammar();

$connection = m::mock(Connection::class);
$connection->shouldReceive('getConfig')->once()->with('charset')->andReturn('utf8mb4');
$connection->shouldReceive('getConfig')->once()->with('collation')->andReturn('utf8mb4_unicode_ci');
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$connection->shouldReceive('statement')->once()->andReturnUsing(function ($sql) {
$this->assertSame('create database `my_temporary_database` default character set `utf8mb4` default collate `utf8mb4_unicode_ci`', $sql);
return true;
});

$builder = new MySqlBuilder($connection);
$builder->createDatabase('my_temporary_database');
}

public function testDropDatabaseIfExists()
{
$grammar = new MySqlGrammar();

$connection = m::mock(Connection::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$connection->shouldReceive('statement')->once()->andReturnUsing(function ($sql) {
$this->assertSame('drop database if exists `my_database_a`', $sql);
return true;
});

$builder = new MySqlBuilder($connection);

$builder->dropDatabaseIfExists('my_database_a');
}
}

0 comments on commit 3e69ba9

Please sign in to comment.