def verify_node_uuid_uniqueness():
    """Check whether the database contains nodes with duplicate UUIDS.

    Note that we have to redefine this method from aiida.manage.database.integrity.verify_node_uuid_uniqueness
    because that uses the default database connection, while here the one created by Alembic should be used instead.

    :raises: IntegrityError if database contains nodes with duplicate UUIDS.
    """
    from alembic import op
    from sqlalchemy.sql import text
    from aiida.common.exceptions import IntegrityError
    query = text(
        'SELECT s.id, s.uuid FROM (SELECT *, COUNT(*) OVER(PARTITION BY uuid) AS c FROM db_dbnode) AS s WHERE c > 1'
        )
    conn = op.get_bind()
    duplicates = conn.execute(query).fetchall()
    if duplicates:
        table = 'db_dbnode'
        command = ('`verdi database integrity detect-duplicate-uuid {table}`'
            .format(table))
        raise IntegrityError(
            """Your table "{}" contains entries with duplicate UUIDS.
Run {} to return to a consistent state"""
            .format(table, command))
