USE provisioning;
ALTER TABLE voip_dbaliases
  ADD COLUMN devid_alias VARCHAR(127) DEFAULT NULL;

USE kamailio;
ALTER TABLE dbaliases ADD COLUMN devid_alias VARCHAR(127) DEFAULT NULL;

USE provisioning;

DROP TRIGGER voip_dba_crepl_trig;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`sipwise`@`localhost`*/ /*!50003 TRIGGER voip_dba_crepl_trig AFTER INSERT ON voip_dbaliases
  FOR EACH ROW BEGIN
  DECLARE dbalias_domain varchar(127);
  DECLARE target_username varchar(127);
  DECLARE target_domain varchar(127);

  SELECT domain INTO dbalias_domain FROM voip_domains where id = NEW.domain_id;
  SELECT a.username, b.domain INTO target_username, target_domain
    FROM voip_subscribers a, voip_domains b
    WHERE a.id <=> NEW.subscriber_id
    AND b.id <=> a.domain_id;

  INSERT INTO kamailio.dbaliases (alias_username, alias_domain,
    username, domain, is_primary, is_devid, devid_alias)
    VALUES(NEW.username, dbalias_domain, target_username, target_domain, NEW.is_primary, NEW.is_devid, NEW.devid_alias);
  END */;;
DELIMITER ;

DROP TRIGGER voip_dba_urepl_trig;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`sipwise`@`localhost`*/ /*!50003 TRIGGER voip_dba_urepl_trig AFTER UPDATE ON voip_dbaliases
  FOR EACH ROW BEGIN
  DECLARE old_dbalias_domain varchar(127);
  DECLARE new_dbalias_domain varchar(127);
  DECLARE target_username varchar(127);
  DECLARE target_domain varchar(127);

  SELECT domain INTO old_dbalias_domain FROM voip_domains where id = OLD.domain_id;
  SELECT domain INTO new_dbalias_domain FROM voip_domains where id = NEW.domain_id;
  SELECT a.username, b.domain INTO target_username, target_domain
    FROM voip_subscribers a, voip_domains b
    WHERE a.id <=> NEW.subscriber_id
    AND b.id <=> a.domain_id;

  UPDATE kamailio.dbaliases SET alias_username = NEW.username, alias_domain = new_dbalias_domain,
    username = target_username, domain = target_domain, is_primary = NEW.is_primary,
    is_devid = NEW.is_devid, devid_alias = NEW.devid_alias
    WHERE alias_username <=> OLD.username
    AND alias_domain <=> old_dbalias_domain
	AND is_primary <=> OLD.is_primary
	AND is_devid <=> OLD.is_devid
	AND devid_alias <=> OLD.devid_alias;
  END */;;
DELIMITER ;

