Commit 28a4a994 authored by John Davis's avatar John Davis
Browse files

Do not downgrade to unique index if nonunique data present

parent 59186d03
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -6,6 +6,11 @@ Create Date: 2024-10-08 14:08:28.418055

"""

import logging

from alembic import op
from sqlalchemy import text

from galaxy.model.database_object_names import build_index_name
from galaxy.model.migrations.util import (
    alter_column,
@@ -14,6 +19,8 @@ from galaxy.model.migrations.util import (
    transaction,
)

log = logging.getLogger(__name__)

# revision identifiers, used by Alembic.
revision = "9a5207190a4d"
down_revision = "a99a5b52ccb8"
@@ -35,6 +42,15 @@ def upgrade():

def downgrade():
    with transaction():
        drop_index(index_name, table_name)
        alter_column(table_name, column_name, nullable=True)

        stmt = text("SELECT 1 FROM role GROUP BY name HAVING count(*) > 1")
        has_nonunique_values = op.get_bind().scalar(stmt)
        if not has_nonunique_values:
            drop_index(index_name, table_name)
            create_index(index_name, table_name, [column_name], unique=True)
        else:
            msg = f"""This downgrade requires creating a unique index on the `{table_name}.{column_name}` field.
This operation cannot proceed due to the existence of non-unique values in that column, which are the result of Galaxy v24.2 (and above)
operating under normal conditions. The current non-unique index will remain unchanged."""
            log.error(msg)