def process_null_records(meta, scan=True):
    """Scans the database for null instance_uuid records for processing.

    :param meta: sqlalchemy.MetaData object, assumes tables are reflected.
    :param scan: If True, does a query and fails the migration if NULL instance
                 uuid entries found. If False, makes instances.uuid
                 non-nullable.
    """
    if scan:
        for table in reversed(meta.sorted_tables):
            # NOTE(mriedem): There is a periodic task in the network manager
            # that calls nova.db.api.fixed_ip_disassociate_all_by_timeout which
            # will set fixed_ips.instance_uuid to None by design, so we have to
            # skip the fixed_ips table otherwise we'll wipeout the pool of
            # fixed IPs.
            if table.name not in ('fixed_ips', 'shadow_fixed_ips'):
                scan_for_null_records(table, 'instance_uuid', check_fkeys=True)

    for table_name in ('instances', 'shadow_instances'):
        table = meta.tables[table_name]
        if scan:
            scan_for_null_records(table, 'uuid', check_fkeys=False)
        else:
            # The record is gone so make the uuid column non-nullable.
            table.columns.uuid.alter(nullable=False)
dataset/ETHPy150Open BU-NU-CLOUD-SP16/Trusted-Platform-Module-nova/nova/db/sqlalchemy/migrate_repo/versions/267_instance_uuid_non_nullable.py/process_null_records/original