Created
July 24, 2012 14:56
-
-
Save CashWilliams/3170446 to your computer and use it in GitHub Desktop.
drush dbprefix command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Add and Remove prefix commands. | |
* | |
* You can copy this file to any of the following | |
* 1. A .drush folder in your HOME folder. | |
* 2. Anywhere in a folder tree below an active module on your site. | |
* 3. /usr/share/drush/commands (configurable) | |
* 4. In an arbitrary folder specified with the --include option. | |
* 5. Drupal's sites/all/drush folder. | |
*/ | |
/** | |
* Implementation of hook_drush_command(). | |
* | |
* @return | |
* An associative array describing your command(s). | |
*/ | |
function dbprefix_drush_command() { | |
$items = array(); | |
// The 'dbprefix-remove' command | |
$items['dbprefix-remove'] = array( | |
'description' => "Removes the existing db prefix from a site's database tables.", | |
'examples' => array( | |
'drush dbprefix-remove' => "Removes the existing db prefix from a site's database tables.", | |
), | |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE, | |
); | |
// The 'dbprefix-add' command | |
$items['dbprefix-add'] = array( | |
'description' => "Adds a new db prefix to a site's database tables.", | |
'arguments' => array( | |
'prefix' => 'New prefix', | |
), | |
'examples' => array( | |
'drush dbprefix-add drup_' => "Adds 'drup_' as a prefix to the site's database tables.", | |
), | |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE, | |
); | |
return $items; | |
} | |
/** | |
* Implementation of hook_drush_help(). | |
* | |
* @param | |
* A string with the help section (prepend with 'drush:') | |
* | |
* @return | |
* A string with the help text for your command. | |
*/ | |
function dbprefix_drush_help($section) { | |
switch ($section) { | |
case 'drush:dbprefix-remove': | |
return dt("This command will removes the existing db prefix from a site's database tables."); | |
case 'drush:dbprefix-add': | |
return dt("This command will add a new prefix to a site's database tables."); | |
} | |
} | |
/** | |
* DBPrefix remove callback. | |
*/ | |
function drush_dbprefix_remove() { | |
$self = drush_sitealias_get_record('@self'); | |
if (empty($self)) { | |
drush_set_error('NO_BOOTSTRAP', dt('This command must be run on an installed Drupal site.')); | |
return; | |
} | |
if (!$db_spec = _drush_sql_get_db_spec()) { | |
drush_set_error(dt('Could not determine database connection parameters. Pass --db-url option.')); | |
return; | |
} | |
if (empty($db_spec['db_prefix'])) { | |
drush_set_error(dt('No existing prefix could be detected. Please see settings.php.')); | |
return; | |
} | |
else { | |
$db_prefix = strtolower($db_spec['db_prefix']); | |
} | |
$table_list = db_query("SHOW TABLES"); | |
switch (drush_drupal_major_version()) { | |
case 6: | |
while ($r = db_fetch_array($table_list)) { | |
$table_old = strtolower(current($r)); | |
if(substr($table_old,0,strlen($db_prefix)) == $db_prefix) { | |
$table_new = substr($table_old, strlen($db_prefix)); | |
dbprefix_table_rename($table_old, $table_new); | |
} | |
else { | |
drush_print(dt('Skipping !table_old.', array('!table_old' => $table_old))); | |
} | |
} | |
drush_print(dt('Table prefix removed. Please adjust settings.php.')); | |
break; | |
case 7: | |
foreach ($table_list as $r) { | |
$r = (array)$r; | |
$table_old = strtolower(current($r)); | |
if(substr($table_old,0,strlen($db_prefix)) == $db_prefix) { | |
$table_new = substr($table_old, strlen($db_prefix)); | |
dbprefix_table_rename($table_old, $table_new); | |
} | |
else { | |
drush_print(dt('Skipping !table_old.', array('!table_old' => $table_old))); | |
} | |
} | |
drush_print(dt('Table prefix removed. Please adjust settings.php.')); | |
break; | |
default: | |
drush_print(dt('Unsupported version of Drupal core.')); | |
break; | |
} | |
} | |
/** | |
* DBPrefix add callback. | |
*/ | |
function drush_dbprefix_add($db_prefix = NULL) { | |
$self = drush_sitealias_get_record('@self'); | |
if (empty($self)) { | |
return drush_set_error('NO_BOOTSTRAP', dt('This command must be run on an installed Drupal site.')); | |
} | |
if (!$db_spec = _drush_sql_get_db_spec()) { | |
drush_set_error(dt('Could not determine database connection parameters.')); | |
return; | |
} | |
if (!empty($db_spec['db_prefix'])) { | |
drush_set_error(dt('Existing prefix detected. Please see settings.php.')); | |
return; | |
} | |
if (!$db_prefix) { | |
drush_set_error(dt('A database prefix must be provided.')); | |
return; | |
} | |
$table_list = db_query("SHOW TABLES"); | |
switch (drush_drupal_major_version()) { | |
case 6: | |
while ($r = db_fetch_array($table_list)) { | |
$table_old = strtolower(current($r)); | |
// ensure $db_prefix doesn't already exist | |
if(substr($table_old,0,strlen($prefix)) != $db_prefix) { | |
$table_new = $db_prefix . $table_old; | |
dbprefix_table_rename($table_old, $table_new); | |
} | |
else { | |
drush_print(dt('Skipping !table_old.', array('!table_old' => $table_old))); | |
} | |
} | |
drush_print(dt('Table prefix added. Please adjust settings.php.')); | |
break; | |
case 7: | |
foreach ($table_list as $r) { | |
$r = (array)$r; | |
$table_old = strtolower(current($r)); | |
// ensure $db_prefix doesn't already exist | |
if(substr($table_old,0,strlen($prefix)) != $db_prefix) { | |
$table_new = $db_prefix . $table_old; | |
dbprefix_table_rename($table_old, $table_new); | |
} | |
else { | |
drush_print(dt('Skipping !table_old.', array('!table_old' => $table_old))); | |
} | |
} | |
drush_print(dt('Table prefix added. Please adjust settings.php.')); | |
break; | |
default: | |
drush_print(dt('Unsupported version of Drupal core.')); | |
break; | |
} | |
} | |
/** | |
* Executes table renames | |
*/ | |
function dbprefix_table_rename($table_old, $table_new) { | |
// we're not using {table} because thats what we're trying to fix here | |
$clean_sql = "DROP TABLE IF EXISTS $table_new"; | |
$rename_sql = "RENAME TABLE $table_old TO $table_new"; | |
if (drush_get_option('simulate')) { | |
drush_print($clean_sql); | |
drush_print($rename_sql); | |
} | |
else { | |
if(drush_get_option('verbose')) { | |
drush_print(dt('Dropping existing table "!table"', array('!table' => $table_new))); | |
} | |
if(!db_query($clean_sql)) { | |
drush_set_error(dt('Aborting - !sql', array('!sql' => $clean_sql))); | |
die(); | |
} | |
if(drush_get_option('verbose')) { | |
drush_print(dt('Renaming "!table_old" to "!table_new"', | |
array( | |
'!table_old' => $table_old, | |
'!table_new' => $table_new, | |
) | |
)); | |
} | |
if(!db_query($rename_sql)) { | |
drush_set_error(dt('Aborting - !sql', array('!sql' => $rename_sql))); | |
die(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment