Skip to content
Snippets Groups Projects
Verified Commit 8715e365 authored by Romain De Laage De Bellefaye's avatar Romain De Laage De Bellefaye
Browse files

[CodiMD] Improve script

parent 46384f35
No related branches found
No related tags found
No related merge requests found
......@@ -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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment