db.py 960 Bytes
Newer Older
1
2
import json

Timothy J. Baek's avatar
Timothy J. Baek committed
3
from peewee import *
4
from peewee_migrate import Router
5
from playhouse.db_url import connect
Tang Ziya's avatar
Tang Ziya committed
6
from config import SRC_LOG_LEVELS, DATA_DIR, DATABASE_URL, BACKEND_DIR
7
import os
8
import logging
Timothy J. Baek's avatar
Timothy J. Baek committed
9

10
11
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["DB"])
12

13
14
15
16
17
18
19
20
21
22

class JSONField(TextField):
    def db_value(self, value):
        return json.dumps(value)

    def python_value(self, value):
        if value is not None:
            return json.loads(value)


23
24
25
26
# Check if the file exists
if os.path.exists(f"{DATA_DIR}/ollama.db"):
    # Rename the file
    os.rename(f"{DATA_DIR}/ollama.db", f"{DATA_DIR}/webui.db")
27
    log.info("Database migrated from Ollama-WebUI successfully.")
28
29
30
else:
    pass

31
32
DB = connect(DATABASE_URL)
log.info(f"Connected to a {DB.__class__.__name__} database.")
Tang Ziya's avatar
Tang Ziya committed
33
router = Router(
Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
34
35
36
    DB,
    migrate_dir=BACKEND_DIR / "apps" / "webui" / "internal" / "migrations",
    logger=log,
Tang Ziya's avatar
Tang Ziya committed
37
)
38
39
router.run()
DB.connect(reuse_if_open=True)