Commit d325f64e authored by Frederik Wegner's avatar Frederik Wegner
Browse files

Added old migration script

parent d0d3d04c
from pymongo import MongoClient
import pymysql.cursors
import json
import time
import argparse
def filterRepresentsInteger(s):
try:
int(s)
return s
except ValueError:
return 0
def dictGetStringSafe(d, k):
if k in d.keys():
return d[k]
else:
return ""
def connect():
return pymysql.connect(host='localhost',
user='root',
password='toor',
db='akamu',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
def insertSubject(cursor, s):
sql = """INSERT IGNORE INTO `subject` (`name`, `code`, `shortform`, `semester`, `department`, `university`, `idmongo`)
VALUES (%s,%s,%s,%s,%s,%s,%s)"""
cursor.execute(sql, (
s['subjectName'],
s['subjectCode'],
s['subjectShortName'],
s['subjectSemester'],
s['subjectDepartment'],
s['subjectUniversity'],
str(s['_id'])
))
def insertTitle(cursor, title):
sql = """INSERT IGNORE INTO `title` (
`name`,
`subject`,
`unlock_score`,
`unlock_win`,
`idmongo`)
SELECT %s,`idsubject`,%s,%s,%s FROM `subject`
WHERE STRCMP(`code`,%s)"""
cursor.execute(sql, (
title['titleName'],
title['unlockScore'],
title['unlockWin'],
str(title['_id']),
#where
title['subjectCode']
))
def insertUser(cursor, user):
sql = """INSERT IGNORE INTO `user` (
`time_registered`,
`name`,
`password`,
`email`,
`semester`,
`experience`,
`verified`,
`university`,
`selected_title`,
`idmongo`)
SELECT %s,%s,%s,%s,%s,%s,%s,%s,`idtitle`,%s FROM `title`
WHERE STRCMP(`name`,%s)"""
cursor.execute(sql, (
time.strftime('%Y-%m-%d %H:%M:%S'),
user['userName'],
user['userPassword'],
user['userEmail'],
filterRepresentsInteger(user['semester']),
user['score'],
user['isVerified'],
user['university'],
str(user['_id']),
#WHERE
user['currentTitle']
))
def accessTypeToLevel(accT):
if accT == "sa":
return 0
elif accT == "a":
return 1
else:
return 2
def insertMaintainer(cursor, m):
sql = """INSERT IGNORE INTO `maintainer` (
`login`,
`password`,
`level`,
`name`,
`email`,
`idmongo`,
`subject`)
SELECT %s,%s,%s,%s,%s,%s,`idsubject` FROM `subject`
WHERE STRCMP(`code`,%s)"""
cursor.execute(sql, (
m['userName'],
m['userPassword'],
accessTypeToLevel(m['accessType']),
m['userFullName'],
m['email'],
str(m['_id']),
#WHERE
m['subjectCode']
))
def insertPool(cursor, p):
sql = """INSERT IGNORE INTO `pool` (
`name`,
`code`,
`shortform`,
`idmongo`
) VALUES (%s,%s,%s,%s)"""
cursor.execute(sql, (
p['parentSubjectName'],
p['parentSubjectCode'],
p['parentSubjectShortName'],
str(['_id'])
))
def insertQuestion(cursor, q):
sql = """INSERT IGNORE INTO `question` (
`text`,
`idmongo`,
`verified`,
`reviewed`,
`subject`
) SELECT %s,%s,%s,%s,`idsubject` FROM `subject`
WHERE STRCMP(`code`,%s)"""
cursor.execute(sql, (
q['questionText'],
str(q['_id']),
dictGetStringSafe(q,'verified'),
dictGetStringSafe(q,'reviewed'),
#WHERE
q['subjectCode']
))
insertAnswer(cursor, q)
insertExplanation(cursor, q)
def insertExplanation(cursor, q):
questionMongoID = str(q['_id'])
sql = """INSERT IGNORE INTO `explanation` (
`text`,
`question`
) SELECT %s,`idquestion` FROM `question`
WHERE STRCMP(`idmongo`,%s)"""
cursor.execute(sql, (dictGetStringSafe(q,'explanation'), questionMongoID))
def insertAnswer(cursor, q):
questionMongoID = str(q['_id'])
sql = """INSERT IGNORE INTO `answer` (
`question`,
`type`
) SELECT `idquestion`,1 FROM `question`
WHERE STRCMP(`idmongo`,%s)"""
cursor.execute(sql, [questionMongoID])
cursor.execute("SELECT LAST_INSERT_ID()")
lastID = cursor.fetchone()['LAST_INSERT_ID()']
for option in q['answers']:
insertOption(cursor, option, lastID)
def insertOption(cursor, option, answerID):
sql = """INSERT IGNORE INTO `option` (
`text`,
`correct`,
`answer`
) VALUES (%s,%s,%s)"""
cursor.execute(sql, (
option['answerText'],
option['isCorrect'],
answerID
))
parser = argparse.ArgumentParser(description='Migrate akamudb from mongodb to mysql.')
parser.add_argument("-d", "--database", type=str, default="akamudb", dest="database",
help="name fo the database")
parser.add_argument("-a", "--address", type=str, default="ds163060.mlab.com:63060", dest="address",
help="address the database is hosted at")
parser.add_argument("-u", "--user", type=str, default="akamu", dest="username",
help="username for database authentication")
parser.add_argument("-p", "--password", type=str, required=False, dest="password",
help="password for database authentication. If not specified commandline prompt will ask to enter it.")
args = parser.parse_args()
if not args.password:
args.password = input("Enter password: ")
sqlConnection = connect()
uri = 'mongodb://{username}:{password}@{address}/{database}'.format(
username=args.username,
password=args.password,
address=args.address,
database=args.database)
client = MongoClient(uri)
db = client['akamudb']
try:
with sqlConnection.cursor() as cursor:
for subject in db['subject'].find():
insertSubject(cursor, subject)
for title in db['title'].find():
insertTitle(cursor, title)
for user in db['appUser'].find():
insertUser(cursor, user)
for pool in db['parentSubject'].find():
insertPool(cursor, pool)
for maintainer in db['webUser'].find():
insertMaintainer(cursor, maintainer)
for question in db['question'].find():
insertQuestion(cursor, question)
sqlConnection.commit()
finally:
sqlConnection.close()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment