From dcd711f0e830bd0d108f9e12899e5342c9816792 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Thu, 3 Feb 2022 16:36:16 +0100 Subject: [PATCH] Dump dependencies as dot file --- demo_usradm_templates | 4 ++-- procrusql.py | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/demo_usradm_templates b/demo_usradm_templates index e754470..9db951c 100755 --- a/demo_usradm_templates +++ b/demo_usradm_templates @@ -2,7 +2,7 @@ import logging import psycopg2 -from procrusql import HaveTable, HaveColumn, HaveData, Ref, fit +from procrusql import HaveTable, HaveColumn, HaveData, Ref, fit, dump_dot logging.basicConfig(format="%(asctime)s %(levelname)s %(name)s %(lineno)d | %(message)s", level=logging.DEBUG) @@ -213,6 +213,6 @@ want = [ db = psycopg2.connect(dbname="procrusql_test") fit(db, want) - +dump_dot() # vim: tw=0 diff --git a/procrusql.py b/procrusql.py index 8fe32c6..866f781 100644 --- a/procrusql.py +++ b/procrusql.py @@ -38,6 +38,11 @@ class Node: self.ready = True return True + def set_order(self): + global order + order += 1 + self.order = order + class HaveData(Node): def __init__(self, name, depends, table, key, extra): super().__init__(name, depends) @@ -64,6 +69,7 @@ class HaveData(Node): self.result = csr.fetchall() log_check.info("Got %d rows", len(self.result)) if self.result: + self.set_order() self.ok = True log_state.info("%s is now ok", self.name) return @@ -83,6 +89,7 @@ class HaveData(Node): self.result = csr.fetchall() log_action.info("Got %d rows", len(self.result)) if self.result: + self.set_order() self.ok = True log_state.info("%s is now ok", self.name) return @@ -109,6 +116,7 @@ class HaveTable(Node): r = csr.fetchall() if len(r) == 1: # Table exists, all ok + self.set_order() self.ok = True log_state.info("%s is now ok", self.name) return @@ -123,6 +131,7 @@ class HaveTable(Node): table=sql.Identifier(self.table) ) csr.execute(q) + self.set_order() self.ok = True log_state.info("%s is now ok", self.name) @@ -154,6 +163,7 @@ class HaveColumn(Node): r = csr.fetchall() if len(r) == 1: # Column exists, all ok + self.set_order() self.ok = True log_state.info("%s is now ok", self.name) return @@ -169,6 +179,7 @@ class HaveColumn(Node): definition=sql.SQL(self.definition), ) csr.execute(q) + self.set_order() self.ok = True log_state.info("%s is now ok", self.name) @@ -197,9 +208,10 @@ class Ref: def fit(_db, _want): - global db, want + global db, want, order db = _db want = _want + order = 0 while True: progress = False not_ok = 0 @@ -218,3 +230,14 @@ def fit(_db, _want): db.commit() log_state.info("Done") + +def dump_dot(): + print("digraph {") + for w in want: + print(f"\t{w.name} [shape=rect]") + for d in w.depends: + print(f"\t{d} -> {w.name} [constraint=false]") + in_order = [w.name for w in sorted(want, key=lambda x: x.order)] + for i in range(1, len(in_order)): + print(f"\t{in_order[i-1]} -> {in_order[i]} [style=dashed]") + print("}")