Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Here's a patch to add this into the docs, as hook_update_N_batch(), since it'd probably be too much to cloud up the example update function hook with batch API stuff. Therefore, I put a bold note clarifying that the name is actually the same as hook_update_N(), with a code sample in the documentation body, as well as in the hook example itself.
I also added a note about the ability to do $ret['#finished'] in the original batch doc, and this one.
I added a link in the new doc to the batch operations doc page.
Also, each batch hook @see's the other.
Thanks!
Comment | File | Size | Author |
---|---|---|---|
#5 | document_hook_update_n_batch.patch | 3 KB | cha0s |
#3 | document_hook_update_n_batch.patch | 3 KB | cha0s |
document_hook_update_n_batch.patch | 3.2 KB | cha0s | |
Comments
Comment #1
yched CreditAttribution: yched commentedNote that 'tie in to the batch API' doesn't mean just $ret['#finished'] - it's about using the $sandbox variable to store persistent data across multiple passes, and $ret['#finished'] to say 'I'm not done yet, please cal me again'.
'An update function may also tie in to the batch API' : this is quite technical and doesn't explain *why* you'd want to do so. I'd suggest introducing this with something like 'If your update task is potentially time consuming, you'll need to implement a multipass update to avoid PHP timeouts. Multipass updates use the $context parameter provided by the batch API to store information between successive calls, and the $ret['#finished'] return value to provide feedback about completion level.'
Comment #2
webchickOh, what great documentation to have! :D
In addition to yched's comments, I think it might actually be more confusing to have this documented as a separate hook (hook_update_N_batch) when it is not actually a separate hook. Could we instead do something like:
Also, if we're going to commit this to 7.x, the database queries need to be updated to DBTNG.
Comment #3
cha0s CreditAttribution: cha0s commentedAlright, round 2. =)
Comment #4
Jody LynnLooks great.
"Time-consuming" needs a hyphen.
Comment #5
cha0s CreditAttribution: cha0s commentedAlrighty, fixed that one.
Comment #6
webchickGreat job!! I committed this to HEAD. Needs to be back-ported and then committed to contributions/docs/developer/hooks/core.php. Anyone with CVS access can do this, so go for it cha0s, and then go ahead and mark this fixed. :)
Comment #7
cha0s CreditAttribution: cha0s commentedThanks, I got this committed to the Drupal 6 docs. Feel free to review/scream if I got it wrong. :)
Comment #8
webchickhttp://cvs.drupal.org/viewvc.py/drupal/contributions/docs/developer/hook...
Looks good here! Marking fixed. Thanks, cha0s! :D
Comment #9
merlinofchaos CreditAttribution: merlinofchaos commentedThe method used in this example has an unfortunate bug. It uses the COUNT of users but it uses user IDs as the step value. Now, for users this is rarely a big problem but if you have user deletions you will have holes n the table and the COUNT will not match the actual max user ID.
Because you're using query range, you should instead use an example more like this:
Ok, so the code above has both 'progress' and 'current' which now mean the same thing, but I was afraid to touch progress because I do not know if that is used by batch api to draw the bar. So that needs to be cleaned up, but pure record counting is the way to go here.
Comment #10
merlinofchaos CreditAttribution: merlinofchaos commentedalso my use of db_query_range is incorrect. The last argument should just be 3, not adding current to it. :P
Comment #11
cha0s CreditAttribution: cha0s commentedIf that's incorrect, then the batch API docs are wrong too, since they use that technique with nodes. http://drupal.org/node/180528
Comment #12
merlinofchaos CreditAttribution: merlinofchaos commentedUpon further review, it is not actually buggy. It is, however, confusing, to have the max and counter not match with no explanation.
Comment #13
cha0s CreditAttribution: cha0s commentedI'm not sure what you mean by "max and counter [don't] match with no explanation". The $ret['#finished'] variable is how you transmit back how far along the batch is. As you can see when progress == max, the result is '1', which will trigger the batch API to finish up.
Could you elaborate a little more what you mean by that, if I didn't understand correctly?
Comment #14
merlinofchaos CreditAttribution: merlinofchaos commentedSure, it uses a COUNT(*) to determine how many to do, but it works on UIDs and filters on UIDs rather than working like a pager, which looks, at first blush, as though it could be buggy. It's not, because there's the separate 'progress' and 'current' variables but it wasn't clear to me at first.
Comment #15
cha0s CreditAttribution: cha0s commentedAh, I gotcha. Well, as you said, uid's can have holes punched out if a user has been deleted... so I'm not sure there's a more elegant solution than this, really.
Do you have other code you think would make more sense that we could plug in here? We could also apply it over on that batch API tutorial (as well as backporting to Drupal 6)
Comment #16
merlinofchaos CreditAttribution: merlinofchaos commentedI suspect simply a few more comments would suffice, to explain why it's being done this way. The only reason to use the UID is that the query is more efficient than using a pager query, though to be honest that efficiency is a very very minor issue. (The pager query being of the type of using LIMIT 700, 3 to pull the 700, 701 and 702 results). On the other hand, using a pager means the data itself is less important so the technique always works.
Comment #17
jhodgdonBump. Does this still need an update?
Comment #18
jhodgdonDocs queue... that is where d6 hook docs live...
Comment #19
jhodgdonActually, moving this back to d8 in case this still needs to be addressed. Can someone propose a patch to fix this confusion?
Comment #20
jhodgdonReviving this issue... what needs to be changed here?
Comment #21
jhodgdonThe hook_update_N() docs were fixed on another issue a while back.