Score:1

Hook post update not found

bf flag

I have a problem with the drush updb command which does not find the hook post updates of my custom module. I noticed that the module install saves the module's name string here in database, table "key_value", in BLOB file.

Table key_value : enter image description here BLOB file :

enter image description here

My post update in mymodule.post_update.php :

enter image description here

As it is present in the database, the command seems to consider that the hooks are executed when not. Is there a trick or something missing to solve this problem? My site is in 9.5.4.

Score:1
ne flag

Sounds like you want to run update hooks on a newly installed module. Then you should use deploy hooks. Other then update and post-update hooks they run also for newly installed modules. The only requirement is you run drush deploy:hook during your deployment routine or simply switch from drush updb to drush deploy which also contains drush updb and drush deploy:hook already and runs everything in the right order.

(drush cim will enable the module and in the end drush deploy:hook will pick the deploy hooks up.)

Create a file MYMODULE.deploy.php and inside there put MYMODULE_deploy_NAME(&$sandbox) and go.

Orane B. avatar
bf flag
I tested your recommendations but I have this message: No pending deploy hook. I also tested via install profile and myprofile.post_update.php file but it can't find my function either...I'm out of ideas
leymannx avatar
ne flag
@OraneB. – You enabled the module of course, right?
Orane B. avatar
bf flag
- Yes, of course
Score:0
cn flag

Any update function listed in existing_updates has already been executed, and Drupal won't execute it again (post-update hooks typically operate on content, and you don't want to run those sorts of updates twice unintentionally).

If you're confident that executing the contents of that function is the right solution to your problem, then just change the function name to something that hasn't been executed before.

Orane B. avatar
bf flag
Thank you but that's not the solution because when the site is deployed in production, we won't be able to re-deliver each time to change each post update name... My question is why the first entry of the post update is stored in the database when installing the custom module when I haven't launched `drush updb` ?
cn flag
Because, by definition, your module should work without any updates being needed when it’s first installed. Update and post hooks are just to bring modules which are already installed in line with what a fresh installation of the same module would provide. It wouldn’t make sense for a module to be installed and immediately have an update available to run, so all of the updates are assumed to have run, and are logged as such
Orane B. avatar
bf flag
Thanks, I think I understand. I have to explain the context then: in my module, I create a taxonomy via hook_install and I use a post_update to add the terms because I need the configuration to be imported to create the term only if the taxonomy exists. Since the hook is executed when the module is installed but the taxonomy has not yet been imported, then the term is not created...What would be the best solution?
cn flag
Ah ok - unless you want the vocab to be auto-removed when the module is uninstalled, there’s probably no point in providing a yml in config/install. Just create the vocab programmatically in hook_install before you create the terms
Orane B. avatar
bf flag
Yes first, I had created the terms in the hook_install with the vocabulary. But the terms were not created. As if the vocabulary did not yet exist when the rest of the script was executed...
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.