Dump dependencies as dot file
This commit is contained in:
parent
f17f07537e
commit
dcd711f0e8
|
@ -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
|
||||
|
|
25
procrusql.py
25
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("}")
|
||||
|
|
Loading…
Reference in New Issue