[drupal-devel] Detecting table existence/absence in a Drupally-correct way
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@4th.com Drupal projects: http://drupal.org/project/user/9184 Sandbox: http://cvs.drupal.org/viewcvs/drupal/contributions/sandbox/syscrusher
On Wed, 13 Apr 2005, Syscrusher wrote:
Good evening!
Goood morning!
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.
You could do "CREATE TABLE IF NOT EXISTS foo ...." and take out the user intervention completely. sed -e 's/CREATE TABLE \([a-z0-9_]*\)/CREATE TABLE IF NOT EXISTS \1/' \ < database.mysql > database-if-exists.mysql should create a proper Drupal db file with that clause for all tables. Cheers, Gerhard
Good evening!
Goood morning!
Good afternoon!
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.
You could do "CREATE TABLE IF NOT EXISTS foo ...." and take out the user intervention completely.
Or issue a SHOW TABLES command if you need a list of available tables in a particular database. Goba
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 14 Apr 2005, at 3:11 PM, Gabor Hojtsy wrote:
Or issue a SHOW TABLES command if you need a list of available tables in a particular database.
Don't use mysql only code. Atleast abstract it into a seperate db_* function. - -- Adrian Rossouw Drupal developer and Bryght Guy http://drupal.org | http://bryght.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFCXnCz6lSw9JdoA/wRAuyOAKCOtsRqfMFAd8mBgdSkucbbqbfhAwCgvnZP SZAHMs3F+7yl8RL26kMaBNc= =hxmu -----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I wrote db_table_exists and a db_column_exists functions recently for the install system, if you want them ? What are you using it for ? On 14 Apr 2005, at 4:39 AM, Gerhard Killesreiter wrote:
On Wed, 13 Apr 2005, Syscrusher wrote:
Good evening!
Goood morning!
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.
You could do "CREATE TABLE IF NOT EXISTS foo ...." and take out the user intervention completely.
sed -e 's/CREATE TABLE \([a-z0-9_]*\)/CREATE TABLE IF NOT EXISTS \1/' \ < database.mysql > database-if-exists.mysql
should create a proper Drupal db file with that clause for all tables.
Cheers, Gerhard
- -- Adrian Rossouw Drupal developer and Bryght Guy http://drupal.org | http://bryght.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFCXnB36lSw9JdoA/wRAssmAKCBnejyEqhyo+YBDYSEpgxc3JEkrQCg3Usr ogQLI49esF5pMK8Y5I6iRoQ= =DYlU -----END PGP SIGNATURE-----
On Thursday 14 April 2005 09:30, Adrian Rossouw wrote:
I wrote db_table_exists and a db_column_exists functions recently for the install system, if you want them ?
What are you using it for ?
I am creating a migration feature for images (in cooperation with Walkah), and I need to detect whether the old-style image table is present. If it is, then I look to see if it has rows, and if so, then a migration makes sense. If the table is missing or empty, then I will disable the migration feature and (if the user goes to that URL anyway) tell them that it's not applicable to their situation. In answer to your original question, though, yes, I'd very much like to borrow that code. I can put a "function_exists()" test in my code so that mine doesn't break if yours already defines the functions. I'll put these functions into a separate include file, and just not include it if they aren't needed. Scott -- ------------------------------------------------------------------------------- Scott Courtney Drupal user name: "syscrusher" http://drupal.org/user/9184 scott@4th.com Drupal projects: http://drupal.org/project/user/9184 Sandbox: http://cvs.drupal.org/viewcvs/drupal/contributions/sandbox/syscrusher
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 14 Apr 2005, at 5:52 PM, Syscrusher wrote:
In answer to your original question, though, yes, I'd very much like to borrow that code. I can put a "function_exists()" test in my code so that mine doesn't break if yours already defines the functions. I'll put these functions into a separate include file, and just not include it if they aren't needed.
I've migrated away from using it at all, but i'm sure i can track it down in my mail archives. - -- Adrian Rossouw Drupal developer and Bryght Guy http://drupal.org | http://bryght.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFCXqVX6lSw9JdoA/wRApp/AJ9gYjZaFqQCte3087EFhU1G7O2QAgCfce28 thkHsrJw0pa+oF1hhVH2WN4= =UejU -----END PGP SIGNATURE-----
On 13 Apr, 2005, at 22:28, Syscrusher wrote:
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).
The only way would probably be to use set_error_handler() to set a new error handler (that just ignores the error) and then restore Drupal's with restore_error_handler().
On Thursday 14 April 2005 08:38, Jim Riggs wrote:
The only way would probably be to use set_error_handler() to set a new error handler (that just ignores the error) and then restore Drupal's with restore_error_handler().
Good idea! I hadn't thought of that -- I'll try it. Thanks! -- ------------------------------------------------------------------------------- Scott Courtney Drupal user name: "syscrusher" http://drupal.org/user/9184 scott@4th.com Drupal projects: http://drupal.org/project/user/9184 Sandbox: http://cvs.drupal.org/viewcvs/drupal/contributions/sandbox/syscrusher
participants (5)
-
Adrian Rossouw -
Gabor Hojtsy -
Gerhard Killesreiter -
Jim Riggs -
Syscrusher