File size: 2,213 Bytes
b2638ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import logging
import os
import shutil

# -------------- DATABASE MANAGEMENT TAB FUNCTIONS --------------
def create_database(db_name):
    logging.info(f"Creating database: {db_name}")
    db_path = f"faiss_index_{db_name}"

    if os.path.exists(db_path):
        return f"Il database '{db_name}' esiste già."

    try:
        os.makedirs(db_path)
        logging.info(f"Database {db_name} created successfully.")
        return f"Database '{db_name}' creato con successo."
    except Exception as e:
        logging.error(f"Errore nella creazione del database: {e}")
        return f"Errore nella creazione del database: {e}"

def delete_database(db_name):
    db_path = f"faiss_index_{db_name}"
    if not os.path.exists(db_path):
        return f"Il database '{db_name}' non esiste."
    try:
        shutil.rmtree(db_path)
        logging.info(f"Database {db_name} eliminato con successo.")
        return f"Database '{db_name}' eliminato con successo."
    except OSError as e:
        logging.error(f"Impossibile eliminare il database {db_name}: {e}")
        return f"Impossibile eliminare il database '{db_name}': {e}"

def modify_database(old_db_name, new_db_name):
    old_db_path = f"faiss_index_{old_db_name}"
    new_db_path = f"faiss_index_{new_db_name}"
    if not os.path.exists(old_db_path):
        return f"Il database '{old_db_name}' non esiste."
    if os.path.exists(new_db_path):
        return f"Il database '{new_db_name}' esiste già."
    try:
        os.rename(old_db_path, new_db_path)
        return f"Database '{old_db_name}' rinominato in '{new_db_name}' con successo."
    except Exception as e:
        return f"Errore durante la modifica del database: {e}"

def list_databases():
    try:
        databases = []
        for item in os.listdir():
            if os.path.isdir(item) and item.startswith("faiss_index_"):
                db_name = item.replace("faiss_index_", "")
                databases.append(db_name)
        # Ensure "default_db" is in the list
        if "default_db" not in databases:
            databases.append("default_db")
        return databases
    except Exception as e:
        logging.error(f"Error listing databases: {e}")
        return []