Skip to content

Commit

Permalink
Merge pull request #346 from kirtangajjar/fix/db-name-hyphen
Browse files Browse the repository at this point in the history
Fix db name with hypen issue
  • Loading branch information
mrrobot47 authored Aug 7, 2020
2 parents 3a79694 + 8d87e86 commit 85e542f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/helper/class-ee-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ protected function delete_site( $level, $site_url, $site_fs_path, $db_data = []
\EE::exec( 'docker volume rm ' . $volume );
}

$db_script_path = \EE\Utils\get_temp_dir() . 'db_exec';

if ( $this->fs->exists( $db_script_path ) ) {
try {
$this->fs->remove( $db_script_path );
} catch ( \Exception $e ) {
\EE::debug( $e );
}
}

if ( ! empty( $db_data['db_host'] ) ) {
\EE\Site\Utils\cleanup_db( $db_data['db_host'], $db_data['db_name'] );
\EE\Site\Utils\cleanup_db_user( $db_data['db_host'], $db_data['db_user'] );
Expand Down
9 changes: 5 additions & 4 deletions src/helper/site-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ function create_user_in_db( $db_host, $db_name = '', $db_user = '', $db_pass = '
$db_user = empty( $db_user ) ? \EE\Utils\random_password( 5 ) : $db_user;
$db_pass = empty( $db_pass ) ? \EE\Utils\random_password() : $db_pass;

$create_string = sprintf( "CREATE USER '%1\$s'@'%%' IDENTIFIED BY '%2\$s'; CREATE DATABASE %3\$s; GRANT ALL PRIVILEGES ON %3\$s.* TO '%1\$s'@'%%'; FLUSH PRIVILEGES;", $db_user, $db_pass, $db_name );
// TODO: Create database only if it does not exist.
$create_string = sprintf( 'CREATE USER "%1$s"@"%%" IDENTIFIED BY "%2$s"; CREATE DATABASE `%3$s`; GRANT ALL PRIVILEGES ON `%3$s`.* TO "%1$s"@"%%"; FLUSH PRIVILEGES;', $db_user, $db_pass, $db_name );

if ( GLOBAL_DB === $db_host ) {

Expand All @@ -166,7 +167,7 @@ function create_user_in_db( $db_host, $db_name = '', $db_user = '', $db_pass = '
}

$db_script_path = \EE\Utils\get_temp_dir() . 'db_exec';
file_put_contents( $db_script_path, sprintf( 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"%s"', $create_string ) );
file_put_contents( $db_script_path, sprintf( 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e\'%s\'', $create_string ) );

EE::exec( sprintf( 'docker cp %s %s:/db_exec', $db_script_path, GLOBAL_DB_CONTAINER ) );
if ( ! EE::exec( sprintf( 'docker exec %s sh db_exec', GLOBAL_DB_CONTAINER ) ) ) {
Expand All @@ -193,11 +194,11 @@ function create_user_in_db( $db_host, $db_name = '', $db_user = '', $db_pass = '
*/
function cleanup_db( $db_host, $db_name, $db_user = '', $db_pass = '' ) {

$cleanup_string = sprintf( 'DROP DATABASE %s;', $db_name );
$cleanup_string = sprintf( 'DROP DATABASE `%s`;', $db_name );

if ( GLOBAL_DB === $db_host ) {
$db_script_path = \EE\Utils\get_temp_dir() . 'db_exec';
file_put_contents( $db_script_path, sprintf( 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"%s"', $cleanup_string ) );
file_put_contents( $db_script_path, sprintf( 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e\'%s\'', $cleanup_string ) );

EE::exec( sprintf( 'docker cp %s %s:/db_exec', $db_script_path, GLOBAL_DB_CONTAINER ) );
EE::exec( sprintf( 'docker exec %s sh db_exec', GLOBAL_DB_CONTAINER ) );
Expand Down

0 comments on commit 85e542f

Please sign in to comment.