From 14fd4f88a55d8a03f9fa592723a95d219f1924af Mon Sep 17 00:00:00 2001 From: MuK IT GmbH Date: Sun, 21 Jul 2019 20:52:38 +0000 Subject: [PATCH] publish muk_utils - 12.0 --- muk_utils/__manifest__.py | 2 +- muk_utils/models/ir_attachment.py | 42 ++++++++------------ muk_utils/tests/test_attachment_migration.py | 2 +- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/muk_utils/__manifest__.py b/muk_utils/__manifest__.py index 3078cec..d4e7429 100644 --- a/muk_utils/__manifest__.py +++ b/muk_utils/__manifest__.py @@ -22,7 +22,7 @@ { "name": "MuK Utils", "summary": """Utility Features""", - "version": '12.0.2.0.1', + "version": '12.0.2.0.2', "category": 'Extra Tools', "license": "LGPL-3", "author": "MuK IT", diff --git a/muk_utils/models/ir_attachment.py b/muk_utils/models/ir_attachment.py index 36421e1..f00e118 100644 --- a/muk_utils/models/ir_attachment.py +++ b/muk_utils/models/ir_attachment.py @@ -97,35 +97,27 @@ class IrAttachment(models.Model): '&', storage_domain[self._storage()], '|', ('res_field', '=', False), ('res_field', '!=', False) ] - self.search(record_domain).migrate() + self.search(record_domain).migrate(batch_size=100) return True @api.multi - def migrate(self): - self.env.cr.commit() + def migrate(self, batch_size=None): + batch_size = batch_size or len(self) storage_location = self._storage().upper() - batch_size = self.env.context.get('migration_batch_size', 100) - batches_to_migrate = math.ceil(len(self) / batch_size) - for batch_index, sub_ids in enumerate(split_every(batch_size, self.ids)): - with api.Environment.manage(): - with registry(self.env.cr.dbname).cursor() as batch_cr: - batch_env = api.Environment(batch_cr, self.env.uid, self.env.context.copy()) - attachment_records = batch_env['ir.attachment'].browse(sub_ids) - batch_records_count = len(attachment_records) - try: - for index, attach in enumerate(attachment_records): - _logger.info("Migrate Attachment %s of %s to %s [Batch %s of %s]", - index + 1, batch_records_count, storage_location, - batch_index + 1, batches_to_migrate - ) - attach.with_context(migration=True).write({ - 'datas': attach.datas - }) - except: - batch_cr.rollback() - raise - else: - batch_cr.commit() + batches = math.ceil(len(self) / batch_size) + + print(batch_size, batches) + + for index, attachment in enumerate(self, start=1): + _logger.info("Migrate Attachment %s of %s to %s [Batch %s of %s]", + index % batch_size, math.ceil(index / batch_size), + storage_location, (index / batch_size) + 1, batches + ) + attachment.with_context(migration=True).write({ + 'datas': attachment.datas + }) + if not index % batch_size: + self.env.cr.commit() #---------------------------------------------------------- # Read diff --git a/muk_utils/tests/test_attachment_migration.py b/muk_utils/tests/test_attachment_migration.py index ebf5802..3f0ad85 100644 --- a/muk_utils/tests/test_attachment_migration.py +++ b/muk_utils/tests/test_attachment_migration.py @@ -47,5 +47,5 @@ class MigrationTestCase(common.TransactionCase): super(MigrationTestCase, self).tearDown() def test_migration(self): - self.model.search([], limit=25).with_context(migration_batch_size=5).action_migrate() + self.model.search([], limit=25).migrate(batch_size=5) \ No newline at end of file