Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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()
Exemplo n.º 4
0
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
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
 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