post-migration.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from odoo import api, SUPERUSER_ID
  2. import logging
  3. _logger = logging.getLogger(__name__)
  4. def migrate(cr, version):
  5. env = api.Environment(cr, SUPERUSER_ID, {})
  6. # Eliminar la restricción de clave foránea problemática
  7. # El campo visibility_condition_m2o_id es polimórfico y puede referenciar diferentes modelos
  8. # por lo que no debe tener una restricción de clave foránea fija
  9. try:
  10. cr.execute("""
  11. ALTER TABLE helpdesk_template_field
  12. DROP CONSTRAINT IF EXISTS helpdesk_template_field_visibility_condition_m2o_id_fkey
  13. """)
  14. _logger.info("Restricción de clave foránea eliminada")
  15. except Exception as e:
  16. _logger.warning(f"No se pudo eliminar la restricción (puede que ya no exista): {e}")
  17. # Migrar valores antiguos de ir.module.module a helpdesk.affected.module
  18. # Buscar campos de template que usen affected_module_id como dependencia
  19. cr.execute("""
  20. SELECT
  21. htf.id,
  22. htf.visibility_condition,
  23. htf.visibility_condition_m2o_id,
  24. imf.id as field_id,
  25. imf.relation as relation_model
  26. FROM helpdesk_template_field htf
  27. JOIN ir_model_fields imf ON htf.visibility_dependency = imf.id
  28. WHERE imf.name = 'affected_module_id'
  29. AND imf.model = 'helpdesk.ticket'
  30. AND (htf.visibility_condition_m2o_id IS NOT NULL
  31. OR (htf.visibility_condition IS NOT NULL AND htf.visibility_condition ~ '^[0-9]+$'))
  32. """)
  33. records_to_migrate = cr.fetchall()
  34. migrated_count = 0
  35. if records_to_migrate:
  36. # Migrar cada registro
  37. for record_id, condition, m2o_id, field_id, relation_model in records_to_migrate:
  38. try:
  39. old_id = m2o_id or (int(condition) if condition and condition.isdigit() else None)
  40. if old_id and relation_model == 'ir.module.module':
  41. # Buscar el módulo antiguo
  42. old_module = env['ir.module.module'].browse(old_id)
  43. if old_module.exists():
  44. module_code = old_module.name
  45. # Buscar el equivalente en el nuevo catálogo
  46. new_module = env['helpdesk.affected.module'].search([
  47. ('code', '=', module_code)
  48. ], limit=1)
  49. if new_module:
  50. # Actualizar el ID
  51. new_id = new_module.id
  52. cr.execute("""
  53. UPDATE helpdesk_template_field
  54. SET visibility_condition_m2o_id = %s,
  55. visibility_condition = %s
  56. WHERE id = %s
  57. """, (new_id, str(new_id), record_id))
  58. migrated_count += 1
  59. else:
  60. # Si no existe en el nuevo catálogo, limpiar el valor
  61. cr.execute("""
  62. UPDATE helpdesk_template_field
  63. SET visibility_condition_m2o_id = NULL,
  64. visibility_condition = NULL
  65. WHERE id = %s
  66. """, (record_id,))
  67. migrated_count += 1
  68. except Exception as e:
  69. _logger.warning(f"Error migrando registro {record_id}: {e}")
  70. continue
  71. cr.commit()
  72. _logger.info(f"Migración completada: {migrated_count} registros procesados de {len(records_to_migrate)} encontrados")