From 152310fe266322f7aa2baacec9067b560ac8a083 Mon Sep 17 00:00:00 2001 From: Christophe Monniez Date: Mon, 14 Jun 2021 09:18:48 +0000 Subject: [PATCH] [IMP] core: add a neutralize CLI command When debugging a database, it's sometimes desirable to neutralize it to avoid undesired side effects like sending emails. This commit adds a generic method `_neutralize` on models to neutralize the current database. This method has to be overridden by modules models that needs to be neutralized. A new CLI command `neutralize`is now available that will permit to neutralize a database from the command line before starting investigations. Pay attention that the neutralize operation cannot be reversed and should only be used on a database copy. Task id: 1818795 Part-of: odoo/odoo#67825 --- odoo/cli/__init__.py | 1 + odoo/cli/neutralize.py | 43 ++++++++++++++++++++++++++++++ odoo/cli/neutralize_watermarks.xml | 39 +++++++++++++++++++++++++++ odoo/models.py | 6 +++++ 4 files changed, 89 insertions(+) create mode 100644 odoo/cli/neutralize.py create mode 100644 odoo/cli/neutralize_watermarks.xml diff --git a/odoo/cli/__init__.py b/odoo/cli/__init__.py index 070cd7e96f2..277e25197e2 100644 --- a/odoo/cli/__init__.py +++ b/odoo/cli/__init__.py @@ -14,3 +14,4 @@ from . import shell from . import start from . import populate from . import tsconfig +from . import neutralize diff --git a/odoo/cli/neutralize.py b/odoo/cli/neutralize.py new file mode 100644 index 00000000000..bc7b9a40b79 --- /dev/null +++ b/odoo/cli/neutralize.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import logging +import optparse +import sys + +import odoo + +from pathlib import Path + +from . import Command + +_logger = logging.getLogger(__name__) + + +class Neutralize(Command): + """neutralize a database""" + + def run(self, args): + parser = odoo.tools.config.parser + group = optparse.OptionGroup(parser, "Neutralize", "Neutralize the database specified by the `-d` argument.") + parser.add_option_group(group) + odoo.tools.config.parse_config(args) + + dbname = odoo.tools.config['db_name'] + if not dbname: + _logger.error('Neutralize command needs a database name. Use "-d" argument') + sys.exit(1) + + registry = odoo.registry(dbname) + _logger.info('Starting database neutralization') + try: + with registry.cursor() as cr: + env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {}) + for model in env.values(): + model._neutralize() + with open(Path(__file__).parent / 'neutralize_watermarks.xml', 'rb') as f: + odoo.tools.convert_xml_import(cr, "__neutralize__", f) + except Exception: + _logger.error('An exception occured during neutralization.') + raise + _logger.info('Neutralization finished') diff --git a/odoo/cli/neutralize_watermarks.xml b/odoo/cli/neutralize_watermarks.xml new file mode 100644 index 00000000000..d3c87228c13 --- /dev/null +++ b/odoo/cli/neutralize_watermarks.xml @@ -0,0 +1,39 @@ + + + + + diff --git a/odoo/models.py b/odoo/models.py index 799e8e27abf..fa66eaab6b4 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -6688,6 +6688,12 @@ Fields: records_batches.append(self.create(create_values)) return self.concat(*records_batches) + def _neutralize(self): + """ Neutralize this model's records. + This should prevent the database from connecting to external services. + """ + return + collections.Set.register(BaseModel) # not exactly true as BaseModel doesn't have index or count