Use SQLite

This commit is contained in:
Joakim Hellsén 2024-03-28 02:58:22 +01:00
commit 20f38de611
No known key found for this signature in database
GPG key ID: D196AE66FEBE1DC9
8 changed files with 11 additions and 91 deletions

View file

@ -15,13 +15,7 @@ def add_global_context(request: HttpRequest) -> dict[str, str | int]:
Returns:
A dictionary with the global context.
"""
from feedvault.stats import get_db_size # noqa: PLC0415
from .models import Feed # noqa: PLC0415
db_size: str = get_db_size()
amount_of_feeds: int = Feed.objects.count()
return {
"db_size": db_size,
"amount_of_feeds": amount_of_feeds,
}
return {"amount_of_feeds": amount_of_feeds}

View file

@ -81,15 +81,14 @@ MIDDLEWARE: list[str] = [
"django_htmx.middleware.HtmxMiddleware",
]
# Use PostgreSQL as the default database
DATABASES: dict[str, dict[str, str]] = {
DATABASE_PATH: str = os.getenv("DATABASE_PATH", "/data")
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.getenv("DB_NAME", default="feedvault"),
"USER": os.getenv("DB_USER", default="feedvault"),
"PASSWORD": os.getenv("DB_PASSWORD", default="feedvault"),
"HOST": os.getenv("DB_HOST", default="localhost"),
"PORT": os.getenv("DB_PORT", default="5432"),
"ENGINE": "django.db.backends.sqlite3",
"NAME": Path(DATABASE_PATH) / "feedvault.sqlite3",
"OPTIONS": {
"timeout": 30,
},
},
}

View file

@ -1,22 +0,0 @@
from __future__ import annotations
import logging
from django.db import connection
logger: logging.Logger = logging.getLogger(__name__)
def get_db_size() -> str:
"""Get the size of the database.
Returns:
str: The size of the database.
"""
# Get Postgres database size
with connection.cursor() as cursor:
cursor.execute("SELECT pg_size_pretty(pg_database_size(current_database()))")
db_size_result: tuple[str, ...] | None = cursor.fetchone()
db_size: str | None = db_size_result[0] if db_size_result else None
return db_size if db_size is not None else "0 MB"

View file

@ -9,7 +9,6 @@ from django.test import Client, TestCase
from django.urls import reverse
from feedvault.models import Domain, Entry, Feed, UserUploadedFile
from feedvault.stats import get_db_size
if TYPE_CHECKING:
from django.http import HttpResponse
@ -238,14 +237,6 @@ class TestSitemap(TestCase):
assert "urlset" in response2.content.decode(), f"Expected 'urlset' in response, got {response2.content}"
class TestStats(TestCase):
def test_db_size(self) -> None:
"""Test if the database size is returned."""
response: str = get_db_size()
assert isinstance(response, str), f"Expected a string, got {response}"
assert "kB" in response, f"Expected 'kB' in response, got {response}"
class TestSearch(TestCase):
def setUp(self) -> None:
"""Create a test feed."""

View file

@ -461,6 +461,8 @@ class SearchView(View):
if not query:
return FeedsView().get(request)
# TODO(TheLovinator): #20 Search more fields
# https://github.com/TheLovinator1/FeedVault/issues/20
feeds: BaseManager[Feed] = Feed.objects.filter(feed_url__icontains=query).order_by("-created_at")[:100]
context = {