From 819c7271301e6d205feccc4a4d2e3f42271fa178 Mon Sep 17 00:00:00 2001 From: jimdale Date: Fri, 6 Nov 2026 15:13:43 +0100 Subject: [PATCH 1/6] added new constraint on Files table --- dds_web/database/models.py | 9 ++++- ...0440766fc1_files_unique_project_id_name.py | 34 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/d50440766fc1_files_unique_project_id_name.py diff --git a/dds_web/database/models.py b/dds_web/database/models.py index 21b005eb9..e96a7337e 100644 --- a/dds_web/database/models.py +++ b/dds_web/database/models.py @@ -946,7 +946,14 @@ class File(db.Model): # Table setup __tablename__ = "files" - __table_args__ = {"extend_existing": True} + #__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..4886c2522 --- /dev/null +++ b/migrations/versions/d50440766fc1_files_unique_project_id_name.py @@ -0,0 +1,34 @@ +"""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 ### From ddc5a26b57aba9b21a9c20d3d600a4e6a8090a3d Mon Sep 17 00:00:00 2001 From: jimdale Date: Tue, 17 Nov 2026 15:24:17 +0100 Subject: [PATCH 2/6] added code to catch IntegrityError triggered by violation of new index constraint --- dds_web/api/files.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/dds_web/api/files.py b/dds_web/api/files.py index 9dfaebe75..8c14c55dd 100644 --- a/dds_web/api/files.py +++ b/dds_web/api/files.py @@ -95,16 +95,21 @@ 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 From d7d2746bba23430c63bb7f4d39c0b04c5c938b17 Mon Sep 17 00:00:00 2001 From: jimdale Date: Tue, 17 Nov 2026 18:07:49 +0100 Subject: [PATCH 3/6] fix single quotes in various places --- dds_web/api/files.py | 4 ++-- dds_web/database/models.py | 2 +- .../versions/d50440766fc1_files_unique_project_id_name.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/dds_web/api/files.py b/dds_web/api/files.py index 8c14c55dd..40c5a26d0 100644 --- a/dds_web/api/files.py +++ b/dds_web/api/files.py @@ -100,9 +100,9 @@ def post(self): sqlalchemy.exc.IntegrityError) as err: flask.current_app.logger.debug(err) db.session.rollback() - extra_message = '' + extra_message = "" if isinstance(err, sqlalchemy.exc.IntegrityError): - extra_message = ' - this file already uploaded to this project' + extra_message = " - this file already uploaded to this project" raise DatabaseError( message=str(err), alt_message="Failed to add new file to database" diff --git a/dds_web/database/models.py b/dds_web/database/models.py index e96a7337e..d5e74799f 100644 --- a/dds_web/database/models.py +++ b/dds_web/database/models.py @@ -949,7 +949,7 @@ class File(db.Model): #__table_args__ = {"extend_existing": True} __table_args__ = ( - db.UniqueConstraint('project_id', 'name', name='ix_files_project_id_name'), + db.UniqueConstraint("project_id", "name", name="ix_files_project_id_name"), {"extend_existing": True}, ) diff --git a/migrations/versions/d50440766fc1_files_unique_project_id_name.py b/migrations/versions/d50440766fc1_files_unique_project_id_name.py index 4886c2522..bcf7184e9 100644 --- a/migrations/versions/d50440766fc1_files_unique_project_id_name.py +++ b/migrations/versions/d50440766fc1_files_unique_project_id_name.py @@ -10,8 +10,8 @@ from sqlalchemy.dialects import mysql # revision identifiers, used by Alembic. -revision = 'd50440766fc1' -down_revision = '0cd0a3b251e0' +revision = "d50440766fc1" +down_revision = "0cd0a3b251e0" branch_labels = None depends_on = None From 11503af3cb68642923f9d4ef7d56fb7d14b85d58 Mon Sep 17 00:00:00 2001 From: jimdale Date: Tue, 17 Nov 2026 18:25:16 +0100 Subject: [PATCH 4/6] fix further black formatting complaints --- dds_web/api/files.py | 8 +++++--- dds_web/database/models.py | 4 ---- .../d50440766fc1_files_unique_project_id_name.py | 9 +++++---- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/dds_web/api/files.py b/dds_web/api/files.py index 40c5a26d0..41691ab08 100644 --- a/dds_web/api/files.py +++ b/dds_web/api/files.py @@ -95,9 +95,11 @@ def post(self): try: db.session.commit() - except (sqlalchemy.exc.SQLAlchemyError, - sqlalchemy.exc.OperationalError, - sqlalchemy.exc.IntegrityError) 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 = "" diff --git a/dds_web/database/models.py b/dds_web/database/models.py index d5e74799f..7b1120d3e 100644 --- a/dds_web/database/models.py +++ b/dds_web/database/models.py @@ -946,15 +946,11 @@ 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 index bcf7184e9..7ce06e9fd 100644 --- a/migrations/versions/d50440766fc1_files_unique_project_id_name.py +++ b/migrations/versions/d50440766fc1_files_unique_project_id_name.py @@ -5,6 +5,7 @@ Create Date: 2026-11-05 12:33:27.088461 """ + from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import mysql @@ -19,8 +20,8 @@ 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']) + 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 ### @@ -28,7 +29,7 @@ def upgrade(): 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') + 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 ### From 0b913d80bcc8d474cbff0df22ca4c947f49033a4 Mon Sep 17 00:00:00 2001 From: jimdale Date: Tue, 17 Nov 2026 18:29:22 +0100 Subject: [PATCH 5/6] fix even further black formatting complaints --- dds_web/api/files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dds_web/api/files.py b/dds_web/api/files.py index 41691ab08..9891e2164 100644 --- a/dds_web/api/files.py +++ b/dds_web/api/files.py @@ -98,7 +98,7 @@ def post(self): except ( sqlalchemy.exc.SQLAlchemyError, sqlalchemy.exc.OperationalError, - sqlalchemy.exc.IntegrityError + sqlalchemy.exc.IntegrityError, ) as err: flask.current_app.logger.debug(err) db.session.rollback() From 1fbb1350ab66c1eea56cd37c662b8674a5d22afe Mon Sep 17 00:00:00 2001 From: jimdale Date: Wed, 18 Nov 2026 10:01:34 +0100 Subject: [PATCH 6/6] updated SPRINTLOG --- SPRINTLOG.md | 1 + 1 file changed, 1 insertion(+) 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)