diff --git a/lib/ask/runtime/channel_status_server.ex b/lib/ask/runtime/channel_status_server.ex index 2fcf2d6ff..d47040c48 100644 --- a/lib/ask/runtime/channel_status_server.ex +++ b/lib/ask/runtime/channel_status_server.ex @@ -21,7 +21,11 @@ defmodule Ask.Runtime.ChannelStatusServer do end def poll(pid) do - send(pid, :poll) + send(pid, :poll_once) + end + + def wait(pid) do + GenServer.call(pid, :get) end def get_channel_status(channel_id) do @@ -36,29 +40,31 @@ defmodule Ask.Runtime.ChannelStatusServer do {:reply, get_status_from_state(channel_id, state), state} end + def handle_call(:get, _, state) do + {:reply, state, state} + end + def handle_info(:poll, state) do log_info("polling") try do - Survey.running_channels() - |> Repo.preload(:user) - |> Enum.each(fn c -> + poll_channels(state) - unless c.paused do - previous_status = get_status_from_state(c.id, state) + {:noreply, state} + after + :timer.send_after(@poll_interval, :poll) + end + end - spawn(fn -> - status = ChannelBroker.check_status(c.id) - timestamp = Timex.now() + def handle_info(:poll_once, state) do + log_info("poll forced") - process_channel_status_change(status, previous_status, timestamp, c) - end) - end - end) + try do + poll_channels(state) {:noreply, state} after - :timer.send_after(@poll_interval, :poll) + nil end end @@ -74,6 +80,24 @@ defmodule Ask.Runtime.ChannelStatusServer do Logger.info("ChannelStatusServer: #{message}") end + defp poll_channels(state) do + Survey.running_channels() + |> Repo.preload(:user) + |> Enum.each(fn c -> + + unless c.paused do + previous_status = get_status_from_state(c.id, state) + + spawn(fn -> + status = ChannelBroker.check_status(c.id) + timestamp = Timex.now() + + process_channel_status_change(status, previous_status, timestamp, c) + end) + end + end) + end + defp process_channel_status_change({:down, _messages}, %{status: :down}, _timestamp, _channel) do nil end diff --git a/lib/ask_web/controllers/channel_controller.ex b/lib/ask_web/controllers/channel_controller.ex index 8cc6f8b8f..aa7d8e475 100644 --- a/lib/ask_web/controllers/channel_controller.ex +++ b/lib/ask_web/controllers/channel_controller.ex @@ -107,12 +107,12 @@ defmodule AskWeb.ChannelController do render(conn, "show.json", channel: channel |> Repo.preload([:projects, :user])) end - def pause(conn, %{"id" => id}) do + def pause(conn, %{"channel_id" => id}) do channel_params = %{"paused" => true} AskWeb.ChannelController.update(conn, %{"id" => id, "channel" => channel_params}) end - def unpause(conn, %{"id" => id}) do + def unpause(conn, %{"channel_id" => id}) do channel_params = %{"paused" => false} AskWeb.ChannelController.update(conn, %{"id" => id, "channel" => channel_params}) end diff --git a/priv/repo/structure.sql b/priv/repo/structure.sql index efa4dd542..1aa2e07c7 100644 --- a/priv/repo/structure.sql +++ b/priv/repo/structure.sql @@ -1,9 +1,8 @@ -Info: Using unique option prefix 'database' is error-prone and can break in the future. Please use the full name 'databases' instead. -- MySQL dump 10.19 Distrib 10.3.39-MariaDB, for debian-linux-gnu (x86_64) -- -- Host: db Database: ask_dev -- ------------------------------------------------------ --- Server version 8.0.35 +-- Server version 8.0.36 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; @@ -16,1017 +15,6 @@ Info: Using unique option prefix 'database' is error-prone and can break in the /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; --- --- Current Database: `ask_dev` --- - -CREATE DATABASE /*!32312 IF NOT EXISTS*/ `ask_dev` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */; - -USE `ask_dev`; - --- --- Table structure for table `activity_log` --- - -DROP TABLE IF EXISTS `activity_log`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `activity_log` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `project_id` bigint unsigned DEFAULT NULL, - `user_id` bigint unsigned DEFAULT NULL, - `entity_type` varchar(255) DEFAULT NULL, - `entity_id` int DEFAULT NULL, - `action` varchar(255) DEFAULT NULL, - `metadata` text, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `remote_ip` varchar(255) DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `activity_log_project_id_fkey` (`project_id`), - KEY `activity_log_user_id_fkey` (`user_id`), - CONSTRAINT `activity_log_project_id_fkey` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`), - CONSTRAINT `activity_log_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `audios` --- - -DROP TABLE IF EXISTS `audios`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `audios` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `uuid` char(36) CHARACTER SET ascii COLLATE ascii_general_ci DEFAULT NULL, - `data` mediumblob, - `filename` varchar(255) DEFAULT NULL, - `source` varchar(255) DEFAULT NULL, - `duration` int DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `channel_broker_queue` --- - -DROP TABLE IF EXISTS `channel_broker_queue`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `channel_broker_queue` ( - `channel_id` bigint unsigned NOT NULL, - `respondent_id` bigint unsigned NOT NULL, - `queued_at` datetime NOT NULL, - `priority` tinyint NOT NULL, - `size` int NOT NULL, - `token` varchar(255) NOT NULL, - `not_before` datetime DEFAULT NULL, - `not_after` datetime DEFAULT NULL, - `reply` blob, - `last_contact` datetime DEFAULT NULL, - `contacts` int DEFAULT NULL, - `channel_state` blob, - PRIMARY KEY (`channel_id`,`respondent_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `channels` --- - -DROP TABLE IF EXISTS `channels`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `channels` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `name` varchar(255) DEFAULT NULL, - `type` varchar(255) DEFAULT NULL, - `provider` varchar(255) DEFAULT NULL, - `settings` text, - `user_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `base_url` varchar(255) DEFAULT NULL, - `patterns` text, - `paused` tinyint(1) DEFAULT '0', - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `channels_user_id_index` (`user_id`), - CONSTRAINT `channels_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `completed_respondents` --- - -DROP TABLE IF EXISTS `completed_respondents`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `completed_respondents` ( - `survey_id` bigint unsigned NOT NULL, - `questionnaire_id` int NOT NULL, - `quota_bucket_id` int NOT NULL, - `mode` varchar(255) NOT NULL, - `date` date NOT NULL, - `count` int DEFAULT '0', - PRIMARY KEY (`survey_id`,`questionnaire_id`,`quota_bucket_id`,`mode`,`date`), - CONSTRAINT `completed_respondents_survey_id_fkey` FOREIGN KEY (`survey_id`) REFERENCES `surveys` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `floip_endpoints` --- - -DROP TABLE IF EXISTS `floip_endpoints`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `floip_endpoints` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `survey_id` bigint unsigned DEFAULT NULL, - `uri` varchar(255) DEFAULT NULL, - `last_pushed_response_id` bigint unsigned DEFAULT NULL, - `retries` int DEFAULT '0', - `name` varchar(255) DEFAULT NULL, - `state` varchar(255) DEFAULT NULL, - `auth_token` varchar(255) DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - UNIQUE KEY `floip_endpoints_survey_id_uri_auth_token_index` (`survey_id`,`uri`,`auth_token`), - KEY `floip_endpoints_last_pushed_response_id_fkey` (`last_pushed_response_id`), - KEY `floip_endpoints_survey_id_index` (`survey_id`), - CONSTRAINT `floip_endpoints_last_pushed_response_id_fkey` FOREIGN KEY (`last_pushed_response_id`) REFERENCES `responses` (`id`), - CONSTRAINT `floip_endpoints_survey_id_fkey` FOREIGN KEY (`survey_id`) REFERENCES `surveys` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `folders` --- - -DROP TABLE IF EXISTS `folders`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `folders` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `name` varchar(255) DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `project_id` bigint unsigned DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - UNIQUE KEY `folders_name_project_id_index` (`name`,`project_id`), - KEY `folders_project_id_index` (`project_id`), - CONSTRAINT `folders_project_id_fkey` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `invites` --- - -DROP TABLE IF EXISTS `invites`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `invites` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `code` varchar(255) DEFAULT NULL, - `level` varchar(255) DEFAULT NULL, - `project_id` bigint unsigned DEFAULT NULL, - `email` varchar(255) DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `inviter_email` text, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - UNIQUE KEY `project_id` (`project_id`,`email`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `oauth_tokens` --- - -DROP TABLE IF EXISTS `oauth_tokens`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `oauth_tokens` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `provider` varchar(255) DEFAULT NULL, - `access_token` text, - `user_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `expires_at` datetime DEFAULT NULL, - `base_url` varchar(255) DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - UNIQUE KEY `oauth_tokens_user_id_provider_base_url_index` (`user_id`,`provider`,`base_url`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `panel_surveys` --- - -DROP TABLE IF EXISTS `panel_surveys`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `panel_surveys` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `name` varchar(255) DEFAULT NULL, - `project_id` bigint unsigned DEFAULT NULL, - `folder_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `panel_surveys_project_id_fkey` (`project_id`), - KEY `panel_surveys_folder_id_fkey` (`folder_id`), - CONSTRAINT `panel_surveys_folder_id_fkey` FOREIGN KEY (`folder_id`) REFERENCES `folders` (`id`), - CONSTRAINT `panel_surveys_project_id_fkey` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `project_channels` --- - -DROP TABLE IF EXISTS `project_channels`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `project_channels` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `channel_id` bigint unsigned DEFAULT NULL, - `project_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - UNIQUE KEY `project_channels_channel_id_project_id_index` (`channel_id`,`project_id`), - KEY `project_channels_project_id_fkey` (`project_id`), - CONSTRAINT `project_channels_channel_id_fkey` FOREIGN KEY (`channel_id`) REFERENCES `channels` (`id`) ON DELETE CASCADE, - CONSTRAINT `project_channels_project_id_fkey` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `project_memberships` --- - -DROP TABLE IF EXISTS `project_memberships`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `project_memberships` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `user_id` bigint unsigned DEFAULT NULL, - `project_id` bigint unsigned DEFAULT NULL, - `level` varchar(255) DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `project_memberships_user_id_fkey` (`user_id`), - KEY `project_memberships_project_id_fkey` (`project_id`), - CONSTRAINT `project_memberships_project_id_fkey` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`) ON DELETE CASCADE, - CONSTRAINT `project_memberships_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `projects` --- - -DROP TABLE IF EXISTS `projects`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `projects` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `name` varchar(255) DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `salt` varchar(255) DEFAULT NULL, - `colour_scheme` varchar(255) DEFAULT 'default', - `archived` tinyint(1) DEFAULT '0', - `timezone` varchar(255) DEFAULT NULL, - `initial_success_rate` double DEFAULT NULL, - `eligibility_rate` double DEFAULT NULL, - `response_rate` double DEFAULT NULL, - `valid_respondent_rate` double DEFAULT NULL, - `batch_limit_per_minute` int DEFAULT NULL, - `batch_size` int DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `questionnaire_variables` --- - -DROP TABLE IF EXISTS `questionnaire_variables`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `questionnaire_variables` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `name` varchar(255) DEFAULT NULL, - `project_id` bigint unsigned DEFAULT NULL, - `questionnaire_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `questionnaire_variables_project_id_index` (`project_id`), - KEY `questionnaire_variables_questionnaire_id_index` (`questionnaire_id`), - CONSTRAINT `questionnaire_variables_project_id_fkey` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`), - CONSTRAINT `questionnaire_variables_questionnaire_id_fkey` FOREIGN KEY (`questionnaire_id`) REFERENCES `questionnaires` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `questionnaires` --- - -DROP TABLE IF EXISTS `questionnaires`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `questionnaires` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `name` varchar(255) DEFAULT NULL, - `project_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `modes` varchar(255) DEFAULT NULL, - `steps` longtext, - `languages` text, - `default_language` varchar(255) DEFAULT NULL, - `valid` tinyint(1) DEFAULT NULL, - `settings` text, - `snapshot_of` bigint unsigned DEFAULT NULL, - `quota_completed_steps` longtext, - `deleted` tinyint(1) DEFAULT '0', - `description` text, - `partial_relevant_config` varchar(255) DEFAULT NULL, - `archived` tinyint(1) DEFAULT '0', - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `questionnaires_project_id_index` (`project_id`), - KEY `questionnaires_snapshot_of_fkey` (`snapshot_of`), - CONSTRAINT `questionnaires_project_id_fkey` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`), - CONSTRAINT `questionnaires_snapshot_of_fkey` FOREIGN KEY (`snapshot_of`) REFERENCES `questionnaires` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `quota_buckets` --- - -DROP TABLE IF EXISTS `quota_buckets`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `quota_buckets` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `condition` text, - `quota` int DEFAULT NULL, - `count` int DEFAULT NULL, - `survey_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `quota_buckets_survey_id_fkey` (`survey_id`), - CONSTRAINT `quota_buckets_survey_id_fkey` FOREIGN KEY (`survey_id`) REFERENCES `surveys` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `rememberables` --- - -DROP TABLE IF EXISTS `rememberables`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `rememberables` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `series_hash` varchar(255) DEFAULT NULL, - `token_hash` varchar(255) DEFAULT NULL, - `token_created_at` datetime DEFAULT NULL, - `user_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - UNIQUE KEY `rememberables_user_id_series_hash_token_hash_index` (`user_id`,`series_hash`,`token_hash`), - KEY `rememberables_user_id_index` (`user_id`), - KEY `rememberables_series_hash_index` (`series_hash`), - KEY `rememberables_token_hash_index` (`token_hash`), - CONSTRAINT `rememberables_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `respondent_disposition_history` --- - -DROP TABLE IF EXISTS `respondent_disposition_history`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `respondent_disposition_history` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `disposition` varchar(255) DEFAULT NULL, - `respondent_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `mode` varchar(255) DEFAULT NULL, - `survey_id` bigint unsigned DEFAULT NULL, - `respondent_hashed_number` varchar(255) DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `respondent_disposition_history_respondent_id_index` (`respondent_id`), - KEY `respondent_disposition_history_survey_id_id_index` (`survey_id`,`id`), - CONSTRAINT `respondent_disposition_history_respondent_id_fkey` FOREIGN KEY (`respondent_id`) REFERENCES `respondents` (`id`) ON DELETE CASCADE, - CONSTRAINT `respondent_disposition_history_survey_id_fkey` FOREIGN KEY (`survey_id`) REFERENCES `surveys` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `respondent_group_channels` --- - -DROP TABLE IF EXISTS `respondent_group_channels`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `respondent_group_channels` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `respondent_group_id` bigint unsigned DEFAULT NULL, - `channel_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `mode` varchar(255) DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `respondent_group_channels_respondent_group_id_index` (`respondent_group_id`), - KEY `respondent_group_channels_channel_id_index` (`channel_id`), - CONSTRAINT `respondent_group_channels_channel_id_fkey` FOREIGN KEY (`channel_id`) REFERENCES `channels` (`id`), - CONSTRAINT `respondent_group_channels_respondent_group_id_fkey` FOREIGN KEY (`respondent_group_id`) REFERENCES `respondent_groups` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `respondent_groups` --- - -DROP TABLE IF EXISTS `respondent_groups`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `respondent_groups` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `name` varchar(255) DEFAULT NULL, - `survey_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `sample` longtext, - `respondents_count` int DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `respondent_groups_survey_id_fkey` (`survey_id`), - CONSTRAINT `respondent_groups_survey_id_fkey` FOREIGN KEY (`survey_id`) REFERENCES `surveys` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `respondent_stats` --- - -DROP TABLE IF EXISTS `respondent_stats`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `respondent_stats` ( - `survey_id` bigint unsigned NOT NULL, - `questionnaire_id` int NOT NULL, - `state` varchar(255) NOT NULL, - `disposition` varchar(255) NOT NULL, - `quota_bucket_id` int NOT NULL, - `mode` varchar(255) NOT NULL, - `count` int DEFAULT '0', - PRIMARY KEY (`survey_id`,`questionnaire_id`,`state`,`disposition`,`quota_bucket_id`,`mode`), - CONSTRAINT `respondent_stats_survey_id_fkey` FOREIGN KEY (`survey_id`) REFERENCES `surveys` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `respondents` --- - -DROP TABLE IF EXISTS `respondents`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `respondents` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `phone_number` varchar(255) DEFAULT NULL, - `survey_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `state` varchar(255) DEFAULT 'pending', - `session` text, - `completed_at` datetime DEFAULT NULL, - `timeout_at` datetime DEFAULT NULL, - `sanitized_phone_number` varchar(255) DEFAULT NULL, - `quota_bucket_id` int DEFAULT NULL, - `questionnaire_id` bigint unsigned DEFAULT NULL, - `mode` varchar(255) DEFAULT NULL, - `respondent_group_id` bigint unsigned DEFAULT NULL, - `hashed_number` varchar(255) DEFAULT NULL, - `disposition` varchar(255) DEFAULT NULL, - `lock_version` int DEFAULT '1', - `language` varchar(255) DEFAULT NULL, - `effective_modes` varchar(255) DEFAULT NULL, - `stats` longtext NOT NULL, - `section_order` varchar(255) DEFAULT NULL, - `retry_stat_id` bigint unsigned DEFAULT NULL, - `canonical_phone_number` varchar(255) DEFAULT NULL, - `user_stopped` tinyint(1) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `respondents_survey_id_index` (`survey_id`), - KEY `respondents_survey_id_state_index` (`survey_id`,`state`), - KEY `respondents_state_timeout_at_index` (`state`,`timeout_at`), - KEY `respondents_questionnaire_id_fkey` (`questionnaire_id`), - KEY `respondents_respondent_group_id_fkey` (`respondent_group_id`), - KEY `respondents_hashed_number_index` (`hashed_number`), - KEY `respondents_updated_at_index` (`updated_at`), - KEY `respondents_sanitized_phone_number_index` (`sanitized_phone_number`), - KEY `respondents_retry_stat_id_fkey` (`retry_stat_id`), - KEY `respondents_canonical_phone_number_index` (`canonical_phone_number`), - CONSTRAINT `respondents_questionnaire_id_fkey` FOREIGN KEY (`questionnaire_id`) REFERENCES `questionnaires` (`id`), - CONSTRAINT `respondents_respondent_group_id_fkey` FOREIGN KEY (`respondent_group_id`) REFERENCES `respondent_groups` (`id`) ON DELETE CASCADE, - CONSTRAINT `respondents_retry_stat_id_fkey` FOREIGN KEY (`retry_stat_id`) REFERENCES `retry_stats` (`id`) ON DELETE CASCADE, - CONSTRAINT `respondents_survey_id_fkey` FOREIGN KEY (`survey_id`) REFERENCES `surveys` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; -/*!50003 SET @saved_cs_client = @@character_set_client */ ; -/*!50003 SET @saved_cs_results = @@character_set_results */ ; -/*!50003 SET @saved_col_connection = @@collation_connection */ ; -/*!50003 SET character_set_client = utf8mb3 */ ; -/*!50003 SET character_set_results = utf8mb3 */ ; -/*!50003 SET collation_connection = utf8mb3_general_ci */ ; -/*!50003 SET @saved_sql_mode = @@sql_mode */ ; -/*!50003 SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO' */ ; -DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`%`*/ /*!50003 TRIGGER `respondents_ins` AFTER INSERT ON `respondents` FOR EACH ROW BEGIN - - INSERT INTO respondent_stats(survey_id, questionnaire_id, state, disposition, quota_bucket_id, mode, `count`) - VALUES (NEW.survey_id, IFNULL(NEW.questionnaire_id, 0), NEW.state, NEW.disposition, IFNULL(NEW.quota_bucket_id, 0), IFNULL(NEW.mode, ''), 1) - ON DUPLICATE KEY UPDATE `count` = `count` + 1 - ; - -END */;; -DELIMITER ; -/*!50003 SET sql_mode = @saved_sql_mode */ ; -/*!50003 SET character_set_client = @saved_cs_client */ ; -/*!50003 SET character_set_results = @saved_cs_results */ ; -/*!50003 SET collation_connection = @saved_col_connection */ ; -/*!50003 SET @saved_cs_client = @@character_set_client */ ; -/*!50003 SET @saved_cs_results = @@character_set_results */ ; -/*!50003 SET @saved_col_connection = @@collation_connection */ ; -/*!50003 SET character_set_client = utf8mb3 */ ; -/*!50003 SET character_set_results = utf8mb3 */ ; -/*!50003 SET collation_connection = utf8mb3_general_ci */ ; -/*!50003 SET @saved_sql_mode = @@sql_mode */ ; -/*!50003 SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO' */ ; -DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`%`*/ /*!50003 TRIGGER `on_respondents_completed_ins` AFTER INSERT ON `respondents` FOR EACH ROW BEGIN - - IF NEW.disposition = 'completed' THEN - INSERT INTO completed_respondents(survey_id, questionnaire_id, quota_bucket_id, mode, date, count) - VALUES (NEW.survey_id, IFNULL(NEW.questionnaire_id, 0), IFNULL(NEW.quota_bucket_id, 0), IFNULL(NEW.mode, ''), DATE(NEW.updated_at), 1) - ON DUPLICATE KEY UPDATE `count` = `count` + 1; - END IF; - -END */;; -DELIMITER ; -/*!50003 SET sql_mode = @saved_sql_mode */ ; -/*!50003 SET character_set_client = @saved_cs_client */ ; -/*!50003 SET character_set_results = @saved_cs_results */ ; -/*!50003 SET collation_connection = @saved_col_connection */ ; -/*!50003 SET @saved_cs_client = @@character_set_client */ ; -/*!50003 SET @saved_cs_results = @@character_set_results */ ; -/*!50003 SET @saved_col_connection = @@collation_connection */ ; -/*!50003 SET character_set_client = utf8mb3 */ ; -/*!50003 SET character_set_results = utf8mb3 */ ; -/*!50003 SET collation_connection = utf8mb3_general_ci */ ; -/*!50003 SET @saved_sql_mode = @@sql_mode */ ; -/*!50003 SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO' */ ; -DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`%`*/ /*!50003 TRIGGER `on_respondents_completed_upd` AFTER UPDATE ON `respondents` FOR EACH ROW BEGIN - - IF NEW.disposition = 'completed' AND OLD.disposition <> 'completed' THEN - INSERT INTO completed_respondents(survey_id, questionnaire_id, quota_bucket_id, mode, date, count) - VALUES (NEW.survey_id, IFNULL(NEW.questionnaire_id, 0), IFNULL(NEW.quota_bucket_id, 0), IFNULL(NEW.mode, ''), DATE(NEW.updated_at), 1) - ON DUPLICATE KEY UPDATE `count` = `count` + 1; - ELSEIF NEW.disposition <> 'completed' AND OLD.disposition = 'completed' THEN - UPDATE completed_respondents - SET `count` = `count` - 1 - WHERE survey_id = OLD.survey_id - AND questionnaire_id = IFNULL(OLD.questionnaire_id, 0) - AND quota_bucket_id = IFNULL(OLD.quota_bucket_id, 0) - AND mode = IFNULL(OLD.mode, '') - AND date = DATE(OLD.updated_at); - END IF; - -END */;; -DELIMITER ; -/*!50003 SET sql_mode = @saved_sql_mode */ ; -/*!50003 SET character_set_client = @saved_cs_client */ ; -/*!50003 SET character_set_results = @saved_cs_results */ ; -/*!50003 SET collation_connection = @saved_col_connection */ ; -/*!50003 SET @saved_cs_client = @@character_set_client */ ; -/*!50003 SET @saved_cs_results = @@character_set_results */ ; -/*!50003 SET @saved_col_connection = @@collation_connection */ ; -/*!50003 SET character_set_client = utf8mb4 */ ; -/*!50003 SET character_set_results = utf8mb4 */ ; -/*!50003 SET collation_connection = utf8mb4_general_ci */ ; -/*!50003 SET @saved_sql_mode = @@sql_mode */ ; -/*!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' */ ; -DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`%`*/ /*!50003 TRIGGER `respondents_upd` AFTER UPDATE ON `respondents` FOR EACH ROW BEGIN - - - - - - - DECLARE temp_stats INT; - - SELECT count(*) - INTO temp_stats - FROM respondent_stats - WHERE (survey_id = OLD.survey_id - AND questionnaire_id = IFNULL(OLD.questionnaire_id, 0) - AND state = OLD.state - AND disposition = OLD.disposition - AND quota_bucket_id = IFNULL(OLD.quota_bucket_id, 0) - AND mode = IFNULL(OLD.mode, '')) - OR (survey_id = NEW.survey_id - AND questionnaire_id = IFNULL(NEW.questionnaire_id, 0) - AND state = NEW.state - AND disposition = NEW.disposition - AND quota_bucket_id = IFNULL(NEW.quota_bucket_id, 0) - AND mode = IFNULL(NEW.mode, '')) - FOR UPDATE; - - UPDATE respondent_stats - SET `count` = `count` - 1 - WHERE survey_id = OLD.survey_id - AND questionnaire_id = IFNULL(OLD.questionnaire_id, 0) - AND state = OLD.state - AND disposition = OLD.disposition - AND quota_bucket_id = IFNULL(OLD.quota_bucket_id, 0) - AND mode = IFNULL(OLD.mode, '') - ; - - INSERT INTO respondent_stats(survey_id, questionnaire_id, state, disposition, quota_bucket_id, mode, `count`) - VALUES (NEW.survey_id, IFNULL(NEW.questionnaire_id, 0), NEW.state, NEW.disposition, IFNULL(NEW.quota_bucket_id, 0), IFNULL(NEW.mode, ''), 1) - ON DUPLICATE KEY UPDATE `count` = `count` + 1 - ; - -END */;; -DELIMITER ; -/*!50003 SET sql_mode = @saved_sql_mode */ ; -/*!50003 SET character_set_client = @saved_cs_client */ ; -/*!50003 SET character_set_results = @saved_cs_results */ ; -/*!50003 SET collation_connection = @saved_col_connection */ ; -/*!50003 SET @saved_cs_client = @@character_set_client */ ; -/*!50003 SET @saved_cs_results = @@character_set_results */ ; -/*!50003 SET @saved_col_connection = @@collation_connection */ ; -/*!50003 SET character_set_client = utf8mb3 */ ; -/*!50003 SET character_set_results = utf8mb3 */ ; -/*!50003 SET collation_connection = utf8mb3_general_ci */ ; -/*!50003 SET @saved_sql_mode = @@sql_mode */ ; -/*!50003 SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO' */ ; -DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`%`*/ /*!50003 TRIGGER `respondents_del` AFTER DELETE ON `respondents` FOR EACH ROW BEGIN - - UPDATE respondent_stats - SET `count` = `count` - 1 - WHERE survey_id = OLD.survey_id - AND questionnaire_id = IFNULL(OLD.questionnaire_id, 0) - AND state = OLD.state - AND disposition = OLD.disposition - AND quota_bucket_id = IFNULL(OLD.quota_bucket_id, 0) - AND mode = IFNULL(OLD.mode, '') - ; - -END */;; -DELIMITER ; -/*!50003 SET sql_mode = @saved_sql_mode */ ; -/*!50003 SET character_set_client = @saved_cs_client */ ; -/*!50003 SET character_set_results = @saved_cs_results */ ; -/*!50003 SET collation_connection = @saved_col_connection */ ; -/*!50003 SET @saved_cs_client = @@character_set_client */ ; -/*!50003 SET @saved_cs_results = @@character_set_results */ ; -/*!50003 SET @saved_col_connection = @@collation_connection */ ; -/*!50003 SET character_set_client = utf8mb3 */ ; -/*!50003 SET character_set_results = utf8mb3 */ ; -/*!50003 SET collation_connection = utf8mb3_general_ci */ ; -/*!50003 SET @saved_sql_mode = @@sql_mode */ ; -/*!50003 SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO' */ ; -DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`%`*/ /*!50003 TRIGGER `on_respondents_completed_del` AFTER DELETE ON `respondents` FOR EACH ROW BEGIN - - IF OLD.disposition = 'completed' THEN - UPDATE completed_respondents - SET `count` = `count` - 1 - WHERE survey_id = OLD.survey_id - AND questionnaire_id = IFNULL(OLD.questionnaire_id, 0) - AND quota_bucket_id = IFNULL(OLD.quota_bucket_id, 0) - AND mode = IFNULL(OLD.mode, '') - AND date = DATE(OLD.updated_at); - END IF; - -END */;; -DELIMITER ; -/*!50003 SET sql_mode = @saved_sql_mode */ ; -/*!50003 SET character_set_client = @saved_cs_client */ ; -/*!50003 SET character_set_results = @saved_cs_results */ ; -/*!50003 SET collation_connection = @saved_col_connection */ ; - --- --- Table structure for table `responses` --- - -DROP TABLE IF EXISTS `responses`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `responses` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `field_name` varchar(255) DEFAULT NULL, - `value` varchar(255) DEFAULT NULL, - `respondent_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `responses_respondent_id_index` (`respondent_id`), - CONSTRAINT `responses_respondent_id_fkey` FOREIGN KEY (`respondent_id`) REFERENCES `respondents` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `retry_stats` --- - -DROP TABLE IF EXISTS `retry_stats`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `retry_stats` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `mode` varchar(255) NOT NULL, - `attempt` int NOT NULL, - `retry_time` varchar(255) NOT NULL, - `count` int NOT NULL, - `survey_id` bigint unsigned NOT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `ivr_active` tinyint(1) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - UNIQUE KEY `retry_stats_mode_attempt_retry_time_ivr_active_survey_id_index` (`mode`,`attempt`,`retry_time`,`ivr_active`,`survey_id`), - KEY `retry_stats_survey_id_fkey` (`survey_id`), - CONSTRAINT `retry_stats_survey_id_fkey` FOREIGN KEY (`survey_id`) REFERENCES `surveys` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `schema_migrations` --- - -DROP TABLE IF EXISTS `schema_migrations`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `schema_migrations` ( - `version` bigint NOT NULL, - `inserted_at` datetime DEFAULT NULL, - PRIMARY KEY (`version`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `sessions` --- - -DROP TABLE IF EXISTS `sessions`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `sessions` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `token` varchar(255) DEFAULT NULL, - `user_type` varchar(255) DEFAULT NULL, - `user_id` varchar(255) DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - UNIQUE KEY `sessions_token_index` (`token`), - KEY `sessions_user_id_index` (`user_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `short_links` --- - -DROP TABLE IF EXISTS `short_links`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `short_links` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `hash` varchar(255) DEFAULT NULL, - `name` varchar(255) DEFAULT NULL, - `target` varchar(255) DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `survey_log_entries` --- - -DROP TABLE IF EXISTS `survey_log_entries`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `survey_log_entries` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `survey_id` int DEFAULT NULL, - `mode` varchar(255) DEFAULT NULL, - `respondent_id` int DEFAULT NULL, - `respondent_hashed_number` varchar(255) DEFAULT NULL, - `channel_id` int DEFAULT NULL, - `disposition` varchar(255) DEFAULT NULL, - `action_type` varchar(255) DEFAULT NULL, - `action_data` longtext, - `timestamp` datetime DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `survey_log_entries_survey_id_respondent_hashed_number_id_index` (`survey_id`,`respondent_hashed_number`,`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `survey_questionnaires` --- - -DROP TABLE IF EXISTS `survey_questionnaires`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `survey_questionnaires` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `survey_id` bigint unsigned DEFAULT NULL, - `questionnaire_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `survey_questionnaires_survey_id_index` (`survey_id`), - KEY `survey_questionnaires_questionnaire_id_index` (`questionnaire_id`), - CONSTRAINT `survey_questionnaires_questionnaire_id_fkey` FOREIGN KEY (`questionnaire_id`) REFERENCES `questionnaires` (`id`), - CONSTRAINT `survey_questionnaires_survey_id_fkey` FOREIGN KEY (`survey_id`) REFERENCES `surveys` (`id`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `surveys` --- - -DROP TABLE IF EXISTS `surveys`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `surveys` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `name` varchar(255) DEFAULT NULL, - `project_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `state` varchar(255) DEFAULT 'pending', - `cutoff` int DEFAULT NULL, - `mode` text, - `sms_retry_configuration` text, - `ivr_retry_configuration` text, - `started_at` datetime DEFAULT NULL, - `quota_vars` text, - `comparisons` text, - `fallback_delay` varchar(255) DEFAULT NULL, - `count_partial_results` tinyint(1) DEFAULT '0', - `mobileweb_retry_configuration` text, - `simulation` tinyint(1) DEFAULT '0', - `exit_code` int DEFAULT NULL, - `exit_message` varchar(255) DEFAULT NULL, - `schedule` text, - `floip_package_id` varchar(255) DEFAULT NULL, - `description` text, - `locked` tinyint(1) DEFAULT '0', - `folder_id` bigint unsigned DEFAULT NULL, - `ended_at` datetime DEFAULT NULL, - `incentives_enabled` tinyint(1) DEFAULT '1', - `first_window_started_at` datetime DEFAULT NULL, - `last_window_ends_at` datetime DEFAULT NULL, - `panel_survey_id` bigint unsigned DEFAULT NULL, - `generates_panel_survey` tinyint(1) DEFAULT '0', - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `surveys_project_id_index` (`project_id`), - KEY `surveys_id_index` (`id`), - KEY `surveys_folder_id_index` (`folder_id`), - KEY `surveys_panel_survey_id_fkey` (`panel_survey_id`), - CONSTRAINT `surveys_folder_id_fkey` FOREIGN KEY (`folder_id`) REFERENCES `folders` (`id`), - CONSTRAINT `surveys_panel_survey_id_fkey` FOREIGN KEY (`panel_survey_id`) REFERENCES `panel_surveys` (`id`), - CONSTRAINT `surveys_project_id_fkey` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `translations` --- - -DROP TABLE IF EXISTS `translations`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `translations` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `mode` varchar(255) DEFAULT NULL, - `source_lang` varchar(255) DEFAULT NULL, - `source_text` longtext, - `target_lang` varchar(255) DEFAULT NULL, - `target_text` longtext, - `project_id` bigint unsigned DEFAULT NULL, - `questionnaire_id` bigint unsigned DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `scope` varchar(255) DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - KEY `translations_project_id_index` (`project_id`), - KEY `translations_questionnaire_id_index` (`questionnaire_id`), - CONSTRAINT `translations_project_id_fkey` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`), - CONSTRAINT `translations_questionnaire_id_fkey` FOREIGN KEY (`questionnaire_id`) REFERENCES `questionnaires` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `users` --- - -DROP TABLE IF EXISTS `users`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `users` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `email` varchar(255) DEFAULT NULL, - `encrypted_password` varchar(255) DEFAULT NULL, - `inserted_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, - `settings` text, - `password_hash` varchar(255) DEFAULT NULL, - `reset_password_token` varchar(255) DEFAULT NULL, - `reset_password_sent_at` datetime DEFAULT NULL, - `confirmation_token` varchar(255) DEFAULT NULL, - `confirmed_at` datetime DEFAULT NULL, - `confirmation_sent_at` datetime DEFAULT NULL, - `name` varchar(255) DEFAULT '', - `remember_created_at` datetime DEFAULT NULL, - `unconfirmed_email` varchar(255) DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - UNIQUE KEY `users_email_index` (`email`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping routines for database 'ask_dev' --- - --- --- Current Database: `ask_dev` --- - -CREATE DATABASE /*!32312 IF NOT EXISTS*/ `ask_dev` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */; - -USE `ask_dev`; - -- -- Table structure for table `activity_log` -- @@ -1663,11 +651,11 @@ DELIMITER ; DELIMITER ;; /*!50003 CREATE*/ /*!50017 DEFINER=`root`@`%`*/ /*!50003 TRIGGER `respondents_upd` AFTER UPDATE ON `respondents` FOR EACH ROW BEGIN - - - - - + # This is a hack for using the `select for update` + # so we can lock all the records that will get updated + # by this trigger before actually modifying them + # so we prevent this trigger to generate deadlocks + # see #1744 DECLARE temp_stats INT; SELECT count(*) diff --git a/test/ask_web/controllers/channel_controller_test.exs b/test/ask_web/controllers/channel_controller_test.exs index a7a05323c..7c0dae8ca 100644 --- a/test/ask_web/controllers/channel_controller_test.exs +++ b/test/ask_web/controllers/channel_controller_test.exs @@ -218,4 +218,40 @@ defmodule AskWeb.ChannelControllerTest do end end end + + describe "pause" do + setup %{conn: conn, user: user} do + settings = Ask.TestChannel.settings(Ask.TestChannel.new) + channel = insert(:channel, user: user, type: "sms", settings: settings) + {:ok, conn: conn, user: user, channel: channel} + end + + test "pause channel", %{conn: conn, user: user, channel: channel} do + conn = + post conn, channel_pause_path(conn, :pause, channel.id) + + assert json_response(conn, 200) + + channel = + user + |> assoc(:channels) + |> Repo.one!() + + assert %{paused: true} = channel + end + + test "unpause channel", %{conn: conn, user: user, channel: channel} do + conn = + post conn, channel_unpause_path(conn, :unpause, channel.id) + + assert json_response(conn, 200) + + channel = + user + |> assoc(:channels) + |> Repo.one!() + + assert %{paused: false} = channel + end + end end diff --git a/test/ask_web/controllers/survey_controller_test.exs b/test/ask_web/controllers/survey_controller_test.exs index 4983dd727..aa294bc65 100644 --- a/test/ask_web/controllers/survey_controller_test.exs +++ b/test/ask_web/controllers/survey_controller_test.exs @@ -2774,7 +2774,7 @@ defmodule AskWeb.SurveyControllerTest do end test "stops respondents only for the stopped survey", %{conn: conn, user: user} do - start_survey_canceller_supervisor() + start_survey_canceller_supervisor() project = create_project_for_user(user) questionnaire = insert(:questionnaire, name: "test", project: project) survey = insert(:survey, project: project, state: :running)