

def main(dev=False, _db=None):
    _db = (_db or db)
    init_app(routes=False)
    count = 0
    skipped = 0
    scripts_utils.add_file_logger(logger, __file__)
    logger.info('Iterating over all registrations')
    prepare_nodes()
    ensure_schemas()
    node_documents = _db['node'].find({
        'is_registration': True,
    })
    for node in node_documents:
        registered_schemas = []
        registered_meta = {
            
        }
        schemas = node['registered_meta']
        if (not schemas):
            logger.info('Node: {0} is registered but has no registered_meta'.format(node['_id']))
            continue
        for (schema_id, schema) in schemas.iteritems():
            name = _id_to_name(from_mongo(schema_id))
            schema = from_json_or_fail(schema)
            try:
                meta_schema = MetaSchema.find(Q('name', 'eq', name)).sort('-schema_version')[0]
            except IndexError as e:
                logger.error('No MetaSchema matching name: {0} found for node: {1}.'.format(name, node['_id']))
                skipped += 1
                if dev:
                    continue
                else:
                    raise e
            else:
                registered_meta[meta_schema._id] = {key: {
                    'value': value,
                } for (key, value) in schema.items()}
                registered_schemas.append(meta_schema._id)
        db['node'].update({
            '_id': node['_id'],
        }, {
            '$set': {
                'registered_meta': registered_meta,
                'registered_schema': registered_schemas,
            },
        })
        count = (count + 1)
    logger.info('Done with {0} nodes migrated and {1} nodes skipped.'.format(count, skipped))

dataset/ETHPy150Open CenterForOpenScience/osf.io/scripts/migration/migrate_registered_meta.py main/original