redisign and bug fix
This commit is contained in:
parent
a18b5ad1ce
commit
8542ce8e01
10 changed files with 1622 additions and 1169 deletions
|
|
@ -1,6 +1,6 @@
|
|||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from sqlalchemy.orm import declarative_base, sessionmaker
|
||||
from sqlalchemy import Column, Integer, String, DateTime, Boolean, Text
|
||||
from sqlalchemy.orm import declarative_base
|
||||
from sqlalchemy import Column, Integer, String, Boolean, text
|
||||
import os
|
||||
|
||||
Base = declarative_base()
|
||||
|
|
@ -31,16 +31,33 @@ class Event(Base):
|
|||
start_time = Column(String) # HH:MM
|
||||
duration_min = Column(Integer, nullable=False)
|
||||
title = Column(String, nullable=False)
|
||||
repeat_weekly = Column(Boolean, default=False)
|
||||
weekday = Column(Integer) # 0=Monday, 6=Sunday, только для weekly events
|
||||
|
||||
|
||||
class WeeklyTaskException(Base):
|
||||
__tablename__ = "weekly_task_exceptions"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
task_id = Column(Integer, index=True)
|
||||
weekday = Column(Integer, nullable=False) # 0=Monday, 6=Sunday
|
||||
date = Column(String, nullable=False) # YYYY-MM-DD
|
||||
action = Column(String, nullable=False) # "delete" или "replace"
|
||||
replacement_title = Column(String) # для action="replace"
|
||||
replacement_date = Column(String) # для переноса/замены одного вхождения
|
||||
|
||||
|
||||
class EventException(Base):
|
||||
__tablename__ = "event_exceptions"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
event_id = Column(Integer, index=True, nullable=False)
|
||||
date = Column(String, nullable=False)
|
||||
action = Column(String, nullable=False) # "delete" или "replace"
|
||||
replacement_title = Column(String)
|
||||
replacement_date = Column(String)
|
||||
replacement_start_time = Column(String)
|
||||
replacement_duration_min = Column(Integer)
|
||||
|
||||
|
||||
class TelegramUser(Base):
|
||||
|
|
@ -53,6 +70,37 @@ class TelegramUser(Base):
|
|||
async def init_db():
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
await _run_migrations(conn)
|
||||
|
||||
|
||||
async def _run_migrations(conn):
|
||||
table_columns = await _get_table_columns(conn)
|
||||
|
||||
task_columns = table_columns.get("tasks", set())
|
||||
if "weekday" not in task_columns:
|
||||
await conn.execute(text("ALTER TABLE tasks ADD COLUMN weekday INTEGER"))
|
||||
|
||||
event_columns = table_columns.get("events", set())
|
||||
if "repeat_weekly" not in event_columns:
|
||||
await conn.execute(text("ALTER TABLE events ADD COLUMN repeat_weekly BOOLEAN DEFAULT 0"))
|
||||
if "weekday" not in event_columns:
|
||||
await conn.execute(text("ALTER TABLE events ADD COLUMN weekday INTEGER"))
|
||||
|
||||
exception_columns = table_columns.get("weekly_task_exceptions", set())
|
||||
if "task_id" not in exception_columns:
|
||||
await conn.execute(text("ALTER TABLE weekly_task_exceptions ADD COLUMN task_id INTEGER"))
|
||||
if "replacement_date" not in exception_columns:
|
||||
await conn.execute(text("ALTER TABLE weekly_task_exceptions ADD COLUMN replacement_date TEXT"))
|
||||
|
||||
|
||||
async def _get_table_columns(conn):
|
||||
table_names = ["tasks", "events", "weekly_task_exceptions", "event_exceptions"]
|
||||
columns = {}
|
||||
for table_name in table_names:
|
||||
result = await conn.exec_driver_sql(f"PRAGMA table_info({table_name})")
|
||||
rows = result.fetchall()
|
||||
columns[table_name] = {row[1] for row in rows}
|
||||
return columns
|
||||
|
||||
|
||||
async def get_db():
|
||||
|
|
@ -61,4 +109,3 @@ async def get_db():
|
|||
yield session
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue