Title: Patch for HyperDB
Last modified: August 22, 2016

---

# Patch for HyperDB

 *  Resolved [Toni Ginard](https://wordpress.org/support/users/toniginard/)
 * (@toniginard)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/patch-for-hyperdb/)
 * Multisite Clone Duplicator 1.1 does not work when using the extension HyperDB
   because it does not take into account that the blogs can be in different databases.
 * I have written some changes to get it working when there are several databases
   in the same server. The changes are for file lib/data.php and are as follows:
 * 1.- Change this line:
    `$sql_query = $wpdb->prepare('SHOW TABLES LIKE %s',$from_site_prefix.'%');`
   by this line: `$sql_query = $wpdb->prepare('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.
   TABLES WHERE TABLE_NAME LIKE %s', $from_site_prefix . '%');`
 * 2.- Add the following code after line 56:
 *     ```
       $sql_query = $wpdb->prepare('SELECT TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = %s', $table);
       $schema = MUCD_Data::do_sql_query($sql_query, 'var');
       ```
   
 * 3.- Change this line:
    `MUCD_Data::do_sql_query('CREATE TABLE IF NOT EXISTS '.
   $table_name . ' LIKE ' . $table);` by this line: `MUCD_Data::do_sql_query('CREATE
   TABLE IF NOT EXISTS ' . $table_name . ' LIKE ' . $schema . '.' . $table);`
 * 4.- And, finally, change this line:
    `MUCD_Data::do_sql_query('INSERT ' . $table_name.'
   SELECT * FROM ' . $table);` by this line: `MUCD_Data::do_sql_query('INSERT ' .
   $table_name . ' SELECT * FROM ' . $schema . '.' . $table);`
 * Hope this may help to somebody! 🙂
 * Toni

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [Pierre Dargham](https://wordpress.org/support/users/pdargham/)
 * (@pdargham)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/patch-for-hyperdb/#post-5572470)
 * Hi !
 * Thank you for the patch !
 * I just tried it and it makes sense. We will run a few more tests and it will 
   available in the next version of the plugin (probably within two weeks).
 * Best regards,
 * Pierre D.
 *  Thread Starter [Toni Ginard](https://wordpress.org/support/users/toniginard/)
 * (@toniginard)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/patch-for-hyperdb/#post-5572471)
 * Hi Pierre,
 * Two thoughts about HyperDB:
 * HyperDB supports using several database servers. This patch doesn’t address that.
   To add support for that case would require a lot of changes and, as long as I
   don’t need it, I haven’t worked on it.
 * So this patch adds partial support for HyperDB: when there are several databases
   in the **same** database server.
 * Best wishes,
 * Toni
 *  Plugin Author [Pierre Dargham](https://wordpress.org/support/users/pdargham/)
 * (@pdargham)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/patch-for-hyperdb/#post-5572517)
 * Hi Toni,
 * I just released the new version, with several bugfixs, and your patch.
 * Thank you for the thoughts. I was aware of that, and as soon a I can, I will 
   try to work on HyperDB and understand how exactly multi-db is working. If you
   work on it, feel free to help us !
 * I think we could at least come up with well thinked hooks that would permit to
   do the appropriate actions to change the target database when needed.
 * But you’re right, a complete support for HyperDB multi-database mode would require
   a lot of changes, maybe on the GUI too (something to choose the target DB when
   duplicating a site, if HyperDB is active ?).
 * Best regards,
 * Pierre
 *  Thread Starter [Toni Ginard](https://wordpress.org/support/users/toniginard/)
 * (@toniginard)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/patch-for-hyperdb/#post-5572526)
 * Hi Pierre,
 * Sorry for the late response. I really don’t have time to work on this…
 * I tried to understand how does HyperDB works, but I was unsuccessful. I even 
   tried to examine the binary log of MySQL to reproduce the steps that it does,
   but I suppose that not everything is logged.
 * HyperDB introduces a transparent layer in WordPress that “magically” connects
   to the correct database when someone accesses a blog. Of course it’s not magic,
   but that’s the point: find out the algorism in the HyperDB code, which is not
   so simple as it may appear. I couldn’t find it. 🙁
 * One possible approach is as described above: do the same that HyperDB does. But
   another possibility to explore is allow HyperDB to do the work:
 * HyperDB defines a class called hyperdb that extends wpdb. After that, instead
   of using the original wpdb, it creates an instance of the extended class, with
   the same name, that replaces the original. From that moment on, WordPress works
   with wpdb as usual and everything is fine.
 * So, the second approach could be to behave as WordPress and let HyperDB connect
   to the correct server and database. I must say that I don’t know how to do it.
   I’m even not sure that it is possible! 🙂
 * Best wishes,
 * Toni
 *  Plugin Author [Pierre Dargham](https://wordpress.org/support/users/pdargham/)
 * (@pdargham)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/patch-for-hyperdb/#post-5572533)
 * Hi Toni !
 * I think the patch for HyperDB caused a few problems, and at the end, I removed
   it for release 1.3.0
 * The line that selected the table schema was wrong because it could select any
   schema with same tables than the one we want to copy :
 *     ```
       $sql_query = $wpdb->prepare('SELECT TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = \'%s\'', $table);
       $schema = MUCD_Data::do_sql_query($sql_query, 'var');
       ```
   
 * In a DB with several WP installations and same prefixes, it caused the plugin
   to duplicate wrong sites.
 * I failed to find how to fix this bug and keep HyperDB compatibility, this is 
   why i removed it for now.
 * If you have time to work on a solution, I’ll be glad to get back HyperDB partial
   compatibility 🙂
 * Feel free to fork the [plugin on github](https://github.com/pierre-dargham/multisite-clone-duplicator).
   We could create a branch for HyperDB compatibility issues.
 * Bests regards,
 * Pierre
 *  Thread Starter [Toni Ginard](https://wordpress.org/support/users/toniginard/)
 * (@toniginard)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/patch-for-hyperdb/#post-5572534)
 * Hi Pierre,
 * Yes, you’re right: In the information schema there are references to all the 
   tables in the server and it is a problem in some cases.
 * I had a look at it and I think we can take advantage of the fact that the latest
   blog accessed before the copy is the original blog, so we can get the data base
   this way:
 * $schema = $wpdb->last_used_server[‘name’];
 * So you can change this line:
 * $sql_query = $wpdb->prepare(‘SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
   WHERE TABLE_NAME LIKE \’%s\”, $like . ‘%’);
 * by this two:
 * $schema = $wpdb->last_used_server[‘name’];
    $sql_query = $wpdb->prepare(‘SELECT
   TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE \’%s\’ AND TABLE_SCHEMA
   = \’%s\”, $like . ‘%’, $schema);
 * A consequence of that is that you can remove the lines 63 and 64, because we 
   already know the data base:
 * // $sql_query = $wpdb->prepare(‘SELECT TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES
   WHERE TABLE_NAME = \’%s\”, $table);
    // $schema = MUCD_Data::do_sql_query($sql_query,‘
   var’);
 * Those changes worked for me in my development environment, using HyperDB, but
   the code should be tested without HyperDB. Anyway, it should work because it 
   doesn’t rely on HyperDB but in WordPress itself.
 * All the best,
 * Toni

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Patch for HyperDB’ is closed to new replies.

 * ![](https://ps.w.org/multisite-clone-duplicator/assets/icon-256x256.png?rev=1011466)
 * [MultiSite Clone Duplicator](https://wordpress.org/plugins/multisite-clone-duplicator/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/multisite-clone-duplicator/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/multisite-clone-duplicator/)
 * [Active Topics](https://wordpress.org/support/plugin/multisite-clone-duplicator/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/multisite-clone-duplicator/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/multisite-clone-duplicator/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [Toni Ginard](https://wordpress.org/support/users/toniginard/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/patch-for-hyperdb/#post-5572534)
 * Status: resolved