I was looking for an easy way to get views to pull select statements from a slave database with pressflow without having to hack the views module. I tried doing this through a hook_views_data_alter(), but that didn't seem to work.
I added another db connection to my settings.php file like so:
$db_url['default'] = "mysql://user:pass@masterdb/mydb;
$db_url['slave'] = "mysql://user:pass@slavedb/mydb;
$db_url_slave = "mysql://user:pass@slavedb/mydb;and then in a custom module I did this: (mymodule.views.inc)
/**
* Modify all views to query the Slave database
*/
function mymodule_views_data_alter(&$data) {
foreach($data as $table => $value) {
$data[$table]['table']['base']['database'] = 'slave';
}
}I set the module weight to 200, still with no luck. Views was functioning, but wasn't using the database connection that I created called $db_url['slave'].
I ended up just hacking views and changed any db_query and db_query_range to the respected pressflow db_query_slave and db_query_range_slave for any SELECT queries in the views.inc file.
I'm curious if anyone has come up with a cleaner solution of doing this. I have attached a patch for the views.inc file in case it benefits anyone.
| Attachment | Size |
|---|---|
| views_slave.patch_.txt | 2.78 KB |
Comments
I will try to test this soon
This looks good, I will try to test it soon. I will have to set up a pair of mysql replication servers to do it.
One strategy I will use in testing, is I will stop replication to simulate a very large lag in the slave.
Any news on this subject
Any news on this subject guys?
Right hook?
If that hook doesn't work one could try http://drupalcontrib.org/api/function/hook_views_query_alter/6
Not sure that is the right hook http://views-help.doc.logrus.com/help/views/api-tables
Here's an article on this
Here's an article on this very subject for you. :)
http://www.lullabot.com/articles/querying-slave-database-with-views