You are here

function drush_paranoiasanitize_paranoia_sql_sanitize_whitelist in Paranoia 7

Callback for `drush paranoia-sql-sanitize-whitelist`.

File

paranoiasanitize/paranoiasanitize.drush.inc, line 128
Drush integration for the paranoiasanitize module.

Code

function drush_paranoiasanitize_paranoia_sql_sanitize_whitelist() {
  drush_log(dt('You started sanitizing your db in a manner that is really, really destructive.'), 'warning');
  drush_log(dt('If you have multiple Drupal sites storing tables in a single database and you use prefixes to separate them then this tool cannot handle that and will delete a bunch of stuff you probably want to keep. Also, kinda crazy architecture you have there, just fyi.'), 'warning');
  if (!drush_confirm(dt('Do you want to continue?'))) {
    drush_die('Aborting.');
  }

  // Get a list of tables that exist in the db and their columns.
  $existing_tables = db_query('SHOW TABLES;')
    ->fetchAllKeyed(0, 0);
  foreach ($existing_tables as $table_name) {
    $existing_tables[$table_name] = array(
      'fields' => _paranoiasanitize_get_columns($table_name),
    );
  }

  // Get a list of what to do with tables from hooks.
  global $databases;
  $whitelist_tables = array();

  // Like module_invoke_all, but respects weights & only stores top result.
  $hook = 'paranoiasanitize_sql_sanitize_operations';
  foreach (module_implements($hook) as $module) {
    $function = $module . '_' . $hook;
    if (function_exists($function)) {
      $result = call_user_func_array($function, array());
      foreach ($result as $table_name => $actions) {

        // TODO: it could be valuable to try to merge together values if not
        // all columns are covered. Perhaps two implementations together get
        // to a valid set of queries.
        if (!array_key_exists($table_name, $whitelist_tables)) {

          // Also modify that list to be aware of prefixes.
          $whitelist_tables[$table_name] = $actions;
        }
        else {
          watchdog('paranoiasanitize', 'Skipping @module data for @table as a prior hook provided this data', array(
            '@module' => $module,
            '@table' => $table_name,
          ), WATCHDOG_WARNING);
        }
      }
    }
  }

  // Compare whitelist and potential changes and determine what to do.
  $derived_changes = _paranoiasanitize_decide_what_to_do($existing_tables, $whitelist_tables);
  drush_log(dt('Welp, there is the list of changes that will be made.'), 'warning');
  if (!drush_confirm(dt('Do you want to continue?'))) {
    drush_die('Aborting.');
  }

  // Do the stuff in the list.
  foreach ($derived_changes as $table_name => $data) {
    foreach ($data['queries'] as $query) {
      if (!empty($query)) {
        db_query($query);
      }
    }
  }
}