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.

82 lines
2.6 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 sequence product_images_id_seq '
  51. 'rename to base_multi_image_image_id_seq')
  52. cr.execute(
  53. 'alter table base_multi_image_image rename product_id to owner_id')
  54. cr.execute(
  55. 'alter table base_multi_image_image '
  56. 'drop constraint product_images_product_id_fkey')
  57. cr.execute(
  58. 'alter table base_multi_image_image '
  59. "add column owner_model varchar not null default 'product.template',"
  60. "add column storage varchar not null default 'db'")
  61. cr.execute(
  62. 'alter table base_multi_image_image '
  63. 'alter column owner_model drop default')
  64. # we assume all images apply to all variants
  65. cr.execute(
  66. "update base_multi_image_image set "
  67. "owner_id=p.product_tmpl_id "
  68. "from product_product p where p.id=owner_id"
  69. )
  70. # and there might be dangling links
  71. cr.execute('delete from base_multi_image_image where owner_id is null')