Dump dependencies as dot file

This commit is contained in:
Peter J. Holzer 2022-02-03 16:36:16 +01:00 committed by Peter J. Holzer
parent f17f07537e
commit dcd711f0e8
2 changed files with 26 additions and 3 deletions

View File

@ -2,7 +2,7 @@
import logging import logging
import psycopg2 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) 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") db = psycopg2.connect(dbname="procrusql_test")
fit(db, want) fit(db, want)
dump_dot()
# vim: tw=0 # vim: tw=0

View File

@ -38,6 +38,11 @@ class Node:
self.ready = True self.ready = True
return True return True
def set_order(self):
global order
order += 1
self.order = order
class HaveData(Node): class HaveData(Node):
def __init__(self, name, depends, table, key, extra): def __init__(self, name, depends, table, key, extra):
super().__init__(name, depends) super().__init__(name, depends)
@ -64,6 +69,7 @@ class HaveData(Node):
self.result = csr.fetchall() self.result = csr.fetchall()
log_check.info("Got %d rows", len(self.result)) log_check.info("Got %d rows", len(self.result))
if self.result: if self.result:
self.set_order()
self.ok = True self.ok = True
log_state.info("%s is now ok", self.name) log_state.info("%s is now ok", self.name)
return return
@ -83,6 +89,7 @@ class HaveData(Node):
self.result = csr.fetchall() self.result = csr.fetchall()
log_action.info("Got %d rows", len(self.result)) log_action.info("Got %d rows", len(self.result))
if self.result: if self.result:
self.set_order()
self.ok = True self.ok = True
log_state.info("%s is now ok", self.name) log_state.info("%s is now ok", self.name)
return return
@ -109,6 +116,7 @@ class HaveTable(Node):
r = csr.fetchall() r = csr.fetchall()
if len(r) == 1: if len(r) == 1:
# Table exists, all ok # Table exists, all ok
self.set_order()
self.ok = True self.ok = True
log_state.info("%s is now ok", self.name) log_state.info("%s is now ok", self.name)
return return
@ -123,6 +131,7 @@ class HaveTable(Node):
table=sql.Identifier(self.table) table=sql.Identifier(self.table)
) )
csr.execute(q) csr.execute(q)
self.set_order()
self.ok = True self.ok = True
log_state.info("%s is now ok", self.name) log_state.info("%s is now ok", self.name)
@ -154,6 +163,7 @@ class HaveColumn(Node):
r = csr.fetchall() r = csr.fetchall()
if len(r) == 1: if len(r) == 1:
# Column exists, all ok # Column exists, all ok
self.set_order()
self.ok = True self.ok = True
log_state.info("%s is now ok", self.name) log_state.info("%s is now ok", self.name)
return return
@ -169,6 +179,7 @@ class HaveColumn(Node):
definition=sql.SQL(self.definition), definition=sql.SQL(self.definition),
) )
csr.execute(q) csr.execute(q)
self.set_order()
self.ok = True self.ok = True
log_state.info("%s is now ok", self.name) log_state.info("%s is now ok", self.name)
@ -197,9 +208,10 @@ class Ref:
def fit(_db, _want): def fit(_db, _want):
global db, want global db, want, order
db = _db db = _db
want = _want want = _want
order = 0
while True: while True:
progress = False progress = False
not_ok = 0 not_ok = 0
@ -218,3 +230,14 @@ def fit(_db, _want):
db.commit() db.commit()
log_state.info("Done") 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("}")