Skip to content

Commit

Permalink
Keep the unique flag in DropIndexOp
Browse files Browse the repository at this point in the history
Fixed issue where the "unique" flag of an ``Index`` would not be maintained
when generating downgrade migrations.  Pull request courtesy Iuri de
Silvio.

Fixes:  #1371
Closes: #1372
Pull-request: sqlalchemy/alembic#1372
Pull-request-sha: 515b4ed049048d4f5d178ed1777018d5e0c34968

Change-Id: Id4ff7212e2738f2b38ba0a9a8f12bccdc1796b55
  • Loading branch information
Iuri de Silvio authored and zzzeek committed Dec 6, 2023
1 parent 0fc0879 commit 6827b4d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions alembic/operations/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ def from_index(cls, index: Index) -> DropIndexOp:
table_name=index.table.name,
schema=index.table.schema,
_reverse=CreateIndexOp.from_index(index),
unique=index.unique,
**index.kwargs,
)

Expand Down
7 changes: 7 additions & 0 deletions docs/build/unreleased/1371.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. change::
:tags: bug, autogenerate
:tickets: 1370

Fixed issue where the "unique" flag of an ``Index`` would not be maintained
when generating downgrade migrations. Pull request courtesy Iuri de
Silvio.
9 changes: 9 additions & 0 deletions tests/test_autogen_diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,7 @@ def setUp(self):
Column("x", Integer()),
)
self.ix = Index("ix1", t.c.id)
self.ix_unique = Index("ix2", t.c.id, unique=True)
fk = ForeignKeyConstraint(["t_id"], ["t.id"])
q = Table("q", m, Column("t_id", Integer()), fk)
self.table = t
Expand Down Expand Up @@ -1905,6 +1906,14 @@ def test_create_index(self):
eq_(op.to_index(), schemacompare.CompareIndex(self.ix))
eq_(op.reverse().to_index(), schemacompare.CompareIndex(self.ix))

def test_create_unique_index(self):
op = ops.CreateIndexOp.from_index(self.ix_unique)
eq_(op.to_index(), schemacompare.CompareIndex(self.ix_unique))
eq_(
op.reverse().to_index(),
schemacompare.CompareIndex(self.ix_unique),
)


class MultipleMetaDataTest(AutogenFixtureTest, TestBase):
def test_multiple(self):
Expand Down

0 comments on commit 6827b4d

Please sign in to comment.