From 639da91e61973268efe885ecc5524185748da762 Mon Sep 17 00:00:00 2001 From: Mike Dearman <5084545+devmonkey22@users.noreply.github.com> Date: Wed, 13 Jun 2018 03:20:09 -0400 Subject: [PATCH] #290 Fix silk_clear_request_log errors on Postgres (#291) --- .../commands/silk_clear_request_log.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/silk/management/commands/silk_clear_request_log.py b/silk/management/commands/silk_clear_request_log.py index 4b345133..3c6f12ff 100644 --- a/silk/management/commands/silk_clear_request_log.py +++ b/silk/management/commands/silk_clear_request_log.py @@ -14,16 +14,15 @@ def delete_model(model): table = model._meta.db_table if 'mysql' in engine or 'postgresql' in engine: # Use "TRUNCATE" on the table - cursor = connection.cursor() - if 'mysql' in engine: - cursor.execute("SET FOREIGN_KEY_CHECKS=0;") - if 'postgres' in engine: - cursor.execute("ALTER TABLE %s DISABLE TRIGGER ALL;", [table]) - cursor.execute("TRUNCATE TABLE %s", [table]) - if 'mysql' in engine: - cursor.execute("SET FOREIGN_KEY_CHECKS=1;") - if 'postgres' in engine: - cursor.execute("ALTER TABLE %s ENABLE TRIGGER ALL;", [table]) + with connection.cursor() as cursor: + if 'mysql' in engine: + cursor.execute("SET FOREIGN_KEY_CHECKS=0;") + cursor.execute("TRUNCATE TABLE {0}".format(table)) + cursor.execute("SET FOREIGN_KEY_CHECKS=1;") + elif 'postgres' in engine: + cursor.execute("ALTER TABLE {0} DISABLE TRIGGER ALL;".format(table)) + cursor.execute("TRUNCATE TABLE {0} CASCADE".format(table)) + cursor.execute("ALTER TABLE {0} ENABLE TRIGGER ALL;".format(table)) return # Manually delete rows because sqlite does not support TRUNCATE and