Fix JSON to Mongo migration (#2346)

Signed-off-by: Toby Harradine <tobyharradine@gmail.com>
This commit is contained in:
Toby Harradine 2018-12-29 10:51:31 +11:00 committed by GitHub
parent 3a8da1f82b
commit 2bd05a5a04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
import argparse import argparse
import asyncio import asyncio
import json import json
@ -177,26 +176,21 @@ def basic_setup():
async def json_to_mongo(current_data_dir: Path, storage_details: dict): async def json_to_mongo(current_data_dir: Path, storage_details: dict):
from redbot.core.drivers.red_mongo import Mongo from redbot.core.drivers.red_mongo import Mongo
core_data_file = list(current_data_dir.glob("core/settings.json"))[0] core_data_file = current_data_dir / "core" / "settings.json"
m = Mongo("Core", "0", **storage_details) driver = Mongo(cog_name="Core", identifier="0", **storage_details)
with core_data_file.open(mode="r") as f: with core_data_file.open(mode="r") as f:
core_data = json.loads(f.read()) core_data = json.loads(f.read())
collection = m.get_collection() data = core_data.get("0", {})
await collection.update_one( for key, value in data.items():
{"_id": m.unique_cog_identifier}, update={"$set": core_data["0"]}, upsert=True await driver.set(key, value=value)
)
for p in current_data_dir.glob("cogs/**/settings.json"): for p in current_data_dir.glob("cogs/**/settings.json"):
cog_name = p.parent.stem
with p.open(mode="r") as f: with p.open(mode="r") as f:
cog_data = json.loads(f.read()) cog_data = json.load(f)
cog_i = None for identifier, data in cog_data.items():
for ident in list(cog_data.keys()): driver = Mongo(cog_name, identifier, **storage_details)
cog_i = str(hash(ident)) for key, value in data.items():
cog_m = Mongo(p.parent.stem, cog_i, **storage_details) await driver.set(key, value=value)
cog_c = cog_m.get_collection()
for ident in list(cog_data.keys()):
await cog_c.update_one(
{"_id": cog_m.unique_cog_identifier}, update={"$set": cog_data[cog_i]}, upsert=True
)
async def mongo_to_json(current_data_dir: Path, storage_details: dict): async def mongo_to_json(current_data_dir: Path, storage_details: dict):