[IMP] core: use inert SQL based neutralization

Before this commit the neutralize system introduced in v16 was using ORM
methods in order to change appropriate records. Although flexible, this approach
could lead to call some methods with side effects while neutralizing
(eg: overloads of write).

This patch converts the neutralize system to a safer "inert" SQL based approach
by migrating the generic method _neutralize to SQL files exposed in the
data folder.

Task id: 2961687

closes odoo/odoo#102792

X-original-commit: e5dbded9bb363351feff7ca8a56c7f8a6860f492
Related: odoo/enterprise#32580
Signed-off-by: Fabien Meghazi <fme@odoo.com>
This commit is contained in:
Laurent Desausoi
2022-10-09 22:04:00 +02:00
committed by Fabien Meghazi
parent 90ddaf871d
commit 7593c073d2
106 changed files with 275 additions and 594 deletions
+23 -18
View File
@@ -7,8 +7,6 @@ import sys
import odoo
from pathlib import Path
from . import Command
_logger = logging.getLogger(__name__)
@@ -20,28 +18,35 @@ class Neutralize(Command):
def run(self, args):
parser = odoo.tools.config.parser
group = optparse.OptionGroup(parser, "Neutralize", "Neutralize the database specified by the `-d` argument.")
group.add_option("--stdout", action="store_true", dest="to_stdout",
help="Output the neutralization SQL instead of applying it")
parser.add_option_group(group)
odoo.tools.config.parse_config(args)
opt = 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')
if not opt.to_stdout:
_logger.info("Starting %s database neutralization", dbname)
try:
with registry.cursor() as cr:
env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
for model in env.values():
model._neutralize()
env['ir.config_parameter'].set_param('database.is_neutralized', True)
# Signal changes so the config parameter we just set gets updated in a
# running database without requiring a restart.
env.registry.signal_changes()
with open(Path(__file__).parent / 'neutralize_watermarks.xml', 'rb') as f:
odoo.tools.convert_xml_import(cr, "__neutralize__", f)
with odoo.sql_db.db_connect(dbname).cursor() as cursor:
installed_modules = odoo.modules.neutralize.get_installed_modules(cursor)
queries = odoo.modules.neutralize.get_neutralization_queries(installed_modules)
if opt.to_stdout:
# pylint: disable=bad-builtin
print('BEGIN;')
for query in queries:
# pylint: disable=bad-builtin
print(query.rstrip(";") + ";")
# pylint: disable=bad-builtin
print("COMMIT;")
else:
for query in queries:
cursor.execute(query)
_logger.info("Neutralization finished")
except Exception:
_logger.error('An exception occured during neutralization.')
raise
_logger.info('Neutralization finished')
_logger.critical("An error occurred during the neutralization. THE DATABASE IS NOT NEUTRALIZED!")
sys.exit(1)
-39
View File
@@ -1,39 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<template t-name="neutralize_banner" inherit_id="web.layout">
<xpath expr="//body" position="inside">
<div>
<span style="text-align: center;
color: #FFFFFF;
background-color: #D0442C;
position: relative;
display: block;
font-size: 16px;">
This database is neutralized.
</span>
</div>
</xpath>
</template>
<template t-name="neutralize_ribbon" inherit_id="web.layout">
<xpath expr="//body" position="inside">
<div>
<span style="width: 400px;
top: 55px;
left: -100px;
font-size: 32px;
text-align: center;
padding: 10px;
line-height: 30px;
color: #f0f0f0;
transform: rotate(-45deg);
position: fixed;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
background: #D0442C;
opacity: 0.6;
pointer-events: none;
text-transform: uppercase;
z-index:9999">Neutralized</span>
</div>
</xpath>
</template>
</odoo>