? 698264_better_tables_key_11.patch
? 698264_better_tables_key_2++.patch
? 698264_better_tables_key_2.patch
? 698264_better_tables_key_7.patch
? 698264_better_tables_key_9.patch
? drush_better_db_tables.patch
? option_typo_upgrade_drush.patch
? strip_trail_spaces.sh
? includes/table.inc
Index: example.drushrc.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drush/example.drushrc.php,v
retrieving revision 1.29
diff -u -p -r1.29 example.drushrc.php
--- example.drushrc.php	29 Jan 2010 06:14:26 -0000	1.29
+++ example.drushrc.php	1 Feb 2010 22:04:01 -0000
@@ -67,7 +67,7 @@
  * array or add a new element.
  */
 $options['structure-tables'] = array(
- 'common' => array('accesslog', 'cache', 'cache_filter', 'cache_menu', 'cache_page', 'history', 'search_dataset', 'search_index', 'search_total', 'sessions', 'watchdog'),
+ 'common' => array('accesslog', 'cache', 'cache_*', 'history', 'search_dataset', 'search_index', 'search_total', 'sessions', 'watchdog'),
 );
 
 /*
Index: commands/sql/sql.drush.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drush/commands/sql/sql.drush.inc,v
retrieving revision 1.37
diff -u -p -r1.37 sql.drush.inc
--- commands/sql/sql.drush.inc	29 Jan 2010 06:14:26 -0000	1.37
+++ commands/sql/sql.drush.inc	1 Feb 2010 22:04:02 -0000
@@ -67,6 +67,7 @@ function sql_drush_command() {
       '--create-db' => 'Wipe existing tables (i.e. --clean). Postgres only.',
       '--data-only' => 'Omit CREATE TABLE statements. Postgres only.',
       '--ordered-dump' => 'Order by primary key and add line breaks for efficient diff in revision control. Also, faster rsync. Slows down the dump. Mysql only.',
+      '--friendly-table-names' => 'Allow to include the * wildcard in the customized list of tables such as structure-tables. Tables which don\'t exist in the database are ignored. @see example.drushrc.php. Optional.',
     ) + $options,
   );
   $items['sql-query'] = array(
@@ -311,10 +312,107 @@ function _drush_sql_get_table_list($opti
     }
   }
 
+  // Table name expansion and validation are only enabled with proper argument.
+  if (drush_get_option('friendly-table-names')) {
+    // 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 = '/^' . 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 local database.
+ *
+ * @return array
+ *   A list of table names which exist in the current database.
+ */
+function _drush_sql_get_db_table_list() {
+  static $db_tables = array();
+
+  if (!empty($db_tables)) {
+    return $db_tables;
+  }
+
+  $scheme = _drush_sql_get_scheme();
+
+  $query = 'show tables;';
+  if ($scheme == 'pgsql') {
+    $query = "select tablename from pg_tables where schemaname='public';";
+  }
+
+  // We do not have access to the database other than by running the appropriate
+  // command line, sending the results into a file and reading it.
+  if ($query_file = drush_save_data_to_temp_file($query)) {
+    // Create a temporary file for storing the results.
+    $results_file = tempnam(sys_get_temp_dir(), $target_db_url['database'] . '.sql.');
+
+    switch ($scheme) {
+      case 'mysql':
+        $exec = 'mysql' . (drush_get_context('DRUSH_VERBOSE') ? ' -v' : '');
+        $exec .= _drush_sql_get_credentials($db_spec);
+        $exec .= ' ' . drush_get_option('extra');
+        $exec .= " < $query_file > $results_file";
+
+        break;
+      case 'pgsql':
+        $exec = 'psql';
+        $exec .= _drush_sql_get_credentials($db_spec);
+        $exec .= (drush_get_context('DRUSH_VERBOSE') ? '' : ' -q');
+        $exec .= ' ' . (drush_get_option('extra') ? drush_get_option('extra') : "--no-align --field-separator=$'\t' --pset footer=off");
+        $exec .= " --file $query_file > $results_file";
+        break;
+    }
+
+    // Execute the show tables; command.
+    drush_op('system', $exec);
+    // Read all the results back in memory.
+    $results = file($results_file);
+    // We can scratch the query and results files now.
+    drush_op('unlink', $query_file);
+    drush_op('unlink', $results_file);
+
+    // The first result is the column name.
+    unset($results[0]);
+    // Clean up table names.
+    foreach ($results as &$v) {
+      $v = trim($v);
+    }
+    $db_tables = $results;
+  }
+
+  return $db_tables;
+}
+
+/**
  * Command callback. Executes the given SQL query on the Drupal database.
  */
 function drush_sql_query($query) {
