Skip to content

Commit 6034921

Browse files
authored
Merge pull request #205 from wp-cli/fix/185-refactor-defaults-flag-handling
Refactor `--defaults` flag handling
2 parents 9b08c7d + 7b245e4 commit 6034921

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/DB_Command.php

+31-17
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

@@ -718,10 +714,12 @@ public function import( $args, $assoc_args ) {
718714
$result_file = sprintf( '%s.sql', DB_NAME );
719715
}
720716

721-
$mysql_args = [
722-
'database' => DB_NAME,
723-
];
724-
$mysql_args = array_merge( self::get_dbuser_dbpass_args( $assoc_args ), $mysql_args );
717+
// Process options to MySQL.
718+
$mysql_args = array_merge(
719+
[ 'database' => DB_NAME ],
720+
self::get_dbuser_dbpass_args( $assoc_args ),
721+
self::get_mysql_args( $assoc_args )
722+
);
725723

726724
if ( '-' !== $result_file ) {
727725
if ( ! is_readable( $result_file ) ) {
@@ -738,8 +736,6 @@ public function import( $args, $assoc_args ) {
738736
} else {
739737
$result_file = 'STDIN';
740738
}
741-
// Check if any mysql option pass.
742-
$mysql_args = array_merge( $mysql_args, self::get_mysql_args( $assoc_args ) );
743739

744740
$command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) );
745741
WP_CLI::debug( "Running shell command: {$command}", 'db' );
@@ -1548,12 +1544,17 @@ protected function run_query( $query, $assoc_args = [] ) {
15481544

15491545
WP_CLI::debug( "Query: {$query}", 'db' );
15501546

1547+
$mysql_args = array_merge(
1548+
self::get_dbuser_dbpass_args( $assoc_args ),
1549+
self::get_mysql_args( $assoc_args )
1550+
);
1551+
15511552
self::run(
15521553
sprintf(
15531554
'/usr/bin/env mysql%s --no-auto-rehash',
15541555
$this->get_defaults_flag_string( $assoc_args )
15551556
),
1556-
array_merge( $assoc_args, [ 'execute' => $query ] )
1557+
array_merge( [ 'execute' => $query ], $mysql_args )
15571558
);
15581559
}
15591560

@@ -1599,7 +1600,20 @@ private static function run( $cmd, $assoc_args = [], $send_to_shell = true, $int
15991600
unset( $assoc_args['dbpass'], $assoc_args['password'] );
16001601
}
16011602

1602-
$final_args = array_merge( $assoc_args, $required );
1603+
$final_args = array_merge( $required, $assoc_args );
1604+
1605+
// Adapt ordering of arguments.
1606+
uksort(
1607+
$final_args,
1608+
static function ( $a, $b ) {
1609+
switch ( $b ) {
1610+
case 'force':
1611+
return -1;
1612+
default:
1613+
return 1;
1614+
}
1615+
}
1616+
);
16031617

16041618
return Utils\run_mysql_command( $cmd, $final_args, null, $send_to_shell, $interactive );
16051619
}

0 commit comments

Comments
 (0)