Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exisiting attachments migration #310

Open
JacooRe opened this issue Dec 11, 2023 · 5 comments
Open

exisiting attachments migration #310

JacooRe opened this issue Dec 11, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@JacooRe
Copy link

JacooRe commented Dec 11, 2023

This is not documented anywhere I looked whatsoever, so whats our options to migrate existing attachments to an s3 object store ?

  • a button that will start the migration based on my configured setup
@JacooRe JacooRe added the enhancement New feature or request label Dec 11, 2023
@miikanissi
Copy link

I'm wondering about this myself also. Browsing the fs_attachment code I noticed they have extended the base Odoo force_storage() method on ir.attachment. I tried calling that and it indeed started to migrate all the attachments in the system to our S3.

However it started to migrate every attachment which is not what I was looking for. I was hoping for a way to migrate only selected attachments.

@bealdav
Copy link
Member

bealdav commented Jan 23, 2024

Thanks to have investigated on this topic @miikanissi .
Do you any suggestion to filter attachment: maybe by res_field ?
Thanks in advance.

@bealdav
Copy link
Member

bealdav commented Feb 14, 2024

Maybe can help

import logging

from odoo import models

logger = logging.getLogger(__name__)


def get_domain_for_fs_images():
    return [("image_1920", "!=", False), ("main_image_id", "=", False)]


def get_records(self, limit):
    ids = self.env.context.get("active_ids")
    domain = get_domain_for_fs_images()
    if ids:
        domain.append(("id", "in", ids))
    products = self.env[self._name].search(domain, limit=limit)
    names = [x.display_name for x in products]
    logger.info(f"{len(products)} {self._name} with image moved: {names}")
    return products


class ProductTemplate(models.Model):
    _inherit = "product.template"

    def _move_image_to_fs(self, limit=None):
        """can be called by
        - a server action
        - by shell : should specify limit in this case
        """
        products = get_records(self, limit)
        attach = {
            x.res_id: x
            for x in self.env["ir.attachment"].search(
                [
                    ("res_model", "=", "product.template"),
                    ("res_id", "in", products.ids),
                    ("res_field", "=", "image_1920"),
                ]
            )
        }
        for rec in products:
            self.env["fs.product.image"].create(
                {
                    "product_tmpl_id": rec.id,
                    "specific_image": {
                        "content": rec.image_1920,
                        "filename": attach.get(rec.id) and attach[rec.id].name,
                    },
                }
            )
        # Probably need to move variants images from here but as it depends
        # on product attribute values it's more tricky
<odoo>

    <record id="move_img2fs_product_act_serv" model="ir.actions.server">
        <field name="name">Move Image to File Storage</field>
        <field name="model_id" ref="model_product_template" />
        <field name="binding_model_id" ref="model_product_template" />
        <field name="state">code</field>
        <field name="code">env["product.template"]._move_image_to_fs()</field>
    </record>

</odoo>

@CasVissers-360ERP
Copy link

@miikanissi did you find something here? force_storage stores it on S3 but somehow doesn't set fs_filename and updates the store_fname

@CasVissers-360ERP
Copy link

In the end I managed to move it correctly (I think), this worked for me but please test carefully (disclaimer):

  • Run _move_attachment_to_store on attachments that need to be moved
  • After removal run _gc_file_store_unsafe to remove the files from the Odoo filestore

I think the issue with force_storage is that it times out and is never ran completely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants