Skip to content

Commit 0860b2b

Browse files
committed
Add PostgreSQL DB support
1 parent c8ee538 commit 0860b2b

File tree

11 files changed

+113
-45
lines changed

11 files changed

+113
-45
lines changed

.env.example

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ RUN_CREATE_SUPER_USER=True
3838
RUN_CREATE_ADMIN=True
3939

4040
# database
41-
# change DATABASE_ENGINE to "mysql" if you want to use MySQL
42-
DATABASE_ENGINE=sqlite3
43-
# these are only used if DATABASE_ENGINE is mysql
41+
# change DATABASE_ENGINE to "mysql" if you want to use MySQL or "psql" for PostgreSQL
42+
DATABASE_ENGINE=psql
43+
# these are only used if DATABASE_ENGINE is mysql or psql
4444
DATABASE_NAME=redirect
4545
DATABASE_USER=user
4646
DATABASE_PASSWORD=password
4747
DATABASE_HOST=db
48-
DATABASE_PORT=3306
48+
DATABASE_PORT=5432
4949

5050
# Email addresses
5151
DEFAULT_FROM_EMAIL=[email protected]
5252
NO_REPLY_EMAIL=[email protected]
5353

5454
# Email server
55-
EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
55+
EMAIL_BACKEND=django.core.mail.backends.console.EmailBackend
5656
EMAIL_HOST=
5757
EMAIL_HOST_USER=
5858
EMAIL_HOST_PASSWORD=

Makefile

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ stop-mysql: ## stops the mysql dev project
1919
drop-mysql: ## stops the mysql dev project
2020
docker compose --profile mysql down -v -t 60
2121

22+
stop-psql: ## stops the psql dev project
23+
docker compose --profile psql down -t 60
24+
25+
drop-psql: ## stops the psql dev project
26+
docker compose --profile psql down -v -t 60
27+
2228
stop-prod: ## stops the mysql dev project
2329
docker compose -f docker-compose.prod.yml down -t 60
2430

@@ -38,39 +44,45 @@ up-mysql: ## run the project with mysql
3844
upd-mysql: ## run the project with mysql in detached mode
3945
docker compose --profile mysql up -d --build
4046

47+
up-psql: ## run the project with psql
48+
docker compose --profile psql up --build
49+
50+
upd-psql: ## run the project with psql in detached mode
51+
docker compose --profile psql up -d --build
52+
4153
up-prod: ## run the project with mysql
4254
docker compose -f docker-compose.prod.yml up --build
4355

4456
upd-prod: ## run the project with mysql in detached mode
4557
docker compose -f docker-compose.prod.yml up -d --build
4658

4759
### Using the SQLite database
48-
run-sqlite: stop-mysql up-sqlite ## run the project with sqlite and stop the mysql project beforehand
49-
rund-sqlite: stop-mysql upd-sqlite ## run the project with sqlite in detached mode and stop the mysql project beforehand
50-
redo-sqlite: drop-sqlite up-sqlite ## delete the db and rerun the project with sqlite
51-
redod-sqlite: drop-sqlite upd-sqlite ## delete the db and rerun the project with sqlite in detached mode
60+
run-sqlite: stop-psql stop-mysql up-sqlite ## run the project with sqlite and stop the mysql project beforehand
61+
rund-sqlite: stop-psql stop-mysql upd-sqlite ## run the project with sqlite in detached mode and stop the mysql project beforehand
62+
redo-sqlite: drop-sqlite up-sqlite ## delete the db and rerun the project with sqlite
63+
redod-sqlite: drop-sqlite upd-sqlite ## delete the db and rerun the project with sqlite in detached mode
5264

5365
### Using the MySQL database
54-
run-mysql: stop-sqlite up-mysql ## run the project with mysql and stop the sqlite project beforehand
55-
rund-mysql: stop-sqlite upd-mysql ## run the project with mysql in detached mode and stop the sqlite project beforehand
56-
redo-mysql: drop-mysql up-mysql ## delete the db and rerun the project with mysql
57-
redod-mysql: drop-mysql upd-mysql ## delete the db and rerun the project with mysql in detached mode
66+
run-mysql: stop-psql stop-sqlite up-mysql ## run the project with mysql and stop the sqlite project beforehand
67+
rund-mysql: stop-psql stop-sqlite upd-mysql ## run the project with mysql in detached mode and stop the sqlite project beforehand
68+
redo-mysql: drop-mysql up-mysql ## delete the db and rerun the project with mysql
69+
redod-mysql: drop-mysql upd-mysql ## delete the db and rerun the project with mysql in detached mode
5870

59-
### With an image built for production
60-
run-prod: up-prod ## run the project with production settings
61-
rund-prod: upd-prod ## run the project with production settings in detached mode
62-
redo-prod: drop-prod up-prod ## delete the db and rerun the project with mysql
63-
redod-prod: drop-prod upd-prod ## delete the db and rerun the project with mysql in detached mode
71+
### Using the PostgreSQL database
72+
run-psql: stop-mysql stop-sqlite up-psql ## run the project with psql and stop the mysql project beforehand
73+
rund-psql: stop-mysql stop-sqlite upd-psql ## run the project with psql in detached mode and stop the mysql project beforehand
74+
redo-psql: drop-psql up-psql ## delete the db and rerun the project with psql
75+
redod-psql: drop-psql upd-psql ## delete the db and rerun the project with psql in detached mode
6476

6577
### Other run options
6678
run: run-sqlite ## set the default run command to sqlite
6779
redo: redo-sqlite ## set the default redo command to sqlite
6880
rund: rund-sqlite ## set the default run command to sqlite
6981
redod: redod-sqlite ## set the default redo command to sqlite
7082

71-
stop: stop-sqlite stop-mysql stop-prod ## stop all running projects
83+
stop: stop-sqlite stop-mysql stop-psql stop-prod ## stop all running projects
7284

73-
drop: drop-sqlite drop-mysql drop-prod ## drop all databases
85+
drop: drop-sqlite drop-mysql drop-psql drop-prod ## drop all databases
7486

7587

7688
## [Monitoring the containers]
@@ -137,6 +149,7 @@ requirements-update: ## run pip compile and rebuild the requirement
137149
## [Clean-up]
138150
clean-docker: ## stop docker containers and remove orphaned images and volumes
139151
docker compose --profile mysql down -t 60
152+
docker compose --profile psql down -t 60
140153
docker compose --profile sqlite3 down -t 60
141154
docker compose -f docker-compose.prod.yml down -t 60
142155
docker system prune -f

backend/donations/views/ngo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class FormSignature(BaseHandler):
6161
template_name = "signature.html"
6262

6363
def __init__(self, **kwargs):
64-
super().__init__(kwargs)
64+
super().__init__(**kwargs)
6565
self.ngo = None
6666
self.donor = None
6767

backend/redirectioneaza/settings.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,23 +227,28 @@
227227

228228
# Database
229229
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
230+
DATABASE_ENGINE = env("DATABASE_ENGINE")
230231

