'Converts a record from version 4 to version 5. Version 5
uses the magic value -1 instead of "host" for "port" in the
"fake" port entry used to store host scripts (see
`migrate_schema_hosts_3_4()`). Moreover, it changes the
structure of the values of "extraports" from [totalcount,
{"state": count}] to {"total": totalcount, "state": count}.'

@staticmethod
def migrate_schema_hosts_4_5(doc):

    assert (doc['schema_version'] == 4)
    update = {'$set': {'schema_version': 5}}
    updated_ports = False
    updated_extraports = False
    for port in doc.get('ports', []):
        if (port['port'] == 'host'):
            port['port'] = (-1)
            updated_ports = True
    if updated_ports:
        update['$set']['ports'] = doc['ports']
    for (state, (total, counts)) in list(viewitems(doc.get('extraports', {}))):
        doc['extraports'][state] = {'total': total, 'reasons': counts}
        updated_extraports = True
    if updated_extraports:
        update['$set']['extraports'] = doc['extraports']
    return update
