Last active
February 19, 2019 11:51
-
-
Save not-an-aardvark/7473682691b33ae879e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymongo import MongoClient | |
from bson.objectid import ObjectId | |
db = MongoClient('mongodb://localhost:27017/').fapp | |
username_map = {} | |
reverse_map = {} | |
initial_list_of_users = [] | |
print "Beginning database migration.\nAccounting for users..." | |
for user in db.user.find({}): | |
username_map[str(user['_id'])] = user['name'] | |
reverse_map[user['name']] = str(user['_id']) | |
initial_list_of_users.append(user) | |
print "Finished accounting for users.\nMigrating users..." | |
for user in initial_list_of_users: | |
previous_id = str(user['_id']) | |
if previous_id in username_map and reverse_map[username_map[previous_id]] == previous_id: | |
user['_id'] = username_map[previous_id] | |
db.user.insert_one(user) | |
db.user.remove({'_id': ObjectId(previous_id)}) | |
db.user.update_many({}, {'$unset': {'name': 1, 'uid': 1}}) | |
print "Finished migrating users.\nMigrating comments..." | |
for comment in db.comment.find({}): | |
if 'user' in comment and comment['user'] in username_map: | |
db.comment.update_one({'_id': comment['_id']}, {'$set': {'user': username_map[comment['user']]}}) | |
else: | |
print 'Warning: The following comment contains an invalid/missing userid, so it will be deleted.' | |
print comment | |
db.comment.delete_one({'_id': comment['_id']}) | |
print "Finished migrating comments.\nMigrating games..." | |
for game in db.game.find({}): | |
if 'user' in game and game['user'] in username_map: | |
db.game.update_one({'_id': game['_id']}, {'$set': {'user': username_map[game['user']]}}) | |
else: | |
print 'Warning: The following game contains an invalid/missing userid, so it will be deleted.' | |
print game | |
db.game.delete_one({'_id': game['_id']}) | |
print "Finished migrating games.\nMigrating references..." | |
for reference in db.reference.find({}): | |
if 'user' in reference and reference['user'] in username_map and reference['user2']: | |
if reference['user2'][:3] == '/u/': | |
new_user2 = reference['user2'][3:] | |
else: | |
new_user2 = reference['user2'] | |
db.reference.update_one({'_id': reference['_id']}, {'$set': {'user': username_map[reference['user']], 'user2': new_user2}}) | |
else: | |
print 'Warning: The following reference contains an invalid/missing userid, so it will be deleted.' | |
print reference | |
db.reference.delete_one({'_id': reference['_id']}) | |
print "Finished migrating references.\nMigrating modnotes..." | |
for modnote in db.modnote.find({}): | |
if 'refUser' in modnote and modnote['refUser'] in username_map: | |
db.modnote.update_one({'_id': modnote['_id']}, {'$set': {'refUser': username_map[modnote['refUser']]}}) | |
else: | |
print 'Warning: The following modnote contains an invalid/missing userid, so it will be deleted.' | |
print modnote | |
db.modnote.delete_one({'_id': modnote['_id']}) | |
print "Finished migrating modnotes.\nMigrating events..." | |
db.event.update_many({}, {'$rename': {'userName': 'user'}}) | |
print "Finished migrating events.\nDeleting sessions..." | |
db.sessions.drop() | |
print "Finished deleting sessions.\nMigration is now complete." |
Update 2: Now it removes the /u/
in front of the user2 field in References.
Update 3: Now it won't crash if there are duplicate entries for the same username.
Update 4: No more iteration issues, hopefully
Update 5: Now it won't delete references where the user2 field doesn't begin with /u/
.
Update 6: Rename userName
property of Events to user
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update: Now it shouldn't crash if it encounters malformed database entries.