50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
"""定时自动学习开关,以及本月停止/学时月份标记。"""
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
revision = "0014_study_schedule"
|
|
down_revision = "0013_hours_source"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
_CN = timezone(timedelta(hours=8))
|
|
|
|
|
|
def upgrade() -> None:
|
|
with op.batch_alter_table("accounts") as batch:
|
|
batch.add_column(sa.Column("hours_month", sa.String(length=7), nullable=False, server_default=""))
|
|
batch.add_column(sa.Column("study_stop_month", sa.String(length=7), nullable=False, server_default=""))
|
|
batch.add_column(
|
|
sa.Column("study_schedule_enabled", sa.Boolean(), nullable=False, server_default=sa.true())
|
|
)
|
|
key = datetime.now(_CN).strftime("%Y-%m")
|
|
accounts = sa.table(
|
|
"accounts",
|
|
sa.column("hours_month", sa.String),
|
|
sa.column("month_hours", sa.String),
|
|
sa.column("study_paused", sa.Boolean),
|
|
sa.column("study_stop_month", sa.String),
|
|
)
|
|
op.execute(
|
|
accounts.update()
|
|
.where(accounts.c.month_hours != "")
|
|
.where(accounts.c.hours_month == "")
|
|
.values(hours_month=key)
|
|
)
|
|
op.execute(
|
|
accounts.update()
|
|
.where(accounts.c.study_paused.is_(True))
|
|
.where(accounts.c.study_stop_month == "")
|
|
.values(study_stop_month=key)
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
with op.batch_alter_table("accounts") as batch:
|
|
batch.drop_column("study_schedule_enabled")
|
|
batch.drop_column("study_stop_month")
|
|
batch.drop_column("hours_month")
|