common/dal/schema/000020_init.up.sql

58 lines
3.2 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'bot_status') THEN
CREATE TYPE bot_status AS ENUM ('active','stopped', 'banned');
END IF;
END $$;
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'user_result_tg_status') THEN
CREATE TYPE user_result_tg_status AS ENUM ('in_progress','completed');
END IF;
END $$;
CREATE INDEX IF NOT EXISTS results_for_quiz ON answer(quiz_id, result);
CREATE TABLE IF NOT EXISTS telegram_integration(
id bigserial unique not null primary key,
accountid varchar(30) NOT NULL, -- связь с аккаунтом квиза
quizID integer NOT NULL , -- айдишник квиза, с которым связываем интеграцию
bot_token text NOT NULL, -- токен бота
bot_name text NOT NULL DEFAULT '', -- имя бота
repeatable boolean DEFAULT false, -- флаг возможности повторить опрос
deleted boolean NOT NULL default false, -- флаг удаленного
status bot_status NOT NULL DEFAULT 'active',
instance_id integer NOT NULL default 0
);
CREATE TABLE IF NOT EXISTS respondent_state(
id bigserial unique not null primary key,
telegram_id integer NOT NULL, -- айдишник пользователя в телеграме, скорее всего чат id
quizID integer NOT NULL, -- айдишник квиза, который проходится этим респондентом
state bigint NOT NULL,
lang text NOT NULL default '', -- язык опрашиваемого
contact text NOT NULL default '', -- ник пользователя в телеге, если доступен
finish boolean not null default false, -- статус, пройден опрос или нет
session varchar(20) -- сессия пользователя
);
CREATE TABLE IF NOT EXISTS telegram_integration_instance(
id bigserial unique not null primary key,
checkout bigint not null , -- время "подтверждения активности". в идеале должно быть актуальным, но отличаться от всех остальных минимум на 10 секунд. т.е. если у нас есть бот с временем в 10:10:10 и бот с временем в 10:10:31, а сейчас 10:10:32, то валидные слоты это 10:10:21 и 10:10:41. но как это сделать я не знаю. как вариант, можно просто взять бот с максимальным временем и сделать ему +10. и наверное это правильныйй вариант
limited integer not null, --константа с лимитом ботов
amount integer not null -- актуальное количество ботов
);
CREATE TABLE IF NOT EXISTS telegram_user_quiz_result(
id BIGSERIAL PRIMARY KEY,
bot_id BIGINT NOT NULL,
quiz_id BIGINT NOT NULL,
current_field TEXT,
user_answer TEXT DEFAULT '',
state user_result_tg_status not null default 'in_progress',
session varchar(20) NOT NULL ,
question_id bigint NOT NULL, -- id вопроса результата
iter integer not null
)