diff --git a/brewman/alembic/versions/6fb6c96fd408_prevent_duplicate_attendance.py b/brewman/alembic/versions/6fb6c96fd408_prevent_duplicate_attendance.py new file mode 100644 index 00000000..9250acf6 --- /dev/null +++ b/brewman/alembic/versions/6fb6c96fd408_prevent_duplicate_attendance.py @@ -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 ### diff --git a/brewman/brewman/models/attendance.py b/brewman/brewman/models/attendance.py index 8b93c739..d3499e02 100644 --- a/brewman/brewman/models/attendance.py +++ b/brewman/brewman/models/attendance.py @@ -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__( diff --git a/overlord/package.json b/overlord/package.json index e6feed4b..310ca554 100644 --- a/overlord/package.json +++ b/overlord/package.json @@ -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",