diff --git a/auditlog/__manifest__.py b/auditlog/__manifest__.py index a21a00ad2..1656d4e90 100644 --- a/auditlog/__manifest__.py +++ b/auditlog/__manifest__.py @@ -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", diff --git a/auditlog/migrations/14.0.1.1.0/pre-migration.py b/auditlog/migrations/14.0.1.1.0/pre-migration.py deleted file mode 100644 index 53a120e98..000000000 --- a/auditlog/migrations/14.0.1.1.0/pre-migration.py +++ /dev/null @@ -1,73 +0,0 @@ -# © 2018 Pieter Paulussen -# 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") diff --git a/auditlog/models/rule.py b/auditlog/models/rule.py index ff31c6c86..1eb482de8 100644 --- a/auditlog/models/rule.py +++ b/auditlog/models/rule.py @@ -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.""" diff --git a/auditlog/tests/test_auditlog.py b/auditlog/tests/test_auditlog.py index 78d5959b9..88d5e7c0b 100644 --- a/auditlog/tests/test_auditlog.py +++ b/auditlog/tests/test_auditlog.py @@ -2,7 +2,6 @@ # © 2018 Pieter Paulussen # © 2021 Stefan Rijnhart # 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): diff --git a/auditlog/views/auditlog_view.xml b/auditlog/views/auditlog_view.xml index 6b297cfd6..85ed37096 100644 --- a/auditlog/views/auditlog_view.xml +++ b/auditlog/views/auditlog_view.xml @@ -159,7 +159,7 @@ - +