85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
from sqlalchemy import (
|
|
Boolean,
|
|
Column,
|
|
Date,
|
|
DateTime,
|
|
Enum,
|
|
Float,
|
|
ForeignKey,
|
|
Integer,
|
|
MetaData,
|
|
String,
|
|
Table,
|
|
func,
|
|
)
|
|
|
|
from app.core.dto.user import UserRole
|
|
|
|
metadata = MetaData()
|
|
|
|
trainings = Table(
|
|
"trainings",
|
|
metadata,
|
|
Column("id", Integer, primary_key=True),
|
|
Column("date", Date, nullable=False, index=True),
|
|
Column("trainer", String(100)),
|
|
Column("created_at", DateTime(timezone=True), nullable=False, server_default=func.now()),
|
|
)
|
|
|
|
exercises = Table(
|
|
"exercises",
|
|
metadata,
|
|
Column("id", Integer, primary_key=True),
|
|
Column(
|
|
"training_id",
|
|
ForeignKey("trainings.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
),
|
|
Column("name", String(255), nullable=False, index=True),
|
|
Column("splitted_weight", Boolean, nullable=False, server_default="false"),
|
|
Column("created_at", DateTime(timezone=True), nullable=False, server_default=func.now()),
|
|
)
|
|
|
|
approaches = Table(
|
|
"approaches",
|
|
metadata,
|
|
Column("id", Integer, primary_key=True),
|
|
Column(
|
|
"exercise_id",
|
|
ForeignKey("exercises.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
),
|
|
Column("weight", Float, nullable=False),
|
|
Column("reps", Integer, nullable=False),
|
|
Column("created_at", DateTime(timezone=True), nullable=False, server_default=func.now()),
|
|
)
|
|
|
|
users = Table(
|
|
"users",
|
|
metadata,
|
|
Column("id", Integer, primary_key=True),
|
|
Column("email", String(254), nullable=False, unique=True, index=True),
|
|
Column("password_hash", String(255), nullable=False),
|
|
Column("first_name", String(100), nullable=False, server_default=""),
|
|
Column("last_name", String(100), nullable=False, server_default=""),
|
|
# VARCHAR + CHECK instead of a native PostgreSQL enum: adding a role is a plain migration
|
|
Column(
|
|
"role",
|
|
Enum(
|
|
UserRole,
|
|
name="user_role",
|
|
native_enum=False,
|
|
length=20,
|
|
create_constraint=True,
|
|
values_callable=lambda roles: [role.value for role in roles],
|
|
),
|
|
nullable=False,
|
|
server_default=UserRole.trainee.value,
|
|
),
|
|
Column("is_active", Boolean, nullable=False, server_default="true"),
|
|
Column("is_superuser", Boolean, nullable=False, server_default="false"),
|
|
Column("created_at", DateTime(timezone=True), nullable=False, server_default=func.now()),
|
|
Column("last_login", DateTime(timezone=True)),
|
|
)
|