toMigrationCommands method

List<MigrationCommand> toMigrationCommands()

Generates migration commands from the schemas' differences

Implementation

List<MigrationCommand> toMigrationCommands() {
  final removedTables =
      droppedTables.map((item) => item.toCommand(shouldDrop: true)).cast<DropTable>();

  // TODOdetect if dropped column is a foreign key joins association AND WRITE TEST

  // Only drop column if the table isn't being dropped too
  final removedColumns = droppedColumns
      .where((item) => !removedTables.any((command) => command.name == item.tableName))
      .map((c) => c.toCommand(shouldDrop: true))
      .cast<DropColumn>();

  final addedColumns = insertedColumns.where((c) => !c.isPrimaryKey).toSet();
  final added = [insertedTables, addedColumns]
      .map(
        (generatedSet) => generatedSet.map((item) => item.toCommand()),
      )
      .expand((s) => s)
      .cast<MigrationCommand>();

  final addedIndices = createdIndices.map((c) => c.toCommand());
  final removedIndices = droppedIndices.map((c) => c.toCommand());

  return [removedTables, removedColumns, added, addedIndices, removedIndices]
      .expand((l) => l)
      .toList();
}