while updating one of my modules to drupal 6, i noticed a slight regression due to the schema API -- i have no way to get feedback about the queries being run on install/uninstall. in drupal 5 i could just $q1 = db_query('blah') to get that info.

i use this info to print a 'success' or 'failed' message related to the module install or uninstall.

what do you think about returning $ret from drupal_install_schema() and drupal_uninstall_schema()? should be able to inspect that to get the necessary info.

Comments

bjaspan’s picture

I think you meant $ret = update_sql($query) not $ret = db_query($query). db_query never returned the 'success' or 'failed' thing.

All Schema API functions generate the update_sql() output array and, in fact, drupal_install_schema() and drupal_uninstall_schema() actually collect them (they have to since the API functions require the argument). They do not yet return anything, so it is easy enough to return them.

Patch attached.

bjaspan’s picture

Status: Active » Needs review
hunmonk’s picture

Title: Regression: can't get query results on install/uninstall » Regression: install/uninstall deficiencies in Schema API
StatusFileSize
new1.74 KB

couple of corrections:

  1. $ret is actually an array of arrays, so i corrected the doxygen appropriately
  2. we really should be testing for the existence of a table before we drop it on uninstall, so i've added that. normally i'd roll another patch for that, but it's a trivial change which affects the very same code. tested it and it works perfectly.

FYI, here's how i'm using this:

function asterisk_install() {
  $ret = drupal_install_schema('asterisk');

  $failed = array();
  foreach ($ret as $query) {
  	if (!$query['success']) {
  	  $failed[] = $query['query'];
  	}
  }
  if (empty($failed)) {
    drupal_set_message(t('Asterisk module installed successfully.'));
  }
  else {
    drupal_set_message(t('Table installation for the Asterisk module was unsuccessful. The following queries failed: !queries', array('!queries' => theme('item_list', $failed))), 'error');
  }
}

this allows me to give the user a bit of intelligent output about the module installation.

actually, in the old days, i would just do $q1 = db_query('blah'), as i indicated above, which would at least give me a TRUE/FALSE on the query. AFAICT, we always used db_query(), not update_sql(), for initial install queries, even in core.

bjaspan’s picture

You say "We really should be testing for the existence of a table before we drop it on uninstall" but I'm not convinced. We don't check if a table exists before creating it, and we don't check if the schema matches our expectations before we alter it. Our entire schema maintenance system is built on assuming things are the way we left them. I don't see much harm in checking if a table exists, and I'm not concerned about the extra query. On the other hand, avoiding a "DROP TABLE failed because table does not exist" error is also not that important and, I'd even say, getting such an error when it should not occur provides useful information to the administrator. So, I'd vote for not checking before db_drop_table().

Note that "assuming things are the way we left them" is absolutely on the table for discussion for D7. Schema.module already knows how to inspect the database and compare it to hook_schema results. This can lead us towards automatic schema updates instead of hand-written hook_update_n() functions, at least for many common cases. However, I'd still say that we can/should assume that any table in hook_schema() exists and if it doesn't report an error.

Incidentally, making hook_install() unnecessary for modules that do nothing but drupal_install_schema() is also on the table for D7. The central hook_install replacement should probably report on failed table creation as you are doing in asterisk.module; there is no reason for that code to be duplicated among many modules.

hunmonk’s picture

StatusFileSize
new1.62 KB

good points all the way around. given the larger current system we've got, it's probabaly smart to wait on the db_table_exists() change.

attached removes that, and simply corrects the code comments from your original patch.

hunmonk’s picture

Title: Regression: install/uninstall deficiencies in Schema API » Regression: can't get query results on install/uninstall

back to original title :)

hunmonk’s picture

Status: Needs review » Reviewed & tested by the community

i've tested this patch -- it works, and it's a simple fix that provides install/uninstall hooks with useful information.

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Great, thanks, committed.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

pancho’s picture

Just a notice: a similar patch by Rob Loach has been declined in October 2007 (see #187121)