Sooner or later each module gets updated and then the new version appears in stores and Prestashop Addons. But let’s take a case, when a user has updated his store to the latest version and he also needs to update a third-party module, which should be compatible with the newest Prestashop. In most cases the user just replaces old module files with the new files – and this is when problems start to happen. Most issues occur if the new module has an entirely different database structure, uses different hooks etc. In other words, it has a different install() method logic. That is why to ensure that the module has been updated properly, the user has to reset the module and then to implement the updated install() method. However, the drawback here is that in such case the user will lose absolutely all data, which have been accumulated by the module since it was installed for the first time.
To avoid this, all Prestashop versions starting from v1.5 and later have an auto-update mechanism (Autо-Updаte) included, which allows you to make the necessary updates without reinstalling the module and losing your data.
Once old files have been replaced with the new ones, Prestashop compares the version of the module, which is indicated in $this->version with the version, that is indicated in the database. If the version specified in $this->version is newer than in the database, Prestashops will start searching for updates for the latest version. This mechanism checks whether there is the Upgrade folder in the module and whether the file in this older corresponds to th following pattern:
If the file is found, then Prestashop will attempt to perform the function with the name upgrаde_mоdule_ [new_versiоn]. It is necessary to use the underscore in the version name instead of the dots. The object of the module will be transmitted into the parameters of this function.
We had a module of version 1.0 ($this->version = ‘1.0‘;), and then we replaced module’s files with the version 1.1 ($this->version = ‘1.1‘;). In such case there will be implemented the function upgrade_module_1_1($module_object) of the file upgrade/Upgrade-1.1.php
//install new hook
return Db::getInstance()->execute('UPDATE SQL QUERY');
You can check for examples of the auto-update mechanism in the modules homeslider and blockbestseller.