OCA / storage

GNU Affero General Public License v3.0
66 stars 148 forks source link

Module tries to add already existing columns when installing a second time #338

Open archipell opened 4 months ago

archipell commented 4 months ago

Module

fs_attachment

Describe the bug

This bug only exist when you are developing a new module on top of fs_attachment.

fs_attachment module in its pre_init_hook function creates the necessary columns to work. These columns are not removed when module installation is made (by user or by Odoo whenever another depending module could not be installed for example). This creates a very annoying problem in development mode: since columns are already created, fs_attachment module cannot be reinstalled (because it crashes when it reruns pre_init_hook to create already created columns). The only solution is to delete the columns manually or delete whole database and start from scratch.

In conclusion, fs_attachment pre_init_hook is not stateless, it should create the columns only if not done before on DB. This bug can also happen whenever you have already installed/uninstalled fs_attachment once and somewhere in the future user wants to reinstall it.

To Reproduce

Affected versions: v16.0.1.0.2

Steps to reproduce the behavior:

  1. Create a module depending on fs_attachment
  2. Create a pre_init_hook that does not work (raise exception for example)
  3. Launch odoo and install new module TWICE

Expected behavior On the first launch, your new module should not be installed since it raises an exception. On the second launch, fs_attachment tries to create columns that already exist (for example in following log: fs_storage_id already exists in ir_attachment).

Additional context 2024-02-08 09:12:27,399 10560 ERROR dev-archi-custom-project odoo.sql_db: bad query: ALTER TABLE ir_attachment ADD COLUMN fs_storage_id INTEGER; ALTER TABLE ir_attachment ADD FOREIGN KEY (fs_storage_id) REFERENCES fs_storage(id);

ERROR: ERREUR: la colonne « fs_storage_id » de la relation « ir_attachment » existe déjà

2024-02-08 09:12:27,408 10560 WARNING dev-archi-custom-project odoo.modules.loading: Transient module states were reset 2024-02-08 09:12:27,408 10560 ERROR dev-archi-custom-project odoo.modules.registry: Failed to load registry Traceback (most recent call last): File "repository\odoo\modules\registry.py", line 90, in new odoo.modules.load_modules(registry, force_demo, status, update_module) File "repository\odoo\modules\loading.py", line 488, in load_modules processed_modules += load_marked_modules(cr, graph, File "repository\odoo\modules\loading.py", line 372, in load_marked_modules loaded, processed = load_module_graph( File "repository\odoo\modules\loading.py", line 195, in load_module_graph getattr(py_module, pre_init)(cr) File "repository\oca-addons\fs_attachment\hooks.py", line 13, in pre_init_hook cr.execute( File "repository\odoo\sql_db.py", line 314, in execute res = self._obj.execute(query, params) psycopg2.errors.DuplicateColumn: ERREUR: la colonne « fs_storage_id » de la relation « ir_attachment » existe déjà

2024-02-08 09:12:27,415 10560 ERROR dev-archi-custom-project odoo.http: Exception during request handling. Traceback (most recent call last): File "repository\odoo\http.py", line 1998, in call response = request._serve_db() File "repository\odoo\http.py", line 1584, in _serve_db return service_model.retrying(self._serve_ir_http, self.env) File "repository\odoo\service\model.py", line 133, in retrying result = func() File "repository\odoo\http.py", line 1611, in _serve_ir_http response = self.dispatcher.dispatch(rule.endpoint, args) File "repository\odoo\http.py", line 1815, in dispatch result = self.request.registry['ir.http']._dispatch(endpoint) File "repository\odoo\addons\base\models\ir_http.py", line 154, in _dispatch result = endpoint(request.params) File "repository\odoo\http.py", line 697, in route_wrapper result = endpoint(self, *args, *params_ok) File "repository\odoo\addons\web\controllers\dataset.py", line 46, in call_button action = self._call_kw(model, method, args, kwargs) File "repository\odoo\addons\web\controllers\dataset.py", line 33, in _call_kw return call_kw(request.env[model], method, args, kwargs) File "repository\odoo\api.py", line 461, in call_kw result = _call_kw_multi(method, model, args, kwargs) File "repository\odoo\api.py", line 448, in _call_kw_multi result = method(recs, args, kwargs) File "", line 2, in button_immediate_install File "repository\odoo\addons\base\models\ir_module.py", line 76, in check_and_log return method(self, *args, *kwargs) File "repository\odoo\addons\base\models\ir_module.py", line 491, in button_immediate_install return self._button_immediate_function(type(self).button_install) File "repository\odoo\addons\base\models\ir_module.py", line 615, in _button_immediate_function registry = modules.registry.Registry.new(self._cr.dbname, update_module=True) File "", line 2, in new File "repository\odoo\tools\func.py", line 87, in locked return func(inst, args, **kwargs) File "repository\odoo\modules\registry.py", line 90, in new odoo.modules.load_modules(registry, force_demo, status, update_module) File "repository\odoo\modules\loading.py", line 488, in load_modules processed_modules += load_marked_modules(cr, graph, File "repository\odoo\modules\loading.py", line 372, in load_marked_modules loaded, processed = load_module_graph( File "repository\odoo\modules\loading.py", line 195, in load_module_graph getattr(py_module, pre_init)(cr) File "repository\oca-addons\fs_attachment\hooks.py", line 13, in pre_init_hook cr.execute( File "repository\odoo\sql_db.py", line 314, in execute res = self._obj.execute(query, params) psycopg2.errors.DuplicateColumn: ERREUR: la colonne « fs_storage_id » de la relation « ir_attachment » existe déjà