set autocommit=0;
use provisioning;

DROP TRIGGER IF EXISTS provisioning.voip_faxp_crepl_trig;
DROP TRIGGER IF EXISTS provisioning.voip_faxp_urepl_trig;
DROP TRIGGER IF EXISTS provisioning.voip_faxp_drepl_trig;
DROP TRIGGER IF EXISTS provisioning.voip_faxd_crepl_trig;
DROP TRIGGER IF EXISTS provisioning.voip_faxd_urepl_trig;
DROP TRIGGER IF EXISTS provisioning.voip_faxd_drepl_trig;

CREATE TABLE `voip_fax_journal` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `subscriber_id` int(10) unsigned NOT NULL,
  `time` decimal(13,3) NOT NULL,
  `direction` enum('in','out','mtf') NOT NULL,
  `duration` int(11) unsigned NOT NULL DEFAULT '0',
  `caller` varchar(255) NOT NULL,
  `callee` varchar(255) NOT NULL,
  `pages` int(10) unsigned NOT NULL DEFAULT '0',
  `reason` varchar(255) NOT NULL,
  `status` varchar(255) NOT NULL,
  `signal_rate` int(10) unsigned NOT NULL DEFAULT '0',
  `quality` varchar(255) NOT NULL DEFAULT '',
  `filename` varchar(255) NOT NULL DEFAULT '',
  `sid` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `voip_fj_sub_dir_idx` (`subscriber_id`,`direction`),
  KEY `voip_fj_time_idx` (`time`),
  CONSTRAINT `v_fj_ref` FOREIGN KEY (`subscriber_id`) REFERENCES `voip_subscribers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `voip_mail_to_fax_preferences` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `subscriber_id` int(11) unsigned NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT '0',
  `secret_key` varchar(255) DEFAULT NULL,
  `last_secret_key_modify` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `secret_key_renew` enum('never','daily','weekly','monthly') NOT NULL DEFAULT 'never',
  PRIMARY KEY (`id`),
  UNIQUE KEY `mtf_p_sub_idx` (`subscriber_id`),
  CONSTRAINT `v_mtf_p_subscriber_id_ref` FOREIGN KEY (`subscriber_id`) REFERENCES `voip_subscribers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `voip_mail_to_fax_secret_renew_notify` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `subscriber_id` int(11) unsigned NOT NULL,
  `destination` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `mtf_srn_subdest_idx` (`subscriber_id`,`destination`),
  CONSTRAINT `v_mtf_secret_renew_notify_ref` FOREIGN KEY (`subscriber_id`) REFERENCES `voip_subscribers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `voip_mail_to_fax_acl` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `subscriber_id` int(11) unsigned NOT NULL,
  `from_email` varchar(255) DEFAULT NULL,
  `received_from` varchar(255) DEFAULT NULL,
  `destination` varchar(255) DEFAULT NULL,
  `use_regex` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `mtf_acl_sub_idx` (`subscriber_id`),
  KEY `voip_mtf_acl_fe_idx` (`from_email`),
  CONSTRAINT `v_mtf_acl_ref` FOREIGN KEY (`subscriber_id`) REFERENCES `voip_subscribers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

commit;
