Skip to content

Instantly share code, notes, and snippets.

@davereid
Last active August 29, 2015 13:57
Show Gist options
  • Save davereid/9680778 to your computer and use it in GitHub Desktop.
Save davereid/9680778 to your computer and use it in GitHub Desktop.
<?php
/**
* Implements_drush_help_alter().
*/
function multisite_sql_drush_help_alter(&$command) {
if ($command['command'] == 'sql-drop') {
$command['options']['ignore-prefix'] = 'Ignore database prefixing if found.';
}
}
/**
* Implements hook_drush_command_alter().
*/
function multisite_sql_drush_command_alter(&$command) {
if ($command['command'] == 'sql-drop') {
$command['callback'] = 'multisite_sql_drush_sql_drop';
}
}
function multisite_sql_drush_sql_drop() {
drush_sql_bootstrap_further();
$db_spec = _drush_sql_get_db_spec();
$prefix = _multisite_sql_drush_using_prefix($db_spec);
if (!$db_spec) {
return drush_set_error('DRUSH_SQL_NO_DATABASE', dt("No database to operate on."));
}
if ($prefix) {
if (!drush_confirm(dt('Do you really want to drop all tables matching !prefix in the database !db?', array('!db' => $db_spec['database'], '!prefix' => $prefix)))) {
return drush_user_abort();
}
}
elseif (!drush_confirm(dt('Do you really want to drop all tables in the database !db?', array('!db' => $db_spec['database'])))) {
return drush_user_abort();
}
_multisite_sql_drush_sql_drop($db_spec);
}
// n.b. site-install uses _drush_sql_drop as a fallback technique if
// drop database; create database fails. If _drush_sql_drop()
// is rewritten to also use that technique, it should maintain
// the drop tables code here as a fallback.
function _multisite_sql_drush_sql_drop($db_spec = NULL) {
$tables = _multisite_sql_drush_sql_get_db_table_list($db_spec);
$scheme = _drush_sql_get_scheme($db_spec);
if (count($tables)) {
if ($scheme === 'sqlite') {
$sql = '';
// SQLite only wants one table per DROP TABLE command (so we have to do
// "DROP TABLE foo; DROP TABLE bar;" instead of "DROP TABLE foo, bar;").
foreach ($tables as $table) {
$sql .= "DROP TABLE $table; ";
}
// We can't use drush_op('db_query', $sql) because it will only perform one
// SQL command and we're technically performing several.
$exec = _drush_sql_connect($db_spec);
$exec .= " '{$sql}'";
return drush_op_system($exec) == 0;
}
else {
$sql = 'DROP TABLE '. implode(', ', $tables);
return _drush_sql_query($sql, $db_spec);
}
}
else {
drush_log(dt('No tables to drop.'), 'ok');
}
return TRUE;
}
function _multisite_sql_drush_using_prefix($db_spec) {
if (!empty($db_spec['prefix']['default']) && !drush_get_option(array('db-url', 'db-spec', 'ignore-prefix'))) {
return $db_spec['prefix']['default'];
}
}
function _multisite_sql_drush_sql_get_db_table_list($db_spec) {
$tables = _drush_sql_get_db_table_list($db_spec);
// If dealing with prefixed tables, only return tables that match the prefix.
if ($prefix = _multisite_sql_drush_using_prefix($db_spec)) {
$tables = array_filter($tables, function($table) use ($prefix) {
return strpos($table, $prefix) === 0;
});
}
return $tables;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment