def create_tables(): """Create the tables for the objects if they exist.""" try_db_connect() try_es_connect() create_elastic_index() for obj in ORM_OBJECTS: if not obj.table_exists(): obj.create_table() obj.create_elastic_mapping() DB.close()
def try_db_connect(attempts=0): """Recursively try to connect to the database.""" try: DB.connect() except OperationalError: if attempts < DATABASE_CONNECT_ATTEMPTS: sleep(DATABASE_WAIT) attempts += 1 try_db_connect(attempts) else: raise OperationalError
def create_tables(): """ Create the tables for the objects if they exist. """ try_db_connect() try_es_connect() create_elastic_index() for obj in ORM_OBJECTS: if not obj.table_exists(): obj.create_table() obj.create_elastic_mapping() DB.close()
def try_db_connect(attempts=0): """ Recursively try to connect to the database. """ try: DB.connect() except OperationalError, ex: if attempts < DATABASE_CONNECT_ATTEMPTS: sleep(DATABASE_WAIT) attempts += 1 try_db_connect(attempts) else: raise ex
def _update_id_list(names_list, model_obj, field_name): names_list = map(str, names_list) local_list = UploadEntries._get_id_list(names_list, model_obj, field_name) diff = list(set(names_list) - set(local_list.keys())) insert_list = [{field_name: str(o)} for o in diff] if insert_list: with DB.atomic(): model_obj.insert_many(insert_list).execute() return UploadEntries._get_id_list(names_list, model_obj, field_name)
def _insert_kv_mappings(item, key_cache, value_cache, transaction_id): insert_list = [] for field in item: value = str(item[str(field)]) key_id = key_cache[str(field)] value_id = value_cache[value] insert_list.append({ 'key': key_id, 'value': value_id, 'transaction': transaction_id }) insert_count = 0 if insert_list: with DB.atomic(): for insert_item in insert_list: TransactionKeyValue.get_or_create(**insert_item) insert_count += 1 return insert_count