You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Antiun Ingeniería S.L. - Jairo Llopis
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import api, SUPERUSER_ID
  5. import logging
  6. _logger = logging.getLogger(__name__)
  7. def pre_init_hook_for_submodules(cr, model, field):
  8. """Moves images from single to multi mode.
  9. Feel free to use this as a ``pre_init_hook`` for submodules.
  10. :param str model:
  11. Model name, like ``product.template``.
  12. :param str field:
  13. Binary field that had the images in that :param:`model`, like
  14. ``image``.
  15. """
  16. env = api.Environment(cr, SUPERUSER_ID, dict())
  17. with cr.savepoint():
  18. cr.execute(
  19. """
  20. INSERT INTO base_multi_image_image (
  21. owner_id,
  22. owner_model,
  23. storage,
  24. file_db_store
  25. )
  26. SELECT
  27. id,
  28. %%s,
  29. 'db',
  30. %(field)s
  31. FROM
  32. %(table)s
  33. WHERE
  34. %(field)s IS NOT NULL
  35. """ % {"table": env[model]._table, "field": field},
  36. (model,)
  37. )
  38. def pre_init_hook(cr):
  39. """run the migration for product_images"""
  40. migrate_from_product_images(cr)
  41. def migrate_from_product_images(cr):
  42. """If we're installed on a database which has product_images from 7,
  43. move its table so that we use the already existing images"""
  44. cr.execute("SELECT 1 FROM pg_class WHERE relname = 'product_images'")
  45. if not cr.fetchone():
  46. return
  47. cr.execute(
  48. 'alter table product_images rename to base_multi_image_image')
  49. cr.execute(
  50. 'alter table base_multi_image_image rename product_id to owner_id')
  51. cr.execute(
  52. 'alter table base_multi_image_image '
  53. "add column owner_model varchar not null default 'product.template',"
  54. "add column storage varchar not null default 'db'")
  55. cr.execute(
  56. 'alter table base_multi_image_image '
  57. 'alter column owner_model drop default')
  58. # we assume all images apply to all variants
  59. cr.execute(
  60. "update base_multi_image_image set "
  61. "owner_id=p.product_tmpl_id "
  62. "from product_product p where p.id=owner_id"
  63. )
  64. # and there might be dangling links
  65. cr.execute('delete from base_multi_image_image where owner_id is null')