Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove Magento table prefix script with dry-run/test
<?php
$database_host = "localhost";
$database_user = "username";
$database_password = "password";
$magento_database = "databasename";
$table_prefix = "prefixhere_";
$dryrun = true; // change to false when you want to commit changes
$db = mysql_connect($database_host, $database_user, $database_password);
mysql_select_db($magento_database);
$query = "SHOW TABLES";
$result = mysql_query($query) or die('### Error cannot connect to DB');
if ($dryrun) { echo "### No changes made, just testing <br />"; }
while($row = mysql_fetch_array($result)) {
$old_table = $row[0];
if ($new_table = preg_replace('/^'.$table_prefix.'/', '', $old_table)) {
if ($new_table != $old_table ) {
echo "Rename: " . $old_table . ", to: ". $new_table;
if (!$dryrun) {
$query = "RENAME TABLE `$old_table` TO `$new_table`";
mysql_query($query);
echo " -> DONE <br />";
} else { echo " -> Just checking <br />"; }
} else {
echo "Nothing to rename: " . $old_table . "<br />";
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.