? includes/table.inc
Index: commands/sql/sql.drush.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drush/commands/sql/sql.drush.inc,v
retrieving revision 1.42
diff -u -p -r1.42 sql.drush.inc
--- commands/sql/sql.drush.inc	30 Mar 2010 13:22:19 -0000	1.42
+++ commands/sql/sql.drush.inc	31 Mar 2010 21:54:37 -0000
@@ -18,6 +18,8 @@ function sql_drush_help($section) {
       return dt('Quickly enter the mysql command line.');
     case 'drush:sql-dump':
       return dt('Prints the whole database to STDOUT or save to a file.');
+    case 'drush:sql-hash':
+      return dt('Output hash values for each table in the database.');
     case 'drush:sql-query':
       return dt("Usage: drush [options] sql-query <query>...\n<query> is a SQL statement, which can alternatively be passed via STDIN. Any additional arguments are passed to the mysql command directly.");
     case 'drush:sql-sync':
@@ -60,10 +62,12 @@ function sql_drush_command() {
     ),
     'options' => array(
       '--result-file' => 'Save to a file. The file should be relative to Drupal root. Recommended.',
-      '--skip-tables-key' => 'A key in the $skip_tables array. @see example.drushrc.php. Optional.',
-      '--structure-tables-key' => 'A key in the $structure_tables array. @see example.drushrc.php. Optional.',
       '--tables-key' => 'A key in the $tables array. Optional.',
       '--tables-list' => 'A comma-separated list of tables to transfer. Optional.',
+      '--skip-tables-key' => 'A key in the $skip_tables array. @see example.drushrc.php. Optional.',
+      '--skip-tables-list' => 'A comma-separated list of tables to skip.  Optional.',
+      '--structure-tables-key' => 'A key in the $structure_tables array. @see example.drushrc.php. Optional.',
+      '--structure-tables-list' => 'A comma-separated list of tables whose table data will be excluded from the dump. Optional.',
       '--ordered-dump' => 'Use this option to output ordered INSERT statements in the sql-dump.Useful when backups are managed in a Version Control System. Optional.',
       '--create-db' => 'Wipe existing tables.',
       '--data-only' => 'Omit CREATE TABLE statements. Postgres only.',
@@ -80,6 +84,7 @@ function sql_drush_command() {
        'query' => 'A SQL query. Mandatory.',
     ),
     'options' => array(
+      '--result-file' => 'Save to a file. The file should be relative to Drupal root. Optional.',
       '--extra' => 'Add custom options to the mysql command.',
     ) + $options,
     'aliases' => array('sqlq'),
@@ -95,10 +100,12 @@ function sql_drush_command() {
       'to' => 'Name of subdirectory within /sites or a site-alias.',
     ),
     'options' => array(
-      '--skip-tables-key' => 'A key in the $skip_tables array. @see example.drushrc.php. Optional.',
-      '--structure-tables-key' => 'A key in the $structure_tables array. @see example.drushrc.php. Optional.',
       '--tables-key' => 'A key in the $tables array. Optional.',
       '--tables-list' => 'A comma-separated list of tables to transfer. Optional.',
+      '--skip-tables-key' => 'A key in the $skip_tables array. @see example.drushrc.php. Optional.',
+      '--skip-tables-list' => 'A comma-separated list of tables to skip.  Optional.',
+      '--structure-tables-key' => 'A key in the $structure_tables array. @see example.drushrc.php. Optional.',
+      '--structure-tables-list' => 'A comma-separated list of tables whose table data will be excluded from the dump. Optional.',
       '--cache' => 'Skip dump if result file exists and is less than "cache" hours old. Optional; default is 24 hours.',
       '--no-cache' => 'Do not cache the sql-dump file.',
       '--no-dump' => 'Do not dump the sql database; always use an existing dump file.',
@@ -130,6 +137,18 @@ function sql_drush_command() {
     'options' => $options,
     'aliases' => array('sqlc'),
   );
+  $items['sql-hash'] = array(
+    'description' => "Output hash values for each table in the database.  Run before and after an operation on a Drupal site to track table usage.",
+    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE,
+    'options' => array(
+      '--tables-key' => 'A key in the $tables array. Optional.',
+      '--tables-list' => 'A comma-separated list of tables to hash. Optional.',
+      '--skip-tables-key' => 'A key in the $skip_tables array. @see example.drushrc.php. Optional.',
+      '--skip-tables-list' => 'A comma-separated list of tables to skip.  Optional.',
+      '--structure-tables-key' => 'A key in the $structure_tables array. @see example.drushrc.php. Optional.',
+    ),
+    'aliases' => array('sqlh'),
+  );
   return $items;
 }
 
@@ -175,6 +194,41 @@ function drush_sql_connect() {
 }
 
 /**
+ * Print out an md5 hash for every table in the database.
+ */
+function drush_sql_hash() {
+  // Find out which tables to include or skip.
+  $skip_tables = array_merge(_drush_sql_get_table_list('skip-tables'), _drush_sql_get_table_list('structure-tables'));
+  $hash_tables = _drush_sql_get_table_list('tables');
+  drush_unset_option('tables-key');
+  
+  // We will re-use the same tmp file to store the
+  // sql tables in, and also to store every sql dump file.
+  $tmp_file = drush_tempnam('sql-hash.sql.');
+  drush_set_option('result-file', $tmp_file);
+  drush_sql_query('show tables;');
+  
+  // Get the table list from the dump file.  Drop the
+  // first line, as it is always the column header ('tables').
+  $tables = file($tmp_file, FILE_IGNORE_NEW_LINES);
+  array_shift($tables);
+  foreach($tables as $table) {
+    // Skip this table if it is not selected, or if it is unselected
+    if ((empty($hash_tables) || in_array($table, $hash_tables)) && (!in_array($table, $skip_tables))) {
+      if (is_file($tmp_file)) {
+	unlink($tmp_file);
+      }
+      drush_set_option('tables-list', $table);
+      drush_sql_dump_execute();
+      if (is_file($tmp_file)) {
+	$hash = md5_file($tmp_file);
+	drush_print($table . ': ' . $hash);
+      }
+    }
+  }
+}
+
+/**
  * Command callback. Outputs the entire Drupal database in SQL format using mysqldump.
  */
 function drush_sql_dump_execute() {
@@ -351,6 +405,9 @@ function _drush_sql_query($query, $db_sp
         $exec = 'mysql' . (drush_get_context('DRUSH_VERBOSE') ? ' -v' : '');
         $exec .= _drush_sql_get_credentials($db_spec);
         $exec .= ' ' . drush_get_option('extra');
+        if ($output_file = drush_get_option('result-file')) {
+          $exec .= ' --result-file '. $output_file;
+        }
         $exec .= " < $file";
 
         break;
@@ -360,6 +417,9 @@ function _drush_sql_query($query, $db_sp
         $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 $file";
+        if ($output_file = drush_get_option('result-file')) {
+          $exec .= ' -o '. $output_file;
+        }
         break;
     }
 
