Fix MongoDB to JSON migration and warn about Mongo driver (#2373)

Resolves #2372.

Signed-off-by: Toby Harradine <tobyharradine@gmail.com>
This commit is contained in:
Toby Harradine 2019-01-18 11:26:33 +11:00 committed by GitHub
parent 32bd47e105
commit 0d4e6a0865
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -114,7 +114,7 @@ def get_storage_type():
print() print()
print("Please choose your storage backend (if you're unsure, choose 1).") print("Please choose your storage backend (if you're unsure, choose 1).")
print("1. JSON (file storage, requires no database).") print("1. JSON (file storage, requires no database).")
print("2. MongoDB") print("2. MongoDB (not recommended, currently unstable)")
storage = input("> ") storage = input("> ")
try: try:
storage = int(storage) storage = int(storage)
@ -198,22 +198,23 @@ async def mongo_to_json(current_data_dir: Path, storage_details: dict):
m = Mongo("Core", "0", **storage_details) m = Mongo("Core", "0", **storage_details)
db = m.db db = m.db
collection_names = await db.collection_names(include_system_collections=False) collection_names = await db.list_collection_names()
for c_name in collection_names: for collection_name in collection_names:
if c_name == "Core": if collection_name == "Core":
c_data_path = current_data_dir / "core" c_data_path = current_data_dir / "core"
else: else:
c_data_path = current_data_dir / "cogs/{}".format(c_name) c_data_path = current_data_dir / "cogs" / collection_name
output = {} c_data_path.mkdir(parents=True, exist_ok=True)
docs = await db[c_name].find().to_list(None) # Every cog name has its own collection
c_id = None collection = db[collection_name]
for item in docs: async for document in collection.find():
item_id = item.pop("_id") # Every cog has its own document.
if not c_id: # This means if two cogs have the same name but different identifiers, they will
c_id = str(hash(item_id)) # be two separate documents in the same collection
output[item_id] = item cog_id = document.pop("_id")
target = JSON(c_name, c_id, data_path_override=c_data_path) driver = JSON(collection_name, cog_id, data_path_override=c_data_path)
await target.jsonIO._threadsafe_save_json(output) for key, value in document.items():
await driver.set(key, value=value)
async def edit_instance(): async def edit_instance():