Skip to content

Instantly share code, notes, and snippets.

@pounard
Created May 29, 2015 14:36
Show Gist options
  • Save pounard/670042218adf719ded72 to your computer and use it in GitHub Desktop.
Save pounard/670042218adf719ded72 to your computer and use it in GitHub Desktop.
Restores all modules version to their latest number (useful if you did a wrong UPDATE in {system} table).
require_once DRUPAL_ROOT . '/includes/update.inc';
$modules = db_query("SELECT name, 1 FROM {system} WHERE type = 'module'")->fetchAllKeyed();
foreach (array_keys($modules) as $key) {
module_load_install($key);
$updates = [];
// From module.inc/update.inc.
$regexp = '/^' . $key . '_update_(?P<version>\d+)$/';
$functions = get_defined_functions();
foreach (preg_grep('/_\d+$/', $functions['user']) as $function) {
$matches = [];
if (preg_match($regexp, $function, $matches)) {
$updates[] = $matches['version'];
}
}
sort($updates, SORT_NUMERIC);
// End of from module.inc/update.inc.
$modules[$key] = $updates;
}
foreach ($modules as $module => $updates) {
if (empty($updates)) {
db_query("UPDATE {system} SET schema_version = 7000 WHERE name = :module", [':module' => $module]);
print "Fixed $module to 7000\n";
} else {
$max = max($updates);
print "Fixed $module to $max\n";
db_query("UPDATE {system} SET schema_version = :max WHERE name = :module", [':module' => $module, ':max' => $max]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment