|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import time |
| 4 | +import glob |
| 5 | +from datetime import datetime |
| 6 | +import psycopg2 as sql |
| 7 | +from importlib.machinery import SourceFileLoader |
| 8 | + |
| 9 | +mapping_util = SourceFileLoader('mapping_util', os.path.dirname(os.path.realpath(__file__)) + '/mapping_util.py').load_module() |
| 10 | + |
| 11 | +# --------------------------------------------------------- |
| 12 | +# MAIN PROGRAM |
| 13 | +# --------------------------------------------------------- |
| 14 | +def main(): |
| 15 | + ret = True |
| 16 | + global db_conf |
| 17 | + |
| 18 | + try: |
| 19 | + (ret, dir_study, db_conf, debug) = mapping_util.get_parameters() |
| 20 | + if ret == True and dir_study != '': |
| 21 | + database_name = db_conf['database'] |
| 22 | + result_schema = db_conf['result_schema'] |
| 23 | + dir_sql = os.getcwd() + '\\sql_scripts\\' |
| 24 | + dir_sql_processed = os.getcwd() + '\\sql_scripts' + db_conf['dir_processed'] |
| 25 | + dir_data = db_conf['dir_cdm_data'] + '/' |
| 26 | + dir_data_processed = db_conf['dir_cdm_data'] + db_conf['dir_processed'] |
| 27 | + if not os.path.exists(dir_data_processed): |
| 28 | + os.makedirs(dir_data_processed) |
| 29 | +# --------------------------------------------------------- |
| 30 | +# Ask the user for DROP confirmation |
| 31 | +# --------------------------------------------------------- |
| 32 | + qa = input('Are you sure you want to DROP ' + database_name + ' Achilles and DQD tables (y/n):') |
| 33 | + while qa.lower() not in ['y', 'n', 'yes', 'no']: |
| 34 | + qa = input('I did not understand that. Are you sure you want to DROP ' + database_name + ' Achilles and DQD tables (y/n):') |
| 35 | + if qa.lower() in ['y', 'yes']: |
| 36 | + fname = dir_sql + '9a_cdm_post_drop.sql' |
| 37 | + print('Calling ' + fname + ' ...') |
| 38 | + ret = mapping_util.execute_sql_file_parallel(db_conf, fname, False) |
| 39 | + if ret == True: |
| 40 | +# --------------------------------------------------------- |
| 41 | +# Ask the user for LOAD confirmation |
| 42 | +# --------------------------------------------------------- |
| 43 | + qa = input('Are you sure you want to CREATE/LOAD ' + database_name + ' Achilles and DQD tables (y/n):') |
| 44 | + while qa.lower() not in ['y', 'n', 'yes', 'no']: |
| 45 | + qa = input('I did not understand that. Are you sure you want to CREATE/LOAD ' + database_name + ' Achilles and DQD tables (y/n):') |
| 46 | + if qa.lower() in ['y', 'yes']: |
| 47 | +# --------------------------------------------------------- |
| 48 | +# Create tables |
| 49 | +# --------------------------------------------------------- |
| 50 | + fname = dir_sql + '9b_cdm_post_create.sql' |
| 51 | + print('Calling ' + fname + ' ...') |
| 52 | + ret = mapping_util.execute_sql_file_parallel(db_conf, fname, False) |
| 53 | +# --------------------------------------------------------- |
| 54 | +# Load data - Parallel execution |
| 55 | +# --------------------------------------------------------- |
| 56 | + if ret == True: |
| 57 | + data_provider = db_conf['data_provider'] |
| 58 | + prefix = '' |
| 59 | + if data_provider == 'cprd': |
| 60 | + extension = '.txt' |
| 61 | + separator = ' ' |
| 62 | + elif data_provider == 'iqvia': |
| 63 | + extension = '.csv' # + sorted(glob.iglob(folder + '\\*.out')) |
| 64 | + separator = ' ' |
| 65 | + elif data_provider == 'thin': |
| 66 | + extension = '.csv' |
| 67 | + separator = ',' |
| 68 | + elif data_provider == 'ukbiobank': |
| 69 | + extension = '.tsv' |
| 70 | + separator = ' ' |
| 71 | + tbl_list = db_conf['tbl_cdm_post'] |
| 72 | + tbl_list_full = [result_schema + "." + tbl for tbl in tbl_list] |
| 73 | + print(tbl_list_full) |
| 74 | + file_list = [[dir_data + '*' + tbl + '*' + extension] for tbl in tbl_list] |
| 75 | + print(file_list) |
| 76 | + ret = mapping_util.load_files_parallel(db_conf, result_schema, tbl_list, file_list, dir_data_processed, separator) |
| 77 | + if ret == True: |
| 78 | + print('Finished loading cdm vocabulary.') |
| 79 | +# --------------------------------------------------------- |
| 80 | +# Ask the user for PK/IDX creation confirmation |
| 81 | +# --------------------------------------------------------- |
| 82 | + if ret == True: |
| 83 | + qa = input('Are you sure you want to CREATE PK/IDXs on the ' + database_name + ' Achilles and DQD tables (y/n):') |
| 84 | + while qa.lower() not in ['y', 'n', 'yes', 'no']: |
| 85 | + qa = input('Are you sure you want to CREATE PK/IDXs on all the ' + database_name + ' Achilles and DQD tables (y/n):') |
| 86 | + if qa.lower() in ['y', 'yes']: |
| 87 | +# --------------------------------------------------------- |
| 88 | +# Build PKs & IDXs |
| 89 | +# --------------------------------------------------------- |
| 90 | + print('Build PKs and IDXs ...') |
| 91 | + sql_file_list = sorted(glob.iglob(dir_sql + '9c_cdm_post_pk_idx_*.sql')) |
| 92 | + ret = mapping_util.execute_sql_files_parallel(db_conf, sql_file_list, True) |
| 93 | + if ret == True: |
| 94 | + print('Finished adding ' + database_name + ' Achilles/DQD PKs/indexes') |
| 95 | +# --------------------------------------------------------- |
| 96 | +# Ask the user for RECORD COUNTS confirmation |
| 97 | +# --------------------------------------------------------- |
| 98 | + if ret == True: |
| 99 | + qa = input('Are you sure you want to COUNT the records for all the ' + database_name + ' tables (y/n):') |
| 100 | + while qa.lower() not in ['y', 'n', 'yes', 'no']: |
| 101 | + qa = input('Are you sure you want to COUNT the records for all the ' + database_name + ' tables (y/n):') |
| 102 | + if qa.lower() in ['y', 'yes']: |
| 103 | + source_nok_schema = db_conf['source_nok_schema'] |
| 104 | + tbl_list_count = tbl_db_list + [source_nok_schema + "." + tbl for tbl in db_conf[tbl_db] if tbl not in ('practice', 'staff')] |
| 105 | + ret = mapping_util.get_table_count_parallel(db_conf, tbl_list_count, source_schema + '._records') |
| 106 | + if ret == True: |
| 107 | + print('Finished counting on ' + database_name + ' data\n') |
| 108 | +# --------------------------------------------------------- |
| 109 | +# Move CODE to the processed directory? |
| 110 | +# --------------------------------------------------------- |
| 111 | + if ret == True: |
| 112 | + qa = input('Are you sure you want to MOVE all the source CODE in the "processed" folder (y/n):') |
| 113 | + while qa.lower() not in ['y', 'n', 'yes', 'no']: |
| 114 | + qa = input('I did not understand that. Are you sure you want to MOVE all the source CODE in the "processed" folder (y/n):') |
| 115 | + if qa.lower() in ['y', 'yes']: |
| 116 | + for f in glob.iglob(dir_sql + '1*.sql'): |
| 117 | + file_processed = dir_sql_processed + os.path.basename(f) |
| 118 | + os.rename(f, file_processed) |
| 119 | + print('Finished MOVING code files') |
| 120 | + except: |
| 121 | + print(str(sys.exc_info()[1])) |
| 122 | + |
| 123 | +# --------------------------------------------------------- |
| 124 | +# Protect entry point |
| 125 | +# --------------------------------------------------------- |
| 126 | +if __name__ == "__main__": |
| 127 | + main() |
0 commit comments