231-
DATABASES = {
232-
"default": {
233-
"ENGINE": "django.db.backends.sqlite3",
234-
"NAME": os.path.abspath(os.path.join(BASE_DIR, ".db_sqlite", "db.sqlite3")),
235-
}
232+
REMOTE_DATABASE_ENGINES = {
233+
"mysql": "django.db.backends.mysql",
234+
"psql": "django.db.backends.postgresql",
236235
}
237-
238-
if env.str("DATABASE_ENGINE") == "mysql":
236+
if DATABASE_ENGINE in REMOTE_DATABASE_ENGINES.keys():
237+
DATABASES = {
238+
"default": {
239+
"ENGINE": REMOTE_DATABASE_ENGINES[DATABASE_ENGINE],
240+
"NAME": env("DATABASE_NAME"),
241+
"USER": env("DATABASE_USER"),
242+
"PASSWORD": env("DATABASE_PASSWORD"),
243+
"HOST": env("DATABASE_HOST"),
244+
"PORT": env("DATABASE_PORT"),
245+
}
246+
}
247+
else:
239248
DATABASES = {
240249
"default": {
241-
"ENGINE": "django.db.backends.mysql",
242-
"NAME": env.str("DATABASE_NAME"),
243-
"USER": env.str("DATABASE_USER"),
244-
"PASSWORD": env.str("DATABASE_PASSWORD"),
245-
"HOST": env.str("DATABASE_HOST"),
246-
"PORT": env.str("DATABASE_PORT"),
250+
"ENGINE": "django.db.backends.sqlite3",
251+
"NAME": os.path.abspath(os.path.join(BASE_DIR, ".db_sqlite", "db.sqlite3")),
247252
}
248253
}
249254

@@ -421,6 +426,7 @@
421426
LIST_OF_COUNTIES = [county[1] for county in COUNTIES_CHOICES]
422427
FORM_COUNTIES = deepcopy(LIST_OF_COUNTIES)
423428
FORM_COUNTIES.pop(LIST_OF_COUNTIES.index("București"))
429+
FORM_COUNTIES.insert(0, "Național")
424430
CONTACT_EMAIL_ADDRESS = env.str("CONTACT_EMAIL_ADDRESS")
425431

426432
# Django Q2

backend/requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ platformdirs==4.1.0
121121
# via black
122122
psutil==5.9.8
123123
# via -r requirements.txt
124+
psycopg2-binary==2.9.9
125+
# via -r requirements.txt
124126
pycparser==2.21
125127
# via
126128
# -r requirements.txt

backend/requirements.in

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ django-environ~=0.11.2
44
# captcha
55
django-recaptcha==4.0.0
66

7+
# encrypting data
8+
cryptography==42.0.2
9+
710
# django-q
811
django-q2~=1.6.1
912
blessed~=1.20.0 # optional requirement for django-q2
1013
psutil~=5.9.8 # optional requirement for django-q2
1114
croniter~=2.0.1 # optional requirement for django-q2
1215

13-
# MySQL database
16+
# database
1417
pymysql~=1.1.0
15-
cryptography==42.0.2
18+
psycopg2-binary~=2.9.9
1619

1720
# Sentry
1821
sentry-sdk[django]==1.40.0

backend/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ pillow==10.2.0
7777
# via reportlab
7878
psutil==5.9.8
7979
# via -r requirements.in
80+
psycopg2-binary==2.9.9
81+
# via -r requirements.in
8082
pycparser==2.21
8183
# via cffi
8284
pymysql==1.1.0

dkcp.base.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ services:
1818
- "PYTHONUNBUFFERED=1"
1919
restart: on-failure
2020

21-
db_base:
21+
db_base_mysql:
2222
image: mysql:8.2.0
2323
restart: always
2424
env_file:
@@ -32,3 +32,17 @@ services:
3232
MYSQL_ROOT_PASSWORD: "${DATABASE_PASSWORD}"
3333
ports:
3434
- "${DATABASE_PORT:-3306}:3306"
35+
36+
db_base_psql:
37+
image: postgres:16.1
38+
restart: always
39+
env_file:
40+
- .env
41+
volumes:
42+
- redirect_psql:/var/lib/postgresql/data
43+
environment:
44+
POSTGRES_DB: "${DATABASE_NAME}"
45+
POSTGRES_USER: "${DATABASE_USER}"
46+
POSTGRES_PASSWORD: "${DATABASE_PASSWORD}"
47+
ports:
48+
- "${DATABASE_PORT:-5432}:5432"

docker-compose.prod.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ services:
1515
- ./backend/media:/var/www/redirect/backend/media
1616
environment:
1717
- "ENVIRONMENT=production"
18+
- "DATABASE_ENGINE=psql"
1819
ports:
1920
- "${WEBAPP_PORT:-8080}:80"
2021
depends_on:
@@ -35,7 +36,8 @@ services:
3536
db:
3637
extends:
3738
file: dkcp.base.yml
38-
service: db_base
39+
service: db_base_psql
40+
container_name: redirect_psql
3941

4042
volumes:
41-
redirect_mysql:
43+
redirect_psql:

docker-compose.yml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,22 @@ services:
2020
container_name: redirect_dev
2121
environment:
2222
- "DATABASE_ENGINE=mysql"
23+
- "DATABASE_HOST=redirect_mysql"
24+
- "DATABASE_PORT=3306"
2325
depends_on:
24-
- db
26+
- db_mysql_dev
27+
28+
webapp_psql:
29+
profiles: [ "psql" ]
30+
extends:
31+
service: redirect_dev_base
32+
container_name: redirect_dev
33+
environment:
34+
- "DATABASE_ENGINE=psql"
35+
- "DATABASE_HOST=redirect_psql"
36+
- "DATABASE_PORT=5432"
37+
depends_on:
38+
- db_psql_dev
2539

2640
redirect_dev_base:
2741
profiles: [ "_base" ]
@@ -36,6 +50,8 @@ services:
3650
- ./backend:/var/www/redirect/backend
3751
environment:
3852
- "ENVIRONMENT=development"
53+
env_file:
54+
- .env
3955
ports:
4056
- "${WEBAPP_PORT:-8080}:8000"
4157

@@ -55,14 +71,24 @@ services:
5571
ports:
5672
- "${API_PORT:-8123}:80"
5773

58-
db:
74+
db_mysql_dev:
5975
profiles: [ "mysql" ]
6076
extends:
6177
file: dkcp.base.yml
62-
service: db_base
63-
container_name: redirect_mysql_dev
78+
service: db_base_mysql
79+
container_name: redirect_mysql
6480
volumes:
6581
- ./docker/init-mysql:/docker-entrypoint-initdb.d
6682

83+
db_psql_dev:
84+
profiles: [ "psql" ]
85+
extends:
86+
file: dkcp.base.yml
87+
service: db_base_psql
88+
container_name: redirect_psql
89+
volumes:
90+
- ./docker/init-psql:/docker-entrypoint-initdb.d
91+
6792
volumes:
6893
redirect_mysql:
94+
redirect_psql:

0 commit comments

Comments
 (0)