Skip to content

Commit 45921f4

Browse files
committed
[IMP] modules, models: remove inherits
When removing a module, all its models (and their fields) are also removed. However inherits of such models had to be cleaned up manually.
1 parent df0b0b6 commit 45921f4

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/util/models.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def _unknown_model_id(cr):
5858
return cr.fetchone()[0]
5959

6060

61-
def remove_model(cr, model, drop_table=True, ignore_m2m=()):
61+
def remove_model(cr, model, drop_table=True, ignore_m2m=(), remove_inherits=False):
6262
"""
6363
Remove a model and its references from the database.
6464
@@ -67,6 +67,7 @@ def remove_model(cr, model, drop_table=True, ignore_m2m=()):
6767
6868
:param str model: name of the model to remove
6969
:param bool drop_table: whether to drop the table of this model
70+
:param bool remove_inherits: whether to also clean inherits of ``model``
7071
:param list(str) or str ignore_m2m: list of m2m tables to ignore - not removed, use
7172
`"*"` to ignore (keep) all m2m tables
7273
"""
@@ -162,6 +163,16 @@ def remove_model(cr, model, drop_table=True, ignore_m2m=()):
162163

163164
_remove_import_export_paths(cr, model)
164165

166+
for inh in for_each_inherit(cr, model):
167+
if remove_inherits:
168+
remove_inherit_from_model(cr, inh.model, model)
169+
else:
170+
_logger.warning(
171+
"Model %r is being removed, to also remove its inherit from %r set the `remove_inherits` flag in `remove_model()`.",
172+
model,
173+
inh.model,
174+
)
175+
165176
_rm_refs(cr, model)
166177

167178
cr.execute(

src/util/modules.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ def module_installed(cr, module):
120120
return modules_installed(cr, module)
121121

122122

123-
def uninstall_module(cr, module):
123+
def uninstall_module(cr, module, remove_model_inherits=False):
124124
"""
125125
Uninstall and remove all records owned by a module.
126126
127127
:param str module: name of the module to uninstall
128+
:param bool remove_model_inherits: whether to remove inherits of the module's models
128129
"""
129130
cr.execute("SELECT id FROM ir_module_module WHERE name=%s", (module,))
130131
(mod_id,) = cr.fetchone() or [None]
@@ -233,13 +234,13 @@ def uninstall_module(cr, module):
233234
if model_ids:
234235
cr.execute("SELECT model FROM ir_model WHERE id IN %s", [tuple(model_ids)])
235236
for (model,) in cr.fetchall():
236-
delete_model(cr, model)
237+
delete_model(cr, model, remove_inherits=remove_model_inherits)
237238

238239
if field_ids:
239240
cr.execute("SELECT model, name FROM ir_model_fields WHERE id IN %s", [tuple(field_ids)])
240241
for model, name in cr.fetchall():
241242
if name == "id":
242-
delete_model(cr, model)
243+
delete_model(cr, model, remove_inherits=remove_model_inherits)
243244
else:
244245
remove_field(cr, model, name)
245246

@@ -284,14 +285,15 @@ def uninstall_theme(cr, theme, base_theme=None):
284285
uninstall_module(cr, theme)
285286

286287

287-
def remove_module(cr, module):
288+
def remove_module(cr, module, remove_model_inherits=False):
288289
"""
289290
Completely remove a module.
290291
291292
This operation is equivalent to uninstall and removal of *all* references to
292293
the module - no trace of it is left in the database.
293294
294295
:param str module: name of the module to remove
296+
:param bool remove_model_inherits: whether to remove inherits of the module's models
295297
296298
.. warning::
297299
Since this function removes *all* data associated to the module. Ensure to
@@ -301,7 +303,7 @@ def remove_module(cr, module):
301303
# module need to be currently installed and running as deletions
302304
# are made using orm.
303305

304-
uninstall_module(cr, module)
306+
uninstall_module(cr, module, remove_model_inherits=remove_model_inherits)
305307
cr.execute("DELETE FROM ir_module_module_dependency WHERE name=%s", (module,))
306308
cr.execute("DELETE FROM ir_module_module WHERE name=%s RETURNING id", (module,))
307309
if cr.rowcount:

0 commit comments

Comments
 (0)