Skip to content

Commit 5d48526

Browse files
feat: add command to run migration before starting server, environment variables and README for database migrations (#264)
* feat: update environment variables and README for database migrations * feat: update environment variables and docker-compose for PostgreSQL configuration * feat: add --noinput flag to manage.py migrate command in Dockerfiles
1 parent f64c2a1 commit 5d48526

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

.env.example

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ SWAGGER_UI_PATH='/api/schema'
3131

3232
3333

34-
POSTGRES_HOST: postgres
35-
POSTGRES_PORT: 5432
36-
POSTGRES_DB: todo_postgres
37-
POSTGRES_USER: todo_user
38-
POSTGRES_PASSWORD: todo_password
34+
POSTGRES_DB=todo_postgres
35+
POSTGRES_HOST=postgres
36+
POSTGRES_PASSWORD=todo_password
37+
POSTGRES_PORT=5432
38+
POSTGRES_USER=todo_user

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,7 @@ dmypy.json
103103
cython_debug/
104104

105105
.ruff_cache
106-
mongo_data
107-
logs
106+
/mongo_data
107+
/logs
108+
109+
/postgres_data

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,28 @@
8383
```
8484
4. On making changes to code and saving, live reload will work in this case as well
8585
86+
## Database Migrations
87+
88+
When making changes to Django models, you need to create and apply migrations:
89+
90+
1. **Create migrations** (run this after modifying models):
91+
```
92+
python manage.py makemigrations
93+
```
94+
95+
2. **Apply migrations** (run this to update the database schema):
96+
```
97+
python manage.py migrate
98+
```
99+
100+
3. **In Docker environment:**
101+
```
102+
docker compose exec django-app python manage.py makemigrations
103+
docker compose exec django-app python manage.py migrate
104+
```
105+
106+
**Note:** The docker-compose.yml automatically runs `migrate` on startup, but you must manually run `makemigrations` after model changes.
107+
86108
## Command reference
87109
1. To run the tests, run the following command
88110
```

docker-compose.yml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ services:
44
container_name: todo-django-app
55
command: >
66
sh -c "
7-
python manage.py makemigrations &&
8-
python manage.py migrate &&
9-
python manage.py shell -c 'from todo_project.db.init import initialize_database; initialize_database()' &&
10-
python manage.py runserver 0.0.0.0:8000
7+
python manage.py migrate --noinput &&
8+
python -Xfrozen_modules=off manage.py runserver_debug 0.0.0.0:8000 --debug-port 5678
119
"
1210
environment:
13-
MONGODB_URI: mongodb://db:27017
11+
MONGODB_URI: mongodb://db:27017/?replicaSet=rs0
1412
DB_NAME: todo-app
1513
PYTHONUNBUFFERED: 1
1614
PYDEVD_DISABLE_FILE_VALIDATION: 1
@@ -36,39 +34,34 @@ services:
3634
tty: true
3735

3836
postgres:
39-
image: postgres:15
37+
image: postgres:17.6
4038
container_name: todo-postgres
4139
environment:
4240
POSTGRES_DB: todo_postgres
4341
POSTGRES_USER: todo_user
4442
POSTGRES_PASSWORD: todo_password
45-
POSTGRES_HOST_AUTH_METHOD: trust
4643
ports:
4744
- "5432:5432"
4845
volumes:
4946
- postgres_data:/var/lib/postgresql/data
50-
- ./init-scripts:/docker-entrypoint-initdb.d
5147
healthcheck:
5248
test:
5349
[
5450
"CMD-SHELL",
55-
"pg_isready -U todo_user -d todo_app && echo 'Postgres healthcheck passed'",
51+
"pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}",
5652
]
5753
interval: 10s
5854
timeout: 5s
5955
retries: 5
60-
start_period: 10s
61-
62-
6356

6457
db:
6558
image: mongo:latest
66-
command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017"]
59+
command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017", "--quiet"]
6760
container_name: todo-mongo
6861
ports:
6962
- "27017:27017"
7063
volumes:
71-
- ./mongo_data:/data/db
64+
- mongo_data:/data/db
7265
healthcheck:
7366
test:
7467
[
@@ -119,3 +112,4 @@ services:
119112

120113
volumes:
121114
postgres_data:
115+
mongo_data:

production.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ COPY . .
5858
EXPOSE 8000
5959

6060
# Run the application.
61-
CMD ["gunicorn", "todo_project.wsgi", "--bind", "0.0.0.0:8000"]
61+
CMD ["sh", "-c", "python manage.py migrate --noinput && gunicorn todo_project.wsgi --bind 0.0.0.0:8000"]

0 commit comments

Comments
 (0)