DROP TRIGGER IF EXISTS `update_session_trig`;
DELIMITER ;;
CREATE TRIGGER `update_session_trig`
AFTER UPDATE ON `sessions`
FOR EACH ROW 
BEGIN
	DECLARE user_type_var INT;
	SELECT type.user_type INTO user_type_var FROM user_type_t AS type INNER JOIN users AS user ON user.type = type.user_type WHERE user.id = NEW.user_id;
	IF OLD.session_id != NEW.session_id THEN
    	INSERT INTO `audit_trail` (`action_type`,`user_id`,`user_type`,`action_timestamp`) VALUES (1,NEW.user_id,user_type_var,NOW());
  END IF;
END;;
DELIMITER ;
