def detect_unexpected_links(apps, schema_editor):
    """Scan the database for any links that are unexpected.

    The checks will verify that there are no outgoing `call` or `return` links from calculation nodes and that if a
    workflow node has a `create` link, it has at least an accompanying return link to the same data node, or it has a
    `call` link to a calculation node that takes the created data node as input.
    """
    from aiida.backends.general.migrations.provenance_redesign import INVALID_LINK_SELECT_STATEMENTS
    from aiida.manage.database.integrity import write_database_integrity_violation
    with schema_editor.connection.cursor() as cursor:
        for sql, warning_message in INVALID_LINK_SELECT_STATEMENTS:
            cursor.execute(sql)
            results = cursor.fetchall()
            if results:
                headers = ['UUID source', 'UUID target', 'link type',
                    'link label']
                write_database_integrity_violation(results, headers,
                    warning_message)
