I have a module that I created. Recently I made a new version of it. The new version of the module has a new variable in it's config file. When the new version is installed from scratch - everything works fine. But if there is a previous version present (that doesn't have a new variable in it's config) and it is then updated with a new one, it appears that the config file in /files/config_[hash]/active is left from the older version. As a result the new version doesn't find the needed variable that it tries to read from older config and that causes errors. I wonder how can I ensure that when user updates my module, the existing module's config file adds new variables provided by a new version?

I guess I could add some code to hook_install in module.install file that would check and add new variables to existing config file. But I wonder if there is some automated method that would compare config file from previous version and the one provided by a new version of module and merge them together?

Accepted answer

There's no automated way to do this (AFAIK) so what you'd do is implement hook_update_N() and add the new value there (using config_set()).

Most helpful answers

Check the schema_version column in the system table in the database. For modules that don't have any hook_update_N() functions, the value should be 0. For modules with hook_update_N() functions, the value will be the N number of the last update hook that was run (e.g. 1005).

So to test an update hook, set this value in the DB to either 0 or to the number of the update hook prior to the one you're testing.

Hope that helps.

Comments

@BWPanda, thank you very much for the help! I will try this way.

I have also got a related question. How can I simulate the update process to test if my hook_update_N() is triggered and works properly?

Check the schema_version column in the system table in the database. For modules that don't have any hook_update_N() functions, the value should be 0. For modules with hook_update_N() functions, the value will be the N number of the last update hook that was run (e.g. 1005).

So to test an update hook, set this value in the DB to either 0 or to the number of the update hook prior to the one you're testing.

Hope that helps.

Thank you very much! That is extremely helpful!