def check_if_upgrade_needed():
    """Check if the upgrade has already been applied."""
    connection = op.get_bind()
    inspector = inspect(connection)

    collections_columns = {
        col["name"]
        for col in inspector.get_columns("collections", schema=project_name)
    }

    if "user_count" in collections_columns:
        print(
            "Migration not needed: collections table already has count columns"
        )
        return False
    else:
        print("Migration needed: collections table needs count columns")
        return True
Check if the upgrade has already been applied.
py/migrations/versions/c45a9cf6a8a4_add_user_and_document_count_to_.py