2 * Implements hook_update_N().
4 function {{ machine_name }}_update_N(&$sandbox) {
5 // For non-multipass updates, the signature can simply be;
6 // function {{ machine_name }}_update_N() {
8 // For most updates, the following is sufficient.
9 db_add_field('mytable1', 'newcol', array('type' => 'int', 'not null' => TRUE, 'description' => 'My new integer column.'));
11 // However, for more complex operations that may take a long time,
12 // you may hook into Batch API as in the following example.
14 // Update 3 users at a time to have an exclamation point after their names.
15 // (They're really happy that we can do batch API in this hook!)
16 if (!isset($sandbox['progress'])) {
17 $sandbox['progress'] = 0;
18 $sandbox['current_uid'] = 0;
19 // We'll -1 to disregard the uid 0...
20 $sandbox['max'] = db_query('SELECT COUNT(DISTINCT uid) FROM {users}')->fetchField() - 1;
23 $users = db_select('users', 'u')
24 ->fields('u', array('uid', 'name'))
25 ->condition('uid', $sandbox['current_uid'], '>')
27 ->orderBy('uid', 'ASC')
30 foreach ($users as $user) {
33 ->fields(array('name' => $user->name))
34 ->condition('uid', $user->uid)
37 $sandbox['progress']++;
38 $sandbox['current_uid'] = $user->uid;
41 $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
43 // To display a message to the user when the update is completed, return it.
44 // If you do not want to display a completion message, simply return nothing.
45 return t('The update did what it was supposed to do.');
47 // In case of an error, simply throw an exception with an error message.
48 throw new DrupalUpdateException('Something went wrong; here is what you should do.');