Commit 48372a9a authored by Frederik Wegner's avatar Frederik Wegner
Browse files

Stripped test data from deploy data. Debugged mongo-migrate script.

parent 8114eed1
-- -----------------------------------------------------
-- Insert test data
-- -----------------------------------------------------
INSERT IGNORE INTO resource (idresource, uri) VALUES (1,"noavatar.svg");
INSERT IGNORE INTO title (idtitle, name, subject, unlock_score, unlock_win) VALUES (1,"",1,0,0);
INSERT IGNORE INTO avatar (idavatar, image, level) VALUES (1,1,0); -- default "no avatar"-avatar
INSERT IGNORE INTO university (iduniversity, name, shortform, city, country)
VALUES (1, "TU-Darmstadt", "TU-D", "Darmstadt", "Germany");
INSERT IGNORE INTO university (iduniversity, name, shortform, city, country)
VALUES (2, "TU-Muenchen", "TU-M", "Muenchen", "Germany");
INSERT IGNORE INTO university (iduniversity, name, shortform, city, country)
VALUES (3, "Universidad Porto", "UP", "Porto", "Portugal");
INSERT IGNORE INTO maintainer (idmaintainer, login, password, level, name, university, email)
VALUES (1, "u1", "p1", 1, "u1", 1, "u1@gmail.com");
INSERT IGNORE INTO maintainer (idmaintainer, login, password, level, name, university, email)
VALUES (2, "u2", "p2", 2, "u2", 2, "u2@gmail.com");
INSERT IGNORE INTO maintainer (idmaintainer, login, password, level, name, university, email)
VALUES (3, "u3", "p3", 3, "u3", 3, "u3@gmail.com");
INSERT IGNORE INTO maintainer (idmaintainer, login, password, level, name, university, email)
VALUES (4, "u4", "p4", 3, "u4", 1, "u4@gmail.com");
INSERT IGNORE INTO maintainer (idmaintainer, login, password, level, name, university, email)
VALUES (5, "u5", "p5", 2, "u5", 2, "u5@gmail.com");
INSERT IGNORE INTO subject (idsubject, name, shortform, department, university, owner, description)
VALUES (1, "Mathe 1", "M I", "Mathematik", 1, 1, "Mathematik 1");
INSERT IGNORE INTO subject (idsubject, name, shortform, department, university, owner, description)
VALUES (2, "Mathe 2", "M II", "Mathematik", 1, 2, "Mathematik 2");
INSERT IGNORE INTO subject (idsubject, name, shortform, department, university, owner, description)
VALUES (3, "Mathe 3", "M III", "Mathematik", 2, 3, "Mathematik 3");
INSERT IGNORE INTO subject (idsubject, name, shortform, department, university, owner, description)
VALUES (4, "Algorithmen und Datenstrukturen", "AuD", "Algorithmik", 1, 1, "Algorithmen und Datenstrukturen");
INSERT IGNORE INTO subject (idsubject, name, shortform, department, university, owner, description)
VALUES (5, "Aussagen und Praedikatenlogik", "AuPl", "Formale Sprachen", 1, 2, "Aussagen und Praedikatenlogik");
INSERT IGNORE INTO subject (idsubject, name, shortform, department, university, owner, description)
VALUES (6, "Funktionale und Objektorientierte Programmierung", "FOP", "Algorithmik", 2, 3, "Funktionale und Objektorientierte Programmierung");
INSERT INTO resource (idresource, uri) VALUES (1,"noavatar.svg");
INSERT INTO avatar (idavatar, image, level) VALUES (1,1,0); -- default "no avatar"-avatar
......@@ -3,6 +3,8 @@ import pymysql.cursors
import json
import time
import argparse
import random
import string
class FakeConnection:
......@@ -25,7 +27,7 @@ class FileWriteCursor:
self.f = f
def __enter__(self):
self.execute("AUTOCOMMIT=0")
self.execute("SET AUTOCOMMIT=0")
return self
def __exit__(self, ab, b, c):
......@@ -60,7 +62,6 @@ def dictGetBoolStringSafe(d, k):
def cleanTables(cursor):
cursor.execute("SET FOREIGN_KEY_CHECKS=0", ())
tables = [
"resource",
'round',
'title',
'avatar',
......@@ -96,7 +97,7 @@ def insertUniversityDefault(cursor):
cursor.execute(sql, ())
def insertSubject(cursor, s):
sql = """INSERT IGNORE INTO `subject` (`name`, `shortform`, `department`, `idmongo`)
sql = """INSERT INTO `subject` (`name`, `shortform`, `department`, `idmongo`)
VALUES ('%s','%s','%s','%s')"""
cursor.execute(sql, (
s['subjectName'].replace("'", "\\'"),
......@@ -106,14 +107,14 @@ def insertSubject(cursor, s):
))
def insertTitle(cursor, title):
sql = """INSERT IGNORE INTO `title` (
sql = """INSERT INTO `title` (
`name`,
`subject`,
`unlock_score`,
`unlock_win`,
`idmongo`)
SELECT '%s',`idsubject`,%s,%s,'%s' FROM `subject`
WHERE STRCMP(`shortform`,'%s')"""
WHERE `shortform`='%s'"""
cursor.execute(sql, (
title['titleName'].replace("'", "\\'"),
title['unlockScore'],
......@@ -124,6 +125,9 @@ def insertTitle(cursor, title):
))
def insertUser(cursor, user):
newpw = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8))
newpw = bytes(newpw, 'ascii')
newpwhash = bcrypt.hashpw(newpw, bcrypt.gensalt(rounds=10, prefix=b"2a"))
sql = """INSERT IGNORE INTO `user` (
`username`,
`password`,
......@@ -135,10 +139,12 @@ def insertUser(cursor, user):
`selected_title`,
`idmongo`)
SELECT '%s','%s','%s',%s,%s,%s,%s,`idtitle`,'%s' FROM `title`
WHERE STRCMP(`name`,'%s')"""
WHERE `name`='%s';"""
sql += " -- PW:" + newpw
print(sql)
cursor.execute(sql, (
user['userName'].replace("'", "\\'"),
user['userPassword'],
newpwhash,
user['userEmail'],
filterRepresentsInteger(user['semester']),
user['score'],
......@@ -158,7 +164,7 @@ def accessTypeToLevel(accT):
return 2
def insertMaintainer(cursor, m):
sql = """INSERT IGNORE INTO `maintainer` (
sql = """INSERT INTO `maintainer` (
`login`,
`password`,
`level`,
......@@ -168,30 +174,38 @@ def insertMaintainer(cursor, m):
`idmongo`)
SELECT '%s','%s','%s','%s',1,'%s', '%s'"""
cursor.execute(sql, (
m['userName'].replace("'", "\\'"),
m['userName'].replace("'", "\\'")[:45],
m['userPassword'],
accessTypeToLevel(m['accessType']),
m['userFullName'].replace("'", "\\'"),
m['email'],
m['userFullName'].replace("'", "\\'")[:45],
m['email'][:45],
str(m['_id'])
))
def insertPool(cursor, p):
sql = """INSERT IGNORE INTO `pool` (
# pool names are restricted to 45 characters
name = p['parentSubjectName'].replace("'", "\\'")
nameCut = name[:45]
nameRest = name[45:]
description = "Description missing!"
if len(nameRest) > 0:
description = "pool names can only be 45 characters long. Original name " + name + "was cut during migration"
sql = """INSERT INTO `pool` (
`name`,
`shortform`,
`description`,
`image`,
`idmongo`
) VALUES ('%s','%s',"Description missing!",1,'%s')"""
) VALUES ('%s','%s','%s',1,'%s')"""
cursor.execute(sql, (
p['parentSubjectName'].replace("'", "\\'"),
nameCut,
p['parentSubjectCode'],
description,
str(p['_id'])
))
def insertQuestion(cursor, q):
sql = """INSERT IGNORE INTO `question` (
sql = """INSERT INTO `question` (
`text`,
`idmongo`,
`verified`,
......@@ -200,7 +214,7 @@ def insertQuestion(cursor, q):
`answertype`,
`subject`
) SELECT '%s','%s',%s,%s, %s, '%s',`idsubject` FROM `subject`
WHERE STRCMP(`shortform`,'%s')"""
WHERE `shortform`='%s'"""
cursor.execute(sql, (
q['questionText'].replace("'", "\\'"),
str(q['_id']),
......@@ -216,11 +230,11 @@ def insertQuestion(cursor, q):
def insertExplanation(cursor, q):
questionMongoID = str(q['_id'])
sql = """INSERT IGNORE INTO `explanation` (
sql = """INSERT INTO `explanation` (
`text`,
`question`
) SELECT '%s',`idquestion` FROM `question`
WHERE STRCMP(`idmongo`,'%s')"""
WHERE `idmongo`='%s'"""
cursor.execute(sql, (
dictGetStringSafe(q,'explanation'),
questionMongoID
......@@ -229,12 +243,12 @@ def insertExplanation(cursor, q):
def insertOptionAnswer(cursor, q):
questionMongoID = str(q['_id'])
for option in q['answers']:
sql = """INSERT IGNORE INTO `optionanswer` (
sql = """INSERT INTO `optionanswer` (
`text`,
`correct`,
`question`
) SELECT '%s', %s, `idquestion` FROM `question`
WHERE STRCMP(`idmongo`,'%s')"""
WHERE `idmongo`='%s'"""
cursor.execute(sql, (
option['answerText'].replace("'", "\\'"),
option['isCorrect'],
......
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