From 0c1c9c49865d0bd7a348ea64a0fa2dc6908afb89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20K=C3=BCnzel?= <simonk@fsmpi.rwth-aachen.de> Date: Mon, 13 May 2024 23:46:04 +0200 Subject: [PATCH] Request insertion auto increment value explicitly --- chapters.py | 3 ++- db.py | 12 +++++++----- edit.py | 4 +++- jobmanagement.py | 3 ++- sorter.py | 3 ++- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/chapters.py b/chapters.py index bbb60ef..c5573fb 100644 --- a/chapters.py +++ b/chapters.py @@ -40,7 +40,8 @@ def suggest_chapter(lectureid): course = query('SELECT * FROM courses WHERE id = ?', lecture['course_id'])[0] id = modify( 'INSERT INTO chapters (lecture_id, time, text, time_created, time_updated, created_by, submitted_by) VALUES (?, ?, ?, ?, ?, ?, ?)', - lectureid, chapter_start, text, datetime.now(), datetime.now(), session.get('user', {'dbid':None})['dbid'], submitter + lectureid, chapter_start, text, datetime.now(), datetime.now(), session.get('user', {'dbid':None})['dbid'], submitter, + get_id=True ) chapter = query('SELECT * FROM chapters WHERE id = ?', id)[0] if not ismod(): diff --git a/db.py b/db.py index d12252c..583035c 100644 --- a/db.py +++ b/db.py @@ -100,8 +100,8 @@ def query(operation, *params, delim="sep", nlfix=True): try: cur = get_dbcursor() cur.execute(operation, params) - except mysql.connector.errors.InternalError as e: - if e.msg == 'Deadlock found when trying to get lock; try restarting transaction': + except Exception as e: + if str(e) == 'Deadlock found when trying to get lock; try restarting transaction': tries += 1 retry = True else: @@ -109,8 +109,8 @@ def query(operation, *params, delim="sep", nlfix=True): rows = [] try: rows = cur.fetchall() - except mysql.connector.errors.InterfaceError as e: - if e.msg == 'No result set to fetch from.': + except Exception as e: + if str(e) == 'No result set to fetch from.' or str(e) == "the last operation didn't produce a result": # no problem, we were just at the end of the result set pass else: @@ -129,10 +129,12 @@ def query(operation, *params, delim="sep", nlfix=True): ptr[name] = col return res -def modify(operation, *params): +def modify(operation, *params, get_id=False): operation, params = fix_query(operation, params) cur = get_dbcursor() cur.execute(operation, params) + if not get_id: + return None return cur.lastrowid @app.teardown_request diff --git a/edit.py b/edit.py index 711e8a4..6da7d39 100644 --- a/edit.py +++ b/edit.py @@ -227,8 +227,10 @@ def create(table): assert column not in defaults columns.append('"'+column+'"') values.append(val) + assert editable_tables[table]['idcolumn'] == 'id' id = modify('INSERT INTO %s (%s) VALUES (%s)'%(editable_tables[table]['table'], - ','.join(columns), ','.join(['?']*len(values))), *values) + ','.join(columns), ','.join(['?']*len(values))), *values, + get_id=True) if table == 'courses': set_responsible(id, session['user']['dbid'], 1) if 'ref' in request.values: diff --git a/jobmanagement.py b/jobmanagement.py index 3378ef9..8f84ce5 100644 --- a/jobmanagement.py +++ b/jobmanagement.py @@ -50,7 +50,8 @@ def schedule_job(jobtype, data=None, priority=0, queue="default"): if not data: data = {} return modify('INSERT INTO jobs (type, priority, queue, data, time_created) VALUES (?, ?, ?, ?, ?)', - jobtype, priority, queue, json.dumps(data, default=date_json_handler), datetime.now()) + jobtype, priority, queue, json.dumps(data, default=date_json_handler), datetime.now(), + get_id=True) def cancel_job(job_id): query('UPDATE jobs SET state = \'deleted\' WHERE id = ? AND state = \'ready\'', job_id) diff --git a/sorter.py b/sorter.py index a0d24c1..258b242 100644 --- a/sorter.py +++ b/sorter.py @@ -61,7 +61,8 @@ def insert_video(lectureid, dbfilepath, fileformatid, hash="", filesize=-1, dura (lecture_id, visible, path, video_format, title, comment, internal, file_modified, time_created, time_updated, created_by, hash, file_size, duration, source) VALUES (?, ?, ?, ?, '', '', '', ?, ?, ?, ?, ?, ?, ?, ?)''', - lectureid, visible, dbfilepath, fileformatid, datetime.now(), datetime.now(), datetime.now(), -1, hash, filesize, duration, sourceid) + lectureid, visible, dbfilepath, fileformatid, datetime.now(), datetime.now(), datetime.now(), -1, hash, filesize, duration, sourceid, + get_id=True) if not sourceid: query('INSERT INTO sortlog (lecture_id,video_id,path,"when") VALUES (?,?,?,?)', lectureid, video_id, dbfilepath, datetime.now()) schedule_job('probe', {'path': dbfilepath, 'lecture_id': lectureid, 'video_id': video_id, 'import-chapters': True}) -- GitLab