Skip to content

Commit 074da92

Browse files
committed
INTEGRITY: Fix clear database hang issue. Now the database is dropped and recreated.
1 parent 19b19c0 commit 074da92

File tree

3 files changed

+314
-294
lines changed

3 files changed

+314
-294
lines changed

db_functions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ def db_connect():
3232
return conn
3333

3434

35+
def db_connect_root():
36+
base_dir = os.path.dirname(os.path.abspath(__file__))
37+
config_path = os.path.join(base_dir, "mysql_config.json")
38+
with open(config_path) as f:
39+
mysql_cred = json.load(f)
40+
41+
conn = pymysql.connect(
42+
host=mysql_cred["servername"],
43+
user=mysql_cred["username"],
44+
password=mysql_cred["password"],
45+
charset="utf8mb4",
46+
cursorclass=pymysql.cursors.DictCursor,
47+
autocommit=True,
48+
)
49+
50+
return (conn, mysql_cred["dbname"])
51+
52+
3553
def get_checksum_props(checkcode, checksum):
3654
checksize = 0
3755
checktype = checkcode

fileset.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
user_integrity_check,
2424
db_connect,
2525
create_log,
26+
db_connect_root,
2627
)
2728
from collections import defaultdict
29+
from schema import init_database
2830

2931
app = Flask(__name__)
3032

@@ -79,21 +81,13 @@ def index():
7981
@app.route("/clear_database", methods=["POST"])
8082
def clear_database():
8183
try:
82-
conn = db_connect()
84+
(conn, db_name) = db_connect_root()
8385
with conn.cursor() as cursor:
84-
cursor.execute("SET FOREIGN_KEY_CHECKS = 0;")
85-
cursor.execute("TRUNCATE TABLE filechecksum")
86-
cursor.execute("TRUNCATE TABLE history")
87-
cursor.execute("TRUNCATE TABLE transactions")
88-
cursor.execute("TRUNCATE TABLE queue")
89-
cursor.execute("TRUNCATE TABLE file")
90-
cursor.execute("TRUNCATE TABLE fileset")
91-
cursor.execute("TRUNCATE TABLE game")
92-
cursor.execute("TRUNCATE TABLE engine")
93-
cursor.execute("TRUNCATE TABLE log")
94-
cursor.execute("SET FOREIGN_KEY_CHECKS = 1;")
86+
cursor.execute(f"DROP DATABASE IF EXISTS {db_name}")
9587
conn.commit()
96-
print("DATABASE CLEARED")
88+
print("DATABASE DROPPED")
89+
init_database()
90+
print("DATABASE INITIALISED")
9791
except Exception as e:
9892
print(f"Error clearing database: {e}")
9993
finally:

0 commit comments

Comments
 (0)