Skip to content

Commit 47d601c

Browse files
committed
Refactor --defaults flag handling
1 parent 9b08c7d commit 47d601c

File tree

1 file changed

+8
-30
lines changed

1 file changed

+8
-30
lines changed

src/DB_Command.php

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class DB_Command extends WP_CLI_Command {
8080
*/
8181
public function create( $_, $assoc_args ) {
8282

83-
$this->run_query( self::get_create_query(), self::get_dbuser_dbpass_args( $assoc_args ) );
83+
$this->run_query( self::get_create_query(), $assoc_args );
8484

8585
WP_CLI::success( 'Database created.' );
8686
}
@@ -111,7 +111,7 @@ public function create( $_, $assoc_args ) {
111111
public function drop( $_, $assoc_args ) {
112112
WP_CLI::confirm( "Are you sure you want to drop the '" . DB_NAME . "' database?", $assoc_args );
113113

114-
$this->run_query( sprintf( 'DROP DATABASE `%s`', DB_NAME ), self::get_dbuser_dbpass_args( $assoc_args ) );
114+
$this->run_query( sprintf( 'DROP DATABASE `%s`', DB_NAME ), $assoc_args );
115115

116116
WP_CLI::success( 'Database dropped.' );
117117
}
@@ -142,10 +142,8 @@ public function drop( $_, $assoc_args ) {
142142
public function reset( $_, $assoc_args ) {
143143
WP_CLI::confirm( "Are you sure you want to reset the '" . DB_NAME . "' database?", $assoc_args );
144144

145-
$mysql_args = self::get_dbuser_dbpass_args( $assoc_args );
146-
147-
$this->run_query( sprintf( 'DROP DATABASE IF EXISTS `%s`', DB_NAME ), $mysql_args );
148-
$this->run_query( self::get_create_query(), $mysql_args );
145+
$this->run_query( sprintf( 'DROP DATABASE IF EXISTS `%s`', DB_NAME ), $assoc_args );
146+
$this->run_query( self::get_create_query(), $assoc_args );
149147

150148
WP_CLI::success( 'Database reset.' );
151149
}
@@ -187,8 +185,6 @@ public function clean( $_, $assoc_args ) {
187185
$assoc_args
188186
);
189187

190-
$mysql_args = self::get_dbuser_dbpass_args( $assoc_args );
191-
192188
$tables = Utils\wp_get_table_names(
193189
[],
194190
[ 'all-tables-with-prefix' => true ]
@@ -201,7 +197,7 @@ public function clean( $_, $assoc_args ) {
201197
DB_NAME,
202198
$table
203199
),
204-
$mysql_args
200+
$assoc_args
205201
);
206202
}
207203

@@ -721,7 +717,6 @@ public function import( $args, $assoc_args ) {
721717
$mysql_args = [
722718
'database' => DB_NAME,
723719
];
724-
$mysql_args = array_merge( self::get_dbuser_dbpass_args( $assoc_args ), $mysql_args );
725720

726721
if ( '-' !== $result_file ) {
727722
if ( ! is_readable( $result_file ) ) {
@@ -1548,12 +1543,14 @@ protected function run_query( $query, $assoc_args = [] ) {
15481543

15491544
WP_CLI::debug( "Query: {$query}", 'db' );
15501545

1546+
$mysql_args = self::get_mysql_args( $assoc_args );
1547+
15511548
self::run(
15521549
sprintf(
15531550
'/usr/bin/env mysql%s --no-auto-rehash',
15541551
$this->get_defaults_flag_string( $assoc_args )
15551552
),
1556-
array_merge( $assoc_args, [ 'execute' => $query ] )
1553+
array_merge( $mysql_args, [ 'execute' => $query ] )
15571554
);
15581555
}
15591556

@@ -1604,25 +1601,6 @@ private static function run( $cmd, $assoc_args = [], $send_to_shell = true, $int
16041601
return Utils\run_mysql_command( $cmd, $final_args, null, $send_to_shell, $interactive );
16051602
}
16061603

1607-
/**
1608-
* Helper to pluck 'dbuser' and 'dbpass' from associative args array.
1609-
*
1610-
* @param array $assoc_args Associative args array.
1611-
* @return array Array with `dbuser' and 'dbpass' set if in passed-in associative args array.
1612-
*/
1613-
private static function get_dbuser_dbpass_args( $assoc_args ) {
1614-
$mysql_args = [];
1615-
$dbuser = Utils\get_flag_value( $assoc_args, 'dbuser' );
1616-
if ( null !== $dbuser ) {
1617-
$mysql_args['dbuser'] = $dbuser;
1618-
}
1619-
$dbpass = Utils\get_flag_value( $assoc_args, 'dbpass' );
1620-
if ( null !== $dbpass ) {
1621-
$mysql_args['dbpass'] = $dbpass;
1622-
}
1623-
return $mysql_args;
1624-
}
1625-
16261604
/**
16271605
* Gets the column names of a db table differentiated into key columns and text columns and all columns.
16281606
*

0 commit comments

Comments
 (0)