toSql method

String toSql({
  1. required String tableName,
  2. bool ifNotExists = false,
})

Implementation

String toSql({
  required String tableName,
  bool ifNotExists = false,
}) {
  // No need to log a warning here since unsupported indexes will be filtered
  // out by the migration manager.
  if (type != 'btree') {
    return '';
  }

  // A plain SQLite unique index would silently change this constraint.
  if (nullsDistinct == false) {
    throw MigrationUnsupportedByDialectException(
      dialect: DatabaseDialect.sqlite.name,
      reason:
          'Index "$indexName" on table "$tableName" treats null values as '
          'equal, which "${DatabaseDialect.sqlite.name}" cannot express.',
    );
  }

  var uniqueStr = isUnique ? ' UNIQUE' : '';
  var elementStrs = elements.map((e) => '"${e.definition}"');
  var ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';

  return 'CREATE$uniqueStr INDEX$ifNotExistsStr "$indexName" ON "$tableName" '
      '(${elementStrs.join(', ')});\n';
}