hook_update_N(&$sandbox)
Perform a single update between minor versions.
hook_update_N() can only be used to update between minor versions of a module. To upgrade between major versions of Drupal (for example, between Drupal 7 and 8), use the Migrate API instead.
For each change in a module that requires one or more actions to be performed when updating a site, add a new implementation of hook_update_N() to your mymodule.install file (assuming mymodule is the machine name of your module). Implementations of hook_update_N() are named (module name)_update_(number). The numbers are normally composed of three parts:
Examples:
Never renumber update functions. The numeric part of the hook implementation function is stored in the database to keep track of which updates have run, so it is important to maintain this information consistently.
The documentation block preceding this function is stripped of newlines and used as the description for the update on the pending updates task list, which users will see when they run the update.php script.
Writing hook_update_N() functions is tricky. There are several reasons why this is the case:
Because of these reasons, you'll need to use care in writing your update function. Some things to think about:
The following actions are examples of things that are safe to do during updates:
See https://www.drupal.org/node/2535316 for more on writing update functions.
If running your update all at once could possibly cause PHP to time out, use the $sandbox parameter to indicate that the Batch API should be used for your update. In this case, your update function acts as an implementation of callback_batch_operation(), and $sandbox acts as the batch context parameter. In your function, read the state information from the previous run from $sandbox (or initialize), run a chunk of updates, save the state in $sandbox, and set $sandbox['#finished'] to a value between 0 and 1 to indicate the percent completed, or 1 if it is finished (you need to do this explicitly in each pass).
See the Batch operations topic for more information on how to use the Batch API.
array $sandbox: Stores information for batch updates. See above for more information.
string|null Optionally, update hooks may return a translated string that will be displayed to the user after the update has completed. If no message is returned, no message will be presented to the user.
\Drupal\Core\Utility\UpdateException|PDOException In case of error, update hooks should throw an instance of Drupal\Core\Utility\UpdateException with a meaningful message for the user. If a database query fails for whatever reason, it will throw a PDOException.
\Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
node_update_8001
system_update_8004
https://www.drupal.org/node/2535316
function hook_update_N(&$sandbox) { // For non-batch updates, the signature can simply be: // function hook_update_N() { // Example function body for adding a field to a database table, which does // not require a batch operation: $spec = array( 'type' => 'varchar', 'description' => "New Col", 'length' => 20, 'not null' => FALSE, ); $schema = Database::getConnection()->schema(); $schema->addField('mytable1', 'newcol', $spec); // Example of what to do if there is an error during your update. if ($some_error_condition_met) { throw new UpdateException('Something went wrong; here is what you should do.'); } // Example function body for a batch update. In this example, the values in // a database field are updated. if (!isset($sandbox['progress'])) { // This must be the first run. Initialize the sandbox. $sandbox['progress'] = 0; $sandbox['current_pk'] = 0; $sandbox['max'] = Database::getConnection()->query('SELECT COUNT(myprimarykey) FROM {mytable1}')->fetchField() - 1; } // Update in chunks of 20. $records = Database::getConnection()->select('mytable1', 'm') ->fields('m', array('myprimarykey', 'otherfield')) ->condition('myprimarykey', $sandbox['current_pk'], '>') ->range(0, 20) ->orderBy('myprimarykey', 'ASC') ->execute(); foreach ($records as $record) { // Here, you would make an update something related to this record. In this // example, some text is added to the other field. Database::getConnection()->update('mytable1') ->fields(array('otherfield' => $record->otherfield . '-suffix')) ->condition('myprimarykey', $record->myprimarykey) ->execute(); $sandbox['progress']++; $sandbox['current_pk'] = $record->myprimarykey; } $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); // To display a message to the user when the update is completed, return it. // If you do not want to display a completion message, return nothing. return t('All foo bars were updated with the new suffix'); }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Extension!module.api.php/function/hook_update_N/8.1.x