1- import os
21import logging
32
43from click import ClickException
76from sqlalchemy .orm import sessionmaker
87from sqlalchemy_utils import database_exists
98
10- from process_tracker .utilities .logging import console
119from process_tracker .utilities .settings import SettingsManager
1210
11+ from process_tracker .models .model_base import Base
1312from process_tracker .models .actor import Actor
1413from process_tracker .models .extract import ExtractStatus
1514from process_tracker .models .process import ErrorType , ProcessType , ProcessStatus
@@ -32,14 +31,16 @@ def __init__(self, config_location=None):
3231 :param config_location: Location where Process Tracker configuration file is.
3332 :type config_location: file path
3433 """
34+ config = SettingsManager ().config
3535 self .logger = logging .getLogger (__name__ )
36+ self .logger .setLevel (config ['DEFAULT' ]['log_level' ])
3637
37- self .config_location = config_location
38+ self .config_location = config_location
3839
3940 data_store = self .verify_and_connect_to_data_store ()
4041 self .engine = data_store ['engine' ]
41- self .session = data_store ['session' ]
4242 self .meta = data_store ['meta' ]
43+ self .session = data_store ['session' ]
4344 self .data_store_type = data_store ['data_store_type' ]
4445 self .data_store_host = data_store ['data_store_host' ]
4546 self .data_store_port = data_store ['data_store_port' ]
@@ -87,8 +88,11 @@ def initialize_data_store(self, overwrite=False):
8788 self .logger .info ('Attempting to initialize Process Tracker data store.' )
8889 if overwrite :
8990 self .logger .warn ('ALERT - DATA STORE TO BE OVERWRITTEN - ALL DATA WILL BE LOST' )
90- self .meta .reflect ()
91- self .meta .drop_all ()
91+
92+ for table in Base .metadata .table_names ():
93+ self .logger .debug ('Table will be deleted: %s' % table )
94+
95+ Base .metadata .drop_all (self .engine )
9296
9397 version = None
9498 else :
@@ -99,7 +103,7 @@ def initialize_data_store(self, overwrite=False):
99103 if version is None :
100104
101105 self .logger .info ('Data store initialization beginning. Creating data store.' )
102- self . meta .create_all ()
106+ Base . metadata .create_all (self . engine )
103107
104108 self .logger .info ('Setting up application defaults.' )
105109
@@ -365,6 +369,9 @@ def verify_and_connect_to_data_store(self):
365369 supported_data_stores = relational_stores + nonrelational_stores
366370
367371 if data_store_type in supported_data_stores :
372+ engine = ''
373+ meta = ''
374+ session = ''
368375
369376 if data_store_type in relational_stores :
370377 if data_store_type == 'postgresql' :
@@ -376,23 +383,24 @@ def verify_and_connect_to_data_store(self):
376383 , data_store_port ))
377384 if database_exists (engine .url ):
378385 self .logger .info ("Data store exists. Continuing to work." )
386+ else :
387+ self .logger .error ('Data store does not exist. Please create and try again.' )
388+ raise Exception ('Data store does not exist. Please create and try again.' )
379389
380390 session = sessionmaker (bind = engine )
381391
382392 session = session (expire_on_commit = False )
383393 session .execute ("SET search_path TO %s" % data_store_name )
384394
385- meta = MetaData (engine )
395+ meta = MetaData (schema = 'process_tracking' )
386396
387397 elif data_store_type in nonrelational_stores :
388- Session = ''
389398 session = ''
390- meta = ''
391399
392400 data_store = dict ()
393401 data_store ['engine' ] = engine
394- data_store ['session' ] = session
395402 data_store ['meta' ] = meta
403+ data_store ['session' ] = session
396404 data_store ['data_store_type' ] = data_store_type
397405 data_store ['data_store_host' ] = data_store_host
398406 data_store ['data_store_port' ] = data_store_port
0 commit comments