Skip to content

Commit

Permalink
feat: added non-negative check to sql migration
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiam86 committed Sep 9, 2024
1 parent 06b41d6 commit c9d1cb0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def upgrade() -> None:
op.add_column("current_state", sa.Column("balance", sa.Integer(), nullable=True))
op.execute("UPDATE current_state SET balance = 0 WHERE balance IS NULL")
op.alter_column("current_state", "balance", nullable=False)
op.create_check_constraint(
"check_balance_non_negative", "current_state", "balance >= 0"
)
op.alter_column(
"transactions",
"status",
Expand Down
7 changes: 5 additions & 2 deletions backend/database_handler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ class Base(MappedAsDataclass, DeclarativeBase):

class CurrentState(Base):
__tablename__ = "current_state"
__table_args__ = (PrimaryKeyConstraint("id", name="current_state_pkey"),)
__table_args__ = (
PrimaryKeyConstraint("id", name="current_state_pkey"),
CheckConstraint("balance >= 0", name="check_balance_non_negative"),
)

id: Mapped[str] = mapped_column(String(255), primary_key=True)
data: Mapped[dict] = mapped_column(JSONB)
balance: Mapped[int] = mapped_column(Integer, default=0)
balance: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
updated_at: Mapped[Optional[datetime.datetime]] = mapped_column(
DateTime(True),
init=False,
Expand Down

0 comments on commit c9d1cb0

Please sign in to comment.