Magento Sample Data Prefix Script

There is now a new version 1.6 of Magento Sample Data available on . Older sample data version was outdated and didn’t always work as intended, so we were happy to discover that a new version came out.

Unfortunately, sample data doesn’t support Magento table prefixes, so if you have multiple Magento installations in one database, you won’t be able to use the sample data out of the box.

We’ve created a simple script that modifies sample data SQL dump and adds a prefix to all table operations to get around this issue.

<?php

// your prefix here
$prefix = 'mage1610_';

// magento sample data file
$dumpFile = "magento-sample-data-1.6.1.0/magento_sample_data_for_1.6.1.0.sql";

// new sample data file
$outFile = "new-sample-data.sql";

$search = array("TABLES `", "TABLE `", "EXISTS `", "INTO `", "REFERENCES `");
$replace = array("TABLES `$prefix", "TABLE `$prefix", "EXISTS `$prefix", "INTO `$prefix", "REFERENCES `$prefix");

$file = file_get_contents($dumpFile);

if ($file !== FALSE) {
$file = str_ireplace($search, $replace, $file);
if (file_put_contents($outFile,$file) !== FALSE) {
echo "Successfully written new sample data dump to $outFile.n";
} else {
echo "Error writing new sample data dump.n";
}
} else {
echo "Error reading dump file.n";
}

?>

You can also download the script here.

Adjust the $prefix, $dumpFile and $outFile variables and run the script. Then check the new SQL dump file – it should now have all table names prefixed with your new prefix. You can now proceed with sample data installation according to Magento Sample Data guide.

Just tested the script on my new 1.6.1.0 installation and it worked perfectly. Let us now if it works for other versions too.

UPD: Added REFERENCES to search/replace array.

Share on Facebook1Tweet about this on Twitter0Share on LinkedIn0Share on Google+0Share on Reddit0Share on StumbleUpon0
  • Pingback: 易不难官方博客 » Blog Archive » 搭建magento系统

  • Pingback: magento checkout

  • AJ

    Hi, exactly what I have been looking for. but how do I execute the code.
    It would be easier if i could run it from the root directory of the server, or through phpmyadmin

    Thanks

    • Martin Boze

      You can execute it in any way you like, either directly from console or through the web server, you only need to adjust $prefix, $dumpFile and $outFile variables first.