@@ -602,38 +602,6 @@ DELIMITER ;;
602
602
ON DUPLICATE KEY UPDATE `count` = `count` + 1;
603
603
END IF;
604
604
605
- END */ ;;
606
- DELIMITER ;
607
- /* !50003 SET sql_mode = @saved_sql_mode */ ;
608
- /* !50003 SET character_set_client = @saved_cs_client */ ;
609
- /* !50003 SET character_set_results = @saved_cs_results */ ;
610
- /* !50003 SET collation_connection = @saved_col_connection */ ;
611
- /* !50003 SET @saved_cs_client = @@character_set_client */ ;
612
- /* !50003 SET @saved_cs_results = @@character_set_results */ ;
613
- /* !50003 SET @saved_col_connection = @@collation_connection */ ;
614
- /* !50003 SET character_set_client = utf8mb3 */ ;
615
- /* !50003 SET character_set_results = utf8mb3 */ ;
616
- /* !50003 SET collation_connection = utf8mb3_general_ci */ ;
617
- /* !50003 SET @saved_sql_mode = @@sql_mode */ ;
618
- /* !50003 SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO' */ ;
619
- DELIMITER ;;
620
- /* !50003 CREATE*/ /* !50017 DEFINER=`root`@`%`*/ /* !50003 TRIGGER `respondents_upd` AFTER UPDATE ON `respondents` FOR EACH ROW BEGIN
621
-
622
- UPDATE respondent_stats
623
- SET `count` = `count` - 1
624
- WHERE survey_id = OLD.survey_id
625
- AND questionnaire_id = IFNULL(OLD.questionnaire_id, 0)
626
- AND state = OLD.state
627
- AND disposition = OLD.disposition
628
- AND quota_bucket_id = IFNULL(OLD.quota_bucket_id, 0)
629
- AND mode = IFNULL(OLD.mode, '')
630
- ;
631
-
632
- INSERT INTO respondent_stats(survey_id, questionnaire_id, state, disposition, quota_bucket_id, mode, `count`)
633
- VALUES (NEW.survey_id, IFNULL(NEW.questionnaire_id, 0), NEW.state, NEW.disposition, IFNULL(NEW.quota_bucket_id, 0), IFNULL(NEW.mode, ''), 1)
634
- ON DUPLICATE KEY UPDATE `count` = `count` + 1
635
- ;
636
-
637
605
END */ ;;
638
606
DELIMITER ;
639
607
/* !50003 SET sql_mode = @saved_sql_mode */ ;
@@ -665,6 +633,62 @@ DELIMITER ;;
665
633
AND date = DATE(OLD.updated_at);
666
634
END IF;
667
635
636
+ END */ ;;
637
+ DELIMITER ;
638
+ /* !50003 SET sql_mode = @saved_sql_mode */ ;
639
+ /* !50003 SET character_set_client = @saved_cs_client */ ;
640
+ /* !50003 SET character_set_results = @saved_cs_results */ ;
641
+ /* !50003 SET collation_connection = @saved_col_connection */ ;
642
+ /* !50003 SET @saved_cs_client = @@character_set_client */ ;
643
+ /* !50003 SET @saved_cs_results = @@character_set_results */ ;
644
+ /* !50003 SET @saved_col_connection = @@collation_connection */ ;
645
+ /* !50003 SET character_set_client = utf8mb4 */ ;
646
+ /* !50003 SET character_set_results = utf8mb4 */ ;
647
+ /* !50003 SET collation_connection = utf8mb4_general_ci */ ;
648
+ /* !50003 SET @saved_sql_mode = @@sql_mode */ ;
649
+ /* !50003 SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' */ ;
650
+ DELIMITER ;;
651
+ /* !50003 CREATE*/ /* !50017 DEFINER=`root`@`%`*/ /* !50003 TRIGGER `respondents_upd` AFTER UPDATE ON `respondents` FOR EACH ROW BEGIN
652
+
653
+ # This is a hack for using the `select for update`
654
+ # so we can lock all the records that will get updated
655
+ # by this trigger before actually modifying them
656
+ # so we prevent this trigger to generate deadlocks
657
+ # see #1744
658
+ DECLARE temp_stats INT;
659
+
660
+ SELECT count(*)
661
+ INTO temp_stats
662
+ FROM respondent_stats
663
+ WHERE (survey_id = OLD.survey_id
664
+ AND questionnaire_id = IFNULL(OLD.questionnaire_id, 0)
665
+ AND state = OLD.state
666
+ AND disposition = OLD.disposition
667
+ AND quota_bucket_id = IFNULL(OLD.quota_bucket_id, 0)
668
+ AND mode = IFNULL(OLD.mode, ''))
669
+ OR (survey_id = NEW.survey_id
670
+ AND questionnaire_id = IFNULL(NEW.questionnaire_id, 0)
671
+ AND state = NEW.state
672
+ AND disposition = NEW.disposition
673
+ AND quota_bucket_id = IFNULL(NEW.quota_bucket_id, 0)
674
+ AND mode = IFNULL(NEW.mode, ''))
675
+ FOR UPDATE;
676
+
677
+ UPDATE respondent_stats
678
+ SET `count` = `count` - 1
679
+ WHERE survey_id = OLD.survey_id
680
+ AND questionnaire_id = IFNULL(OLD.questionnaire_id, 0)
681
+ AND state = OLD.state
682
+ AND disposition = OLD.disposition
683
+ AND quota_bucket_id = IFNULL(OLD.quota_bucket_id, 0)
684
+ AND mode = IFNULL(OLD.mode, '')
685
+ ;
686
+
687
+ INSERT INTO respondent_stats(survey_id, questionnaire_id, state, disposition, quota_bucket_id, mode, `count`)
688
+ VALUES (NEW.survey_id, IFNULL(NEW.questionnaire_id, 0), NEW.state, NEW.disposition, IFNULL(NEW.quota_bucket_id, 0), IFNULL(NEW.mode, ''), 1)
689
+ ON DUPLICATE KEY UPDATE `count` = `count` + 1
690
+ ;
691
+
668
692
END */ ;;
669
693
DELIMITER ;
670
694
/* !50003 SET sql_mode = @saved_sql_mode */ ;
@@ -994,7 +1018,7 @@ CREATE TABLE `users` (
994
1018
/* !40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */ ;
995
1019
/* !40111 SET SQL_NOTES=@OLD_SQL_NOTES */ ;
996
1020
997
- -- Dump completed on 2024-02-21 5:26:53
1021
+ -- Dump completed on 2024-04-22 22:06:45
998
1022
INSERT INTO ` schema_migrations` (version) VALUES (20160812145257 );
999
1023
INSERT INTO ` schema_migrations` (version) VALUES (20160816183915 );
1000
1024
INSERT INTO ` schema_migrations` (version) VALUES (20160830200454 );
@@ -1213,3 +1237,4 @@ INSERT INTO `schema_migrations` (version) VALUES (20230402091100);
1213
1237
INSERT INTO ` schema_migrations` (version) VALUES (20230405111657 );
1214
1238
INSERT INTO ` schema_migrations` (version) VALUES (20230413101342 );
1215
1239
INSERT INTO ` schema_migrations` (version) VALUES (20230821100203 );
1240
+ INSERT INTO ` schema_migrations` (version) VALUES (20240422175453 );
0 commit comments