Add last_seen timestamp

This commit is contained in:
Peter J. Holzer 2019-01-21 20:42:51 +01:00
parent 79c579c0ef
commit 9edf81640a
1 changed files with 15 additions and 1 deletions

View File

@ -42,11 +42,20 @@ except Exception as e:
table_schema text,
table_name text,
ts timestamptz,
last_seen timestamptz,
sha256 text
)
"""
)
db.commit()
try:
csr.execute("select last_seen from " + audit_table)
csr.fetchone()
except Exception as e:
db.rollback()
csr.execute("alter table " + audit_table + " add column last_seen timestamptz")
db.commit()
csr.execute("select * from information_schema.tables where table_schema = %s", (a.schema,))
@ -74,9 +83,14 @@ for table in csr.fetchall():
old = csr.fetchone()
if not old or old.sha256 != digest:
print(" changed", digest, end="")
q = "insert into {audit_table}(table_schema, table_name, ts, sha256) values(%s, %s, now(), %s)".format(audit_table=audit_table)
q = "insert into {audit_table}(table_schema, table_name, ts, last_seen, sha256) values(%s, %s, now(), now(), %s)".format(audit_table=audit_table)
csr.execute(q, (table.table_schema, table.table_name, digest))
db.commit()
else:
print(" unchanged", digest, end="")
q = "update {audit_table} set last_seen=now() where id = %s".format(audit_table=audit_table)
csr.execute(q, (old.id,))
db.commit()
db.rollback() # to close transaction
print(flush=True)