Skip to content

Instantly share code, notes, and snippets.

@CashWilliams
Created June 29, 2012 02:28
Show Gist options
  • Save CashWilliams/3015279 to your computer and use it in GitHub Desktop.
Save CashWilliams/3015279 to your computer and use it in GitHub Desktop.
Drupal 7 drush script to remove database prefix
<?php
// current table prefix to be removed
$prefix = "drup_";
// echo generated statments rather then run them
$pretend = FALSE;
/////////////////////////////////////////////////////////////////////
$table_list = db_query("SHOW TABLES");
$prefix = strtolower($prefix);
foreach ($table_list as $r) {
$r = (array)$r;
$table_old = strtolower(current($r));
// check for $prefix on this table
if(substr($table_old,0,strlen($prefix)) == $prefix) {
$table_new = substr($table_old, strlen($prefix));
// first drop $table_new incase it already exists
$clean_sql = "DROP TABLE IF EXISTS $table_new";
// rename prefix table to standard/nonprefix name
$rename_sql = "RENAME TABLE $table_old TO $table_new";
if($pretend) {
print $clean_sql."\n";
print $rename_sql."\n";
} else {
if(!db_query($clean_sql)) {
die("Aborting - $clean_sql \n");
}
if(!db_query($rename_sql)) {
die("Aborting - $rename_sql \n");
}
}
} else {
print "$table_old skipped \n";
}
}
print "\nDone \n\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment