db.py 1.22 KB
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
12
13
14
15
from peewee_migrate import Router
from playhouse.db_url import connect

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

16
17
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["DB"])
18

19
20
21
22
23
24
25
26
27
28

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)


29
30
register_peewee_databases()

31
32
33
34
# 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")
35
    log.info("Database migrated from Ollama-WebUI successfully.")
36
37
38
else:
    pass

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