From 6762152b1e0e7698699a0c55ce2a51a49408fc97 Mon Sep 17 00:00:00 2001 From: abpwrs Date: Sun, 29 Mar 2020 14:35:39 -0500 Subject: [PATCH] SAVE: working w/ zain irl --- convert/fmp.py | 103 -- convert/generate_json_from_fmp.py | 38 - {convert => db}/.gitignore | 0 convert/init/.gitkeep => db/README.md | 0 database.env => db/database.env | 0 db/db.py | 19 + .../.gitkeep => db/docker/base.dockerfile | 0 db/enter_psql.bash | 1 + db/fk.py | 9 + db/fk.sql | 17 + db/fmp.py | 98 ++ {convert => db}/fmp_to_postgres.py | 0 db/init-db.bash | 17 + .../init/20200305_2357_init_db.sql | 4 +- db/init/20200309_1938_init_db.sql | 280 ++++ db/json_from_fmp.py | 30 + {convert/seed => db/models}/.gitkeep | 0 .../models/20200305_2357_model.json | 0 .../models/20200309_1936_model.json | 0 db/models/20200309_1938_model.json | 1306 +++++++++++++++++ db/postgres_init_from_json.py | 79 + .../postgres_seed_from_json.py | 2 +- db/postgres_to_fmp.py | 0 db/psql.py | 7 + db/seed-db.bash | 17 + db/seed/.gitignore | 2 + docker-compose.yml | 2 +- 27 files changed, 1886 insertions(+), 145 deletions(-) delete mode 100644 convert/fmp.py delete mode 100644 convert/generate_json_from_fmp.py rename {convert => db}/.gitignore (100%) rename convert/init/.gitkeep => db/README.md (100%) rename database.env => db/database.env (100%) create mode 100644 db/db.py rename convert/models/.gitkeep => db/docker/base.dockerfile (100%) create mode 100644 db/enter_psql.bash create mode 100644 db/fk.py create mode 100644 db/fk.sql create mode 100644 db/fmp.py rename {convert => db}/fmp_to_postgres.py (100%) create mode 100755 db/init-db.bash rename {convert => db}/init/20200305_2357_init_db.sql (99%) create mode 100644 db/init/20200309_1938_init_db.sql create mode 100644 db/json_from_fmp.py rename {convert/seed => db/models}/.gitkeep (100%) rename {convert => db}/models/20200305_2357_model.json (100%) rename convert/postgres_to_fmp.py => db/models/20200309_1936_model.json (100%) create mode 100644 db/models/20200309_1938_model.json create mode 100644 db/postgres_init_from_json.py rename convert/generate_postgres_seed_from_json.py => db/postgres_seed_from_json.py (95%) create mode 100644 db/postgres_to_fmp.py create mode 100644 db/psql.py create mode 100755 db/seed-db.bash create mode 100644 db/seed/.gitignore diff --git a/convert/fmp.py b/convert/fmp.py deleted file mode 100644 index 20d7cd2..0000000 --- a/convert/fmp.py +++ /dev/null @@ -1,103 +0,0 @@ -import pyodbc - - -""" -File Maker Pro Connection Object -""" - - -class FMP(object): - __connection_string = "DSN=chifmp;UID=hjohnson;PWD=hjohnson" - - def __init__(self): - self.fmp_table_names = [ - "clinics", - "patients", - "visits", - "diagnoses", - "diagnoses_made", - "meds", - "meds_dispensed", - # "settings", - # "globals", - ] - - self.python_types_to_sql = { - "str": "VARCHAR", - "date": "TIMESTAMP", - "float64": "FLOAT", - "NoneType": "VARCHAR", - } - - self.__connection = pyodbc.connect(FMP.__connection_string) - - def fmp_table_names(self): - return self.fmp_table_names - - @property - def connection(self): - return self.__connection - - -class CHIFMPModel(object): - def __init__(self): - self.chifmp_table_names = [ - "patients", - "visits", - "clinics", - "diagnoses", - "diagnoses_made", - "meds", - "meds_dispensed", - # "globals", - # "settings", - ] - - def get_chifmp_table_names(self): - return self.chifmp_table_names - - def _get_connection_string(self): - # import netrc - - ## Add secret information in the `~/.netrc file - # machine chihaiti.fmp - # account chihaiti #<- the name of the DSN to connetct to - # login fmp_database_name - # password fmp_database_password - # netrcparser = netrc.netrc() - # mylogin, myaccount, mypassword = netrcparser.authenticators("chihaiti.fmp") - connection_string = "DSN=chifmp;UID=hjohnson;PWD=hjohnson" - print("Starting Connection with {0}".format(connection_string)) - # del mylogin, myaccount, mypassword - return connection_string - - def get_connnection(self): - import pyodbc - - return pyodbc.connect(self._get_connection_string()) - - -class QueryElements(object): - """ - Class to hold components used to construct a query - - updates should be done like the following: - - template = "UPDATE patients SET first_name = ? WHERE pk_patient_id = ?" - values = ('My"Name"', 12342) - cursor.execute(template, values); - - """ - - def __init__(self, query_template, query_values): - self.query_template = query_template - self.query_values = query_values - - def get_query_template(self): - return self.query_template - - def get_query_values(self): - return self.query_values - - def __str__(self): - return str(self.query_template) + " " + str(self.query_values) diff --git a/convert/generate_json_from_fmp.py b/convert/generate_json_from_fmp.py deleted file mode 100644 index f5ba179..0000000 --- a/convert/generate_json_from_fmp.py +++ /dev/null @@ -1,38 +0,0 @@ -import json -from datetime import datetime - -import pandas as pd -import pyodbc - -from fmp import FMP - -db_json = dict() -fmp_model = FMP() - -for tbl in fmp_model.fmp_table_names: - query = "select * from {tbl}".format(tbl=tbl) - df = pd.read_sql(query, fmp_model.connection) - - print(tbl) - col_types = [ - df.iloc[1, index].__class__.__name__ for index in range(len(df.columns)) - ] - for elem in zip(df.columns, col_types): - print(elem) - print() - - db_json[tbl] = {} - col_types = [ - df.iloc[1, index].__class__.__name__ for index in range(len(df.columns)) - ] - for elem in zip(df.columns, col_types): - db_json[tbl][elem[0]] = {} - db_json[tbl][elem[0]]["type"] = elem[1] - db_json[tbl][elem[0]]["pk"] = "pk" == elem[0][:2] - db_json[tbl][elem[0]]["fk"] = "fk" == elem[0][:2] - - -curr_time = datetime.now().strftime("%Y%m%d_%H%M") - -with open("./models/{time}_model.json".format(time=curr_time), "w") as f: - json.dump(db_json, f, indent=4) diff --git a/convert/.gitignore b/db/.gitignore similarity index 100% rename from convert/.gitignore rename to db/.gitignore diff --git a/convert/init/.gitkeep b/db/README.md similarity index 100% rename from convert/init/.gitkeep rename to db/README.md diff --git a/database.env b/db/database.env similarity index 100% rename from database.env rename to db/database.env diff --git a/db/db.py b/db/db.py new file mode 100644 index 0000000..cb0394c --- /dev/null +++ b/db/db.py @@ -0,0 +1,19 @@ +from abc import ABC, abstractmethod + + +class DB(ABC): + def __init__(self): + self._model = None + self.datetime_format = "%Y%m%d_%H%M" + + @abstractmethod + def write_model(self): + pass + + @abstractmethod + def parse_database(self) -> dict(): + pass + + @abstractmethod + def init_from_json(self, json_model: dict()): + pass diff --git a/convert/models/.gitkeep b/db/docker/base.dockerfile similarity index 100% rename from convert/models/.gitkeep rename to db/docker/base.dockerfile diff --git a/db/enter_psql.bash b/db/enter_psql.bash new file mode 100644 index 0000000..0366ad9 --- /dev/null +++ b/db/enter_psql.bash @@ -0,0 +1 @@ +psql --host=database --username=user --dbname=database diff --git a/db/fk.py b/db/fk.py new file mode 100644 index 0000000..21a6b2d --- /dev/null +++ b/db/fk.py @@ -0,0 +1,9 @@ +query = "SELECT tc.table_schema, tc.constraint_name, tc.table_name, kcu.column_name, ccu.table_schema AS foreign_table_schema, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='patients';" + +from fmp import FMP +import pandas as pd + +fmp_model = FMP() + +df = pd.read_sql(query, fmp_model.connection) +print(df) diff --git a/db/fk.sql b/db/fk.sql new file mode 100644 index 0000000..d8ce91a --- /dev/null +++ b/db/fk.sql @@ -0,0 +1,17 @@ +SELECT + tc.table_schema, + tc.constraint_name, + tc.table_name, + kcu.column_name, + ccu.table_schema AS foreign_table_schema, + ccu.table_name AS foreign_table_name, + ccu.column_name AS foreign_column_name +FROM + information_schema.table_constraints AS tc + JOIN information_schema.key_column_usage AS kcu + ON tc.constraint_name = kcu.constraint_name + AND tc.table_schema = kcu.table_schema + JOIN information_schema.constraint_column_usage AS ccu + ON ccu.constraint_name = tc.constraint_name + AND ccu.table_schema = tc.table_schema +WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='patients'; \ No newline at end of file diff --git a/db/fmp.py b/db/fmp.py new file mode 100644 index 0000000..99ee197 --- /dev/null +++ b/db/fmp.py @@ -0,0 +1,98 @@ +import json +from datetime import datetime + +import pandas as pd +import pyodbc +from tqdm import tqdm + +from db import DB + + +""" +File Maker Pro Connection Object + +Desc: +This file contains the primary logic for interacting with the file maker pro database +""" + + +class FMP(DB): + def __init__(self, dsn=None, username=None, password=None): + DB.__init__(self) + if dsn is None: + dsn = input("Enter the FMP DSN: ") + if username is None: + username = input("Enter your FMP username: ") + if password is None: + password = input("Enter your FMP password: ") + + self.__dsn = dsn + self.__username = username + self.__password = password + + self.__connection_string = "DSN={0};UID={1};PWD={2}".format( + self.__dsn, self.__username, self.__password + ) + + self.__fmp_table_names = [ + "clinics", + "patients", + "visits", + "diagnoses", + "diagnoses_made", + "meds", + "meds_dispensed", + # "settings", + # "globals", + ] + + self.__python_types_to_sql = { + "str": "VARCHAR", + "date": "TIMESTAMP", + "float64": "FLOAT", + "NoneType": "VARCHAR", + } + + self.__pyodbc_connection = pyodbc.connect(self.__connection_string) + + @property + def python_types_to_sql(self): + return self.__python_types_to_sql + + @property + def fmp_table_names(self): + return self.__fmp_table_names + + @property + def connection(self): + return self.__pyodbc_connection + + # ABSTRACT BASE CLASS MEHTODS + def init_from_json(model_object): + self._model = model_object + + def parse_database(self) -> dict(): + self._model = dict() + for tbl in tqdm(self.fmp_table_names): + query = "select * from {tbl}".format(tbl=tbl) + df = pd.read_sql(query, self.connection) + + self._model[tbl] = {} + col_types = [ + df.iloc[1, index].__class__.__name__ for index in range(len(df.columns)) + ] + + for elem in zip(df.columns, col_types): + self._model[tbl][elem[0]] = {} + self._model[tbl][elem[0]]["type"] = elem[1] + self._model[tbl][elem[0]]["pk"] = "pk" == elem[0][:2] + self._model[tbl][elem[0]]["fk"] = "fk" == elem[0][:2] + return self._model + + def write_model(self, model_object=None): + curr_time = datetime.now().strftime(self.datetime_format) + with open("./models/{time}_model.json".format(time=curr_time), "w") as f: + if model_object is not None: + json.dump(model_object, f, indent=4) + else: + json.dump(self._model, f, indent=4) diff --git a/convert/fmp_to_postgres.py b/db/fmp_to_postgres.py similarity index 100% rename from convert/fmp_to_postgres.py rename to db/fmp_to_postgres.py diff --git a/db/init-db.bash b/db/init-db.bash new file mode 100755 index 0000000..225d2f5 --- /dev/null +++ b/db/init-db.bash @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Author: "hertz" +# Date: 20200310 + +# args: +# 1 -- +# 2 -- + +#if [[ $# -ne 1 ]]; then +# echo "Usage: $0 ..." +# exit +#fi + + +# script: + + diff --git a/convert/init/20200305_2357_init_db.sql b/db/init/20200305_2357_init_db.sql similarity index 99% rename from convert/init/20200305_2357_init_db.sql rename to db/init/20200305_2357_init_db.sql index d89c1b1..61314ad 100644 --- a/convert/init/20200305_2357_init_db.sql +++ b/db/init/20200305_2357_init_db.sql @@ -238,7 +238,7 @@ create table chi_20200305_2357.visits ( create table chi_20200305_2357.diagnoses ( pk_diagnosis_id FLOAT primary key, diagnosis_name VARCHAR , - pk_diagnosis_key VARCHAR primary key, + pk_diagnosis_key VARCHAR , creation_date TIMESTAMP , creation_acct VARCHAR , modified_date TIMESTAMP , @@ -262,7 +262,7 @@ create table chi_20200305_2357.meds ( creation_acct VARCHAR , modified_date TIMESTAMP , modified_acct VARCHAR , - pk_med_key VARCHAR primary key, + pk_med_key VARCHAR , med_type VARCHAR ); diff --git a/db/init/20200309_1938_init_db.sql b/db/init/20200309_1938_init_db.sql new file mode 100644 index 0000000..0e371ae --- /dev/null +++ b/db/init/20200309_1938_init_db.sql @@ -0,0 +1,280 @@ +create schema chi_20200309_1938; +create table chi_20200309_1938.clinics ( + pk_clinic_id FLOAT primary key, + clinic_name VARCHAR , + creation_date TIMESTAMP , + creation_acct VARCHAR , + modified_date TIMESTAMP , + modified_acct VARCHAR +); + +create table chi_20200309_1938.patients ( + pk_patient_id FLOAT primary key, + creation_date TIMESTAMP , + creation_acct VARCHAR , + modified_date TIMESTAMP , + modified_acct VARCHAR , + last_name VARCHAR , + first_name VARCHAR , + gender VARCHAR , + face_photo VARCHAR , + birthdate VARCHAR , + phone VARCHAR , + town VARCHAR , + unique_name VARCHAR , + unique_ID_name VARCHAR , + nickname VARCHAR , + needs_review VARCHAR , + notes_modification_status VARCHAR , + fk_geonameid VARCHAR +); + +create table chi_20200309_1938.visits ( + pk_visit_id FLOAT primary key, + creation_date TIMESTAMP , + creation_acct VARCHAR , + modified_date TIMESTAMP , + modified_acct VARCHAR , + fk_patient_id FLOAT , + fk_clinic_id FLOAT , + visit_date TIMESTAMP , + visit_age FLOAT , + ob_pregnant VARCHAR , + ob_last_menstrual_period VARCHAR , + bp_systolic_blood_pressure FLOAT , + bp_diastolic_blood_pressure FLOAT , + bp_mean_arterial_pressure FLOAT , + temp_in_fahrenheit FLOAT , + pulse_rate FLOAT , + weight_in_lbs FLOAT , + height_in_inches FLOAT , + glasses_given VARCHAR , + sunglasses_given VARCHAR , + dx_surgical_candidate VARCHAR , + surgical_comments VARCHAR , + visit_age_unit VARCHAR , + chief_complaint VARCHAR , + history_of_present_illness VARCHAR , + visit_age_years FLOAT , + exam_comments VARCHAR , + tests_comments VARCHAR , + tests_blood_sugar_result FLOAT , + tests_pregnancy_result VARCHAR , + tests_urine_glucose_result VARCHAR , + tests_urine_protein_result VARCHAR , + tests_urine_leukocytes_result VARCHAR , + tests_urine_nitrites_result VARCHAR , + ob_gravida FLOAT , + ob_para FLOAT , + ob_abortus FLOAT , + ob_history VARCHAR , + ob_wants_planning VARCHAR , + albendazole_given VARCHAR , + first_visit VARCHAR , + known_med_allergies VARCHAR , + meds_currently_taken VARCHAR , + tests_pregnancy_given VARCHAR , + tests_blood_sugar_given VARCHAR , + tests_eye_exam_given VARCHAR , + tests_urine_analysis_given VARCHAR , + dx_asthma VARCHAR , + dx_allergies VARCHAR , + dx_anemia VARCHAR , + dx_malnutrition VARCHAR , + dx_diabetes VARCHAR , + dx_pterygium VARCHAR , + dx_cataract VARCHAR , + dx_dry_eye VARCHAR , + dx_conjunctivitis VARCHAR , + dx_fever VARCHAR , + dx_possible_malaria VARCHAR , + dx_filariasis VARCHAR , + dx_gastritis VARCHAR , + dx_gerd VARCHAR , + dx_gastroenteritis VARCHAR , + dx_dysentery VARCHAR , + dx_possible_cholera VARCHAR , + dx_headache VARCHAR , + dx_hernia VARCHAR , + dx_hernia_type VARCHAR , + dx_hypertension VARCHAR , + dx_stroke VARCHAR , + dx_angina VARCHAR , + dx_chf VARCHAR , + dx_musculoskeletal_pain VARCHAR , + dx_otitis VARCHAR , + dx_otitis_ear VARCHAR , + dx_pneumonia VARCHAR , + dx_sinusitis VARCHAR , + dx_uri VARCHAR , + dx_pregnant VARCHAR , + dx_pregnant_weeks FLOAT , + dx_atopic_derm VARCHAR , + dx_fungal VARCHAR , + dx_scabies VARCHAR , + dx_impetigo VARCHAR , + dx_tinea_capitus VARCHAR , + dx_possible_std VARCHAR , + dx_possible_tb VARCHAR , + dx_vaginitis_candidal VARCHAR , + dx_vaginitis_trich VARCHAR , + dx_vaginitis_bv VARCHAR , + dx_other VARCHAR , + dx_other_text VARCHAR , + tx_multivitamin VARCHAR , + tx_pain_acetaminophen VARCHAR , + tx_gi_omeprazole VARCHAR , + tx_gi_pepto_bismol VARCHAR , + tx_gi_cimetidine VARCHAR , + tx_gi_tums VARCHAR , + tx_gi_ors VARCHAR , + tx_infections_albendazole VARCHAR , + tx_infections_amoxicillin VARCHAR , + tx_infections_azithromycin VARCHAR , + tx_infections_bactrim VARCHAR , + tx_infections_cipro VARCHAR , + tx_infections_doxycycline VARCHAR , + tx_infections_flagyl VARCHAR , + tx_infections_fluconazole VARCHAR , + tx_infections_griseofulvin VARCHAR , + tx_infections_ivermectin VARCHAR , + tx_infections_keflex VARCHAR , + tx_infections_rocephin VARCHAR , + tx_cardiac_amlodipine VARCHAR , + tx_cardiac_aspirin VARCHAR , + tx_cardiac_atenolol VARCHAR , + tx_cardiac_hctz VARCHAR , + tx_cardiac_lasix VARCHAR , + tx_cardiac_enalapril VARCHAR , + tx_topical_clotrimazole VARCHAR , + tx_topical_hydrocortisone VARCHAR , + tx_topical_silvadine VARCHAR , + tx_topical_permethirine VARCHAR , + tx_im_dexamethasone VARCHAR , + tx_im_lasix VARCHAR , + tx_misc_albuterol VARCHAR , + tx_misc_benadryl VARCHAR , + tx_misc_ferrous_sulfate VARCHAR , + tx_misc_metformin VARCHAR , + tx_misc_prednisolone VARCHAR , + tx_misc_other VARCHAR , + tx_misc_other_text VARCHAR , + tx_comments VARCHAR , + dx_uti VARCHAR , + tx_multivitamin_varieties VARCHAR , + tx_pain_acetaminophen_varieties VARCHAR , + tx_infections_amoxicillin_varieties VARCHAR , + tx_pain_ibuprofen VARCHAR , + dx_comments VARCHAR , + tx_infections_azithromycin_varieties VARCHAR , + tx_infections_bactrim_varieties VARCHAR , + tx_infections_keflex_varieties VARCHAR , + tx_infections_rocephin_varieties VARCHAR , + dx_vaginitis_general VARCHAR , + dx_needs_to_see_dentist VARCHAR , + dx_needs_to_see_eye_doctor VARCHAR , + dx_healthy VARCHAR , + provider VARCHAR , + tx_saw_dentist VARCHAR , + tx_saw_eye_doctor VARCHAR , + tx_outside_referral VARCHAR , + tx_outside_referral_text VARCHAR , + tx_gi_zinc VARCHAR , + tx_cardiac_methyldopa VARCHAR , + tx_infections_augmentin VARCHAR , + tx_infections_chloroquine VARCHAR , + tx_infections_griseofuvin_varieties VARCHAR , + tx_infections_nitrofurantoin VARCHAR , + tx_topical_double_antibiotic VARCHAR , + tx_misc_fluoxetine VARCHAR , + tx_misc_ocp VARCHAR , + tx_misc_phenytoin VARCHAR , + dx_possible_parasites VARCHAR , + tx_misc_iud VARCHAR , + tests_id_test_given VARCHAR , + tests_id_chlamydia_result VARCHAR , + tests_id_hiv_result VARCHAR , + tests_id_syphilis_result VARCHAR , + tests_id_gonorrhea_result VARCHAR , + tests_eye_exam_right_sphere VARCHAR , + tests_eye_exam_right_cylinder VARCHAR , + tests_eye_exam_right_axis VARCHAR , + tests_eye_exam_right_bifocal VARCHAR , + tests_eye_exam_left_sphere VARCHAR , + tests_eye_exam_left_cylinder VARCHAR , + tests_eye_exam_left_axis VARCHAR , + tests_eye_exam_left_bifocal VARCHAR , + tx_pain_ibuprofen_varieties VARCHAR , + tx_misc_benadryl_varieties VARCHAR , + tests_id_tb_result VARCHAR , + tests_id_confirm_hiv_cd4 VARCHAR , + tests_id_confirm_hiv_result VARCHAR , + tests_id_confirm_hiv_arvt VARCHAR , + tests_id_confirm_hiv_enrolled VARCHAR , + tests_id_confirm_hiv_problems VARCHAR , + tests_id_confirm_tb_result VARCHAR , + tests_id_confirm_tb_comments VARCHAR , + tests_id_confirm_hiv_enrolled_at VARCHAR , + dx_depression VARCHAR , + clinic_trip VARCHAR , + tx_chw_referral VARCHAR , + tx_misc_depo VARCHAR , + tx_misc_paroxetine VARCHAR , + tx_misc_keppra VARCHAR , + dx_totalcount FLOAT , + tx_totalcount FLOAT , + tx_given_glasses VARCHAR , + tx_misc_labetolol VARCHAR , + tx_misc_carbamazepine VARCHAR , + tx_misc_terazosin VARCHAR , + tx_gi_ranitidine VARCHAR , + tx_cardiac_lisinopril VARCHAR , + tx_topical_triple_antibiotic VARCHAR , + tx_misc_prednisone VARCHAR , + clinic VARCHAR , + notes_modification_status VARCHAR +); + +create table chi_20200309_1938.diagnoses ( + pk_diagnosis_id FLOAT primary key, + diagnosis_name VARCHAR , + pk_diagnosis_key VARCHAR , + creation_date TIMESTAMP , + creation_acct VARCHAR , + modified_date TIMESTAMP , + modified_acct VARCHAR +); + +create table chi_20200309_1938.diagnoses_made ( + pk_diagnoses_made FLOAT primary key, + fk_visit_id FLOAT , + fk_diagnosis_key VARCHAR , + creation_date TIMESTAMP , + creation_acct VARCHAR , + modified_date TIMESTAMP , + modified_acct VARCHAR +); + +create table chi_20200309_1938.meds ( + pk_med_id FLOAT primary key, + med_name VARCHAR , + creation_date TIMESTAMP , + creation_acct VARCHAR , + modified_date TIMESTAMP , + modified_acct VARCHAR , + pk_med_key VARCHAR , + med_type VARCHAR +); + +create table chi_20200309_1938.meds_dispensed ( + pk_meds_dispensed FLOAT primary key, + fk_visit_id FLOAT , + fk_med_key VARCHAR , + creation_date TIMESTAMP , + creation_acct VARCHAR , + modified_date TIMESTAMP , + modified_acct VARCHAR , + dispensed_total FLOAT , + med_dosage VARCHAR +); + diff --git a/db/json_from_fmp.py b/db/json_from_fmp.py new file mode 100644 index 0000000..f2729ad --- /dev/null +++ b/db/json_from_fmp.py @@ -0,0 +1,30 @@ +import argparse +import sys + +from fmp import FMP + +""" +Generate JSON from file maker pro + +DESC: +This file will take in a data source name (dsn), username, and password, and generate to the +best of it's abilities a json file that represents the structure of the database +""" + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument("-d", "--dsn", help="dsn name") + parser.add_argument("-u", "--username", help="username for dsn") + parser.add_argument("-p", "--password", help="password for dsn") + return parser.parse_args() + + +def main(argv): + fmp = FMP(argv.dsn, argv.username, argv.password) + model = fmp.parse_database() + fmp.write_model(model) + + +if __name__ == "__main__": + sys.exit(main(parse_args())) diff --git a/convert/seed/.gitkeep b/db/models/.gitkeep similarity index 100% rename from convert/seed/.gitkeep rename to db/models/.gitkeep diff --git a/convert/models/20200305_2357_model.json b/db/models/20200305_2357_model.json similarity index 100% rename from convert/models/20200305_2357_model.json rename to db/models/20200305_2357_model.json diff --git a/convert/postgres_to_fmp.py b/db/models/20200309_1936_model.json similarity index 100% rename from convert/postgres_to_fmp.py rename to db/models/20200309_1936_model.json diff --git a/db/models/20200309_1938_model.json b/db/models/20200309_1938_model.json new file mode 100644 index 0000000..ffcac33 --- /dev/null +++ b/db/models/20200309_1938_model.json @@ -0,0 +1,1306 @@ +{ + "clinics": { + "pk_clinic_id": { + "type": "float64", + "pk": true, + "fk": false + }, + "clinic_name": { + "type": "str", + "pk": false, + "fk": false + }, + "creation_date": { + "type": "date", + "pk": false, + "fk": false + }, + "creation_acct": { + "type": "str", + "pk": false, + "fk": false + }, + "modified_date": { + "type": "date", + "pk": false, + "fk": false + }, + "modified_acct": { + "type": "str", + "pk": false, + "fk": false + } + }, + "patients": { + "pk_patient_id": { + "type": "float64", + "pk": true, + "fk": false + }, + "creation_date": { + "type": "date", + "pk": false, + "fk": false + }, + "creation_acct": { + "type": "str", + "pk": false, + "fk": false + }, + "modified_date": { + "type": "date", + "pk": false, + "fk": false + }, + "modified_acct": { + "type": "str", + "pk": false, + "fk": false + }, + "last_name": { + "type": "str", + "pk": false, + "fk": false + }, + "first_name": { + "type": "str", + "pk": false, + "fk": false + }, + "gender": { + "type": "str", + "pk": false, + "fk": false + }, + "face_photo": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "birthdate": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "phone": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "town": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "unique_name": { + "type": "str", + "pk": false, + "fk": false + }, + "unique_ID_name": { + "type": "str", + "pk": false, + "fk": false + }, + "nickname": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "needs_review": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "notes_modification_status": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "fk_geonameid": { + "type": "NoneType", + "pk": false, + "fk": true + } + }, + "visits": { + "pk_visit_id": { + "type": "float64", + "pk": true, + "fk": false + }, + "creation_date": { + "type": "date", + "pk": false, + "fk": false + }, + "creation_acct": { + "type": "str", + "pk": false, + "fk": false + }, + "modified_date": { + "type": "date", + "pk": false, + "fk": false + }, + "modified_acct": { + "type": "str", + "pk": false, + "fk": false + }, + "fk_patient_id": { + "type": "float64", + "pk": false, + "fk": true + }, + "fk_clinic_id": { + "type": "float64", + "pk": false, + "fk": true + }, + "visit_date": { + "type": "date", + "pk": false, + "fk": false + }, + "visit_age": { + "type": "float64", + "pk": false, + "fk": false + }, + "ob_pregnant": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "ob_last_menstrual_period": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "bp_systolic_blood_pressure": { + "type": "float64", + "pk": false, + "fk": false + }, + "bp_diastolic_blood_pressure": { + "type": "float64", + "pk": false, + "fk": false + }, + "bp_mean_arterial_pressure": { + "type": "float64", + "pk": false, + "fk": false + }, + "temp_in_fahrenheit": { + "type": "float64", + "pk": false, + "fk": false + }, + "pulse_rate": { + "type": "float64", + "pk": false, + "fk": false + }, + "weight_in_lbs": { + "type": "float64", + "pk": false, + "fk": false + }, + "height_in_inches": { + "type": "float64", + "pk": false, + "fk": false + }, + "glasses_given": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "sunglasses_given": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_surgical_candidate": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "surgical_comments": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "visit_age_unit": { + "type": "str", + "pk": false, + "fk": false + }, + "chief_complaint": { + "type": "str", + "pk": false, + "fk": false + }, + "history_of_present_illness": { + "type": "str", + "pk": false, + "fk": false + }, + "visit_age_years": { + "type": "float64", + "pk": false, + "fk": false + }, + "exam_comments": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_comments": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_blood_sugar_result": { + "type": "float64", + "pk": false, + "fk": false + }, + "tests_pregnancy_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_urine_glucose_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_urine_protein_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_urine_leukocytes_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_urine_nitrites_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "ob_gravida": { + "type": "float64", + "pk": false, + "fk": false + }, + "ob_para": { + "type": "float64", + "pk": false, + "fk": false + }, + "ob_abortus": { + "type": "float64", + "pk": false, + "fk": false + }, + "ob_history": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "ob_wants_planning": { + "type": "str", + "pk": false, + "fk": false + }, + "albendazole_given": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "first_visit": { + "type": "str", + "pk": false, + "fk": false + }, + "known_med_allergies": { + "type": "str", + "pk": false, + "fk": false + }, + "meds_currently_taken": { + "type": "str", + "pk": false, + "fk": false + }, + "tests_pregnancy_given": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_blood_sugar_given": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_eye_exam_given": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_urine_analysis_given": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_asthma": { + "type": "str", + "pk": false, + "fk": false + }, + "dx_allergies": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_anemia": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_malnutrition": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_diabetes": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_pterygium": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_cataract": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_dry_eye": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_conjunctivitis": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_fever": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_possible_malaria": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_filariasis": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_gastritis": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_gerd": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_gastroenteritis": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_dysentery": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_possible_cholera": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_headache": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_hernia": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_hernia_type": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_hypertension": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_stroke": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_angina": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_chf": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_musculoskeletal_pain": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_otitis": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_otitis_ear": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_pneumonia": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_sinusitis": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_uri": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_pregnant": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_pregnant_weeks": { + "type": "float64", + "pk": false, + "fk": false + }, + "dx_atopic_derm": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_fungal": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_scabies": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_impetigo": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_tinea_capitus": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_possible_std": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_possible_tb": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_vaginitis_candidal": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_vaginitis_trich": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_vaginitis_bv": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_other": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_other_text": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_multivitamin": { + "type": "str", + "pk": false, + "fk": false + }, + "tx_pain_acetaminophen": { + "type": "str", + "pk": false, + "fk": false + }, + "tx_gi_omeprazole": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_gi_pepto_bismol": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_gi_cimetidine": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_gi_tums": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_gi_ors": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_albendazole": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_amoxicillin": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_azithromycin": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_bactrim": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_cipro": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_doxycycline": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_flagyl": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_fluconazole": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_griseofulvin": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_ivermectin": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_keflex": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_rocephin": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_cardiac_amlodipine": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_cardiac_aspirin": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_cardiac_atenolol": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_cardiac_hctz": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_cardiac_lasix": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_cardiac_enalapril": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_topical_clotrimazole": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_topical_hydrocortisone": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_topical_silvadine": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_topical_permethirine": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_im_dexamethasone": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_im_lasix": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_albuterol": { + "type": "str", + "pk": false, + "fk": false + }, + "tx_misc_benadryl": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_ferrous_sulfate": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_metformin": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_prednisolone": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_other": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_other_text": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_comments": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_uti": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_multivitamin_varieties": { + "type": "str", + "pk": false, + "fk": false + }, + "tx_pain_acetaminophen_varieties": { + "type": "str", + "pk": false, + "fk": false + }, + "tx_infections_amoxicillin_varieties": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_pain_ibuprofen": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_comments": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_azithromycin_varieties": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_bactrim_varieties": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_keflex_varieties": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_rocephin_varieties": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_vaginitis_general": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_needs_to_see_dentist": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_needs_to_see_eye_doctor": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_healthy": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "provider": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_saw_dentist": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_saw_eye_doctor": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_outside_referral": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_outside_referral_text": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_gi_zinc": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_cardiac_methyldopa": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_augmentin": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_chloroquine": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_griseofuvin_varieties": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_infections_nitrofurantoin": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_topical_double_antibiotic": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_fluoxetine": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_ocp": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_phenytoin": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_possible_parasites": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_iud": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_test_given": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_chlamydia_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_hiv_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_syphilis_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_gonorrhea_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_eye_exam_right_sphere": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_eye_exam_right_cylinder": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_eye_exam_right_axis": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_eye_exam_right_bifocal": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_eye_exam_left_sphere": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_eye_exam_left_cylinder": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_eye_exam_left_axis": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_eye_exam_left_bifocal": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_pain_ibuprofen_varieties": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_benadryl_varieties": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_tb_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_confirm_hiv_cd4": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_confirm_hiv_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_confirm_hiv_arvt": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_confirm_hiv_enrolled": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_confirm_hiv_problems": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_confirm_tb_result": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_confirm_tb_comments": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tests_id_confirm_hiv_enrolled_at": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_depression": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "clinic_trip": { + "type": "str", + "pk": false, + "fk": false + }, + "tx_chw_referral": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_depo": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_paroxetine": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_keppra": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "dx_totalcount": { + "type": "float64", + "pk": false, + "fk": false + }, + "tx_totalcount": { + "type": "float64", + "pk": false, + "fk": false + }, + "tx_given_glasses": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_labetolol": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_carbamazepine": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_terazosin": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_gi_ranitidine": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_cardiac_lisinopril": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_topical_triple_antibiotic": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "tx_misc_prednisone": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "clinic": { + "type": "NoneType", + "pk": false, + "fk": false + }, + "notes_modification_status": { + "type": "NoneType", + "pk": false, + "fk": false + } + }, + "diagnoses": { + "pk_diagnosis_id": { + "type": "float64", + "pk": true, + "fk": false + }, + "diagnosis_name": { + "type": "str", + "pk": false, + "fk": false + }, + "pk_diagnosis_key": { + "type": "str", + "pk": true, + "fk": false + }, + "creation_date": { + "type": "date", + "pk": false, + "fk": false + }, + "creation_acct": { + "type": "str", + "pk": false, + "fk": false + }, + "modified_date": { + "type": "date", + "pk": false, + "fk": false + }, + "modified_acct": { + "type": "str", + "pk": false, + "fk": false + } + }, + "diagnoses_made": { + "pk_diagnoses_made": { + "type": "float64", + "pk": true, + "fk": false + }, + "fk_visit_id": { + "type": "float64", + "pk": false, + "fk": true + }, + "fk_diagnosis_key": { + "type": "str", + "pk": false, + "fk": true + }, + "creation_date": { + "type": "date", + "pk": false, + "fk": false + }, + "creation_acct": { + "type": "str", + "pk": false, + "fk": false + }, + "modified_date": { + "type": "date", + "pk": false, + "fk": false + }, + "modified_acct": { + "type": "str", + "pk": false, + "fk": false + } + }, + "meds": { + "pk_med_id": { + "type": "float64", + "pk": true, + "fk": false + }, + "med_name": { + "type": "str", + "pk": false, + "fk": false + }, + "creation_date": { + "type": "date", + "pk": false, + "fk": false + }, + "creation_acct": { + "type": "str", + "pk": false, + "fk": false + }, + "modified_date": { + "type": "date", + "pk": false, + "fk": false + }, + "modified_acct": { + "type": "str", + "pk": false, + "fk": false + }, + "pk_med_key": { + "type": "str", + "pk": true, + "fk": false + }, + "med_type": { + "type": "str", + "pk": false, + "fk": false + } + }, + "meds_dispensed": { + "pk_meds_dispensed": { + "type": "float64", + "pk": true, + "fk": false + }, + "fk_visit_id": { + "type": "float64", + "pk": false, + "fk": true + }, + "fk_med_key": { + "type": "str", + "pk": false, + "fk": true + }, + "creation_date": { + "type": "date", + "pk": false, + "fk": false + }, + "creation_acct": { + "type": "str", + "pk": false, + "fk": false + }, + "modified_date": { + "type": "date", + "pk": false, + "fk": false + }, + "modified_acct": { + "type": "str", + "pk": false, + "fk": false + }, + "dispensed_total": { + "type": "float64", + "pk": false, + "fk": false + }, + "med_dosage": { + "type": "str", + "pk": false, + "fk": false + } + } +} \ No newline at end of file diff --git a/db/postgres_init_from_json.py b/db/postgres_init_from_json.py new file mode 100644 index 0000000..d3bb04c --- /dev/null +++ b/db/postgres_init_from_json.py @@ -0,0 +1,79 @@ +import argparse +import json +import os +import sys +from datetime import datetime + +from fmp import FMP + +db_json = dict() +fmp_model = FMP() + + +# 1st (sql file) +# 2nd (write to sql connection) +def write_command(string, out_file): + with open(out_file, "w") as f: + f.write(string) + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument("-m", "--model", required=True, help="JSON model file to seed") + args = parser.parse_args() + print(args) + if not os.path.isfile(os.path.abspath(args.model)): + raise ValueError("File: {0} doesn't exist".format(args.model)) + return args + + +def main(argv): + model_file = argv.model + with open(model_file, "r") as f: + data_model = json.load(f) + + date_time = "_".join(os.path.split(os.path.abspath(model_file))[-1].split("_")[:2]) + output_file = "./init/{date_time}_init_db.sql".format(date_time=date_time) + + schema = "chi_{date_time}".format(date_time=date_time) + command = "create schema {schema};\n".format(schema=schema) + for table_name, attribute_dict in data_model.items(): + command += "create table {schema}.{table_name} (\n".format( + schema=schema, table_name=table_name + ) + features = list(attribute_dict.keys()) + last_elem = features[-1] + + # HACK: mark the first feautre name with 'pk' in + # the name as the primary key even if others exist. + pk_found = False + for feature in features: + + extra_param_string = "" + + if attribute_dict[feature]["pk"] and not pk_found: + extra_param_string += "primary key" + pk_found = True + + # elif attribute_dict[feature]["fk"]: + # extra_param_string += "foreign key" + + command += "\t{attr_name} {attr_type} {extra_params}".format( + attr_name=feature, + attr_type=fmp_model.python_types_to_sql[ + attribute_dict[feature]["type"] + ], + extra_params=extra_param_string, + ) + if feature != last_elem: + command += ",\n" + else: + command += "\n" + + command += ");\n\n" + + write_command(command, output_file) + + +if __name__ == "__main__": + sys.exit(main(parse_args())) diff --git a/convert/generate_postgres_seed_from_json.py b/db/postgres_seed_from_json.py similarity index 95% rename from convert/generate_postgres_seed_from_json.py rename to db/postgres_seed_from_json.py index 25a6067..e9b761c 100644 --- a/convert/generate_postgres_seed_from_json.py +++ b/db/postgres_seed_from_json.py @@ -32,7 +32,7 @@ def main(argv): with open(model_file, "r") as f: data_model = json.load(f) - date_time = '_'.join(os.path.split(os.path.abspath(model_file))[-1].split('_')[:2]) + date_time = "_".join(os.path.split(os.path.abspath(model_file))[-1].split("_")[:2]) schema = "chi_{date_time}".format(date_time=date_time) command = "create schema {schema};\n".format(schema=schema) for table_name, attribute_dict in data_model.items(): diff --git a/db/postgres_to_fmp.py b/db/postgres_to_fmp.py new file mode 100644 index 0000000..e69de29 diff --git a/db/psql.py b/db/psql.py new file mode 100644 index 0000000..6ddba78 --- /dev/null +++ b/db/psql.py @@ -0,0 +1,7 @@ +# import some python ORM for SQl +from db import DB + + +class PSQL(DB): + def __init__(self,): + pass diff --git a/db/seed-db.bash b/db/seed-db.bash new file mode 100755 index 0000000..225d2f5 --- /dev/null +++ b/db/seed-db.bash @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Author: "hertz" +# Date: 20200310 + +# args: +# 1 -- +# 2 -- + +#if [[ $# -ne 1 ]]; then +# echo "Usage: $0 ..." +# exit +#fi + + +# script: + + diff --git a/db/seed/.gitignore b/db/seed/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/db/seed/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 4c85104..5b61553 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,7 +13,7 @@ services: database: image: "postgres" # use latest official postgres version env_file: - - database.env # configure postgres + - ./db/database.env # configure postgres volumes: - database-data:/var/lib/postgresql/data/ # persist data even if container shuts down ports: