[drupal-devel] Detecting table existence/absence in a Drupally-correct way

Syscrusher scott at 4th.com
Thu Apr 14 03:29:16 UTC 2005


Good evening!

Is there a well-behaved way to use Drupal's DB abstraction to find out if a
table exists? I'm working on a migration feature, and need to be able to
detect the existence of the table so that I can issue a user-friendly "you don't
need to do this step" message if the table isn't there.

The obvious thing to try was calling @db_query() to try to suppress the
error message. That does what I need, but puts the database error into the output
shown to the user. Since I know what is the specific situation, I want to
suppress that output for this call only, without overriding the site-wide
setting that causes errors to go only into the log. (Ideally, this particular
error doesn't belong in the log, either, since I essentially am generating
it on purpose).

It might be worth adding a new parameter to db_query() and db_queryd() in the
HEAD version to allow this sort of error suppression, in cases like mine where
the calling code wants to explicitly take responsibility for error handling.
And I know this isn't a trivial thing across databases, but..."Boy, it sure
would be nice if there was a way to ask Drupal for a list of names of all the
tables that exist in the database." :-)   I think I have some code lying about
that does this (though not in the Drupal environment) which worked with at
least PostgreSQL and MySQL and (if I recall) ODBC. If it's of interest to
someone, I'll try to dig it up and look at porting it into Drupal. Before I
put in that effort, though, I'd like to have some indication of whether or
not it was likely that kind of code would be accepted into core.

Comments/suggestions welcome.

Scott

-- 
-------------------------------------------------------------------------------
Scott Courtney     Drupal user name: "syscrusher"   http://drupal.org/user/9184
scott at 4th.com      Drupal projects: http://drupal.org/project/user/9184
Sandbox:  http://cvs.drupal.org/viewcvs/drupal/contributions/sandbox/syscrusher



More information about the drupal-devel mailing list