def is_migration_initial(migration):
    """Return whether a migration is an initial migration.

    Initial migrations are those that set up an app or models for the first
    time. Generally, they should be limited to model creations, or to those
    adding fields to a (non-migration-aware) model for the first time. They
    also should not have any dependencies on other migrations within the same
    app.

    An initial migration should be able to be safely soft-applied (in other
    words, ignored if the model already appears to exist in the database).

    Migrations on Django 1.9+ may declare themselves as explicitly initial
    or explicitly not initial.

    Args:
        migration (django.db.migrations.Migration):
            The migration to check.

    Returns:
        bool:
        ``True`` if the migration appears to be an initial migration.
        ``False`` if it does not.
    """
    # NOTE: The general logic here is based on the checks done in
    #       MigrationExecutor.detect_soft_applied.

    # Migration.initial was introduced in Django 1.9.
    initial = getattr(migration, 'initial', None)

    if initial is False:
        return False
    elif initial is None:
        # If the migration has any dependencies within the same app, it can't
        # be initial.
        for dep_app_label, dep_app_name in migration.dependencies:
            if dep_app_label == migration.app_label:
                return False

    return True