Fix: Prevent duplicate valid attendances

This commit is contained in:
Amritanshu Agrawal 2021-09-11 17:21:01 +05:30
parent dee053c115
commit ceaf93d1cd
3 changed files with 45 additions and 2 deletions

View File

@ -0,0 +1,35 @@
"""att
Revision ID: 6fb6c96fd408
Revises: 071e8f29d257
Create Date: 2021-09-11 17:03:39.649878
"""
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision = "6fb6c96fd408"
down_revision = "071e8f29d257"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_index(
"only_one_valid_attendance",
"attendances",
["employee_id", "date"],
unique=True,
postgresql_where=sa.text("is_valid = true"),
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index("only_one_valid_attendance", table_name="attendances", postgresql_where=sa.text("is_valid = true"))
# ### end Alembic commands ###

View File

@ -8,6 +8,7 @@ from sqlalchemy import (
Date,
DateTime,
ForeignKey,
Index,
Integer,
Numeric,
select,
@ -20,7 +21,6 @@ from .meta import Base
class Attendance(Base):
__tablename__ = "attendances"
id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
employee_id = Column("employee_id", UUID(as_uuid=True), ForeignKey("employees.id"))
date = Column("date", Date, nullable=False)
@ -30,6 +30,15 @@ class Attendance(Base):
user_id = Column("user_id", UUID(as_uuid=True), ForeignKey("users.id"))
is_valid = Column("is_valid", Boolean)
__table_args__ = (
Index(
"only_one_valid_attendance",
employee_id,
date,
unique=True,
postgresql_where=(is_valid == True), # noqa: E712
),
)
user = relationship("User", primaryjoin="User.id==Attendance.user_id")
def __init__(

View File

@ -48,7 +48,6 @@
"@angular/compiler-cli": "^12.2.4",
"@angular/language-service": "^12.2.4",
"@types/jasmine": "~3.7.4",
"@types/mathjs": "^9.4.2",
"@types/node": "^16.7.10",
"@typescript-eslint/eslint-plugin": "4.30.0",
"@typescript-eslint/parser": "4.30.0",