toMigrationCommands method

List<MigrationCommand> toMigrationCommands()

Generates migration commands from the schemas' differences

Implementation

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

  // TODO detect 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) {
        return !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) {
        return generatedSet.map((item) {
          return 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();
}