compareWith method Null safety

String compareWith(
  1. TableDeclaration otherTable
)

Implementation

String compareWith(TableDeclaration otherTable) {
  var lines = [];
  final fieldsNeedsToAdd = fields
      .where((e1) => otherTable.fields
          .where((e2) => e2.fieldName == e1.fieldName)
          .toList()
          .isEmpty)
      .toList();
  // TODO: 加入需要新增的列
  lines.addAll(fieldsNeedsToAdd.map((e) => alterTableAddColumn(e)).toList());

  final fieldsNeedsToRemove = otherTable.fields
      .where((e1) =>
          fields.where((e2) => e2.fieldName == e1.fieldName).toList().isEmpty)
      .toList();
  // TODO: 加入需要删除的列
  lines.addAll(
      fieldsNeedsToRemove.map((e) => alterTableDropColumn(e)).toList());

  final indexNeedToAdd = indexes
      .where((idx) => otherTable.indexes
          .where((cidx) => cidx.indexName == idx.indexName)
          .toList()
          .isEmpty)
      .toList();
  final indexNeedToDelete = otherTable.indexes
      .where((idx) => indexes
          .where((nidx) => nidx.indexName == idx.indexName)
          .toList()
          .isEmpty)
      .toList();

  lines.addAll(indexNeedToAdd.map((e) => createIndexSQL(e)).toList());
  lines.addAll(indexNeedToDelete.map((e) => dropIndexSQL(e)).toList());

  final uniqueNeedToAdd = uniqueIndexes
      .where((u1) => otherTable.uniqueIndexes
          .where((u2) => u1.indexName == u2.indexName)
          .toList()
          .isEmpty)
      .toList();

  final uniqueNeedToDelete = otherTable.uniqueIndexes
      .where((u1) => uniqueIndexes
          .where((u2) => u1.indexName == u2.indexName)
          .toList()
          .isEmpty)
      .toList();

  lines.addAll(uniqueNeedToAdd.map((e) => createUniqueSQL(e)).toList());
  lines.addAll(uniqueNeedToDelete.map((e) => dropUniqueSQL(e)).toList());

  return lines.join('\n\n');
}