| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- from odoo import api, SUPERUSER_ID
- import logging
- _logger = logging.getLogger(__name__)
- def migrate(cr, version):
- env = api.Environment(cr, SUPERUSER_ID, {})
-
- # Eliminar la restricción de clave foránea problemática
- # El campo visibility_condition_m2o_id es polimórfico y puede referenciar diferentes modelos
- # por lo que no debe tener una restricción de clave foránea fija
- try:
- cr.execute("""
- ALTER TABLE helpdesk_template_field
- DROP CONSTRAINT IF EXISTS helpdesk_template_field_visibility_condition_m2o_id_fkey
- """)
- _logger.info("Restricción de clave foránea eliminada")
- except Exception as e:
- _logger.warning(f"No se pudo eliminar la restricción (puede que ya no exista): {e}")
-
- # Migrar valores antiguos de ir.module.module a helpdesk.affected.module
- # Buscar campos de template que usen affected_module_id como dependencia
- cr.execute("""
- SELECT
- htf.id,
- htf.visibility_condition,
- htf.visibility_condition_m2o_id,
- imf.id as field_id,
- imf.relation as relation_model
- FROM helpdesk_template_field htf
- JOIN ir_model_fields imf ON htf.visibility_dependency = imf.id
- WHERE imf.name = 'affected_module_id'
- AND imf.model = 'helpdesk.ticket'
- AND (htf.visibility_condition_m2o_id IS NOT NULL
- OR (htf.visibility_condition IS NOT NULL AND htf.visibility_condition ~ '^[0-9]+$'))
- """)
-
- records_to_migrate = cr.fetchall()
- migrated_count = 0
-
- if records_to_migrate:
- # Migrar cada registro
- for record_id, condition, m2o_id, field_id, relation_model in records_to_migrate:
- try:
- old_id = m2o_id or (int(condition) if condition and condition.isdigit() else None)
-
- if old_id and relation_model == 'ir.module.module':
- # Buscar el módulo antiguo
- old_module = env['ir.module.module'].browse(old_id)
- if old_module.exists():
- module_code = old_module.name
- # Buscar el equivalente en el nuevo catálogo
- new_module = env['helpdesk.affected.module'].search([
- ('code', '=', module_code)
- ], limit=1)
-
- if new_module:
- # Actualizar el ID
- new_id = new_module.id
- cr.execute("""
- UPDATE helpdesk_template_field
- SET visibility_condition_m2o_id = %s,
- visibility_condition = %s
- WHERE id = %s
- """, (new_id, str(new_id), record_id))
- migrated_count += 1
- else:
- # Si no existe en el nuevo catálogo, limpiar el valor
- cr.execute("""
- UPDATE helpdesk_template_field
- SET visibility_condition_m2o_id = NULL,
- visibility_condition = NULL
- WHERE id = %s
- """, (record_id,))
- migrated_count += 1
- except Exception as e:
- _logger.warning(f"Error migrando registro {record_id}: {e}")
- continue
-
- cr.commit()
- _logger.info(f"Migración completada: {migrated_count} registros procesados de {len(records_to_migrate)} encontrados")
|