Skip to content

Commit 18d88c9

Browse files
jdieterrafiss
authored andcommitted
Remove duplicates_constraint value for unique indexes
In PostgreSQL, unlike CockroachDB, the pg_constraints table doesn't contain any entries for unique indexes, which means that the `get_indexes` function doesn't return the `duplicates_constraint` value for unique indexes. The alembic PostgreSQL driver, used for both PostgreSQL and CockroachDB, filters out any indexes that have the `duplicates_constraint` value set, which causes Alembic to believe that the constraint has been converted into an index, rather than recognizing that they're the same thing. This commit fixes the problem by manually filtering out the `duplicates_constraint` value for all unique indexes. Signed-off-by: Jonathan Dieter <jonathan.dieter@spearline.com>
1 parent 8a86af6 commit 18d88c9

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

cockroachdb/sqlalchemy/dialect.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,14 @@ def get_columns(self, conn, table_name, schema=None, **kw):
256256

257257
def get_indexes(self, conn, table_name, schema=None, **kw):
258258
if self._is_v192plus:
259-
return super().get_indexes(conn, table_name, schema, **kw)
259+
indexes = super().get_indexes(conn, table_name, schema, **kw)
260+
# We need to remove the `duplicates_constraints` value from unique indexes, otherwise
261+
# alembic tries to delete and recreate unique indexes. This is consistent with
262+
# postgresql which doesn't set the duplicates_constraint flag on unique indexes
263+
for index in indexes:
264+
if index["unique"] and "duplicates_constraint" in index:
265+
del index["duplicates_constraint"]
266+
return indexes
260267

261268
# The Cockroach database creates a UNIQUE INDEX implicitly whenever the
262269
# UNIQUE CONSTRAINT construct is used. Currently we are just ignoring all unique indexes,

0 commit comments

Comments
 (0)