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

    # First check if the document_info table exists
    if not inspector.has_table("document_info", schema=project_name):
        print(
            f"Migration not needed: '{project_name}.document_info' table doesn't exist yet"
        )
        return False

    # Then check if the columns exist
    existing_columns = [
        col["name"]
        for col in inspector.get_columns("document_info", schema=project_name)
    ]

    needs_upgrade = "summary" not in existing_columns

    if needs_upgrade:
        print(
            "Migration needed: 'summary' column does not exist in document_info table"
        )
    else:
        print(
            "Migration not needed: 'summary' column already exists in document_info table"
        )

    return needs_upgrade
Check if the upgrade has already been applied or is needed.
py/migrations/versions/2fac23e4d91b_migrate_to_document_search.py