From 3e69ba93d8c66c431bed94e8dab479964d471802 Mon Sep 17 00:00:00 2001 From: zds <49744633+zds-s@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:00:15 +0800 Subject: [PATCH] Added `createDatabase` and `dropDatabaseIfExists` methods to `Hyperf\Database\Schmea`. (#6825) --- src/Schema/Grammars/MySqlGrammar.php | 34 +++++++++++++++ src/Schema/MySqlBuilder.php | 20 +++++++++ src/Schema/Schema.php | 2 + tests/DatabaseMysqlBuilderTest.php | 64 ++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 tests/DatabaseMysqlBuilderTest.php diff --git a/src/Schema/Grammars/MySqlGrammar.php b/src/Schema/Grammars/MySqlGrammar.php index 9d47284..b6add9a 100755 --- a/src/Schema/Grammars/MySqlGrammar.php +++ b/src/Schema/Grammars/MySqlGrammar.php @@ -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. * diff --git a/src/Schema/MySqlBuilder.php b/src/Schema/MySqlBuilder.php index b4a5990..113b6e2 100755 --- a/src/Schema/MySqlBuilder.php +++ b/src/Schema/MySqlBuilder.php @@ -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. * diff --git a/src/Schema/Schema.php b/src/Schema/Schema.php index 93fdf2b..7544a8f 100644 --- a/src/Schema/Schema.php +++ b/src/Schema/Schema.php @@ -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() diff --git a/tests/DatabaseMysqlBuilderTest.php b/tests/DatabaseMysqlBuilderTest.php new file mode 100644 index 0000000..46989eb --- /dev/null +++ b/tests/DatabaseMysqlBuilderTest.php @@ -0,0 +1,64 @@ +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'); + } +}