33
44import click
55
6- from .hnhm import Plan , PlanType
6+ from .core import HnhmError
7+ from .plan_printer import print_plan
78from .hnhm_registry import HnhmRegistry
8- from .core import (
9- HnhmError ,
10- CreateLink ,
11- LayoutType ,
12- RemoveLink ,
13- CreateGroup ,
14- RemoveGroup ,
15- CreateEntity ,
16- RemoveEntity ,
17- CreateAttribute ,
18- RemoveAttribute ,
19- AddGroupAttribute ,
20- RemoveGroupAttribute ,
21- )
229
2310
2411def import_registry (module : str ) -> HnhmRegistry :
@@ -33,91 +20,6 @@ def import_registry(module: str) -> HnhmRegistry:
3320 return registry
3421
3522
36- def print_plan (plan : Plan ):
37- if plan .is_empty ():
38- click .secho ("Your DWH is up to date." , fg = "green" )
39- return
40-
41- entities_mutations = sorted (plan .entities_mutations .items (), key = lambda kv : kv [0 ])
42- links_mutations = sorted (plan .links_mutations .items (), key = lambda kv : kv [0 ])
43-
44- click .secho ("Plan:" )
45- for entity_name , plan_collection in entities_mutations :
46- if plan_collection .type == PlanType .CREATE :
47- symbol , color = "+" , "green"
48- elif plan_collection .type == PlanType .REMOVE :
49- symbol , color = "-" , "red"
50- elif plan_collection .type == PlanType .UPDATE :
51- symbol , color = "[u]" , "yellow"
52- else :
53- raise HnhmError ()
54-
55- click .secho (f"\n { symbol } entity '{ entity_name } '" , fg = color )
56- for entity_mutation in plan_collection .mutations :
57- match entity_mutation :
58- case CreateEntity (entity = entity ):
59- if entity .layout .type == LayoutType .HNHM :
60- click .secho (f" + hub '{ entity .name } '" , fg = "green" )
61- else :
62- click .secho (f" + stage '{ entity .name } '" , fg = "green" )
63- for attribute in entity .attributes .values ():
64- click .secho (f" |attribute '{ attribute .name } '" , fg = "green" )
65-
66- case CreateAttribute (entity = _, attribute = attribute ):
67- click .secho (f" + attribute '{ attribute .name } '" , fg = "green" )
68-
69- case CreateGroup (entity = _, group = group ):
70- click .secho (f" + group '{ group .name } '" , fg = "green" )
71- for attribute in group .attributes .values ():
72- click .secho (f" |attribute '{ attribute .name } '" , fg = "green" )
73-
74- case AddGroupAttribute (entity = _, group = group , attribute = attribute ):
75- click .secho (f" [u] group '{ group .name } '" , fg = "yellow" )
76- click .secho (f" +attribute '{ attribute .name } '" , fg = "green" )
77-
78- case RemoveEntity (entity = entity ):
79- if entity .layout .type == LayoutType .HNHM :
80- click .secho (f" - hub '{ entity .name } '" , fg = "red" )
81- else :
82- click .secho (f" - stage '{ entity .name } '" , fg = "red" )
83- for attribute in entity .attributes .values ():
84- click .secho (f" |attribute '{ attribute .name } '" , fg = "red" )
85-
86- case RemoveAttribute (entity = _, attribute = attribute ):
87- click .secho (f" - attribute '{ attribute .name } '" , fg = "red" )
88-
89- case RemoveGroup (entity = _, group = group ):
90- click .secho (f" - group '{ group .name } '" , fg = "red" )
91- for attribute in group .attributes .values ():
92- click .secho (f" | attribute '{ attribute .name } '" , fg = "red" )
93-
94- case RemoveGroupAttribute (entity = _, group = group , attribute = attribute ):
95- click .secho (f" [u] group '{ group .name } '" , fg = "yellow" )
96- click .secho (f" -attribute '{ attribute .name } '" , fg = "red" )
97-
98- for link_name , plan_collection in links_mutations :
99- if plan_collection .type == PlanType .CREATE :
100- symbol , color = "+" , "green"
101- elif plan_collection .type == PlanType .REMOVE :
102- symbol , color = "-" , "red"
103- elif plan_collection .type == PlanType .UPDATE :
104- symbol , color = "[u]" , "yellow"
105- else :
106- raise HnhmError ()
107-
108- click .secho (f"\n { symbol } link '{ link_name } '" , fg = color )
109- for link_mutation in plan_collection .mutations :
110- match link_mutation :
111- case RemoveLink (link = link ):
112- for element in link .elements :
113- click .secho (f" |element '{ element .entity .name } '" , fg = color )
114- case CreateLink (link = link ):
115- for element in link .elements :
116- click .secho (f" |element '{ element .entity .name } '" , fg = color )
117-
118- click .secho ()
119-
120-
12123@click .group ()
12224def cli ():
12325 pass
@@ -153,7 +55,7 @@ def apply(dwh_module: str, yes: bool):
15355 if plan .is_empty ():
15456 return
15557
156- if yes or click .confirm ("Apply mutations ?" ):
58+ if yes or click .confirm ("Apply migrations ?" ):
15759 with registry .hnhm as hnhm :
15860 hnhm .apply (plan )
15961
0 commit comments