11import logging
22
33from django .apps import apps
4- from django .db import connection
54
65from django_pgviews .signals import view_synced , all_views_synced
76from django_pgviews .view import create_view , View , MaterializedView , create_materialized_view
@@ -38,13 +37,16 @@ def run_backlog(self, backlog, **kwargs):
3837
3938
4039class ViewSyncer (RunBacklog ):
41- def run (self , force , update , materialized_views_check_sql_changed = False , ** options ):
40+ def run (self , force , update , using , materialized_views_check_sql_changed = False , ** options ):
4241 if super ().run (
43- force = force , update = update , materialized_views_check_sql_changed = materialized_views_check_sql_changed
42+ force = force ,
43+ update = update ,
44+ using = using ,
45+ materialized_views_check_sql_changed = materialized_views_check_sql_changed ,
4446 ):
45- all_views_synced .send (sender = None )
47+ all_views_synced .send (sender = None , using = using )
4648
47- def run_backlog (self , backlog , * , force , update , materialized_views_check_sql_changed , ** kwargs ):
49+ def run_backlog (self , backlog , * , force , update , using , materialized_views_check_sql_changed , ** kwargs ):
4850 """Installs the list of models given from the previous backlog
4951
5052 If the correct dependent views have not been installed, the view
@@ -67,6 +69,10 @@ def run_backlog(self, backlog, *, force, update, materialized_views_check_sql_ch
6769 continue # Skip
6870
6971 try :
72+ connection = view_cls .get_view_connection (using = using )
73+ if not connection :
74+ logger .info ("Skipping pgview %s (migrations not allowed on %s)" , name , using )
75+ continue # Skip
7076 if isinstance (view_cls (), MaterializedView ):
7177 status = create_materialized_view (
7278 connection , view_cls , check_sql_changed = materialized_views_check_sql_changed
@@ -86,6 +92,7 @@ def run_backlog(self, backlog, *, force, update, materialized_views_check_sql_ch
8692 force = force ,
8793 status = status ,
8894 has_changed = status not in ("EXISTS" , "FORCE_REQUIRED" ),
95+ using = using ,
8996 )
9097 self .finished .append (name )
9198 except Exception as exc :
@@ -114,10 +121,10 @@ def run_backlog(self, backlog, *, force, update, materialized_views_check_sql_ch
114121
115122
116123class ViewRefresher (RunBacklog ):
117- def run (self , concurrently , ** kwargs ):
118- return super ().run (concurrently = concurrently , ** kwargs )
124+ def run (self , concurrently , using , ** kwargs ):
125+ return super ().run (concurrently = concurrently , using = using , ** kwargs )
119126
120- def run_backlog (self , backlog , * , concurrently , ** kwargs ):
127+ def run_backlog (self , backlog , * , concurrently , using , ** kwargs ):
121128 new_backlog = []
122129 for view_cls in backlog :
123130 skip = False
@@ -132,6 +139,11 @@ def run_backlog(self, backlog, *, concurrently, **kwargs):
132139 logger .info ("Putting pgview at back of queue: %s" , name )
133140 continue # Skip
134141
142+ # Don't refresh views not associated with this database
143+ connection = view_cls .get_view_connection (using = using )
144+ if not connection :
145+ continue
146+
135147 if issubclass (view_cls , MaterializedView ):
136148 view_cls .refresh (concurrently = concurrently )
137149 logger .info ("pgview %s refreshed" , name )
0 commit comments