Wednesday, 13 July 2016

Magento Remove & Replace Database Table Prefix

Steps:

  1. Take a database backup.
  2. Create a File Named "filename.php" and placed at root directory.
  3. Paste the above Script in it.
  4. before run the file if $dryrun=true; it mins only check the rename process or $dryrun=false; it mins rename the table.
  5. All tables will be renamed.
  6. go to app/etc/local.xml <table_prefix><![CDATA[prefix]]></table_prefix>
  7. you're done.
<?php

$database_host      = "";
$database_user      = "";
$database_password  = "";
$magento_database   = "";
$table_prefix       = "ex_";
$dryrun =  true; // change to false when you want to commit changes
$link = mysqli_connect($database_host, $database_user, $database_password, $magento_database);
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$query = "SHOW TABLES";

$result = mysqli_query($link,$query) or die('### Error cannot connect to DB');
if ($dryrun) { echo "### No changes made, just testing <br />"; }
while($row = mysqli_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`";
mysqli_query($link,$query);
echo " -> DONE <br />";
} else { echo " -> Just checking <br />"; }
} else {
echo "Nothing to rename: " . $old_table  . "<br />";
}
    }
}
?>

1 comment: