FastAPI + SQLAlchemy/Alembic + MariaDB backend with Jinja2/htmx/Alpine server-rendered frontend. Multi-user via Google OAuth, daily habit tracking, monthly/weekly history views, Web Push reminders via APScheduler, and PWA support (manifest, service worker, offline caching). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
77 lines
3.2 KiB
Python
77 lines
3.2 KiB
Python
"""initial schema: habit, habit_log, push_subscription, habit_notification_log
|
|
|
|
Revision ID: 0001_initial
|
|
Revises:
|
|
Create Date: 2026-07-09
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
revision: str = "0001_initial"
|
|
down_revision: Union[str, None] = None
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table(
|
|
"habit",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("name", sa.String(length=200), nullable=False),
|
|
sa.Column("habit_type", sa.String(length=20), nullable=False),
|
|
sa.Column("status", sa.String(length=20), nullable=False, server_default="active"),
|
|
sa.Column("weekdays_mask", sa.SmallInteger(), nullable=False, server_default="127"),
|
|
sa.Column("reminder_time", sa.Time(), nullable=True),
|
|
sa.Column("sort_order", sa.Integer(), nullable=True),
|
|
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
|
|
sa.Column("completed_at", sa.DateTime(), nullable=True),
|
|
sa.CheckConstraint("habit_type in ('build','quit')", name="ck_habit_habit_type"),
|
|
sa.CheckConstraint("status in ('active','completed')", name="ck_habit_status"),
|
|
mysql_engine="InnoDB",
|
|
mysql_charset="utf8mb4",
|
|
)
|
|
|
|
op.create_table(
|
|
"habit_log",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("habit_id", sa.Integer(), sa.ForeignKey("habit.id", ondelete="CASCADE"), nullable=False),
|
|
sa.Column("log_date", sa.Date(), nullable=False),
|
|
sa.Column("checked_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
|
|
sa.UniqueConstraint("habit_id", "log_date", name="uq_habit_log_habit_date"),
|
|
mysql_engine="InnoDB",
|
|
mysql_charset="utf8mb4",
|
|
)
|
|
|
|
op.create_table(
|
|
"push_subscription",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("endpoint", sa.String(length=512), nullable=False, unique=True),
|
|
sa.Column("p256dh_key", sa.String(length=255), nullable=False),
|
|
sa.Column("auth_key", sa.String(length=255), nullable=False),
|
|
sa.Column("user_agent", sa.String(length=255), nullable=True),
|
|
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
|
|
mysql_engine="InnoDB",
|
|
mysql_charset="utf8mb4",
|
|
)
|
|
|
|
op.create_table(
|
|
"habit_notification_log",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("habit_id", sa.Integer(), sa.ForeignKey("habit.id", ondelete="CASCADE"), nullable=False),
|
|
sa.Column("notify_date", sa.Date(), nullable=False),
|
|
sa.Column("sent_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
|
|
sa.UniqueConstraint("habit_id", "notify_date", name="uq_notification_habit_date"),
|
|
mysql_engine="InnoDB",
|
|
mysql_charset="utf8mb4",
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_table("habit_notification_log")
|
|
op.drop_table("push_subscription")
|
|
op.drop_table("habit_log")
|
|
op.drop_table("habit")
|