diff --git a/SPRINTLOG.md b/SPRINTLOG.md index c8acdbee3..a96f02ca4 100644 --- a/SPRINTLOG.md +++ b/SPRINTLOG.md @@ -711,3 +711,4 @@ _Nothing merged during this sprint_ - Bump dnspython from 2.9.0 to 2.9.2 ([#1893]https://github.com/ScilifelabDataCentre/dds_web/pull/1893) - Fix missing host in email links in local dev after Werkzeug 3.1.7+ bump ([#1894]https://github.com/ScilifelabDataCentre/dds_web/pull/1894) +- Added db-level UniqueConstraint on Files table to prevent duplicate (file, project_id) pairs ([#1890]https://github.com/ScilifelabDataCentre/dds_web/pull/1890) diff --git a/dds_web/api/files.py b/dds_web/api/files.py index 9dfaebe75..9891e2164 100644 --- a/dds_web/api/files.py +++ b/dds_web/api/files.py @@ -95,16 +95,23 @@ def post(self): try: db.session.commit() - except (sqlalchemy.exc.SQLAlchemyError, sqlalchemy.exc.OperationalError) as err: + except ( + sqlalchemy.exc.SQLAlchemyError, + sqlalchemy.exc.OperationalError, + sqlalchemy.exc.IntegrityError, + ) as err: flask.current_app.logger.debug(err) db.session.rollback() + extra_message = "" + if isinstance(err, sqlalchemy.exc.IntegrityError): + extra_message = " - this file already uploaded to this project" raise DatabaseError( message=str(err), alt_message="Failed to add new file to database" + ( ": Database malfunction." if isinstance(err, sqlalchemy.exc.OperationalError) - else "." + else f"{extra_message}." ), ) from err diff --git a/dds_web/database/models.py b/dds_web/database/models.py index 21b005eb9..7b1120d3e 100644 --- a/dds_web/database/models.py +++ b/dds_web/database/models.py @@ -946,7 +946,10 @@ class File(db.Model): # Table setup __tablename__ = "files" - __table_args__ = {"extend_existing": True} + __table_args__ = ( + db.UniqueConstraint("project_id", "name", name="ix_files_project_id_name"), + {"extend_existing": True}, + ) # Columns id = db.Column(db.BigInteger, primary_key=True, autoincrement=True) diff --git a/migrations/versions/d50440766fc1_files_unique_project_id_name.py b/migrations/versions/d50440766fc1_files_unique_project_id_name.py new file mode 100644 index 000000000..7ce06e9fd --- /dev/null +++ b/migrations/versions/d50440766fc1_files_unique_project_id_name.py @@ -0,0 +1,35 @@ +"""files_unique_project_id_name + +Revision ID: d50440766fc1 +Revises: 0cd0a3b251e0 +Create Date: 2026-11-05 12:33:27.088461 + +""" + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql + +# revision identifiers, used by Alembic. +revision = "d50440766fc1" +down_revision = "0cd0a3b251e0" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + + with op.batch_alter_table("files", schema=None) as batch_op: + batch_op.create_unique_constraint("ix_files_project_id_name", ["project_id", "name"]) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + + with op.batch_alter_table("files", schema=None) as batch_op: + batch_op.drop_constraint("ix_files_project_id_name", type_="unique") + + # ### end Alembic commands ###