db.py 1.09 KB
Newer Older
1
import os
2
import logging
3
import json
Timothy J. Baek's avatar
Timothy J. Baek committed
4

5
from peewee import *
6
7
8
9
from peewee_migrate import Router
from playhouse.db_url import connect

from apps.webui.internal.wrappers import PeeweeConnectionState, register_peewee_databases
10
from config import SRC_LOG_LEVELS, DATA_DIR, DATABASE_URL, BACKEND_DIR
11

12
13
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["DB"])
14

15
16
17
18
19
20
21
22
23
24

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)


25
26
register_peewee_databases()

27
28
29
30
# 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")
31
    log.info("Database migrated from Ollama-WebUI successfully.")
32
33
34
else:
    pass

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