[MIG] auditlog: Migration to 16.0
parent
852a18783b
commit
c8671aa35b
|
@ -3,7 +3,7 @@
|
|||
|
||||
{
|
||||
"name": "Audit Log",
|
||||
"version": "15.0.1.1.1",
|
||||
"version": "16.0.1.0.0",
|
||||
"author": "ABF OSIELL, Odoo Community Association (OCA)",
|
||||
"license": "AGPL-3",
|
||||
"website": "https://github.com/OCA/server-tools",
|
||||
|
|
|
@ -1,73 +0,0 @@
|
|||
# © 2018 Pieter Paulussen <pieter_paulussen@me.com>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
import logging
|
||||
|
||||
|
||||
def migrate(cr, version):
|
||||
if not version:
|
||||
return
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.info(
|
||||
"Creating columns: auditlog_log (model_name, model_model) "
|
||||
"and auditlog_log_line (field_name, field_description)."
|
||||
)
|
||||
cr.execute(
|
||||
"""
|
||||
ALTER TABLE auditlog_log
|
||||
ADD COLUMN IF NOT EXISTS model_name VARCHAR,
|
||||
ADD COLUMN IF NOT EXISTS model_model VARCHAR;
|
||||
ALTER TABLE auditlog_log_line
|
||||
ADD COLUMN IF NOT EXISTS field_name VARCHAR,
|
||||
ADD COLUMN IF NOT EXISTS field_description VARCHAR;
|
||||
ALTER TABLE auditlog_rule
|
||||
ADD COLUMN IF NOT EXISTS model_name VARCHAR,
|
||||
ADD COLUMN IF NOT EXISTS model_model VARCHAR;
|
||||
"""
|
||||
)
|
||||
logger.info(
|
||||
"Creating indexes on auditlog_log column 'model_id' and "
|
||||
"auditlog_log_line column 'field_id'."
|
||||
)
|
||||
cr.execute(
|
||||
"""
|
||||
CREATE INDEX IF NOT EXISTS
|
||||
auditlog_log_model_id_index ON auditlog_log (model_id);
|
||||
CREATE INDEX IF NOT EXISTS
|
||||
auditlog_log_line_field_id_index ON auditlog_log_line (field_id);
|
||||
"""
|
||||
)
|
||||
logger.info(
|
||||
"Preemptive fill auditlog_log columns: 'model_name' and " "'model_model'."
|
||||
)
|
||||
cr.execute(
|
||||
"""
|
||||
UPDATE auditlog_log al
|
||||
SET model_name = im.name, model_model = im.model
|
||||
FROM ir_model im
|
||||
WHERE im.id = al.model_id AND model_name IS NULL
|
||||
"""
|
||||
)
|
||||
logger.info(
|
||||
"Preemtive fill of auditlog_log_line columns: 'field_name' and"
|
||||
" 'field_description'."
|
||||
)
|
||||
cr.execute(
|
||||
"""
|
||||
UPDATE auditlog_log_line al
|
||||
SET field_name = imf.name, field_description = imf.field_description
|
||||
FROM ir_model_fields imf
|
||||
WHERE imf.id = al.field_id AND field_name IS NULL
|
||||
"""
|
||||
)
|
||||
logger.info(
|
||||
"Preemptive fill auditlog_rule columns: 'model_name' and " "'model_model'."
|
||||
)
|
||||
cr.execute(
|
||||
"""
|
||||
UPDATE auditlog_rule al
|
||||
SET model_name = im.name, model_model = im.model
|
||||
FROM ir_model im
|
||||
WHERE im.id = al.model_id AND model_name IS NULL
|
||||
"""
|
||||
)
|
||||
logger.info("Successfully updated auditlog tables")
|
|
@ -223,17 +223,19 @@ class AuditlogRule(models.Model):
|
|||
if updated:
|
||||
modules.registry.Registry(self.env.cr.dbname).signal_changes()
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
"""Update the registry when a new rule is created."""
|
||||
if "model_id" not in vals or not vals["model_id"]:
|
||||
raise UserError(_("No model defined to create line."))
|
||||
model = self.env["ir.model"].sudo().browse(vals["model_id"])
|
||||
vals.update({"model_name": model.name, "model_model": model.model})
|
||||
new_record = super().create(vals)
|
||||
if new_record._register_hook():
|
||||
for vals in vals_list:
|
||||
if "model_id" not in vals or not vals["model_id"]:
|
||||
raise UserError(_("No model defined to create line."))
|
||||
model = self.env["ir.model"].sudo().browse(vals["model_id"])
|
||||
vals.update({"model_name": model.name, "model_model": model.model})
|
||||
new_records = super().create(vals_list)
|
||||
updated = [record._register_hook() for record in new_records]
|
||||
if any(updated):
|
||||
modules.registry.Registry(self.env.cr.dbname).signal_changes()
|
||||
return new_record
|
||||
return new_records
|
||||
|
||||
def write(self, vals):
|
||||
"""Update the registry when existing rules are updated."""
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
# © 2018 Pieter Paulussen <pieter_paulussen@me.com>
|
||||
# © 2021 Stefan Rijnhart <stefan@opener.amsterdam>
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
from odoo.modules.migration import load_script
|
||||
from odoo.tests.common import Form, TransactionCase
|
||||
|
||||
from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
|
||||
|
@ -298,7 +297,7 @@ class TestFieldRemoval(TransactionCase):
|
|||
def assert_values(self):
|
||||
"""Assert that the denormalized field and model info is present
|
||||
on the auditlog records"""
|
||||
self.logs.refresh()
|
||||
self.logs.invalidate_recordset()
|
||||
self.assertEqual(self.logs[0].model_name, "x_test_model")
|
||||
self.assertEqual(self.logs[0].model_model, "x_test.model")
|
||||
|
||||
|
@ -307,7 +306,7 @@ class TestFieldRemoval(TransactionCase):
|
|||
self.assertEqual(log_lines[0].field_name, "x_test_field")
|
||||
self.assertEqual(log_lines[0].field_description, "x_Test Field")
|
||||
|
||||
self.auditlog_rule.refresh()
|
||||
self.auditlog_rule.invalidate_recordset()
|
||||
self.assertEqual(self.auditlog_rule.model_name, "x_test_model")
|
||||
self.assertEqual(self.auditlog_rule.model_model, "x_test.model")
|
||||
|
||||
|
@ -330,34 +329,6 @@ class TestFieldRemoval(TransactionCase):
|
|||
# Assert rule values
|
||||
self.assertFalse(self.auditlog_rule.model_id)
|
||||
|
||||
def test_02_migration(self):
|
||||
"""Test the migration of the data model related to this feature"""
|
||||
# Drop the data model
|
||||
self.env.cr.execute(
|
||||
"""ALTER TABLE auditlog_log
|
||||
DROP COLUMN model_name, DROP COLUMN model_model"""
|
||||
)
|
||||
self.env.cr.execute(
|
||||
"""ALTER TABLE auditlog_rule
|
||||
DROP COLUMN model_name, DROP COLUMN model_model"""
|
||||
)
|
||||
self.env.cr.execute(
|
||||
"""ALTER TABLE auditlog_log_line
|
||||
DROP COLUMN field_name, DROP COLUMN field_description"""
|
||||
)
|
||||
|
||||
# Recreate the data model
|
||||
mod = load_script(
|
||||
"auditlog/migrations/14.0.1.1.0/pre-migration.py", "pre-migration"
|
||||
)
|
||||
mod.migrate(self.env.cr, "14.0.1.0.2")
|
||||
|
||||
# Values are restored
|
||||
self.assert_values()
|
||||
|
||||
# The migration script is tolerant if the data model is already in place
|
||||
mod.migrate(self.env.cr, "14.0.1.0.2")
|
||||
|
||||
|
||||
class TestAuditlogFullCaptureRecord(TransactionCase, AuditlogCommon):
|
||||
def setUp(self):
|
||||
|
|
|
@ -159,7 +159,7 @@
|
|||
<field name="http_request_id" />
|
||||
</group>
|
||||
<group string="Fields updated">
|
||||
<field name="line_ids" readonly="1" nolabel="1">
|
||||
<field name="line_ids" readonly="1" nolabel="1" colspan="2">
|
||||
<form string="Log - Field updated">
|
||||
<group>
|
||||
<field name="field_id" readonly="1" />
|
||||
|
|
Loading…
Reference in New Issue