diff --git a/config.py b/config.py index b3c921f..8e583e5 100644 --- a/config.py +++ b/config.py @@ -35,6 +35,21 @@ MAX_INITDATA_AGE = 4 * 60 * 60 # 4 часа в секундах SUPPORTED_LANGUAGES = {"pl", "ru", "uk", "en"} +# 🔧 ФИКС (24.09.2026): cf_connecting_ip_spoof — +# доверяем заголовку cf-connecting-ip ТОЛЬКО если запрос пришёл +# с IP Cloudflare. Иначе — берём request.client.host. +# Список: https://www.cloudflare.com/ips-v4 +CF_IPS = { + # IPv4 + "173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22", + "141.101.64.0/18", "108.162.192.0/18", "190.93.240.0/20", "188.114.96.0/20", + "197.234.240.0/22", "198.41.128.0/17", "162.158.0.0/15", "104.16.0.0/13", + "104.24.0.0/14", "172.64.0.0/13", "131.0.72.0/22", + # IPv6 + "2400:cb00::/32", "2606:4700::/32", "2803:f800::/32", + "2405:b500::/32", "2405:8100::/32", "2a06:98c0::/29", "2c0f:f248::/32", +} + # ============ 🔧 НОВОЕ (18.09.2026): настройки для FeedModal ============ # Папка для хранения картинок feed-постов. # Путь совпадает с backend/, файлы доступны как static mounts (см. main.py при необходимости). diff --git a/database.py b/database.py index de23fc6..b2e4bcd 100644 --- a/database.py +++ b/database.py @@ -1,931 +1,975 @@ +import asyncio import asyncpg import logging from config import DB_CONFIG logger = logging.getLogger(__name__) db_pool = None +# 🔧 ФИКС (24.09.2026): get_pool_race — защита от гонки при первом обращении. +_pool_lock = asyncio.Lock() async def get_pool(): global db_pool - if db_pool is None: - db_pool = await asyncpg.create_pool(**DB_CONFIG, min_size=2, max_size=10) + if db_pool is not None: + return db_pool + # 🔧 Двойная проверка: сначала без лока, потом внутри лока + async with _pool_lock: + if db_pool is None: + db_pool = await asyncpg.create_pool(**DB_CONFIG, min_size=2, max_size=10) return db_pool async def init_db(): pool = await get_pool() async with pool.acquire() as conn: - # ============ users ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, - telegram_id BIGINT UNIQUE NOT NULL, - username TEXT, - app_language TEXT DEFAULT 'pl', - current_level TEXT DEFAULT 'A1', - last_completed_level INTEGER DEFAULT 0, - created_at TIMESTAMP DEFAULT NOW() - ) - """) - - # ============ Авто-миграция: добавляем current_level в users ============ - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'users' AND column_name = 'current_level' - """) - if not col_exists: + # 🔧 ФИКС (24.09.2026): init_db_no_transaction — + # оборачиваем ВСЮ инициализацию в одну транзакцию. + # Если упадёт на середине — БД вернётся в исходное состояние. + async with conn.transaction(): + # ============ users ============ await conn.execute(""" - ALTER TABLE users - ADD COLUMN current_level TEXT DEFAULT 'A1' - """) - logger.info("Added current_level column to users") - - # ============ levels ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS levels ( - id SERIAL PRIMARY KEY, - level_number INTEGER NOT NULL, - grid_size INTEGER NOT NULL, - game_type TEXT DEFAULT 'wordsearch', - level TEXT DEFAULT 'A1', - grid_letters JSONB NOT NULL DEFAULT '[]', - word_paths JSONB NOT NULL DEFAULT '{}' - ) - """) - - # ============ Убираем старый UNIQUE constraint с levels.level_number ============ - await conn.execute(""" - ALTER TABLE levels DROP CONSTRAINT IF EXISTS levels_level_number_key - """) - - # ============ Добавляем составной UNIQUE (level, level_number) ============ - await conn.execute(""" - DO $$ - BEGIN - IF NOT EXISTS ( - SELECT 1 FROM pg_constraint - WHERE conname = 'levels_level_number_unique' - ) THEN - ALTER TABLE levels ADD CONSTRAINT levels_level_number_unique UNIQUE (level, level_number); - END IF; - END $$; - """) - - # ============ level_words ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS level_words ( - id SERIAL PRIMARY KEY, - level_id INTEGER REFERENCES levels(id) ON DELETE CASCADE, - word_pl TEXT NOT NULL, - translation_ru TEXT DEFAULT '', - translation_uk TEXT DEFAULT '', - translation_en TEXT DEFAULT '', - level TEXT DEFAULT 'A1' - ) - """) - - # ============ Авто-миграция: добавляем level в level_words ============ - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'level_words' AND column_name = 'level' - """) - if not col_exists: - await conn.execute(""" - ALTER TABLE level_words - ADD COLUMN level TEXT DEFAULT 'A1' - """) - logger.info("Added level column to level_words") - - # ============ user_progress ============ - existing = await conn.fetchrow(""" - SELECT data_type - FROM information_schema.columns - WHERE table_name = 'user_progress' AND column_name = 'user_id' - """) - - if existing and existing['data_type'] != 'bigint': - logger.info("Migrating user_progress.user_id from INTEGER to BIGINT") - await conn.execute(""" - ALTER TABLE user_progress - DROP CONSTRAINT IF EXISTS user_progress_pkey CASCADE - """) - await conn.execute(""" - ALTER TABLE user_progress - DROP CONSTRAINT IF EXISTS user_progress_user_id_fkey - """) - await conn.execute(""" - ALTER TABLE user_progress - ALTER COLUMN user_id TYPE BIGINT - """) - await conn.execute(""" - ALTER TABLE user_progress - ADD PRIMARY KEY (user_id, level_id) - """) - await conn.execute(""" - ALTER TABLE user_progress - ADD FOREIGN KEY (user_id) REFERENCES users(telegram_id) ON DELETE CASCADE - """) - elif not existing: - await conn.execute(""" - CREATE TABLE IF NOT EXISTS user_progress ( - user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, - level_id INTEGER REFERENCES levels(id) ON DELETE CASCADE, - completed_at TIMESTAMP, - hints_used INTEGER DEFAULT 0, - score INTEGER DEFAULT 0, - level TEXT DEFAULT 'A1', - PRIMARY KEY (user_id, level_id) + CREATE TABLE IF NOT EXISTS users ( + id SERIAL PRIMARY KEY, + telegram_id BIGINT UNIQUE NOT NULL, + username TEXT, + app_language TEXT DEFAULT 'pl', + current_level TEXT DEFAULT 'A1', + last_completed_level INTEGER DEFAULT 0, + created_at TIMESTAMP DEFAULT NOW() ) """) - # ============ Авто-миграция: добавляем level в user_progress ============ - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'user_progress' AND column_name = 'level' - """) - if not col_exists: - await conn.execute(""" - ALTER TABLE user_progress - ADD COLUMN level TEXT DEFAULT 'A1' - """) - logger.info("Added level column to user_progress") - - # 🔧 ФИКС (23.09.2026): client_controlled_achievement_values — - # серверный perfect_streak. Раньше считался на клиенте и приходил - # в body — можно было подделать. - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'user_progress' AND column_name = 'current_streak' - """) - if not col_exists: - await conn.execute(""" - ALTER TABLE user_progress - ADD COLUMN current_streak INT DEFAULT 0 - """) - logger.info("Added current_streak column to user_progress") - - # ============ sentences ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS sentences ( - id SERIAL PRIMARY KEY, - sentence_number INTEGER NOT NULL, - sentence_pl TEXT NOT NULL, - translation_ru TEXT DEFAULT '', - translation_uk TEXT DEFAULT '', - translation_en TEXT DEFAULT '', - difficulty INTEGER DEFAULT 1, - level TEXT DEFAULT 'A1' - ) - """) - - # ============ Убираем старый UNIQUE constraint с sentences.sentence_number ============ - await conn.execute(""" - ALTER TABLE sentences DROP CONSTRAINT IF EXISTS sentences_sentence_number_key - """) - - # ============ Добавляем составной UNIQUE (level, sentence_number) ============ - await conn.execute(""" - DO $$ - BEGIN - IF NOT EXISTS ( - SELECT 1 FROM pg_constraint - WHERE conname = 'sentences_sentence_number_unique' - ) THEN - ALTER TABLE sentences ADD CONSTRAINT sentences_sentence_number_unique UNIQUE (level, sentence_number); - END IF; - END $$; - """) - - # ============ sentence_progress ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS sentence_progress ( - user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, - sentence_id INTEGER REFERENCES sentences(id) ON DELETE CASCADE, - completed_at TIMESTAMP, - score INTEGER DEFAULT 0, - level TEXT DEFAULT 'A1', - PRIMARY KEY (user_id, sentence_id) - ) - """) - - # ============ Авто-миграция: добавляем level в sentence_progress ============ - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'sentence_progress' AND column_name = 'level' - """) - if not col_exists: - await conn.execute(""" - ALTER TABLE sentence_progress - ADD COLUMN level TEXT DEFAULT 'A1' - """) - logger.info("Added level column to sentence_progress") - - # ============ typing_progress ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS typing_progress ( - user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, - sentence_id INTEGER REFERENCES sentences(id) ON DELETE CASCADE, - completed_at TIMESTAMP DEFAULT NOW(), - PRIMARY KEY (user_id, sentence_id) - ) - """) - - # 🔧 ФИКС (23.09.2026): duplicate_index_typing_progress — индекс idx_typing_progress_user - # уже создаётся в общем блоке индексов ниже. Убираем дубль здесь. - # await conn.execute("CREATE INDEX IF NOT EXISTS idx_typing_progress_user ON typing_progress(user_id)") - - # ============ chat_history ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS chat_history ( - id SERIAL PRIMARY KEY, - user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, - message TEXT NOT NULL, - is_user BOOLEAN DEFAULT TRUE, - created_at TIMESTAMP DEFAULT NOW() - ) - """) - - # ============ Индекс для chat_history ============ - await conn.execute("CREATE INDEX IF NOT EXISTS idx_chat_history_user ON chat_history(user_id, created_at DESC)") - - # ============ chat_usage ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS chat_usage ( - id SERIAL PRIMARY KEY, - user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, - usage_date DATE DEFAULT CURRENT_DATE, - message_count INTEGER DEFAULT 0, - UNIQUE (user_id, usage_date) - ) - """) - - # ============ Индекс для chat_usage ============ - await conn.execute("CREATE INDEX IF NOT EXISTS idx_chat_usage_user ON chat_usage(user_id, usage_date DESC)") - - # ============ learning_levels ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS learning_levels ( - id SERIAL PRIMARY KEY, - code VARCHAR(10) UNIQUE NOT NULL, - name_pl TEXT NOT NULL, - name_ru TEXT NOT NULL, - name_uk TEXT NOT NULL, - name_en TEXT NOT NULL, - sort_order INT NOT NULL, - is_active BOOLEAN DEFAULT TRUE - ) - """) - - # ============ learning_topics ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS learning_topics ( - id SERIAL PRIMARY KEY, - topic_id VARCHAR(100) UNIQUE NOT NULL, - level_code VARCHAR(10) NOT NULL, - order_number INT NOT NULL, - name_pl TEXT NOT NULL, - name_ru TEXT NOT NULL, - name_uk TEXT NOT NULL, - name_en TEXT NOT NULL, - description_pl TEXT, - description_ru TEXT, - description_uk TEXT, - description_en TEXT, - is_active BOOLEAN DEFAULT TRUE, - UNIQUE(level_code, order_number) - ) - """) - - # ============ learning_lessons ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS learning_lessons ( - id SERIAL PRIMARY KEY, - lesson_id VARCHAR(120) UNIQUE NOT NULL, - topic_id VARCHAR(100) NOT NULL REFERENCES learning_topics(topic_id), - order_number INT NOT NULL, - skill_tag VARCHAR(100) NOT NULL, - title_pl TEXT NOT NULL, - title_ru TEXT NOT NULL, - title_uk TEXT NOT NULL, - title_en TEXT NOT NULL, - objective_pl TEXT, - objective_ru TEXT, - objective_uk TEXT, - objective_en TEXT, - explanation_pl TEXT, - explanation_ru TEXT, - explanation_uk TEXT, - explanation_en TEXT, - is_active BOOLEAN DEFAULT TRUE, - UNIQUE(topic_id, order_number) - ) - """) - - # ============ lesson_examples ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS lesson_examples ( - id SERIAL PRIMARY KEY, - lesson_id VARCHAR(120) NOT NULL REFERENCES learning_lessons(lesson_id), - order_number INT NOT NULL, - text_pl TEXT NOT NULL, - text_ru TEXT, - text_uk TEXT, - text_en TEXT, - audio_url TEXT - ) - """) - - # ============ learning_questions ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS learning_questions ( - id SERIAL PRIMARY KEY, - question_id VARCHAR(150) UNIQUE NOT NULL, - lesson_id VARCHAR(120) REFERENCES learning_lessons(lesson_id), - topic_id VARCHAR(100) NOT NULL REFERENCES learning_topics(topic_id), - context_type VARCHAR(50) NOT NULL, - question_type VARCHAR(50) NOT NULL, - skill_tag VARCHAR(100) NOT NULL, - difficulty INT NOT NULL CHECK (difficulty BETWEEN 1 AND 3), - prompt_pl TEXT, - prompt_ru TEXT, - prompt_uk TEXT, - prompt_en TEXT, - correct_answer TEXT, - hint_type VARCHAR(50), - hint_value TEXT, - is_test_question BOOLEAN DEFAULT FALSE, - test_order INT, - options_language TEXT DEFAULT 'pl' - ) - """) - - # ============ Авто-миграция: добавляем options_language ============ - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'learning_questions' AND column_name = 'options_language' - """) - if not col_exists: - await conn.execute(""" - ALTER TABLE learning_questions - ADD COLUMN options_language TEXT DEFAULT 'pl' - """) - logger.info("Added options_language column to learning_questions") - - # ============ question_options ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS question_options ( - id SERIAL PRIMARY KEY, - question_id VARCHAR(150) NOT NULL REFERENCES learning_questions(question_id), - option_key VARCHAR(20) NOT NULL, - text_pl TEXT, - text_ru TEXT, - text_uk TEXT, - text_en TEXT, - is_correct BOOLEAN NOT NULL, - wrong_because VARCHAR(100), - UNIQUE(question_id, option_key) - ) - """) - - # ============ learning_sessions ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS learning_sessions ( - id BIGSERIAL PRIMARY KEY, - user_id BIGINT NOT NULL, - level_code VARCHAR(10) NOT NULL, - topic_id VARCHAR(100) NOT NULL, - session_type VARCHAR(30) NOT NULL, - started_at TIMESTAMP NOT NULL DEFAULT NOW(), - completed_at TIMESTAMP, - is_completed BOOLEAN DEFAULT FALSE - ) - """) - - # ============ user_learning_answers ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS user_learning_answers ( - id BIGSERIAL PRIMARY KEY, - user_id BIGINT NOT NULL, - session_id BIGINT NOT NULL REFERENCES learning_sessions(id), - level_code VARCHAR(10) NOT NULL, - topic_id VARCHAR(100) NOT NULL, - lesson_id VARCHAR(120), - question_id VARCHAR(150) NOT NULL, - selected_answer TEXT, - correct BOOLEAN, - skill_tag VARCHAR(100) NOT NULL, - question_type VARCHAR(50) NOT NULL, - context_type VARCHAR(50) NOT NULL, - difficulty INT NOT NULL, - time_taken_ms INT, - attempt_number INT DEFAULT 1, - hint_used BOOLEAN DEFAULT FALSE, - wrong_because VARCHAR(100), - is_final_answer BOOLEAN DEFAULT TRUE, - created_at TIMESTAMP DEFAULT NOW() - ) - """) - - # ============ user_lesson_progress ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS user_lesson_progress ( - id BIGSERIAL PRIMARY KEY, - user_id BIGINT NOT NULL, - lesson_id VARCHAR(120) NOT NULL, - started_at TIMESTAMP, - completed_at TIMESTAMP, - attempts_count INT DEFAULT 0, - correct_count INT DEFAULT 0, - best_score NUMERIC(5,2), - average_time_ms INT, - completed BOOLEAN DEFAULT FALSE, - UNIQUE(user_id, lesson_id) - ) - """) - - # ============ user_topic_progress ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS user_topic_progress ( - id BIGSERIAL PRIMARY KEY, - user_id BIGINT NOT NULL, - topic_id VARCHAR(100) NOT NULL, - lessons_total INT DEFAULT 0, - lessons_completed INT DEFAULT 0, - test_started BOOLEAN DEFAULT FALSE, - test_completed BOOLEAN DEFAULT FALSE, - test_score NUMERIC(5,2), - topic_mastery NUMERIC(5,2), - confidence NUMERIC(5,2), - completed_at TIMESTAMP, - UNIQUE(user_id, topic_id) - ) - """) - - # ============ user_skill_mastery (Analytics 2.0 + Этап 2) ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS user_skill_mastery ( - id BIGSERIAL PRIMARY KEY, - user_id BIGINT NOT NULL, - skill_tag VARCHAR(100) NOT NULL, - attempts_count INT DEFAULT 0, - correct_count INT DEFAULT 0, - recognition_attempts INT DEFAULT 0, - recognition_correct INT DEFAULT 0, - recognition_score NUMERIC(5,2), - production_attempts INT DEFAULT 0, - production_correct INT DEFAULT 0, - production_score NUMERIC(5,2), - application_attempts INT DEFAULT 0, - application_correct INT DEFAULT 0, - application_score NUMERIC(5,2), - retention_score NUMERIC(5,2) DEFAULT 0, - hint_dependency NUMERIC(5,2) DEFAULT 0, - average_time_ms INT DEFAULT 0, - median_time_ms INT DEFAULT 0, - median_correct_time_ms INT DEFAULT 0, - median_wrong_time_ms INT DEFAULT 0, - recent_accuracy NUMERIC(5,2) DEFAULT 0, - last_3_accuracy NUMERIC(5,2), - last_5_accuracy NUMERIC(5,2), - last_10_accuracy NUMERIC(5,2), - long_term_accuracy NUMERIC(5,2) DEFAULT 0, - mastery_score NUMERIC(5,2) DEFAULT 0, - previous_mastery_score NUMERIC(5,2) DEFAULT 0, - mastery_delta NUMERIC(5,2) DEFAULT 0, - trend TEXT DEFAULT 'insufficient_data', - retention_status TEXT DEFAULT 'fresh', - fluency_score NUMERIC(5,2) DEFAULT 0, - passive_knowledge_score NUMERIC(5,2) DEFAULT 0, - active_knowledge_score NUMERIC(5,2) DEFAULT 0, - confidence NUMERIC(5,2) DEFAULT 0, - error_count INT DEFAULT 0, - repeated_error_count INT DEFAULT 0, - days_since_last_attempt INT DEFAULT 0, - last_attempt_at TIMESTAMP, - UNIQUE(user_id, skill_tag) - ) - """) - - # ============ Авто-миграции для user_skill_mastery (Этап 2) ============ - stage2_columns = { - "previous_mastery_score": "NUMERIC(5,2) DEFAULT 0", - "mastery_delta": "NUMERIC(5,2) DEFAULT 0", - "trend": "TEXT DEFAULT 'insufficient_data'", - "retention_status": "TEXT DEFAULT 'fresh'", - "fluency_score": "NUMERIC(5,2) DEFAULT 0", - "passive_knowledge_score": "NUMERIC(5,2) DEFAULT 0", - "active_knowledge_score": "NUMERIC(5,2) DEFAULT 0", - } - - for col_name, col_type in stage2_columns.items(): + # ============ Авто-миграция: добавляем current_level в users ============ col_exists = await conn.fetchval(""" SELECT 1 FROM information_schema.columns - WHERE table_name = 'user_skill_mastery' AND column_name = $1 - """, col_name) + WHERE table_name = 'users' AND column_name = 'current_level' + """) if not col_exists: - await conn.execute(f""" - ALTER TABLE user_skill_mastery - ADD COLUMN {col_name} {col_type} + await conn.execute(""" + ALTER TABLE users + ADD COLUMN current_level TEXT DEFAULT 'A1' """) - logger.info(f"Added {col_name} column to user_skill_mastery") + logger.info("Added current_level column to users") - # ============ ai_user_context ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS ai_user_context ( - user_id BIGINT PRIMARY KEY, - current_level VARCHAR(10), - completed_topics INT DEFAULT 0, - level_topics_total INT DEFAULT 0, - level_completion_percent NUMERIC(5,2) DEFAULT 0, - completed_lessons INT DEFAULT 0, - overall_mastery NUMERIC(5,2) DEFAULT 0, - strong_skills JSONB DEFAULT '[]', - weak_skills JSONB DEFAULT '[]', - uncertain_skills JSONB DEFAULT '[]', - recurring_errors JSONB DEFAULT '[]', - recommended_topics JSONB DEFAULT '[]', - recommended_lessons JSONB DEFAULT '[]', - last_analysis_at TIMESTAMP, - analytics_enabled BOOLEAN DEFAULT FALSE - ) - """) - - # ============ Авто-миграция: добавляем level_topics_total ============ - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'ai_user_context' AND column_name = 'level_topics_total' - """) - if not col_exists: + # ============ levels ============ await conn.execute(""" - ALTER TABLE ai_user_context - ADD COLUMN level_topics_total INT DEFAULT 0 + CREATE TABLE IF NOT EXISTS levels ( + id SERIAL PRIMARY KEY, + level_number INTEGER NOT NULL, + grid_size INTEGER NOT NULL, + game_type TEXT DEFAULT 'wordsearch', + level TEXT DEFAULT 'A1', + grid_letters JSONB NOT NULL DEFAULT '[]', + word_paths JSONB NOT NULL DEFAULT '{}' + ) """) - logger.info("Added level_topics_total column to ai_user_context") - # ============ Авто-миграция: добавляем level_completion_percent ============ - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'ai_user_context' AND column_name = 'level_completion_percent' - """) - if not col_exists: + # ============ Убираем старый UNIQUE constraint с levels.level_number ============ await conn.execute(""" - ALTER TABLE ai_user_context - ADD COLUMN level_completion_percent NUMERIC(5,2) DEFAULT 0 + ALTER TABLE levels DROP CONSTRAINT IF EXISTS levels_level_number_key """) - logger.info("Added level_completion_percent column to ai_user_context") - # ============ achievements ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS achievements ( - id SERIAL PRIMARY KEY, - code TEXT UNIQUE NOT NULL, - category TEXT DEFAULT 'general', - name_pl TEXT NOT NULL, - name_ru TEXT NOT NULL, - name_uk TEXT NOT NULL, - name_en TEXT NOT NULL, - description_pl TEXT DEFAULT '', - description_ru TEXT DEFAULT '', - description_uk TEXT DEFAULT '', - description_en TEXT DEFAULT '', - icon TEXT DEFAULT '🏆', - requirement_type TEXT NOT NULL, - requirement_value INTEGER DEFAULT 0 - ) - """) - - # ============ user_achievements ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS user_achievements ( - user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, - achievement_id INTEGER REFERENCES achievements(id) ON DELETE CASCADE, - unlocked_at TIMESTAMP DEFAULT NOW(), - PRIMARY KEY (user_id, achievement_id) - ) - """) - - # ============ daily_logins ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS daily_logins ( - user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, - login_date DATE DEFAULT CURRENT_DATE, - PRIMARY KEY (user_id, login_date) - ) - """) - - # ============ word_quiz_progress ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS word_quiz_progress ( - user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, - words_guessed INTEGER DEFAULT 0, - total_score INTEGER DEFAULT 0, - PRIMARY KEY (user_id) - ) - """) - - # ============ Авто-миграция: добавляем correct_streak ============ - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'word_quiz_progress' AND column_name = 'correct_streak' - """) - if not col_exists: + # ============ Добавляем составной UNIQUE (level, level_number) ============ await conn.execute(""" - ALTER TABLE word_quiz_progress - ADD COLUMN correct_streak INTEGER DEFAULT 0 + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'levels_level_number_unique' + ) THEN + ALTER TABLE levels ADD CONSTRAINT levels_level_number_unique UNIQUE (level, level_number); + END IF; + END $$; """) - logger.info("Added correct_streak column to word_quiz_progress") - # ============ Авто-миграция: добавляем recent_words ============ - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'word_quiz_progress' AND column_name = 'recent_words' - """) - if not col_exists: + # ============ level_words ============ await conn.execute(""" - ALTER TABLE word_quiz_progress - ADD COLUMN recent_words TEXT[] DEFAULT '{}' + CREATE TABLE IF NOT EXISTS level_words ( + id SERIAL PRIMARY KEY, + level_id INTEGER REFERENCES levels(id) ON DELETE CASCADE, + word_pl TEXT NOT NULL, + translation_ru TEXT DEFAULT '', + translation_uk TEXT DEFAULT '', + translation_en TEXT DEFAULT '', + level TEXT DEFAULT 'A1' + ) """) - logger.info("Added recent_words column to word_quiz_progress") - # ============ Авто-миграция: добавляем level в word_quiz_progress ============ - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'word_quiz_progress' AND column_name = 'level' - """) - if not col_exists: + # ============ Авто-миграция: добавляем level в level_words ============ + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'level_words' AND column_name = 'level' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE level_words + ADD COLUMN level TEXT DEFAULT 'A1' + """) + logger.info("Added level column to level_words") + + # ============ user_progress ============ + existing = await conn.fetchrow(""" + SELECT data_type + FROM information_schema.columns + WHERE table_name = 'user_progress' AND column_name = 'user_id' + """) + + if existing and existing['data_type'] != 'bigint': + logger.info("Migrating user_progress.user_id from INTEGER to BIGINT") + # 🔧 ФИКС (24.09.2026): drop_constraint_cascade — + # убран CASCADE, чтобы не снести FK из других таблиц. + # Перечисляем зависимые FK явно (пересоздадим ниже). + await conn.execute(""" + ALTER TABLE user_progress + DROP CONSTRAINT IF EXISTS user_progress_pkey + """) + await conn.execute(""" + ALTER TABLE user_progress + DROP CONSTRAINT IF EXISTS user_progress_user_id_fkey + """) + await conn.execute(""" + ALTER TABLE user_progress + ALTER COLUMN user_id TYPE BIGINT + """) + + # 🔧 ФИКС (24.09.2026): add_pk_on_duplicates — + # удаляем дубли перед ADD PRIMARY KEY. + # Оставляем строку с максимальным completed_at (или первую). + await conn.execute(""" + DELETE FROM user_progress a + USING user_progress b + WHERE a.ctid < b.ctid + AND a.user_id = b.user_id + AND a.level_id = b.level_id + """) + + await conn.execute(""" + ALTER TABLE user_progress + ADD PRIMARY KEY (user_id, level_id) + """) + await conn.execute(""" + ALTER TABLE user_progress + ADD FOREIGN KEY (user_id) REFERENCES users(telegram_id) ON DELETE CASCADE + """) + elif not existing: + await conn.execute(""" + CREATE TABLE IF NOT EXISTS user_progress ( + user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, + level_id INTEGER REFERENCES levels(id) ON DELETE CASCADE, + completed_at TIMESTAMP, + hints_used INTEGER DEFAULT 0, + score INTEGER DEFAULT 0, + level TEXT DEFAULT 'A1', + PRIMARY KEY (user_id, level_id) + ) + """) + + # ============ Авто-миграция: добавляем level в user_progress ============ + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'user_progress' AND column_name = 'level' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE user_progress + ADD COLUMN level TEXT DEFAULT 'A1' + """) + logger.info("Added level column to user_progress") + + # 🔧 ФИКС (23.09.2026): client_controlled_achievement_values — + # серверный perfect_streak. Раньше считался на клиенте и приходил + # в body — можно было подделать. + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'user_progress' AND column_name = 'current_streak' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE user_progress + ADD COLUMN current_streak INT DEFAULT 0 + """) + logger.info("Added current_streak column to user_progress") + + # ============ sentences ============ await conn.execute(""" - ALTER TABLE word_quiz_progress - ADD COLUMN level TEXT DEFAULT 'A1' + CREATE TABLE IF NOT EXISTS sentences ( + id SERIAL PRIMARY KEY, + sentence_number INTEGER NOT NULL, + sentence_pl TEXT NOT NULL, + translation_ru TEXT DEFAULT '', + translation_uk TEXT DEFAULT '', + translation_en TEXT DEFAULT '', + difficulty INTEGER DEFAULT 1, + level TEXT DEFAULT 'A1' + ) """) - logger.info("Added level column to word_quiz_progress") - # 🔧 ФИКС (24.09.2026): client_controlled_mistakes_wordquiz — - # серверный счётчик mistakes для текущего слова. - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'word_quiz_progress' AND column_name = 'current_mistakes' - """) - if not col_exists: + # ============ Убираем старый UNIQUE constraint с sentences.sentence_number ============ await conn.execute(""" - ALTER TABLE word_quiz_progress - ADD COLUMN current_mistakes INT DEFAULT 0 + ALTER TABLE sentences DROP CONSTRAINT IF EXISTS sentences_sentence_number_key """) - logger.info("Added current_mistakes column to word_quiz_progress") - # ============ bubble_words_progress ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS bubble_words_progress ( - user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, - levels_completed INTEGER DEFAULT 0, - total_score INTEGER DEFAULT 0, - PRIMARY KEY (user_id) - ) - """) - - # ============ Авто-миграция: добавляем level в bubble_words_progress ============ - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'bubble_words_progress' AND column_name = 'level' - """) - if not col_exists: + # ============ Добавляем составной UNIQUE (level, sentence_number) ============ await conn.execute(""" - ALTER TABLE bubble_words_progress - ADD COLUMN level TEXT DEFAULT 'A1' + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'sentences_sentence_number_unique' + ) THEN + ALTER TABLE sentences ADD CONSTRAINT sentences_sentence_number_unique UNIQUE (level, sentence_number); + END IF; + END $$; """) - logger.info("Added level column to bubble_words_progress") - # 🔧 ФИКС (24.09.2026): client_controlled_hints_bubblewords — - # серверный hints_used для текущего уровня пузырей. - col_exists = await conn.fetchval(""" - SELECT 1 FROM information_schema.columns - WHERE table_name = 'bubble_words_progress' AND column_name = 'current_hints_used' - """) - if not col_exists: + # ============ sentence_progress ============ await conn.execute(""" - ALTER TABLE bubble_words_progress - ADD COLUMN current_hints_used INT DEFAULT 0 + CREATE TABLE IF NOT EXISTS sentence_progress ( + user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, + sentence_id INTEGER REFERENCES sentences(id) ON DELETE CASCADE, + completed_at TIMESTAMP, + score INTEGER DEFAULT 0, + level TEXT DEFAULT 'A1', + PRIMARY KEY (user_id, sentence_id) + ) """) - logger.info("Added current_hints_used column to bubble_words_progress") - # ============ wrong_because_explanations ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS wrong_because_explanations ( - id SERIAL PRIMARY KEY, - wrong_because VARCHAR(100) UNIQUE NOT NULL, - explanation_pl TEXT NOT NULL, - explanation_ru TEXT NOT NULL, - explanation_uk TEXT NOT NULL, - explanation_en TEXT NOT NULL - ) - """) + # ============ Авто-миграция: добавляем level в sentence_progress ============ + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'sentence_progress' AND column_name = 'level' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE sentence_progress + ADD COLUMN level TEXT DEFAULT 'A1' + """) + logger.info("Added level column to sentence_progress") - # ============ teacher_reactions ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS teacher_reactions ( - id SERIAL PRIMARY KEY, - reaction_type VARCHAR(50) NOT NULL, - text_pl TEXT NOT NULL, - text_ru TEXT NOT NULL, - text_uk TEXT NOT NULL, - text_en TEXT NOT NULL - ) - """) + # ============ typing_progress ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS typing_progress ( + user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, + sentence_id INTEGER REFERENCES sentences(id) ON DELETE CASCADE, + completed_at TIMESTAMP DEFAULT NOW(), + PRIMARY KEY (user_id, sentence_id) + ) + """) - # ============ Индекс для teacher_reactions ============ - await conn.execute("CREATE INDEX IF NOT EXISTS idx_teacher_reactions_type ON teacher_reactions(reaction_type)") + # 🔧 ФИКС (23.09.2026): duplicate_index_typing_progress — индекс idx_typing_progress_user + # уже создаётся в общем блоке индексов ниже. Убираем дубль здесь. + # await conn.execute("CREATE INDEX IF NOT EXISTS idx_typing_progress_user ON typing_progress(user_id)") - # ============ teacher_chat_history ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS teacher_chat_history ( - id BIGSERIAL PRIMARY KEY, - user_id BIGINT NOT NULL UNIQUE, - messages JSONB NOT NULL DEFAULT '[]', - updated_at TIMESTAMP DEFAULT NOW() - ) - """) + # ============ chat_history ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS chat_history ( + id SERIAL PRIMARY KEY, + user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, + message TEXT NOT NULL, + is_user BOOLEAN DEFAULT TRUE, + created_at TIMESTAMP DEFAULT NOW() + ) + """) - # ============ teacher_ai_usage ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS teacher_ai_usage ( - id BIGSERIAL PRIMARY KEY, - user_id BIGINT NOT NULL, - usage_date DATE DEFAULT CURRENT_DATE, - command_count INTEGER DEFAULT 0, - UNIQUE(user_id, usage_date) - ) - """) + # ============ Индекс для chat_history ============ + await conn.execute("CREATE INDEX IF NOT EXISTS idx_chat_history_user ON chat_history(user_id, created_at DESC)") - # ============ daily_phrases — фраза дня ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS daily_phrases ( - id SERIAL PRIMARY KEY, - phrase_date DATE UNIQUE NOT NULL, - phrase_pl TEXT NOT NULL, - translations JSONB NOT NULL DEFAULT '{}', - grammar_breakdowns JSONB NOT NULL DEFAULT '{}', - created_at TIMESTAMP DEFAULT NOW() - ) - """) + # ============ chat_usage ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS chat_usage ( + id SERIAL PRIMARY KEY, + user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, + usage_date DATE DEFAULT CURRENT_DATE, + message_count INTEGER DEFAULT 0, + UNIQUE (user_id, usage_date) + ) + """) - # ============ Индекс для daily_phrases ============ - await conn.execute("CREATE INDEX IF NOT EXISTS idx_daily_phrases_date ON daily_phrases(phrase_date DESC)") + # ============ Индекс для chat_usage ============ + await conn.execute("CREATE INDEX IF NOT EXISTS idx_chat_usage_user ON chat_usage(user_id, usage_date DESC)") - # ============ СИСТЕМА ПОДДЕРЖКИ ============ - # ============ support_tickets ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS support_tickets ( - id BIGSERIAL PRIMARY KEY, - user_id BIGINT NOT NULL, - status TEXT DEFAULT 'open', - created_at TIMESTAMP DEFAULT NOW(), - updated_at TIMESTAMP DEFAULT NOW() - ) - """) + # ============ learning_levels ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS learning_levels ( + id SERIAL PRIMARY KEY, + code VARCHAR(10) UNIQUE NOT NULL, + name_pl TEXT NOT NULL, + name_ru TEXT NOT NULL, + name_uk TEXT NOT NULL, + name_en TEXT NOT NULL, + sort_order INT NOT NULL, + is_active BOOLEAN DEFAULT TRUE + ) + """) - # ============ Индекс для support_tickets ============ - await conn.execute("CREATE INDEX IF NOT EXISTS idx_support_tickets_user ON support_tickets(user_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_support_tickets_status ON support_tickets(status)") + # ============ learning_topics ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS learning_topics ( + id SERIAL PRIMARY KEY, + topic_id VARCHAR(100) UNIQUE NOT NULL, + level_code VARCHAR(10) NOT NULL, + order_number INT NOT NULL, + name_pl TEXT NOT NULL, + name_ru TEXT NOT NULL, + name_uk TEXT NOT NULL, + name_en TEXT NOT NULL, + description_pl TEXT, + description_ru TEXT, + description_uk TEXT, + description_en TEXT, + is_active BOOLEAN DEFAULT TRUE, + UNIQUE(level_code, order_number) + ) + """) - # ============ support_messages ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS support_messages ( - id BIGSERIAL PRIMARY KEY, - ticket_id BIGINT NOT NULL REFERENCES support_tickets(id) ON DELETE CASCADE, - sender_id BIGINT NOT NULL, - message TEXT NOT NULL, - is_read BOOLEAN DEFAULT FALSE, - created_at TIMESTAMP DEFAULT NOW() - ) - """) + # ============ learning_lessons ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS learning_lessons ( + id SERIAL PRIMARY KEY, + lesson_id VARCHAR(120) UNIQUE NOT NULL, + topic_id VARCHAR(100) NOT NULL REFERENCES learning_topics(topic_id), + order_number INT NOT NULL, + skill_tag VARCHAR(100) NOT NULL, + title_pl TEXT NOT NULL, + title_ru TEXT NOT NULL, + title_uk TEXT NOT NULL, + title_en TEXT NOT NULL, + objective_pl TEXT, + objective_ru TEXT, + objective_uk TEXT, + objective_en TEXT, + explanation_pl TEXT, + explanation_ru TEXT, + explanation_uk TEXT, + explanation_en TEXT, + is_active BOOLEAN DEFAULT TRUE, + UNIQUE(topic_id, order_number) + ) + """) - # ============ Индекс для support_messages ============ - await conn.execute("CREATE INDEX IF NOT EXISTS idx_support_messages_ticket ON support_messages(ticket_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_support_messages_sender ON support_messages(sender_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_support_messages_unread ON support_messages(ticket_id, is_read)") + # ============ lesson_examples ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS lesson_examples ( + id SERIAL PRIMARY KEY, + lesson_id VARCHAR(120) NOT NULL REFERENCES learning_lessons(lesson_id), + order_number INT NOT NULL, + text_pl TEXT NOT NULL, + text_ru TEXT, + text_uk TEXT, + text_en TEXT, + audio_url TEXT + ) + """) - # ============ 🔧 НОВОЕ (18.09.2026): feed_posts — посты для карусели ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS feed_posts ( - id BIGSERIAL PRIMARY KEY, - section TEXT NOT NULL, - source_language TEXT NOT NULL DEFAULT 'ru', + # ============ learning_questions ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS learning_questions ( + id SERIAL PRIMARY KEY, + question_id VARCHAR(150) UNIQUE NOT NULL, + lesson_id VARCHAR(120) REFERENCES learning_lessons(lesson_id), + topic_id VARCHAR(100) NOT NULL REFERENCES learning_topics(topic_id), + context_type VARCHAR(50) NOT NULL, + question_type VARCHAR(50) NOT NULL, + skill_tag VARCHAR(100) NOT NULL, + difficulty INT NOT NULL CHECK (difficulty BETWEEN 1 AND 3), + prompt_pl TEXT, + prompt_ru TEXT, + prompt_uk TEXT, + prompt_en TEXT, + correct_answer TEXT, + hint_type VARCHAR(50), + hint_value TEXT, + is_test_question BOOLEAN DEFAULT FALSE, + test_order INT, + options_language TEXT DEFAULT 'pl' + ) + """) - title_pl TEXT, - title_ru TEXT, - title_uk TEXT, - title_en TEXT, + # ============ Авто-миграция: добавляем options_language ============ + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'learning_questions' AND column_name = 'options_language' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE learning_questions + ADD COLUMN options_language TEXT DEFAULT 'pl' + """) + logger.info("Added options_language column to learning_questions") - body_pl TEXT, - body_ru TEXT, - body_uk TEXT, - body_en TEXT, + # ============ question_options ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS question_options ( + id SERIAL PRIMARY KEY, + question_id VARCHAR(150) NOT NULL REFERENCES learning_questions(question_id), + option_key VARCHAR(20) NOT NULL, + text_pl TEXT, + text_ru TEXT, + text_uk TEXT, + text_en TEXT, + is_correct BOOLEAN NOT NULL, + wrong_because VARCHAR(100), + UNIQUE(question_id, option_key) + ) + """) - link_url TEXT, - telegram_url TEXT, + # ============ learning_sessions ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS learning_sessions ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL, + level_code VARCHAR(10) NOT NULL, + topic_id VARCHAR(100) NOT NULL, + session_type VARCHAR(30) NOT NULL, + started_at TIMESTAMP NOT NULL DEFAULT NOW(), + completed_at TIMESTAMP, + is_completed BOOLEAN DEFAULT FALSE + ) + """) - author_id BIGINT NOT NULL, - is_pinned BOOLEAN DEFAULT FALSE, - is_published BOOLEAN DEFAULT TRUE, - created_at TIMESTAMP DEFAULT NOW(), - updated_at TIMESTAMP DEFAULT NOW(), - edited_at TIMESTAMP, - expires_at TIMESTAMP - ) - """) + # ============ user_learning_answers ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS user_learning_answers ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL, + session_id BIGINT NOT NULL REFERENCES learning_sessions(id), + level_code VARCHAR(10) NOT NULL, + topic_id VARCHAR(100) NOT NULL, + lesson_id VARCHAR(120), + question_id VARCHAR(150) NOT NULL, + selected_answer TEXT, + correct BOOLEAN, + skill_tag VARCHAR(100) NOT NULL, + question_type VARCHAR(50) NOT NULL, + context_type VARCHAR(50) NOT NULL, + difficulty INT NOT NULL, + time_taken_ms INT, + attempt_number INT DEFAULT 1, + hint_used BOOLEAN DEFAULT FALSE, + wrong_because VARCHAR(100), + is_final_answer BOOLEAN DEFAULT TRUE, + created_at TIMESTAMP DEFAULT NOW() + ) + """) - # ============ Индексы для feed_posts ============ - await conn.execute(""" - CREATE INDEX IF NOT EXISTS idx_feed_posts_section - ON feed_posts(section, created_at DESC) - """) + # ============ user_lesson_progress ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS user_lesson_progress ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL, + lesson_id VARCHAR(120) NOT NULL, + started_at TIMESTAMP, + completed_at TIMESTAMP, + attempts_count INT DEFAULT 0, + correct_count INT DEFAULT 0, + best_score NUMERIC(5,2), + average_time_ms INT, + completed BOOLEAN DEFAULT FALSE, + UNIQUE(user_id, lesson_id) + ) + """) - await conn.execute(""" - CREATE INDEX IF NOT EXISTS idx_feed_posts_pinned - ON feed_posts(section, is_pinned DESC, created_at DESC) - """) + # ============ user_topic_progress ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS user_topic_progress ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL, + topic_id VARCHAR(100) NOT NULL, + lessons_total INT DEFAULT 0, + lessons_completed INT DEFAULT 0, + test_started BOOLEAN DEFAULT FALSE, + test_completed BOOLEAN DEFAULT FALSE, + test_score NUMERIC(5,2), + topic_mastery NUMERIC(5,2), + confidence NUMERIC(5,2), + completed_at TIMESTAMP, + UNIQUE(user_id, topic_id) + ) + """) - await conn.execute(""" - CREATE INDEX IF NOT EXISTS idx_feed_posts_published - ON feed_posts(section, is_published) - """) + # ============ user_skill_mastery (Analytics 2.0 + Этап 2) ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS user_skill_mastery ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL, + skill_tag VARCHAR(100) NOT NULL, + attempts_count INT DEFAULT 0, + correct_count INT DEFAULT 0, + recognition_attempts INT DEFAULT 0, + recognition_correct INT DEFAULT 0, + recognition_score NUMERIC(5,2), + production_attempts INT DEFAULT 0, + production_correct INT DEFAULT 0, + production_score NUMERIC(5,2), + application_attempts INT DEFAULT 0, + application_correct INT DEFAULT 0, + application_score NUMERIC(5,2), + retention_score NUMERIC(5,2) DEFAULT 0, + hint_dependency NUMERIC(5,2) DEFAULT 0, + average_time_ms INT DEFAULT 0, + median_time_ms INT DEFAULT 0, + median_correct_time_ms INT DEFAULT 0, + median_wrong_time_ms INT DEFAULT 0, + recent_accuracy NUMERIC(5,2) DEFAULT 0, + last_3_accuracy NUMERIC(5,2), + last_5_accuracy NUMERIC(5,2), + last_10_accuracy NUMERIC(5,2), + long_term_accuracy NUMERIC(5,2) DEFAULT 0, + mastery_score NUMERIC(5,2) DEFAULT 0, + previous_mastery_score NUMERIC(5,2) DEFAULT 0, + mastery_delta NUMERIC(5,2) DEFAULT 0, + trend TEXT DEFAULT 'insufficient_data', + retention_status TEXT DEFAULT 'fresh', + fluency_score NUMERIC(5,2) DEFAULT 0, + passive_knowledge_score NUMERIC(5,2) DEFAULT 0, + active_knowledge_score NUMERIC(5,2) DEFAULT 0, + confidence NUMERIC(5,2) DEFAULT 0, + error_count INT DEFAULT 0, + repeated_error_count INT DEFAULT 0, + days_since_last_attempt INT DEFAULT 0, + last_attempt_at TIMESTAMP, + UNIQUE(user_id, skill_tag) + ) + """) - # ============ 🔧 НОВОЕ (18.09.2026): feed_attachments — картинки к постам (до 3) ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS feed_attachments ( - id BIGSERIAL PRIMARY KEY, - post_id BIGINT NOT NULL REFERENCES feed_posts(id) ON DELETE CASCADE, - file_path TEXT NOT NULL, - file_type TEXT DEFAULT 'image', - order_number INT NOT NULL DEFAULT 0, - created_at TIMESTAMP DEFAULT NOW() - ) - """) + # ============ Авто-миграции для user_skill_mastery (Этап 2) ============ + # 🔧 ФИКС (24.09.2026): fstring_sql_migration — + # whitelist-проверка имён колонок и типов. Хардкод безопасен, + # но если кто-то расширит словарь — может быть SQL-инъекция. + stage2_columns = { + "previous_mastery_score": "NUMERIC(5,2) DEFAULT 0", + "mastery_delta": "NUMERIC(5,2) DEFAULT 0", + "trend": "TEXT DEFAULT 'insufficient_data'", + "retention_status": "TEXT DEFAULT 'fresh'", + "fluency_score": "NUMERIC(5,2) DEFAULT 0", + "passive_knowledge_score": "NUMERIC(5,2) DEFAULT 0", + "active_knowledge_score": "NUMERIC(5,2) DEFAULT 0", + } - # ============ Индекс для feed_attachments ============ - await conn.execute(""" - CREATE INDEX IF NOT EXISTS idx_feed_attachments_post - ON feed_attachments(post_id, order_number) - """) + # Whitelist: только буквы, цифры, подчёркивания + import re as _re + _SAFE_NAME = _re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$') - # ============ user_learning_answers_archive ============ - await conn.execute(""" - CREATE TABLE IF NOT EXISTS user_learning_answers_archive ( - id BIGSERIAL PRIMARY KEY, - user_id BIGINT NOT NULL, - skill_tag VARCHAR(100) NOT NULL, - correct BOOLEAN, - wrong_because VARCHAR(100), - created_at DATE, - archived_at TIMESTAMP DEFAULT NOW() - ) - """) + for col_name, col_type in stage2_columns.items(): + if not _SAFE_NAME.match(col_name): + logger.error(f"Unsafe column name skipped: {col_name}") + continue + if not _SAFE_NAME.match(col_type.split()[0]): + logger.error(f"Unsafe column type skipped: {col_type}") + continue - # ============ Индексы для архива ============ - await conn.execute("CREATE INDEX IF NOT EXISTS idx_archive_user_date ON user_learning_answers_archive(user_id, created_at)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_archive_skill_tag ON user_learning_answers_archive(skill_tag)") + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'user_skill_mastery' AND column_name = $1 + """, col_name) + if not col_exists: + await conn.execute(f""" + ALTER TABLE user_skill_mastery + ADD COLUMN {col_name} {col_type} + """) + logger.info(f"Added {col_name} column to user_skill_mastery") - # 🔧 ФИКС (24.09.2026): sentence_hint_no_limit + typing_hint_no_limit — - # таблицы для лимита подсказок (1 раз на предложение). - await conn.execute(""" - CREATE TABLE IF NOT EXISTS sentence_hints_used ( - user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, - sentence_id INTEGER REFERENCES sentences(id) ON DELETE CASCADE, - used_at TIMESTAMP DEFAULT NOW(), - PRIMARY KEY (user_id, sentence_id) - ) - """) + # ============ ai_user_context ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS ai_user_context ( + user_id BIGINT PRIMARY KEY, + current_level VARCHAR(10), + completed_topics INT DEFAULT 0, + level_topics_total INT DEFAULT 0, + level_completion_percent NUMERIC(5,2) DEFAULT 0, + completed_lessons INT DEFAULT 0, + overall_mastery NUMERIC(5,2) DEFAULT 0, + strong_skills JSONB DEFAULT '[]', + weak_skills JSONB DEFAULT '[]', + uncertain_skills JSONB DEFAULT '[]', + recurring_errors JSONB DEFAULT '[]', + recommended_topics JSONB DEFAULT '[]', + recommended_lessons JSONB DEFAULT '[]', + last_analysis_at TIMESTAMP, + analytics_enabled BOOLEAN DEFAULT FALSE + ) + """) - await conn.execute(""" - CREATE TABLE IF NOT EXISTS typing_hints_used ( - user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, - sentence_id INTEGER REFERENCES sentences(id) ON DELETE CASCADE, - used_at TIMESTAMP DEFAULT NOW(), - PRIMARY KEY (user_id, sentence_id) - ) - """) + # ============ Авто-миграция: добавляем level_topics_total ============ + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'ai_user_context' AND column_name = 'level_topics_total' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE ai_user_context + ADD COLUMN level_topics_total INT DEFAULT 0 + """) + logger.info("Added level_topics_total column to ai_user_context") - # ============ Индексы ============ - await conn.execute("CREATE INDEX IF NOT EXISTS idx_levels_number ON levels(level_number)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_levels_level ON levels(level)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_levels_level_number ON levels(level, level_number)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_user_progress_user ON user_progress(user_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_level_words_level_id ON level_words(level_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_level_words_level ON level_words(level)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_users_telegram ON users(telegram_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentences_number ON sentences(sentence_number)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentences_level ON sentences(level)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentences_level_number ON sentences(level, sentence_number)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentence_progress_user ON sentence_progress(user_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_typing_progress_user ON typing_progress(user_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_user_progress_score ON user_progress(score DESC)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentence_progress_score ON sentence_progress(score DESC)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_user_achievements_user ON user_achievements(user_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_daily_logins_user ON daily_logins(user_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_daily_logins_date ON daily_logins(login_date DESC)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_learning_topics_level ON learning_topics(level_code)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_learning_lessons_topic ON learning_lessons(topic_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_learning_questions_lesson ON learning_questions(lesson_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_learning_questions_topic ON learning_questions(topic_id)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_learning_answers_user ON user_learning_answers(user_id, created_at DESC)") - await conn.execute("CREATE INDEX IF NOT EXISTS idx_skill_mastery_user ON user_skill_mastery(user_id)") + # ============ Авто-миграция: добавляем level_completion_percent ============ + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'ai_user_context' AND column_name = 'level_completion_percent' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE ai_user_context + ADD COLUMN level_completion_percent NUMERIC(5,2) DEFAULT 0 + """) + logger.info("Added level_completion_percent column to ai_user_context") + + # ============ achievements ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS achievements ( + id SERIAL PRIMARY KEY, + code TEXT UNIQUE NOT NULL, + category TEXT DEFAULT 'general', + name_pl TEXT NOT NULL, + name_ru TEXT NOT NULL, + name_uk TEXT NOT NULL, + name_en TEXT NOT NULL, + description_pl TEXT DEFAULT '', + description_ru TEXT DEFAULT '', + description_uk TEXT DEFAULT '', + description_en TEXT DEFAULT '', + icon TEXT DEFAULT '🏆', + requirement_type TEXT NOT NULL, + requirement_value INTEGER DEFAULT 0 + ) + """) + + # ============ user_achievements ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS user_achievements ( + user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, + achievement_id INTEGER REFERENCES achievements(id) ON DELETE CASCADE, + unlocked_at TIMESTAMP DEFAULT NOW(), + PRIMARY KEY (user_id, achievement_id) + ) + """) + + # ============ daily_logins ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS daily_logins ( + user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, + login_date DATE DEFAULT CURRENT_DATE, + PRIMARY KEY (user_id, login_date) + ) + """) + + # ============ word_quiz_progress ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS word_quiz_progress ( + user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, + words_guessed INTEGER DEFAULT 0, + total_score INTEGER DEFAULT 0, + PRIMARY KEY (user_id) + ) + """) + + # ============ Авто-миграция: добавляем correct_streak ============ + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'word_quiz_progress' AND column_name = 'correct_streak' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE word_quiz_progress + ADD COLUMN correct_streak INTEGER DEFAULT 0 + """) + logger.info("Added correct_streak column to word_quiz_progress") + + # ============ Авто-миграция: добавляем recent_words ============ + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'word_quiz_progress' AND column_name = 'recent_words' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE word_quiz_progress + ADD COLUMN recent_words TEXT[] DEFAULT '{}' + """) + logger.info("Added recent_words column to word_quiz_progress") + + # ============ Авто-миграция: добавляем level в word_quiz_progress ============ + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'word_quiz_progress' AND column_name = 'level' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE word_quiz_progress + ADD COLUMN level TEXT DEFAULT 'A1' + """) + logger.info("Added level column to word_quiz_progress") + + # 🔧 ФИКС (24.09.2026): client_controlled_mistakes_wordquiz — + # серверный счётчик mistakes для текущего слова. + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'word_quiz_progress' AND column_name = 'current_mistakes' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE word_quiz_progress + ADD COLUMN current_mistakes INT DEFAULT 0 + """) + logger.info("Added current_mistakes column to word_quiz_progress") + + # ============ bubble_words_progress ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS bubble_words_progress ( + user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, + levels_completed INTEGER DEFAULT 0, + total_score INTEGER DEFAULT 0, + PRIMARY KEY (user_id) + ) + """) + + # ============ Авто-миграция: добавляем level в bubble_words_progress ============ + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'bubble_words_progress' AND column_name = 'level' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE bubble_words_progress + ADD COLUMN level TEXT DEFAULT 'A1' + """) + logger.info("Added level column to bubble_words_progress") + + # 🔧 ФИКС (24.09.2026): client_controlled_hints_bubblewords — + # серверный hints_used для текущего уровня пузырей. + col_exists = await conn.fetchval(""" + SELECT 1 FROM information_schema.columns + WHERE table_name = 'bubble_words_progress' AND column_name = 'current_hints_used' + """) + if not col_exists: + await conn.execute(""" + ALTER TABLE bubble_words_progress + ADD COLUMN current_hints_used INT DEFAULT 0 + """) + logger.info("Added current_hints_used column to bubble_words_progress") + + # ============ wrong_because_explanations ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS wrong_because_explanations ( + id SERIAL PRIMARY KEY, + wrong_because VARCHAR(100) UNIQUE NOT NULL, + explanation_pl TEXT NOT NULL, + explanation_ru TEXT NOT NULL, + explanation_uk TEXT NOT NULL, + explanation_en TEXT NOT NULL + ) + """) + + # ============ teacher_reactions ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS teacher_reactions ( + id SERIAL PRIMARY KEY, + reaction_type VARCHAR(50) NOT NULL, + text_pl TEXT NOT NULL, + text_ru TEXT NOT NULL, + text_uk TEXT NOT NULL, + text_en TEXT NOT NULL + ) + """) + + # ============ Индекс для teacher_reactions ============ + await conn.execute("CREATE INDEX IF NOT EXISTS idx_teacher_reactions_type ON teacher_reactions(reaction_type)") + + # ============ teacher_chat_history ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS teacher_chat_history ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL UNIQUE, + messages JSONB NOT NULL DEFAULT '[]', + updated_at TIMESTAMP DEFAULT NOW() + ) + """) + + # ============ teacher_ai_usage ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS teacher_ai_usage ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL, + usage_date DATE DEFAULT CURRENT_DATE, + command_count INTEGER DEFAULT 0, + UNIQUE(user_id, usage_date) + ) + """) + + # ============ daily_phrases — фраза дня ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS daily_phrases ( + id SERIAL PRIMARY KEY, + phrase_date DATE UNIQUE NOT NULL, + phrase_pl TEXT NOT NULL, + translations JSONB NOT NULL DEFAULT '{}', + grammar_breakdowns JSONB NOT NULL DEFAULT '{}', + created_at TIMESTAMP DEFAULT NOW() + ) + """) + + # ============ Индекс для daily_phrases ============ + await conn.execute("CREATE INDEX IF NOT EXISTS idx_daily_phrases_date ON daily_phrases(phrase_date DESC)") + + # ============ СИСТЕМА ПОДДЕРЖКИ ============ + # ============ support_tickets ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS support_tickets ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL, + status TEXT DEFAULT 'open', + created_at TIMESTAMP DEFAULT NOW(), + updated_at TIMESTAMP DEFAULT NOW() + ) + """) + + # ============ Индекс для support_tickets ============ + await conn.execute("CREATE INDEX IF NOT EXISTS idx_support_tickets_user ON support_tickets(user_id)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_support_tickets_status ON support_tickets(status)") + + # ============ support_messages ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS support_messages ( + id BIGSERIAL PRIMARY KEY, + ticket_id BIGINT NOT NULL REFERENCES support_tickets(id) ON DELETE CASCADE, + sender_id BIGINT NOT NULL, + message TEXT NOT NULL, + is_read BOOLEAN DEFAULT FALSE, + created_at TIMESTAMP DEFAULT NOW() + ) + """) + + # ============ Индекс для support_messages ============ + await conn.execute("CREATE INDEX IF NOT EXISTS idx_support_messages_ticket ON support_messages(ticket_id)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_support_messages_sender ON support_messages(sender_id)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_support_messages_unread ON support_messages(ticket_id, is_read)") + + # ============ 🔧 НОВОЕ (18.09.2026): feed_posts — посты для карусели ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS feed_posts ( + id BIGSERIAL PRIMARY KEY, + section TEXT NOT NULL, + source_language TEXT NOT NULL DEFAULT 'ru', + + title_pl TEXT, + title_ru TEXT, + title_uk TEXT, + title_en TEXT, + + body_pl TEXT, + body_ru TEXT, + body_uk TEXT, + body_en TEXT, + + link_url TEXT, + telegram_url TEXT, + + author_id BIGINT NOT NULL, + is_pinned BOOLEAN DEFAULT FALSE, + is_published BOOLEAN DEFAULT TRUE, + created_at TIMESTAMP DEFAULT NOW(), + updated_at TIMESTAMP DEFAULT NOW(), + edited_at TIMESTAMP, + expires_at TIMESTAMP + ) + """) + + # ============ Индексы для feed_posts ============ + await conn.execute(""" + CREATE INDEX IF NOT EXISTS idx_feed_posts_section + ON feed_posts(section, created_at DESC) + """) + + await conn.execute(""" + CREATE INDEX IF NOT EXISTS idx_feed_posts_pinned + ON feed_posts(section, is_pinned DESC, created_at DESC) + """) + + await conn.execute(""" + CREATE INDEX IF NOT EXISTS idx_feed_posts_published + ON feed_posts(section, is_published) + """) + + # ============ 🔧 НОВОЕ (18.09.2026): feed_attachments — картинки к постам (до 3) ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS feed_attachments ( + id BIGSERIAL PRIMARY KEY, + post_id BIGINT NOT NULL REFERENCES feed_posts(id) ON DELETE CASCADE, + file_path TEXT NOT NULL, + file_type TEXT DEFAULT 'image', + order_number INT NOT NULL DEFAULT 0, + created_at TIMESTAMP DEFAULT NOW() + ) + """) + + # ============ Индекс для feed_attachments ============ + await conn.execute(""" + CREATE INDEX IF NOT EXISTS idx_feed_attachments_post + ON feed_attachments(post_id, order_number) + """) + + # ============ user_learning_answers_archive ============ + await conn.execute(""" + CREATE TABLE IF NOT EXISTS user_learning_answers_archive ( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL, + skill_tag VARCHAR(100) NOT NULL, + correct BOOLEAN, + wrong_because VARCHAR(100), + created_at DATE, + archived_at TIMESTAMP DEFAULT NOW() + ) + """) + + # ============ Индексы для архива ============ + await conn.execute("CREATE INDEX IF NOT EXISTS idx_archive_user_date ON user_learning_answers_archive(user_id, created_at)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_archive_skill_tag ON user_learning_answers_archive(skill_tag)") + + # 🔧 ФИКС (24.09.2026): sentence_hint_no_limit + typing_hint_no_limit — + # таблицы для лимита подсказок (1 раз на предложение). + await conn.execute(""" + CREATE TABLE IF NOT EXISTS sentence_hints_used ( + user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, + sentence_id INTEGER REFERENCES sentences(id) ON DELETE CASCADE, + used_at TIMESTAMP DEFAULT NOW(), + PRIMARY KEY (user_id, sentence_id) + ) + """) + + await conn.execute(""" + CREATE TABLE IF NOT EXISTS typing_hints_used ( + user_id BIGINT REFERENCES users(telegram_id) ON DELETE CASCADE, + sentence_id INTEGER REFERENCES sentences(id) ON DELETE CASCADE, + used_at TIMESTAMP DEFAULT NOW(), + PRIMARY KEY (user_id, sentence_id) + ) + """) + + # ============ Индексы ============ + # 🔧 ФИКС (24.09.2026): redundant_index_users_telegram — + # убраны дубли на UNIQUE-колонках: + # - idx_users_telegram (telegram_id уже UNIQUE) + # - idx_typing_progress_user (user_id+sentence_id уже PK) + # - idx_user_achievements_user (user_id+achievement_id уже PK) + # - idx_daily_logins_user (user_id+login_date уже PK) + # - idx_skill_mastery_user (user_id+skill_tag уже UNIQUE) + # - idx_user_progress_user (user_id+level_id уже PK) + # - idx_sentence_progress_user (user_id+sentence_id уже PK) + await conn.execute("CREATE INDEX IF NOT EXISTS idx_levels_number ON levels(level_number)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_levels_level ON levels(level)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_levels_level_number ON levels(level, level_number)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_level_words_level_id ON level_words(level_id)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_level_words_level ON level_words(level)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentences_number ON sentences(sentence_number)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentences_level ON sentences(level)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentences_level_number ON sentences(level, sentence_number)") + # Оставляем score-индексы (не дубли) + await conn.execute("CREATE INDEX IF NOT EXISTS idx_user_progress_score ON user_progress(score DESC)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentence_progress_score ON sentence_progress(score DESC)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_daily_logins_date ON daily_logins(login_date DESC)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_learning_topics_level ON learning_topics(level_code)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_learning_lessons_topic ON learning_lessons(topic_id)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_learning_questions_lesson ON learning_questions(lesson_id)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_learning_questions_topic ON learning_questions(topic_id)") + await conn.execute("CREATE INDEX IF NOT EXISTS idx_learning_answers_user ON user_learning_answers(user_id, created_at DESC)") + # конец транзакции logger.info("Database initialized") \ No newline at end of file diff --git a/main.py b/main.py index fa9e001..af86e55 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import asyncio +import ipaddress import logging import os import re @@ -10,7 +11,7 @@ from fastapi import FastAPI, Request, HTTPException from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse from fastapi.staticfiles import StaticFiles -from config import FRONTEND_ORIGIN, UPLOADS_DIR +from config import FRONTEND_ORIGIN, UPLOADS_DIR, CF_IPS from database import init_db from routers import users, game, hints, leaderboard, sentences, achievements, wordquiz, bubblewords, pronunciation, assistant, learning, tts, typing, teacher, support, admin, feed, admin_content from routers.sentences import init_sentences @@ -125,6 +126,25 @@ class RateLimiter: rate_limiter = RateLimiter() +# 🔧 ФИКС (24.09.2026): cf_connecting_ip_spoof — +# проверяем, что IP клиента входит в список Cloudflare. +def _is_cloudflare_ip(ip_str: str) -> bool: + """Проверяет, что IP входит в CF_IPS (Cloudflare).""" + if not ip_str: + return False + try: + ip = ipaddress.ip_address(ip_str) + except ValueError: + return False + for cidr in CF_IPS: + try: + if ip in ipaddress.ip_network(cidr, strict=False): + return True + except ValueError: + continue + return False + + # ============ Фоновое обновление кэша рейтинга ============ async def refresh_leaderboard_cache_loop(): await asyncio.sleep(10) @@ -151,7 +171,9 @@ async def lifespan(app: FastAPI): await init_achievements() await init_learning_data() - asyncio.create_task(rate_limiter.start_cleanup()) + # 🔧 ФИКС (24.09.2026): cleanup_task_not_cancelled — + # сохраняем ссылку и отменяем в shutdown. + cleanup_task = asyncio.create_task(rate_limiter.start_cleanup()) from database import get_pool pool = await get_pool() @@ -218,6 +240,14 @@ async def lifespan(app: FastAPI): except asyncio.CancelledError: logger.info("Leaderboard refresh task cancelled") + # 🔧 ФИКС (24.09.2026): cleanup_task_not_cancelled — + # отменяем cleanup-задачу rate limiter. + cleanup_task.cancel() + try: + await cleanup_task + except asyncio.CancelledError: + logger.info("Rate limiter cleanup task cancelled") + logger.info(f"Filwords API v{APP_VERSION} stopped") @@ -232,16 +262,22 @@ async def rate_limit_middleware(request: Request, call_next): if path in ["/", "/health"]: return await call_next(request) - # 🔧 ФИКС (23.09.2026): xforwarded_for_spoof — раньше любой клиент мог - # подставить X-Forwarded-For и обойти rate limit. - # Теперь порядок: cf-connecting-ip (Cloudflare) → x-forwarded-for (только если - # запрос пришёл с доверенного прокси) → request.client.host. - # TODO: вынести CF_IPS в конфиг и проверять источник по списку. - client_ip = ( - request.headers.get("cf-connecting-ip") - or request.headers.get("x-real-ip") - or (request.client.host if request.client else "unknown") - ) + # 🔧 ФИКС (24.09.2026): cf_connecting_ip_spoof — + # доверяем cf-connecting-ip ТОЛЬКО если запрос пришёл с IP Cloudflare. + # Иначе — берём request.client.host (реальный IP клиента). + real_client_ip = request.client.host if request.client else "unknown" + + if _is_cloudflare_ip(real_client_ip): + # Запрос от Cloudflare — доверяем cf-connecting-ip + client_ip = ( + request.headers.get("cf-connecting-ip") + or request.headers.get("x-real-ip") + or real_client_ip + ) + else: + # Запрос не от Cloudflare — игнорируем заголовки, берём реальный IP + client_ip = real_client_ip + rate_key = f"{client_ip}:{path}" if not rate_limiter.is_allowed(rate_key, path): diff --git a/routers/feed.py b/routers/feed.py index ede219d..b4cc57d 100644 --- a/routers/feed.py +++ b/routers/feed.py @@ -424,7 +424,8 @@ async def admin_update_feed_post(request: Request, post_id: int): "is_pinned": true/false, "is_published": true/false, "expires_at": "..." | null, - "translate_again": true — перевести заново через AI + "translate_again": true, — перевести заново через AI + "removed_images": ["..."] — 🔧 ФИКС (24.09.2026): URL'ы удалённых картинок } """ telegram_id, _ = await require_auth(request) @@ -443,6 +444,38 @@ async def admin_update_feed_post(request: Request, post_id: int): source_language = existing["source_language"] + # 🔧 ФИКС (24.09.2026): existing_image_removal_no_delete — + # обрабатываем удалённые картинки: удаляем из БД и с диска. + removed_images = body.get("removed_images", []) + if removed_images and isinstance(removed_images, list): + for img_url in removed_images: + # img_url приходит как полный URL: https://api.filwordspl.com/uploads/feed/... + # или относительный: /uploads/feed/... + # Приводим к относительному виду для БД + rel_path = img_url + if img_url.startswith("http"): + # Вырезаем /uploads/... + idx = img_url.find("/uploads/") + if idx >= 0: + rel_path = img_url[idx:] + else: + logger.warning(f"[removed_images] cannot parse URL: {img_url}") + continue + + # Удаляем из БД + deleted = await conn.execute(""" + DELETE FROM feed_attachments + WHERE post_id = $1 AND file_path = $2 + """, post_id, rel_path) + deleted_count = int(deleted.split()[-1]) if deleted else 0 + + if deleted_count > 0: + logger.info(f"[removed_images] deleted from DB: {rel_path}") + else: + logger.warning(f"[removed_images] not found in DB: {rel_path}") + + logger.info(f"[removed_images] post #{post_id}: processed {len(removed_images)} items") + # Мерджим поля: если не передано — оставляем старое title = body.get("title", existing.get(f"title_{source_language}") or "") post_body = body.get("body", existing.get(f"body_{source_language}") or "") @@ -516,7 +549,32 @@ async def admin_update_feed_post(request: Request, post_id: int): post_id, ) - return {"status": "ok", "post_id": post_id} + # 🔧 ФИКС (24.09.2026): existing_image_removal_no_delete — + # удаляем файлы с диска ВНЕ pool.acquire. + if removed_images and isinstance(removed_images, list): + for img_url in removed_images: + rel_path = img_url + if img_url.startswith("http"): + idx = img_url.find("/uploads/") + if idx >= 0: + rel_path = img_url[idx:] + else: + continue + + # "/uploads/feed/16_0_abc.jpg" → "feed/16_0_abc.jpg" + rel_disk = rel_path.replace("/uploads/", "", 1).lstrip("/") + abs_path = Path(UPLOADS_DIR) / rel_disk + + try: + if abs_path.exists(): + abs_path.unlink() + logger.info(f"[removed_images] removed file: {abs_path}") + else: + logger.warning(f"[removed_images] file not found: {abs_path}") + except Exception as e: + logger.error(f"[removed_images] failed to remove {abs_path}: {e}") + + return {"status": "ok", "post_id": post_id} # 🔧 ФИКС (18.09.2026): POST вместо DELETE.