CREATE TABLE `was_comment_metrics` (
  `comment_metrics_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `comment_id` int(11) unsigned NOT NULL,
  `likes_count` int(11) unsigned NOT NULL,
  `insert_timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `update_timestamp` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`comment_metrics_id`),
  UNIQUE KEY `idx_comment_id` (`comment_id`)
) ENGINE=Innodb;

CREATE TRIGGER `was_comment_metrics_insert_timestamp` BEFORE INSERT ON `was_comment_metrics` FOR EACH ROW SET NEW.insert_timestamp = NOW();

INSERT INTO was_comment_metrics(comment_id, likes_count)
SELECT
    comment_id,
    COUNT(*) AS likes_count
FROM
    was_comment_feedback
GROUP BY
    comment_id;
