From 2d281cea30b0ee28e0a10ecaf2a03d946d12ef99 Mon Sep 17 00:00:00 2001 From: Holger Levsen Date: Fri, 12 Oct 2012 17:43:44 +0200 Subject: [PATCH] fix time handling --- db_add.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/db_add.py b/db_add.py index edafca7..a9f42c9 100755 --- a/db_add.py +++ b/db_add.py @@ -29,13 +29,19 @@ def db_add_job(con, name): #logger.debug("INSERT INTO jenkins_job(name) VALUES('%s')" % (name)) return cur.fetchone()[0] +def harmonize_timeformat(time) + if time != 'NULL': + if time.isdigit(): + time = "to_timestamp('%s')" % time + else: + time = "'%s'" % time + return time + def db_add_build(con, db_id, number, start, end, status): cur = con.cursor() cur.execute("SAVEPOINT a") - if start != 'NULL' and start.isdigit(): - start = "to_timestamp('%s')" % start - if end != 'NULL' and end.isdigit(): - end = "to_timestamp('%s')" % end + start = harmonize_timeformat(start) + end = harmonize_timeformat(end) try: cur.execute("INSERT INTO jenkins_build(jenkins_job_id, jenkins_build_number, build_start, build_end, status) VALUES(%s, %s, %s, %s, '%s') RETURNING id" % (db_id, number, start, end, status)) con.commit() @@ -47,14 +53,15 @@ def db_add_build(con, db_id, number, start, end, status): def db_update_build(con, db_id, end, status): cur = con.cursor() cur.execute("SAVEPOINT a") - if end != 'NULL' and end.isdigit(): - end = "to_timestamp('%s')" % end + logger.debug('end = %s' % end) + end = harmonize_timeformat(end) + logger.debug('harmonized_end = %s' % end) try: - cur.execute("UPDATE jenkins_build SET build_end = '%s', status = '%s' WHERE id = %s RETURNING id" % (end, status, db_id)) + cur.execute("UPDATE jenkins_build SET build_end = %s, status = '%s' WHERE id = %s RETURNING id" % (end, status, db_id)) con.commit() except psycopg2.DatabaseError as e: cur.execute("ROLLBACK TO a") # have to rollback after failed command - logger.debug("UPDATE jenkins_build SET build_end = '%s', status = '%s' WHERE id = %s RETURNING id" % (end, status, db_id)) + logger.debug("UPDATE jenkins_build SET build_end = %s, status = '%s' WHERE id = %s RETURNING id" % (end, status, db_id)) return cur.fetchone()[0] def db_add_origin(con, origin): -- 2.39.5