like method

List<IndexComparisonWarning> like(
  1. IndexDefinition other, {
  2. bool ignoreCase = false,
})

Compares this index definition with other, returning a list of mismatches.

Implementation

List<IndexComparisonWarning> like(
  IndexDefinition other, {
  bool ignoreCase = false,
}) {
  List<IndexComparisonWarning> mismatches = [];

  if (ignoreCase &&
          indexName.toLowerCase() != other.indexName.toLowerCase() ||
      !ignoreCase && indexName != other.indexName) {
    mismatches.add(
      IndexComparisonWarning(
        name: 'name',
        expected: indexName,
        found: other.indexName,
      ),
    );
  }

  if (type != other.type) {
    mismatches.add(
      IndexComparisonWarning(
        name: 'type',
        expected: type,
        found: other.type,
      ),
    );
  }

  if (isUnique != other.isUnique) {
    mismatches.add(
      IndexComparisonWarning(
        name: 'isUnique',
        expected: '$isUnique',
        found: '${other.isUnique}',
      ),
    );
  }

  if (isPrimary != other.isPrimary) {
    mismatches.add(
      IndexComparisonWarning(
        name: 'isPrimary',
        expected: '$isPrimary',
        found: '${other.isPrimary}',
      ),
    );
  }

  if (predicate != other.predicate) {
    mismatches.add(
      IndexComparisonWarning(
        name: 'predicate',
        expected: predicate,
        found: other.predicate,
      ),
    );
  }

  if (tableSpace != other.tableSpace) {
    mismatches.add(
      IndexComparisonWarning(
        name: 'tableSpace',
        expected: tableSpace,
        found: other.tableSpace,
      ),
    );
  }

  if (ginOperatorClass != other.ginOperatorClass) {
    mismatches.add(
      IndexComparisonWarning(
        name: 'GIN index operator class',
        expected: ginOperatorClass?.name,
        found: other.ginOperatorClass?.name,
      ),
    );
  }

  if (vectorDistanceFunction != other.vectorDistanceFunction) {
    mismatches.add(
      IndexComparisonWarning(
        name: 'vector distance function',
        expected: vectorDistanceFunction?.name,
        found: other.vectorDistanceFunction?.name,
      ),
    );
  }

  if (vectorColumnType != other.vectorColumnType) {
    mismatches.add(
      IndexComparisonWarning(
        name: 'vector column type',
        expected: vectorColumnType?.name,
        found: other.vectorColumnType?.name,
      ),
    );
  }

  // New parameters missing on other (or if other is null)
  parameters?.entries.forEach((entry) {
    if (other.parameters?[entry.key] != entry.value) {
      mismatches.add(
        IndexComparisonWarning(
          name: 'parameter ${entry.key}',
          expected: entry.value,
          found: other.parameters?[entry.key],
        ),
      );
    }
  });

  // Other parameters missing on this (or if this is null)
  other.parameters?.entries.forEach((otherEntry) {
    if (parameters?[otherEntry.key] == null) {
      mismatches.add(
        IndexComparisonWarning(
          name: 'parameter ${otherEntry.key}',
          expected: null,
          found: otherEntry.value,
        ),
      );
    }
  });

  if (elements.length != other.elements.length) {
    mismatches.add(
      IndexComparisonWarning(
        name: 'elements',
        expected: '${elements.length}',
        found: '${other.elements.length}',
      ),
    );
  } else {
    for (int i = 0; i < elements.length; i++) {
      var elementMismatches = elements[i].like(other.elements[i]);
      if (elementMismatches.isNotEmpty) {
        mismatches.addAll(elementMismatches);
      }
    }
  }

  return mismatches;
}