hasIndex method

  1. @override
Future<bool> hasIndex(
  1. String table,
  2. String index, {
  3. String? schema,
  4. String? type,
})
override

Determines if the given table has a specific index. Optionally check by index type ('primary', 'unique', or specific driver type).

Implementation

@override
Future<bool> hasIndex(
  String table,
  String index, {
  String? schema,
  String? type,
}) async {
  final indexes = await listIndexes(table, schema: schema);

  for (final idx in indexes) {
    final typeMatches =
        type == null ||
        (type == 'primary' && idx.primary) ||
        (type == 'unique' && idx.unique) ||
        type.toLowerCase() == idx.type?.toLowerCase();

    if (idx.name.toLowerCase() == index.toLowerCase() && typeMatches) {
      return true;
    }
  }

  return false;
}