Edmonds Commerce Logo
  • home
    • blog
  • ecommerce
    • product catalogue
    • order processing
    • customer services
    • stock control
    • human resources
    • management information
  • development
    • oscommerce
    • php
    • mysql
    • open source
    • performance tuning
  • design
  • marketing
  • contact us
    • pricing

Edmonds Commerce Blog

Freelance PHP Ecommerce and SEO Developer in the UK

Latest Posts

Magento Most Popular Extensions
Google Chrome for Linux Beta
Firefox Address Bar Lag + Solution
Custom Buttons for Firefox

Most Popular Posts

Magento Developer UK Freelance osCommerce UK Magento Training CRE Loaded UK

Mysql Database Migration / Synchronisation Script

June 24th, 2009
Read More mysql

This is a nice little script I just knocked together that helps to synchronise databases when the table structures might not be exactly the same (for example different versions of the same system).

You would need to edit the top section to put the correct DB credentials etc.

This is definitely not to be used on a live DB, purely for aiding development and migration.

PLAIN TEXT
PHP:
  1. <?php
  2. /** Database Synchronisation / Migration Tool
  3. *
  4. * For synching up an old and a new DB schema.
  5. *
  6. * @author EdmondsCommerce.co.uk
  7. *
  8. */
  9.  
  10. //do you want the system to empty the new table before inserting data from the old table?
  11. $truncate=true;
  12.  
  13. /**
  14. * DB Server Credentials - Needs to have full access to both databases
  15. */
  16. $dbHost='localhost';
  17. $dbUser='root';
  18. $dbPass='password';
  19.  
  20. /** Tables to Synch
  21. */
  22. $tablesToSynch = array(
  23.     'categories',
  24.     'categories_description',
  25.     'products',
  26.     'products_description',
  27.     'products_to_categories',
  28.     'manufacturers',
  29. );
  30.  
  31. $dbOld = 'dbold';
  32.  
  33. $dbNew = 'dbnew';
  34.  
  35.  
  36. /*********** CODE BELOW HERE - NO NEED TO EDIT UNLESS YOU WANT TO ***********/
  37. $_conn = mysql_connect($dbHost,$dbUser,$dbPass) or die('DB Connection Failed');
  38.  
  39. $dbOldTables= fetch_tables($dbOld);
  40.  
  41. $dbNewTables = fetch_tables($dbNew);
  42.  
  43.  
  44. foreach($tablesToSynch as $table){
  45.  
  46.     echo '<h3>Doing '.$table.'</h3>';
  47.     //check for common tables
  48.     if(in_array($table, $dbOldTables) && in_array($table, $dbNewTables)){
  49.         //now get column data
  50.         $dbOldTableCols = fetch_columns($dbOld,$table);
  51.  
  52.         $dbNewTableCols = fetch_columns($dbNew,$table);
  53.  
  54.         //now for the column comparison
  55.         $cols=array_intersect($dbOldTableCols, $dbNewTableCols);
  56.  
  57.         //now emptying the new DB if set to do so
  58.         if($truncate){
  59.             db_query("TRUNCATE $dbNew.$table");
  60.         }
  61.  
  62.         //copy old table to new DB so we can copy columns
  63.         $tempTable=copy_table($dbOld, $dbNew, $table);
  64.  
  65.         //now build SQL and run
  66.         $sql = "insert into $dbNew.$table (" . implode(', ',$cols) . ") select " . implode(', ',$cols) . " from $dbNew.$tempTable";
  67.         db_query($sql);
  68.  
  69.         //now drop temp table
  70.         db_query("DROP TABLE $dbNew.$tempTable");
  71.     }
  72.    
  73. }
  74.  
  75. /****** FUNCTIONS ********/
  76.  
  77. function db_query($query){
  78. $output = mysql_query($query) or die('
  79. <h1 style="color: red">Uh Oh......MySQL Error:</h1>
  80. <h3>Query:</h3>
  81. <pre>' . htmlentities($query) . '</pre>
  82. <h3>MySQL Error:</h3>
  83. ' . mysql_error() . '
  84. <hr /> <hr />');    return $output;
  85. }
  86.  
  87.  
  88. function fetch_tables($dbname){
  89.     $query=db_query("show tables from $dbname");
  90.     while($r=mysql_fetch_assoc($query)){
  91.         $return[]=$r["Tables_in_$dbname"];
  92.     }
  93.     return $return;
  94. }
  95.  
  96. function fetch_columns($dbname, $table){
  97.     $query = db_query("SHOW COLUMNS from $dbname.$table");
  98.     while($r= mysql_fetch_assoc($query)){
  99.         $return[]=$r['Field'];
  100.     }
  101.     return $return;
  102. }
  103.  
  104. function copy_table($fromDb, $toDb, $table){
  105.     db_query("DROP TABLE IF EXISTS $toDb.temp_$table");
  106.     db_query("CREATE TABLE $toDb.temp_$table LIKE $fromDb.$table");
  107.     db_query("ALTER TABLE $toDb.temp_$table DISABLE KEYS");
  108.     db_query("INSERT INTO $toDb.temp_$table SELECT * FROM $fromDb.$table");
  109.     db_query("ALTER TABLE $toDb.temp_$table ENABLE KEYS");
  110.     return "temp_$table";
  111. }

Possibly Relevant Posts:

  • Check if MySQL Table Exists
  • Building Spiders: Grab Data, Post Forms and Interact with Web Sites Automatically
  • PHP Into Compiled C++ – Hip Hop by Facebook
  • Zend Framework UK
  • Symfony Developer UK

Feed | Respond | Trackback

Leave a Reply

  • RSS Feed
  • Categories

    • adwords
    • apache
    • barcode
    • business
    • creloaded
    • css
    • curl
    • customer services
    • debugging
    • drupal
    • eclipse
    • ecommerce
    • edmondscommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • git
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • jquery
    • link building
    • linux
    • mac
    • magento
    • management
    • misc
    • mod_rewrite
    • mysql
    • netbeans
    • open suse
    • oscommerce
    • php
    • plesk
    • portfolio
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • ria
    • scraping
    • search engine optimisation
    • security
    • seo
    • spidering
    • symfony
    • twitter
    • ubuntu
    • Uncategorized
    • usability
    • vps
    • web design
    • web development
    • Windows
    • xampp
    • zend framework
    • zip
  • Archives

    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    bulk course cre loaded creloaded css custom developer development directories drupal error find firefox git google hosts file html jaunty javascript leeds links linux magento mysql netbeans oscommerce performance php plesk ppc problem replace search engine optimisation seo server symfony table training ubuntu uk virtualbox web web design xml zend framework
  • Random Posts

    • Check if MySQL Table Exists
    • Basic Server Migration Using SSH + SCP
    • Use Grep To Find Text In Folders
    • Zend Paginator - Actually Pretty Painless :)
    • mod_rewrite Strip Query String
    • Google Analytics Scan
    • Zend Framework Template Renamer Script
    • Jaunty Released
    • PHP MySQL Query Function with Easy Error Message
    • Ultimate osCommerce Checkout - Fast and Friendly
  • Recent Comments

    • admin on Magento Backup Error Filesystem.php on line 234 + Solution
    • admin on Magento Leeds
    • Matthew Dolley on Magento Leeds
    • kash on PHP Email Attachment Function
    • Hussein on PHP Save Images Using cURL
  • Category Specific RSS

    • adwords Feed for all posts filed under adwords
    • apache Feed for all posts filed under apache
    • barcode Feed for all posts filed under barcode
    • business Feed for all posts filed under business
    • creloaded Feed for all posts filed under creloaded
    • css Feed for all posts filed under css
    • curl Feed for all posts filed under curl
    • customer services Feed for all posts filed under customer services
    • debugging Feed for all posts filed under debugging
    • drupal Feed for all posts filed under drupal
    • eclipse Feed for all posts filed under eclipse
    • ecommerce Feed for all posts filed under ecommerce
    • edmondscommerce Feed for all posts filed under edmondscommerce
    • email Feed for all posts filed under email
    • excel Feed for all posts filed under excel
    • firefox Feed for all posts filed under firefox
    • flash Feed for all posts filed under flash
    • gd Feed for all posts filed under gd
    • git Feed for all posts filed under git
    • graphs Feed for all posts filed under graphs
    • hosting Feed for all posts filed under hosting
    • icecat Feed for all posts filed under icecat
    • internet news Feed for all posts filed under internet news
    • javascript Feed for all posts filed under javascript
    • jquery Feed for all posts filed under jquery
    • link building Feed for all posts filed under link building
    • linux Feed for all posts filed under linux
    • mac Feed for all posts filed under mac
    • magento Feed for all posts filed under magento
    • management Feed for all posts filed under management
    • misc Feed for all posts filed under misc
    • mod_rewrite Feed for all posts filed under mod_rewrite
    • mysql Feed for all posts filed under mysql
    • netbeans Feed for all posts filed under netbeans
    • open suse Feed for all posts filed under open suse
    • oscommerce Feed for all posts filed under oscommerce
    • php Feed for all posts filed under php
    • plesk Feed for all posts filed under plesk
    • portfolio Feed for all posts filed under portfolio
    • product catalogue Feed for all posts filed under product catalogue
    • product feed Feed for all posts filed under product feed
    • programming Feed for all posts filed under programming
    • regular expressions Feed for all posts filed under regular expressions
    • ria Feed for all posts filed under ria
    • scraping Feed for all posts filed under scraping
    • search engine optimisation Feed for all posts filed under search engine optimisation
    • security Feed for all posts filed under security
    • seo Feed for all posts filed under seo
    • spidering Feed for all posts filed under spidering
    • symfony Feed for all posts filed under symfony
    • twitter Feed for all posts filed under twitter
    • ubuntu Feed for all posts filed under ubuntu
    • Uncategorized Feed for all posts filed under Uncategorized
    • usability Feed for all posts filed under usability
    • vps Feed for all posts filed under vps
    • web design Feed for all posts filed under web design
    • web development Feed for all posts filed under web development
    • Windows Feed for all posts filed under Windows
    • xampp Feed for all posts filed under xampp
    • zend framework Feed for all posts filed under zend framework
    • zip Feed for all posts filed under zip

Edmonds Commerce related questions? Send us a message or call us on 0844 357 0201.

Freelance PHP Web Design UK Commercial Web Design