@@ -71,7 +71,7 @@ def _schema_and_name(connection, view_name):
71
71
try :
72
72
schema_name = connection .schema_name
73
73
except AttributeError :
74
- schema_name = "public"
74
+ schema_name = None
75
75
76
76
return schema_name , view_name
77
77
@@ -119,6 +119,26 @@ def _create_index_sql(self, *args, **kwargs):
119
119
return statement
120
120
121
121
122
+ def _make_where (** kwargs ):
123
+ where_fragments = []
124
+ params = []
125
+
126
+ for key , value in kwargs .items ():
127
+ if value is None :
128
+ # skip key if value is not specified
129
+ continue
130
+
131
+ if isinstance (value , (list , tuple )):
132
+ in_fragment = ", " .join ("%s" for _ in range (len (value )))
133
+ where_fragments .append (f"{ key } IN ({ in_fragment } )" )
134
+ params .extend (list (value ))
135
+ else :
136
+ where_fragments .append (f"{ key } = %s" )
137
+ params .append (value )
138
+ where_fragment = " AND " .join (where_fragments )
139
+ return where_fragment , params
140
+
141
+
122
142
def _ensure_indexes (connection , cursor , view_cls , schema_name_log ):
123
143
"""
124
144
This function gets called when a materialized view is deemed not needing a re-create. That is however only a part
@@ -131,7 +151,8 @@ def _ensure_indexes(connection, cursor, view_cls, schema_name_log):
131
151
indexes = view_cls ._meta .indexes
132
152
vschema , vname = _schema_and_name (connection , view_name )
133
153
134
- cursor .execute ("SELECT indexname FROM pg_indexes WHERE tablename = %s AND schemaname = %s" , [vname , vschema ])
154
+ where_fragment , params = _make_where (schemaname = vschema , tablename = vname )
155
+ cursor .execute (f"SELECT indexname FROM pg_indexes WHERE { where_fragment } " , params )
135
156
136
157
existing_indexes = {x [0 ] for x in cursor .fetchall ()}
137
158
required_indexes = {x .name for x in indexes }
@@ -143,7 +164,11 @@ def _ensure_indexes(connection, cursor, view_cls, schema_name_log):
143
164
concurrent_index_name = None
144
165
145
166
for index_name in existing_indexes - required_indexes :
146
- cursor .execute (f"DROP INDEX { vschema } .{ index_name } " )
167
+ if vschema :
168
+ full_index_name = f"{ vschema } .{ index_name } "
169
+ else :
170
+ full_index_name = index_name
171
+ cursor .execute (f"DROP INDEX { full_index_name } " )
147
172
logger .info ("pgview dropped index %s on view %s (%s)" , index_name , view_name , schema_name_log )
148
173
149
174
schema_editor : DatabaseSchemaEditor = CustomSchemaEditor (connection )
@@ -188,10 +213,12 @@ def create_materialized_view(connection, view_cls, check_sql_changed=False):
188
213
cursor_wrapper = connection .cursor ()
189
214
cursor = cursor_wrapper .cursor
190
215
216
+ where_fragment , params = _make_where (schemaname = vschema , matviewname = vname )
217
+
191
218
try :
192
219
cursor .execute (
193
- "SELECT COUNT(*) FROM pg_matviews WHERE schemaname = %s and matviewname = %s ;" ,
194
- [ vschema , vname ] ,
220
+ f "SELECT COUNT(*) FROM pg_matviews WHERE { where_fragment } ;" ,
221
+ params ,
195
222
)
196
223
view_exists = cursor .fetchone ()[0 ] > 0
197
224
@@ -206,9 +233,10 @@ def create_materialized_view(connection, view_cls, check_sql_changed=False):
206
233
_drop_mat_view (cursor , temp_viewname )
207
234
_create_mat_view (cursor , temp_viewname , query , view_query .params , with_data = False )
208
235
236
+ definitions_where , definitions_params = _make_where (schemaname = vschema , matviewname = [vname , temp_vname ])
209
237
cursor .execute (
210
- "SELECT definition FROM pg_matviews WHERE schemaname = %s and matviewname IN (%s, %s) ;" ,
211
- [ vschema , vname , temp_vname ] ,
238
+ f "SELECT definition FROM pg_matviews WHERE { definitions_where } ;" ,
239
+ definitions_params ,
212
240
)
213
241
definitions = cursor .fetchall ()
214
242
@@ -265,9 +293,10 @@ def create_view(connection, view_name, view_query: ViewSQL, update=True, force=F
265
293
try :
266
294
force_required = False
267
295
# Determine if view already exists.
296
+ view_exists_where , view_exists_params = _make_where (table_schema = vschema , table_name = vname )
268
297
cursor .execute (
269
- "SELECT COUNT(*) FROM information_schema.views WHERE table_schema = %s and table_name = %s ;" ,
270
- [ vschema , vname ] ,
298
+ f "SELECT COUNT(*) FROM information_schema.views WHERE { view_exists_where } ;" ,
299
+ view_exists_params ,
271
300
)
272
301
view_exists = cursor .fetchone ()[0 ] > 0
273
302
if view_exists and not update :
0 commit comments