diff --git a/commands/sql/sql.drush.inc b/commands/sql/sql.drush.inc index 11644a9..ad86c51 100644 --- a/commands/sql/sql.drush.inc +++ b/commands/sql/sql.drush.inc @@ -57,7 +57,7 @@ function sql_drush_command() { $items['sql-dump'] = array( 'callback' => 'drush_sql_dump_execute', 'description' => 'Exports the Drupal DB as SQL using mysqldump or equivalent.', - 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION, + 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE, 'examples' => array( 'drush sql-dump --result-file=../18.sql' => 'Save SQL dump to the directory above Drupal root.', 'drush sql-dump --skip-tables-key=common' => 'Skip standard tables. @see example.drushrc.com', @@ -402,29 +402,90 @@ function drush_sql_build_dump_command($table_selection, $db_spec = NULL) { * found, or an empty array if there were no matches. */ function _drush_sql_get_table_list($option_name) { + $tables = array(); foreach(array('' => 'cli', 'target-,,source-' => NULL) as $prefix_list => $context) { foreach(explode(',',$prefix_list) as $prefix) { $key_list = drush_get_option($prefix . $option_name . '-key', NULL, $context); foreach(explode(',', $key_list) as $key) { $all_tables = drush_get_option($option_name, array()); - if (array_key_exists($key, $all_tables)) { - return $all_tables[$key]; + if (isset($all_tables[$key])) { + $tables = $all_tables[$key]; + break 3; } if ($option_name != 'tables') { $all_tables = drush_get_option('tables', array()); - if (array_key_exists($key, $all_tables)) { - return $all_tables[$key]; + if (isset($all_tables[$key])) { + $tables = $all_tables[$key]; + break 3; } } } $table_list = drush_get_option($prefix . $option_name . '-list', NULL, $context); - if (isset($table_list)) { - return empty($table_list) ? array() : explode(',', $table_list); + if (!empty($table_list)) { + $tables = explode(',', $table_list); } } } - return array(); + + // Get the existing table names contained in the current database. + $db_tables = _drush_sql_get_db_table_list(); + + // Table name expansion based on * wildcard. + $expanded_db_tables = array(); + foreach ($tables as $k => $table) { + // Only deal with table names containing a wildcard. + if (strpos($table, '*') !== FALSE) { + $pattern = '/^' . preg_quote(str_replace('*', '.*', $table)) . '$/i'; + + // Merge those existing tables which match the pattern with the rest of + // the expanded table names. + $expanded_db_tables += preg_grep($pattern, $db_tables); + // Drop the wildcard table, this is not a valid table name. + unset($tables[$k]); + } + } + + // Ensure all the tables actually exist in the database. + foreach ($tables as $k => $table) { + if (!in_array($table, $db_tables)) { + unset($tables[$k]); + } + } + + // Merge expanded and validated table names. + $tables += $expanded_db_tables; + + // Make sure each table name is unique and sort them. + $tables = array_unique($tables); + sort($tables); + + return $tables; +} + +/** + * Extract the name of all existing tables in the current database. + * + * @return array + * A list of table names which exist in the database. + */ +function _drush_sql_get_db_table_list() { + static $db_tables = array(); + + if (!empty($db_tables)) { + return $db_tables; + } + + $query = 'SHOW TABLES'; + if ($scheme == 'pgsql') { + $query = "SELECT tablename FROM pg_tables WHERE schemaname='public';"; + } + $results = db_query($query); + while ($db_table = drush_db_fetch_object($results)) { + $db_tables[] = current((array)$db_table); + } + + return $db_tables; } /**