Move back to Postgres

This commit is contained in:
Joakim Hellsén 2024-03-17 23:56:24 +01:00
commit f935bf1740
No known key found for this signature in database
GPG key ID: D196AE66FEBE1DC9
9 changed files with 170 additions and 26 deletions

View file

@ -78,23 +78,21 @@ MIDDLEWARE: list[str] = [
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
database_folder: Path = BASE_DIR / "data"
database_folder.mkdir(parents=True, exist_ok=True)
DATABASES: dict[str, dict[str, str | Path | bool | int]] = {
# Use PostgreSQL as the default database
DATABASES: dict[str, dict[str, str]] = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": database_folder / "feedvault.sqlite3",
"ATOMIC_REQUESTS": True,
"timeout": 30,
"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"),
},
}
# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS: list[dict[str, str]] = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",

View file

@ -13,11 +13,10 @@ def get_db_size() -> str:
Returns:
str: The size of the database.
"""
# Get SQLite database size
# Get Postgres database size
with connection.cursor() as cursor:
cursor.execute("PRAGMA page_count")
page_count_result: tuple[int, ...] | None = cursor.fetchone()
page_count: int | None = page_count_result[0] if page_count_result else None
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
db_size: int | None = 4096 * page_count if page_count else None
return f"{db_size / 1024 / 1024:.2f} MB" if db_size is not None else "0 MB"
return db_size if db_size is not None else "0 MB"