@@ -65,17 +65,30 @@ def _copy_custom_indexes(self, src_schema: str, dst_schema: str) -> None:
6565 ).fetchall ()
6666 for idx_name , idx_def in rows :
6767 # Rewrite the CREATE INDEX to target the new schema
68- new_def = idx_def .replace (
69- f" ON { src_schema } ." , f" ON { dst_schema } ."
70- )
68+ new_def = idx_def .replace (f" ON { src_schema } ." , f" ON { dst_schema } ." )
7169 # Avoid name collisions by prefixing with target schema
7270 new_idx_name = f"{ dst_schema } _{ idx_name } "
71+ # Handle both CREATE INDEX and CREATE UNIQUE INDEX
72+ new_def = new_def .replace (
73+ f"CREATE UNIQUE INDEX { idx_name } " ,
74+ f"CREATE UNIQUE INDEX IF NOT EXISTS { new_idx_name } " ,
75+ )
7376 new_def = new_def .replace (
7477 f"CREATE INDEX { idx_name } " ,
7578 f"CREATE INDEX IF NOT EXISTS { new_idx_name } " ,
7679 )
7780 try :
78- conn .execute (text (new_def ))
81+ # Use a savepoint so a single index failure doesn't abort
82+ # the entire transaction and block subsequent indexes.
83+ nested = conn .begin_nested ()
84+ try :
85+ conn .execute (text (new_def ))
86+ nested .commit ()
87+ except Exception as exc :
88+ nested .rollback ()
89+ logger .warning (
90+ f"Could not copy index { idx_name } to { dst_schema } : { exc } "
91+ )
7992 except Exception as exc :
8093 logger .warning (
8194 f"Could not copy index { idx_name } to { dst_schema } : { exc } "
0 commit comments