22# Used in the creation and editing of extract records. Used in conjunction with process tracking.
33from datetime import datetime
44import logging
5- import os
65from os .path import join
76
7+ from sqlalchemy .orm import aliased
88
99from process_tracker .data_store import DataStore
1010from process_tracker .location_tracker import LocationTracker
1111from process_tracker .utilities .settings import SettingsManager
12- from process_tracker .models .extract import Extract , ExtractProcess , ExtractStatus , Location
12+ from process_tracker .models .extract import Extract , ExtractDependency , ExtractProcess , ExtractStatus
1313
1414
1515class ExtractTracker :
@@ -97,6 +97,11 @@ def change_extract_status(self, new_status):
9797 """
9898 status_date = datetime .now ()
9999 if new_status in self .extract_status_types :
100+
101+ if new_status == self .extract_status_loading :
102+
103+ self .extract_dependency_check ()
104+
100105 self .logger .info ('Setting extract status to %s' % new_status )
101106
102107 new_status = self .extract_status_types [new_status ]
@@ -113,6 +118,29 @@ def change_extract_status(self, new_status):
113118 raise Exception ('%s is not a valid extract status type. '
114119 'Please add the status to extract_status_lkup' % new_status )
115120
121+ def extract_dependency_check (self ):
122+ """
123+ Determine if the extract file has any unloaded dependencies before trying to load the file.
124+ :return:
125+ """
126+ child_extract = aliased (Extract )
127+ parent_extract = aliased (Extract )
128+
129+ dependency_hold = self .session .query (ExtractDependency ) \
130+ .join (parent_extract , ExtractDependency .parent_extract_id == parent_extract .extract_id ) \
131+ .join (child_extract , ExtractDependency .child_extract_id == child_extract .extract_id ) \
132+ .join (Extract , Extract .extract_id == parent_extract .extract_id ) \
133+ .join (ExtractStatus , ExtractStatus .extract_status_id == Extract .extract_status_id ) \
134+ .filter (child_extract .extract_id == self .extract .extract_id ) \
135+ .filter (ExtractStatus .extract_status_name .in_ (('loading' , 'initializing' , 'ready' ))) \
136+ .count ()
137+
138+ if dependency_hold > 0 :
139+ self .logger .error ('Extract files that this extract file is dependent on have not been loaded, are being '
140+ 'created, or are in the process of loading.' )
141+ raise Exception ('Extract files that this extract file is dependent on have not been loaded, are being '
142+ 'created, or are in the process of loading.' )
143+
116144 def get_extract_status_types (self ):
117145 """
118146 Get list of process status types and return dictionary.
0 commit comments