@@ -143,17 +143,20 @@ def add_dependency(self, dependency_type, dependency):
143143
144144 self .logger .info ("Extract %s dependency added." % dependency_type )
145145
146- def change_extract_status (self , new_status ):
146+ def change_extract_status (self , new_status , extracts = None ):
147147 """
148148 Change an extract record status.
149+ :param new_status: The name of the status the extract is to be updated to.
150+ :type new_status: str
151+ :param extracts: List of Extract SQLAlchemy objects. Used for dependency check.
149152 :return:
150153 """
151154 status_date = datetime .now ()
152155 if new_status in self .extract_status_types :
153156
154157 if new_status == "loading" :
155158
156- self .extract_dependency_check ()
159+ self .extract_dependency_check (extracts = extracts )
157160
158161 self .logger .info ("Setting extract status to %s" % new_status )
159162
@@ -173,46 +176,86 @@ def change_extract_status(self, new_status):
173176 "Please add the status to extract_status_lkup" % new_status
174177 )
175178
176- def extract_dependency_check (self ):
179+ def extract_dependency_check (self , extracts = None ):
177180 """
178181 Determine if the extract file has any unloaded dependencies before trying to load the file.
182+ :param extracts: List of ExtractTracking SQLAlchemy objects, provided if bulk updating status.
179183 :return:
180184 """
181- child_extract = aliased (Extract )
182- parent_extract = aliased (Extract )
183-
184- dependency_hold = (
185- self .session .query (ExtractDependency )
186- .join (
187- parent_extract ,
188- ExtractDependency .parent_extract_id == parent_extract .extract_id ,
189- )
190- .join (
191- child_extract ,
192- ExtractDependency .child_extract_id == child_extract .extract_id ,
185+ child = aliased (Extract )
186+ parent = aliased (Extract )
187+ dependency_hold = 0
188+
189+ if extracts is not None :
190+
191+ parent_files_hold = (
192+ self .session .query (parent )
193+ .join (parent , ExtractDependency .parent_extract )
194+ .join (child , ExtractDependency .child_extract )
195+ .join (Extract , Extract .extract_id == parent .extract_id )
196+ .join (
197+ ExtractStatus ,
198+ ExtractStatus .extract_status_id == Extract .extract_status_id ,
199+ )
200+ .filter (child .extract_id == self .extract .extract_id )
201+ .filter (
202+ ExtractStatus .extract_status_name .in_ (
203+ ("loading" , "initializing" , "ready" )
204+ )
205+ )
193206 )
194- .join (Extract , Extract .extract_id == parent_extract .extract_id )
195- .join (
196- ExtractStatus ,
197- ExtractStatus .extract_status_id == Extract .extract_status_id ,
207+ extract_names = list ()
208+ for extract in extracts :
209+ self .logger .debug (
210+ "Extracts being compared to %s" % extract .extract .full_filepath ()
211+ )
212+ extract_names .append (extract .extract .full_filepath ())
213+
214+ for extract in parent_files_hold :
215+
216+ self .logger .debug ("Testing if %s is in extracts." % extract )
217+
218+ if extract .full_filepath () not in extract_names :
219+ self .logger .debug ("Extract not found." )
220+ dependency_hold += 1
221+
222+ self .logger .debug (
223+ "We found %s dependencies that will block using this extract."
224+ % dependency_hold
198225 )
199- .filter (child_extract .extract_id == self .extract .extract_id )
200- .filter (
201- ExtractStatus .extract_status_name .in_ (
202- ("loading" , "initializing" , "ready" )
226+ else :
227+ dependency_hold = (
228+ self .session .query (ExtractDependency )
229+ .join (parent , ExtractDependency .parent_extract )
230+ .join (child , ExtractDependency .child_extract )
231+ .join (Extract , Extract .extract_id == parent .extract_id )
232+ .join (
233+ ExtractStatus ,
234+ ExtractStatus .extract_status_id == Extract .extract_status_id ,
235+ )
236+ .filter (child .extract_id == self .extract .extract_id )
237+ .filter (
238+ ExtractStatus .extract_status_name .in_ (
239+ ("loading" , "initializing" , "ready" )
240+ )
203241 )
242+ ).count ()
243+
244+ self .logger .debug (
245+ "We found %s dependencies that will block using this extract."
246+ % dependency_hold
204247 )
205- . count ()
206- )
248+
249+ self . logger . debug ( "Dependency hold is %s" % dependency_hold )
207250
208251 if dependency_hold > 0 :
209252 self .logger .error (
210- "Extract files that this extract file is dependent on have not been loaded, are being "
211- "created, or are in the process of loading."
253+ "Extract files that extract %s is dependent on have not been loaded, are being "
254+ "created, or are in the process of loading." % self . full_filename
212255 )
213256 raise Exception (
214- "Extract files that this extract file is dependent on have not been loaded, are being "
215- "created, or are in the process of loading."
257+ "Extract files that extract %s is dependent on have not been loaded, are being "
258+ "created, or are in the process of loading." % self . full_filename
216259 )
217260
218261 else :
0 commit comments