# Buggy code
def _migrate_create_metadata(cache, out):
    out.warn("Migration: Generating missing metadata files")
    refs = _get_refs(cache)

    for ref in refs:
        try:
            base_folder = os.path.normpath(os.path.join(cache.store, ref.dir_repr()))
            # Force using a package cache layout for everything, we want to alter the cache,
            # not the editables
            layout = PackageCacheLayout(
                base_folder=base_folder, ref=ref, short_paths=False, no_lock=True
            )
            folder = layout.export()
            try:
                manifest = FileTreeManifest.load(folder)
                rrev = manifest.summary_hash
            except:
                rrev = DEFAULT_REVISION_V1
            metadata_path = os.path.join(layout.conan(), PACKAGE_METADATA)
            if not os.path.exists(metadata_path):
                out.info("Creating {} for {}".format(PACKAGE_METADATA, ref))
                prefs = _get_prefs(layout)
                metadata = PackageMetadata()
                metadata.recipe.revision = rrev
                for pref in prefs:
                    try:
                        pmanifest = FileTreeManifest.load(layout.package(pref))
                        prev = pmanifest.summary_hash
                    except:
                        prev = DEFAULT_REVISION_V1
                    metadata.packages[pref.id].revision = prev
                    metadata.packages[
                        pref.id
                    ].recipe_revision = metadata.recipe.revision
                save(metadata_path, metadata.dumps())
        except Exception as e:
            raise ConanException(
                "Something went wrong while generating the metadata.json files "
                "in the cache, please try to fix the issue or wipe the cache: {}"
                ":{}".format(ref, e)
            )
    out.success("Migration: Generating missing metadata files finished OK!\n")


# Traceback (ValueError)
Traceback (most recent call last):
File "build\venv\lib\site-packages\conans\model\ref.py", line 111, in loads
_, name, version, user, channel, revision, _ = ConanFileReference.sep_pattern.split(text)
ValueError: not enough values to unpack (expected 7, got 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "build\venv\lib\site-packages\conans\client\command.py", line 377, in install
ref = ConanFileReference.loads(args.path_or_reference)
File "build\venv\lib\site-packages\conans\model\ref.py", line 114, in loads
"OpenCV/1.0.6@user/stable" % text)
conans.errors.ConanException: Wrong package recipe reference C:/Users/daniel/src/flow
Write something like OpenCV/1.0.6@user/stable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "build\venv\lib\site-packages\conans\client\command.py", line 1579, in run
method(args[0][1:])
File "build\venv\lib\site-packages\conans\client\command.py", line 390, in install
install_folder=args.install_folder)
File "build\venv\lib\site-packages\conans\client\conan_api.py", line 93, in wrapper
return f(*args, **kwargs)
File "build\venv\lib\site-packages\conans\client\conan_api.py", line 580, in install
no_imports=no_imports)
File "build\venv\lib\site-packages\conans\client\manager.py", line 80, in install
installer.install(deps_graph, keep_build)
File "build\venv\lib\site-packages\conans\client\installer.py", line 302, in install
self._build(nodes_by_level, keep_build, root_node, graph_info)
File "build\venv\lib\site-packages\conans\client\installer.py", line 324, in _build
self._handle_node_cache(node, keep_build, processed_package_refs)
File "build\venv\lib\site-packages\conans\client\installer.py", line 404, in _handle_node_cache
assert node.prev, "PREV for %s is None" % str(pref)
AssertionError: PREV for Boost/1.68.0-16@velo3d/thirdparty:df18e90df1fedfd9db3e0852ce38a27c76c5a54b is None

ERROR: PREV for Boost/1.68.0-16@velo3d/thirdparty:df18e90df1fedfd9db3e0852ce38a27c76c5a54b is None

# Fixed code
def _migrate_create_metadata(cache, out):
    out.warn("Migration: Generating missing metadata files")
    refs = _get_refs(cache)

    for ref in refs:
        try:
            base_folder = os.path.normpath(os.path.join(cache.store, ref.dir_repr()))
            # Force using a package cache layout for everything, we want to alter the cache,
            # not the editables
            layout = PackageCacheLayout(
                base_folder=base_folder, ref=ref, short_paths=False, no_lock=True
            )
            folder = layout.export()
            try:
                manifest = FileTreeManifest.load(folder)
                rrev = manifest.summary_hash
            except Exception:
                rrev = DEFAULT_REVISION_V1
            metadata_path = os.path.join(layout.conan(), PACKAGE_METADATA)
            if not os.path.exists(metadata_path):
                out.info("Creating {} for {}".format(PACKAGE_METADATA, ref))
                prefs = _get_prefs(layout)
                metadata = PackageMetadata()
                metadata.recipe.revision = rrev
                for pref in prefs:
                    try:
                        pmanifest = FileTreeManifest.load(layout.package(pref))
                        prev = pmanifest.summary_hash
                    except Exception:
                        prev = DEFAULT_REVISION_V1
                    metadata.packages[pref.id].revision = prev
                    metadata.packages[
                        pref.id
                    ].recipe_revision = metadata.recipe.revision
                save(metadata_path, metadata.dumps())
        except Exception as e:
            raise ConanException(
                "Something went wrong while generating the metadata.json files "
                "in the cache, please try to fix the issue or wipe the cache: {}"
                ":{}".format(ref, e)
            )
    out.success("Migration: Generating missing metadata files finished OK!\n")