From 4fccfb9bcb36abad99050e0b4cde6ba99dd992d6 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Thu, 15 Sep 2016 14:35:01 +0200 Subject: [PATCH] [ADD] migrate from product_images v7 if necessary --- base_multi_image/__init__.py | 2 +- base_multi_image/__openerp__.py | 1 + base_multi_image/hooks.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/base_multi_image/__init__.py b/base_multi_image/__init__.py index ef1af5949..99a0d7c5e 100644 --- a/base_multi_image/__init__.py +++ b/base_multi_image/__init__.py @@ -3,5 +3,5 @@ # Pedro M. Baeza # © 2015 Antiun Ingeniería S.L. - Jairo Llopis # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - from . import models +from .hooks import pre_init_hook diff --git a/base_multi_image/__openerp__.py b/base_multi_image/__openerp__.py index 524b8ddcf..50df28354 100644 --- a/base_multi_image/__openerp__.py +++ b/base_multi_image/__openerp__.py @@ -24,4 +24,5 @@ "images/form.png", "images/kanban.png", ], + "pre_init_hook": "pre_init_hook", } diff --git a/base_multi_image/hooks.py b/base_multi_image/hooks.py index 74d29ed55..6b2e2b58a 100644 --- a/base_multi_image/hooks.py +++ b/base_multi_image/hooks.py @@ -42,3 +42,35 @@ def pre_init_hook_for_submodules(cr, model, field): """ % {"table": env[model]._table, "field": field}, (model,) ) + + +def pre_init_hook(cr): + """run the migration for product_images""" + migrate_from_product_images(cr) + + +def migrate_from_product_images(cr): + """If we're installed on a database which has product_images from 7, + move its table so that we use the already existing images""" + cr.execute("SELECT 1 FROM pg_class WHERE relname = 'product_images'") + if not cr.fetchone(): + return + cr.execute( + 'alter table product_images rename to base_multi_image_image') + cr.execute( + 'alter table base_multi_image_image rename product_id to owner_id') + cr.execute( + 'alter table base_multi_image_image ' + "add column owner_model varchar not null default 'product.template'," + "add column storage varchar not null default 'db'") + cr.execute( + 'alter table base_multi_image_image ' + 'alter column owner_model drop default') + # we assume all images apply to all variants + cr.execute( + "update base_multi_image_image set " + "owner_id=p.product_tmpl_id " + "from product_product p where p.id=owner_id" + ) + # and there might be dangling links + cr.execute('delete from base_multi_image_image where owner_id is null')