just like we've got db_table_exists(), a handy function (especially useful in .install files) to see if a given table already exists in the DB, how about adding support for a sister function to tell you if a certain column exists in a given table? i needed this in signup.install for something, so i figured out how to do it on both MySQL and PgSQL. i figured i'd share. ;) it's a 1-line function in both cases, so it's not going to bloat core that much, and i think i'd be nice to have. if you want to easily test this function on both DB backends, apply the attached patch, then create a php-filtered node on your test site with the following:
$columns = array(
'users' => 'uid',
'userz' => 'uid',
'node' => 'nidz',
);
print '<ul>';
foreach ($columns as $table => $column) {
if (db_table_exists($table)) {
if (db_column_exists($table, $column)) {
print '<li>'. t('Found %column in {@table}', array('%column' => $column, '@table' => $table)) .'</li>';
}
else {
print '<li>'. t('Table {@table} exists, but does not contain %column', array('%column' => $column, '@table' => $table)) .'</li>';
}
}
else {
print '<li>'. t('Table {@table} does not exist.', array('@table' => $table)) .'</li>';
}
}
print '</ul>';
i get the following on both pgsql and mysql on a fresh HEAD install:
- Found uid in {users}
- Table {userz} does not exist.
- Table {node} exists, but does not contain nidz
let me know if this needs any changes before it's RTBC.
thanks,
-derek
p.s. no, i didn't bloat db_column_exists() to ensure the table already exists, i figured that's the caller's problem. on pgsql, the attached version just returns false if you give it a bogus table. on mysql you get an error. i *could* wrap the mysql implementation in a db_table_exists() if we really wanted, but that seems wrong.
Comment | File | Size | Author |
---|---|---|---|
db_column_exists.patch.txt | 2.68 KB | dww |
Comments
Comment #1
dwwoh, i forgot to mention: thanks to Br3nda from IRC for initial pointers on getting this right in PgSQL.
Comment #2
Shiny CreditAttribution: Shiny commentedYou're welcome.
Comment #3
chx CreditAttribution: chx commentedAre you sure this needs database specific, introspection methods? What about:
Comment #4
chx CreditAttribution: chx commentedProblem with my approach: empty tables.
Comment #5
Dries CreditAttribution: Dries commentedWorks for me! :)
Comment #6
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks dww.
Comment #7
dwwadded a mention to the upgrade docs: http://drupal.org/node/114774#db-column-exists
thanks for committing the change. ;)
Comment #8
(not verified) CreditAttribution: commented