52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
# Создайте файл database/migrate.py
|
|
|
|
"""
|
|
Миграция: добавление полей matched_word и match_type в SpamLog
|
|
"""
|
|
import asyncio
|
|
from sqlalchemy import text
|
|
from .manager import get_manager
|
|
|
|
|
|
async def migrate():
|
|
"""Добавляет поля matched_word и match_type если их нет"""
|
|
manager = get_manager()
|
|
await manager.init()
|
|
|
|
async with manager.session_maker() as session:
|
|
try:
|
|
# Проверяем наличие колонок
|
|
result = await session.execute(
|
|
text("PRAGMA table_info(spam_logs)")
|
|
)
|
|
columns = [row[1] for row in result.fetchall()]
|
|
|
|
if 'matched_word' not in columns:
|
|
print("Добавляем колонку matched_word...")
|
|
await session.execute(
|
|
text("ALTER TABLE spam_logs ADD COLUMN matched_word VARCHAR(255)")
|
|
)
|
|
await session.commit()
|
|
print("✅ Колонка matched_word добавлена")
|
|
|
|
if 'match_type' not in columns:
|
|
print("Добавляем колонку match_type...")
|
|
await session.execute(
|
|
text("ALTER TABLE spam_logs ADD COLUMN match_type VARCHAR(50)")
|
|
)
|
|
await session.commit()
|
|
print("✅ Колонка match_type добавлена")
|
|
|
|
print("✅ Миграция завершена успешно!")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Ошибка миграции: {e}")
|
|
await session.rollback()
|
|
|
|
finally:
|
|
await manager.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(migrate())
|