6
6
from django_pgviews .view import create_view , View , MaterializedView
7
7
from django_pgviews .signals import view_synced , all_views_synced
8
8
9
- log = logging .getLogger (' django_pgviews.sync_pgviews' )
9
+ log = logging .getLogger (" django_pgviews.sync_pgviews" )
10
10
11
11
12
12
class ViewSyncer (object ):
13
13
def run (self , force , update , ** options ):
14
14
self .synced = []
15
15
backlog = []
16
16
for view_cls in apps .get_models ():
17
- if not (isinstance (view_cls , type ) and
18
- issubclass (view_cls , View ) and
19
- hasattr (view_cls , 'sql' )):
17
+ if not (isinstance (view_cls , type ) and issubclass (view_cls , View ) and hasattr (view_cls , "sql" )):
20
18
continue
21
19
backlog .append (view_cls )
22
20
loop = 0
@@ -25,57 +23,62 @@ def run(self, force, update, **options):
25
23
backlog = self .run_backlog (backlog , force , update )
26
24
27
25
if loop >= 10 :
28
- log .warn (' pgviews dependencies hit limit. Check if your model dependencies are correct' )
26
+ log .warn (" pgviews dependencies hit limit. Check if your model dependencies are correct" )
29
27
else :
30
28
all_views_synced .send (sender = None )
31
29
32
30
def run_backlog (self , models , force , update ):
33
- ''' Installs the list of models given from the previous backlog
31
+ """ Installs the list of models given from the previous backlog
34
32
35
33
If the correct dependent views have not been installed, the view
36
34
will be added to the backlog.
37
35
38
36
Eventually we get to a point where all dependencies are sorted.
39
- '''
37
+ """
40
38
backlog = []
41
39
for view_cls in models :
42
40
skip = False
43
- name = ' {}.{}' .format (view_cls ._meta .app_label , view_cls .__name__ )
41
+ name = " {}.{}" .format (view_cls ._meta .app_label , view_cls .__name__ )
44
42
for dep in view_cls ._dependencies :
45
43
if dep not in self .synced :
46
44
skip = True
47
45
if skip is True :
48
46
backlog .append (view_cls )
49
- log .info (' Putting pgview at back of queue: %s' , name )
50
- continue # Skip
47
+ log .info (" Putting pgview at back of queue: %s" , name )
48
+ continue # Skip
51
49
52
50
try :
53
- status = create_view (connection , view_cls ._meta .db_table ,
54
- view_cls .sql , update = update , force = force ,
55
- materialized = isinstance (view_cls (), MaterializedView ),
56
- index = view_cls ._concurrent_index )
51
+ status = create_view (
52
+ connection ,
53
+ view_cls ._meta .db_table ,
54
+ view_cls .sql ,
55
+ update = update ,
56
+ force = force ,
57
+ materialized = isinstance (view_cls (), MaterializedView ),
58
+ index = view_cls ._concurrent_index ,
59
+ )
57
60
view_synced .send (
58
- sender = view_cls , update = update , force = force , status = status ,
59
- has_changed = status not in ('EXISTS' , 'FORCE_REQUIRED' ))
61
+ sender = view_cls ,
62
+ update = update ,
63
+ force = force ,
64
+ status = status ,
65
+ has_changed = status not in ("EXISTS" , "FORCE_REQUIRED" ),
66
+ )
60
67
self .synced .append (name )
61
68
except Exception as exc :
62
69
exc .view_cls = view_cls
63
70
exc .python_name = name
64
71
raise
65
72
else :
66
- if status == ' CREATED' :
73
+ if status == " CREATED" :
67
74
msg = "created"
68
- elif status == ' UPDATED' :
75
+ elif status == " UPDATED" :
69
76
msg = "updated"
70
- elif status == ' EXISTS' :
77
+ elif status == " EXISTS" :
71
78
msg = "already exists, skipping"
72
- elif status == ' FORCED' :
79
+ elif status == " FORCED" :
73
80
msg = "forced overwrite of existing schema"
74
- elif status == 'FORCE_REQUIRED' :
75
- msg = (
76
- "exists with incompatible schema, "
77
- "--force required to update" )
78
- log .info ("pgview %(python_name)s %(msg)s" % {
79
- 'python_name' : name ,
80
- 'msg' : msg })
81
+ elif status == "FORCE_REQUIRED" :
82
+ msg = "exists with incompatible schema, " "--force required to update"
83
+ log .info ("pgview %(python_name)s %(msg)s" % {"python_name" : name , "msg" : msg })
81
84
return backlog
0 commit comments