error when running 6003 update: Call to undefined function flag_get_flags()

nadavoid - March 13, 2009 - 00:21
Project:Flag
Version:6.x-1.0-rc1
Component:Flag core
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed
Description

When upgrading flag module on my site, I get a big ugly error:

An error occurred. http://mysite/update.php?id=12&op=do
Fatal error: Call to undefined function flag_get_flags() in /serverside/path/sites/all/modules/flag/flag.install on line 419

I tried several times and kept getting the error, and the upgrade would fail. To fix this, I added
drupal_load('module', 'flag');
just before
$flags = flag_get_flags();
in function flag_update_6003(), line 419, in flag.install.

Doing that allowed the update to succeed.
I didn't see any other support request regarding this error, so it's possible this is an issue only in my specific setup. In any case, would you consider adding this line, in case it helps others avoid this error?

Here is the function with updated code:

/**
* Remove the previous default views that are no longer bundled with Flag.
*
* These views are saved to the database so that they are preserved.
*/
function flag_update_6003() {
  $ret = array();
 
  drupal_load('module', 'flag');
  $flags = flag_get_flags();
  foreach ($flags as $name => $flag) {
    if ($view = views_get_view('flags_'. $name)) {
      if (!$view->disabled && $view->type == t('Default')) {
        $view->save();
        $ret[] = array('success' => TRUE, 'query' => t('The view %name as been saved to the database. Flag no longer provides this view by default.', array('%name' => $view->name)));
      }
    }
  }

  return $ret;
}

#1

quicksketch - March 13, 2009 - 01:44

I'm still not quite sure when .module files are available in update functions. Did you have Flag enabled when you were running the update?

#2

nadavoid - March 13, 2009 - 03:28

It was at first, but it's slightly possible that I didn't when actually running update.php. At one point, I ran update.php and the new flag files may not have been present on the server, and this may have had the effect of uninstalling it.

Running drupal_load() in the update function should be safe, since it includes the .module only if it is not yet loaded.

#3

quicksketch - March 14, 2009 - 05:58
Status:active» fixed

Well either way it's not going hurt adding it in there. Committed to both D5 and D6. Thanks!

#4

mooffie - March 16, 2009 - 22:52
Status:fixed» active

Committed to both D5 and D6.

For D5, the line drupal_load('module', 'flag') won't load 'flag.inc'. The .inc file should be loaded explicitly. The reason is explained in a comment in D5's flag_install().

Well either way it's not going hurt adding it in there.

By adding this line you're enslaving yourself. It's an obligation to do the same in every future update hook that uses the API, and not only in the Flag module but in every module you develop.

update.php seems to do full bootstrap (it does drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL)) so I don't see why explicitly loading the module (or flag.inc) is necessary.

On the other hand, I've just looked into CCK's update hooks and I see that they do have drupal_load('module', MODULE-NAME). It seems to weaken my point. However, Organic Groups's update hooks don't have this line, so perhaps CCK too can do without it. It's worth investigating this.

(Sorry for writing so much about this really minor issue.)

#5

quicksketch - March 16, 2009 - 23:41

Mooffie, no worries. I've noticed in recent versions of Drupal 6 all .install files are loaded even for modules that aren't enabled. I think it must have been some kind of decision that it's necessary to update all modules that have ever been installed, even if they're not currently enabled. However, if the module isn't enabled then that means the .module file is missing of course. I'm not sure when this change was made though, it seems to be a recent change in Drupal 6 versions.

#6

quicksketch - September 14, 2009 - 12:07
Component:Code» Flag core
Status:active» closed

I'm closing this issue since I think module_load() is necessary for Drupal 6, since it loads *_update functions even if the module is disabled. Drupal 5 this isn't necessary since D5 does not load update functions when the module is disabled. So we probably added an unnecessary line to the D5 version, but I'm not going to worry about it as our support for D5 nears its end.

 
 

Drupal is a registered trademark of Dries Buytaert.