I'm extending Table Wizard to support exposing tables in external databases to Views (see #402968). Since Table Wizard depends on the schema module's inspection feature to analyze tables, I've extended it to handle this - patch attached. Basically, the inspect hooks now inspect the active database rather than always using the default database; it's up to the caller to call db_set_active first.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

moshe weitzman’s picture

Could you elaborate on what the code below means? Maybe use the variable name $prior_db_name? It looks like you are just changing to 'default' and then reverting to whatever you had before.


// Get the current active db name
$dbname = db_set_active('default');
db_set_active($dbname);

mikeryan’s picture

FileSize
1.83 KB

Added clarification

webchick’s picture

Wow, that's so ghetto that there's no other way to get the current DB connection's name. :\ I can confirm though; I tried global $db_active, $db_url, etc. Nothing works. I really hope this is better in D7.

Comments:

  1. Small terminology nit-pick. These are not /necessarily/ db names. Settings.php calls them 'connections', so we should use the same word here.
  2. Also, we don't smoosh together words in variable names (dbname). Should be $active_db_connection.
  3. (minor) You've added trailing whitespace.
  4. Given that this is the biggest DrupalWTF I've seen in awhile, we really need to comment this well so that people understand what's going on here and don't try to "fix" it later.

Updated patch that attempts to take these things into account. The code seems like a sensible addition to me, though, and I can confirm it fixes #402968: Read from external DB?.

webchick’s picture

Attempt #2.

This just calls db_set_active() without the 'default' parameter (since that's the default), which makes the code a bit easier to understand. Also tweaked the comments accordingly.

alex_b’s picture

Status: Needs review » Reviewed & tested by the community

I am using this patch with table wizard and it works flawless. Did not test with PostGreSQL but the changes aren't PostGreSQL specific.

Shai’s picture

#5 marked this as, "reviewed & tested by the community". Which of these patches is it referring to? The original, webchick's from #4?, Seems like webchick's improvements are improvements... but what is the process here?

Shai

mikeryan’s picture

Status: Reviewed & tested by the community » Fixed

I've committed webchick's patch.

Status: Fixed » Closed (fixed)
Issue tags: -DrupalWTF

Automatically closed -- issue fixed for 2 weeks with no activity.