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

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

    # If we find a new column name, we don't need to migrate
    # If we find an old column name, we do need to migrate
    if "id" in collections_columns:
        print(
            "Migration not needed: collections table already has 'id' column"
        )
        return False
    elif "collection_id" in collections_columns:
        print("Migration needed: collections table has old column names")
        return True
    else:
        print(
            "Migration not needed: collections table doesn't exist or has different structure"
        )
        return False
Check if the upgrade has already been applied or is needed.
py/migrations/versions/8077140e1e99_v3_api_database_revision.py