From 8715e365bacc9919004d79d539d1a97459f8fba7 Mon Sep 17 00:00:00 2001 From: Romain de Laage <romain.delaage@rdelaage.ovh> Date: Wed, 28 Oct 2020 18:22:32 +0100 Subject: [PATCH] [CodiMD] Improve script --- pica-codimd/deleteOldPad.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/pica-codimd/deleteOldPad.py b/pica-codimd/deleteOldPad.py index 363f5297..25991e12 100755 --- a/pica-codimd/deleteOldPad.py +++ b/pica-codimd/deleteOldPad.py @@ -8,11 +8,12 @@ INTERVAL = environ['OLD_INTERVAL'] USER = environ['POSTGRES_USER'] DATABASE = environ['POSTGRES_DB'] PASSWORD = environ['POSTGRES_PASSWORD'] -HOST = ['DB_HOST'] -PORT = ['DB_PORT'] +HOST = environ['DB_HOST'] +PORT = environ['DB_PORT'] print("===== Begin of deleteOldPad job =====") +#Connect to database try: db = psycopg2.connect("user = " + USER + " dbname = " +DATABASE + " password = " + PASSWORD + " host = " + HOST + " port = " + PORT) except: @@ -20,20 +21,32 @@ except: with db : with db.cursor() as cur: - #Make a backup of old pads in deletedPads + #Get all pads older than INTERVAL try: cur.execute("SELECT \"Notes\".\"id\", \"Notes\".\"content\" FROM \"Notes\" WHERE \"Notes\".\"updatedAt\" < NOW() - interval %s", (INTERVAL,)) except: sys.exit("deleteOldPad : unable to select old pads") - for oldPad in cur.fetchall(): + oldPads = cur.fetchall() + for oldPad in oldPads: + #Backup the pad with open("deletedPads/" + oldPad[0] + '.txt', "w") as f: f.write(oldPad[1]) + #Remove all Revisions related to this pad + try: + cur.execute("DELETE FROM \"Revisions\" WHERE \"Revisions\".\"noteId\" = %s", (oldPad[0],)) + except: + sys.exit("deleteOldPad : unable to delete all revisions related to pad " + oldPad[0]) + #Remove all contributions related to this pad + try: + cur.execute("DELETE FROM \"Authors\" WHERE \"Authors\".\"noteId\" = %s", (oldPad[0],)) + except: + sys.exit("deleteOldPad : unable to delete all contributions related to pad " + oldPad[0]) + #Remove the pad + try: + cur.execute("DELETE FROM \"Notes\" WHERE \"Notes\".\"id\" = %s", (oldPad[0],)) + except: + sys.exit("deleteOldPad : unable to delete the pad " + oldPad[0]) - #Delete the old pads - try: - cur.execute("DELETE FROM \"Notes\" WHERE \"Notes\".\"updatedAt\" < NOW() - interval %s", (INTERVAL,)) - except: - sys.exit("deleteOldPad: unable to delete old pads") print("deleteOldPad : Deleted old pad") db.close() -- GitLab