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

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

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

    if "limits_overrides" in users_columns:
        print(
            "Migration not needed: users table already has limits_overrides column"
        )
        return False
    else:
        print("Migration needed: users table needs limits_overrides column")
        return True
Check if the upgrade has already been applied.
py/migrations/versions/7eb70560f406_add_limits_overrides_to_users.py