Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix db name with hypen issue #346

Merged
merged 7 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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