From 1d4f76de2b5c7e952ef3e45b8c5c1e3e32287bd5 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Tue, 1 Nov 2016 08:55:00 +0100 Subject: [PATCH] Migrate attachments s3 to new pattern So remove the compatibility code altogether --- attachment_s3/__openerp__.py | 2 +- .../migrations/9.0.1.1.0/pre-migration.py | 20 ++++++++++ attachment_s3/models/ir_attachment.py | 38 +++---------------- 3 files changed, 27 insertions(+), 33 deletions(-) create mode 100644 attachment_s3/migrations/9.0.1.1.0/pre-migration.py diff --git a/attachment_s3/__openerp__.py b/attachment_s3/__openerp__.py index 8841fea..8456d40 100644 --- a/attachment_s3/__openerp__.py +++ b/attachment_s3/__openerp__.py @@ -5,7 +5,7 @@ {'name': 'Attachments on S3 storage', 'summary': 'Store assets and attachments on a S3 compatible object storage', - 'version': '9.0.1.0.0', + 'version': '9.0.1.1.0', 'author': 'Camptocamp', 'license': 'AGPL-3', 'category': 'Knowledge Management', diff --git a/attachment_s3/migrations/9.0.1.1.0/pre-migration.py b/attachment_s3/migrations/9.0.1.1.0/pre-migration.py new file mode 100644 index 0000000..65270a8 --- /dev/null +++ b/attachment_s3/migrations/9.0.1.1.0/pre-migration.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +import os + + +def migrate(cr, version): + cr.execute(""" + SELECT value FROM ir_config_parameter + WHERE key = 'ir_attachment.location' + """) + row = cr.fetchone() + bucket = os.environ.get('AWS_BUCKETNAME') + if row[0] == 's3' and bucket: + cr.execute(""" + UPDATE ir_attachment + SET store_fname = 's3://' || %s || '/' || store_fname + WHERE store_fname IS NOT NULL AND store_fname NOT LIKE '%%/%%' + """, (os.environ['AWS_BUCKETNAME'],)) diff --git a/attachment_s3/models/ir_attachment.py b/attachment_s3/models/ir_attachment.py index 1a192a9..69a7de1 100644 --- a/attachment_s3/models/ir_attachment.py +++ b/attachment_s3/models/ir_attachment.py @@ -87,23 +87,9 @@ class IrAttachment(models.Model): @api.model def _file_read_s3(self, fname, bin_size=False): - try: - s3uri = S3Uri(fname) - except ValueError: - # compatibility mode: previously we stored only the key - # of the object, not we store the uri: - # example: - # before: fc02f84c0db500c69204972d27356ffdf0759386 - # now: s3://bucket/fc02f84c0db500c69204972d27356ffdf0759386 - # where 'project-odoo-prod' is the bucket name - bucket_name = None - item_name = fname - else: - bucket_name = s3uri.bucket() - item_name = s3uri.item() - - bucket = self._get_s3_bucket(name=bucket_name) - filekey = bucket.get_key(item_name) + s3uri = S3Uri(fname) + bucket = self._get_s3_bucket(name=s3uri.bucket()) + filekey = bucket.get_key(s3uri.item()) if filekey: read = base64.b64encode(filekey.get_contents_as_string()) else: @@ -113,23 +99,11 @@ class IrAttachment(models.Model): @api.model def _file_read(self, fname, bin_size=False): - storage = self._storage() - if storage == 's3' or fname.startswith('s3://'): - read = self._file_read_s3(fname, bin_size=bin_size) - if not read and not fname.startswith('s3://'): - # If the attachment has been created before the installation - # of the addon, it might still be stored on the filesystem. - # Fallback on the filesystem read. - try: - _super = super(IrAttachment, self) - read = _super._file_read(fname, bin_size=bin_size) - except (IOError, OSError): - # File is missing - read = '' + if fname.startswith('s3://'): + return self._file_read_s3(fname, bin_size=bin_size) else: _super = super(IrAttachment, self) - read = _super._file_read(fname, bin_size=bin_size) - return read + return _super._file_read(fname, bin_size=bin_size) @api.model def _file_write(self, value, checksum):