diff --git a/lms/migrations/versions/b39108c0cd35_index_on_assignment_title_is_not_null.py b/lms/migrations/versions/b39108c0cd35_index_on_assignment_title_is_not_null.py new file mode 100644 index 0000000000..071ba7611c --- /dev/null +++ b/lms/migrations/versions/b39108c0cd35_index_on_assignment_title_is_not_null.py @@ -0,0 +1,32 @@ +"""Index on assignment.title is not null.""" + +from alembic import op +import sqlalchemy as sa + +revision = "b39108c0cd35" +down_revision = "3d0c022c716c" + + +def upgrade() -> None: + # CONCURRENTLY can't be used inside a transaction. Finish the current one. + op.execute("COMMIT") + + op.create_index( + "ix__assignment_title_is_not_null", + "assignment", + ["title"], + unique=False, + postgresql_where=sa.text("title IS NOT NULL"), + postgresql_concurrently=True, + ) + + +def downgrade() -> None: + op.execute("COMMIT") + + op.drop_index( + "ix__assignment_title_is_not_null", + table_name="assignment", + postgresql_where=sa.text("title IS NOT NULL"), + postgresql_concurrently=True, + ) diff --git a/lms/models/assignment.py b/lms/models/assignment.py index c3fa84c0a0..3e4917ac5a 100644 --- a/lms/models/assignment.py +++ b/lms/models/assignment.py @@ -25,9 +25,6 @@ class Assignment(CreatedUpdatedMixin, Base): """ __tablename__ = "assignment" - __table_args__ = ( - sa.UniqueConstraint("resource_link_id", "tool_consumer_instance_guid"), - ) id: Mapped[int] = mapped_column(autoincrement=True, primary_key=True) @@ -91,6 +88,15 @@ class Assignment(CreatedUpdatedMixin, Base): course: Mapped[Course | None] = relationship(Course) + __table_args__ = ( + sa.UniqueConstraint("resource_link_id", "tool_consumer_instance_guid"), + sa.Index( + "ix__assignment_title_is_not_null", + "title", + postgresql_where=title.is_not(None), + ), + ) + def get_canvas_mapped_file_id(self, file_id): return self.extra.get("canvas_file_mappings", {}).get(file_id, file_id)