Skip to content

Commit

Permalink
refactor:database configuration logic.
Browse files Browse the repository at this point in the history
Simplified and clarified database settings to ensure proper handling of `DATABASE_CREDENTIALS`. Adjusted fallback behavior and added comments to avoid misconfigurations, especially in production environments.
  • Loading branch information
hareshkainthdbt committed Dec 17, 2024
1 parent b3a2742 commit 24a9d40
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions fbr/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,48 @@

WSGI_APPLICATION = "fbr.wsgi.application"

# We're going to restructure the below so that we don't end up with
# a sqlite database in production.

# Use DATABASE_URL if it exists, otherwise use sqlite ?
# DATABASES["default"] = dj_database_url.config( # noqa
# default=database_url_from_env("DATABASE_CREDENTIALS")
# )


# if DATABASE_URL := env("DATABASE_URL", default=None):
# DATABASES = {
# "default": {
# **dj_database_url.parse(
# DATABASE_URL,
# engine="postgresql",
# ),
# "ENGINE": "django.db.backends.postgresql",
# }
# }
# else:
# DATABASES = {
# "default": {
# "ENGINE": "django.db.backends.sqlite3",
# "NAME": BASE_DIR / "db.sqlite3",
# }
# }

DATABASES: dict = {"default": {}}

if DATABASE_URL := env("DATABASE_URL", default=None):
# Use DATABASE_URL for local development if available in local.env
DATABASES["default"] = dj_database_url.parse(
DATABASE_URL,
engine="postgresql",
)
elif DATABASE_CREDENTIALS := env("DATABASE_CREDENTIALS", default=None):
# Use DATABASE_CREDENTIALS (server) for deployed environments
if DATABASE_URL := env("DATABASE_CREDENTIALS", default=None):
DATABASES["default"] = dj_database_url.config(
default=database_url_from_env(DATABASE_CREDENTIALS)
default=database_url_from_env("DATABASE_CREDENTIALS")
)
else:
# Empty configuration to allow the codebuild to run without DB config
DATABASES["default"] = dj_database_url.parse("", engine="postgresql")
DATABASES["default"] = dj_database_url.parse(
"",
engine="postgresql",
)

# Ensure the ENGINE is set correctly
DATABASES["default"]["ENGINE"] = "django.db.backends.postgresql"


AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", # noqa: E501
Expand Down

0 comments on commit 24a9d40

Please sign in to comment.