[Eva] Ева, привет! Новая задача: игра «Составь #52

Merged
Drobysh merged 4 commits from eva/chat-ec3e05-backend into main 2026-10-16 11:59:55 +00:00
Showing only changes of commit 3c9548540c - Show all commits
+16 -5
View File
@@ -258,20 +258,31 @@ async def init_db():
) )
""") """)
# ============ Убираем старый UNIQUE constraint с sentences.sentence_number ============ # ============ Убираем составной UNIQUE (level, sentence_number) ============
await conn.execute(""" await conn.execute("""
ALTER TABLE sentences DROP CONSTRAINT IF EXISTS sentences_sentence_number_key ALTER TABLE sentences DROP CONSTRAINT IF EXISTS sentences_sentence_number_unique
""") """)
# ============ Добавляем составной UNIQUE (level, sentence_number) ============ # ============ Пересчёт номеров: единый пул 1..N ============
await conn.execute("""
UPDATE sentences s
SET sentence_number = sub.rn
FROM (
SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS rn
FROM sentences
) sub
WHERE s.id = sub.id
""")
# ============ Добавляем UNIQUE (sentence_number) ============
await conn.execute(""" await conn.execute("""
DO $$ DO $$
BEGIN BEGIN
IF NOT EXISTS ( IF NOT EXISTS (
SELECT 1 FROM pg_constraint SELECT 1 FROM pg_constraint
WHERE conname = 'sentences_sentence_number_unique' WHERE conname = 'sentences_sentence_number_key'
) THEN ) THEN
ALTER TABLE sentences ADD CONSTRAINT sentences_sentence_number_unique UNIQUE (level, sentence_number); ALTER TABLE sentences ADD CONSTRAINT sentences_sentence_number_key UNIQUE (sentence_number);
END IF; END IF;
END $$; END $$;
""") """)