Database
The table
| Table | One row = | Main fields |
|---|---|---|
products_product | a product sold by the grocery | id, name, description, price, stock, created_at, updated_at |
The table name comes from Django: <application>_<model>.
Django's own tables (admin accounts, sessions) also exist, but the application does not use them.
The model and its rules are explained in The products API.
Which database, where
| Where | Database | Why |
|---|---|---|
| Tests (CI and local) | SQLite, created and destroyed by pytest | Fast, nothing to install |
| Your machine | SQLite or PostgreSQL | Your choice, with DB_HOST |
| dev | PostgreSQL 17, db container | Same as prod |
| prod | PostgreSQL 17, db Swarm service | — |
The choice is made in settings.py: if DB_HOST is set, PostgreSQL, otherwise SQLite. See Configuration and startup.
Data persists
In dev and prod, PostgreSQL's files are in a Docker volume named pgdata. Restarting or redeploying the container does not lose the data.
The production database
Nobody changes the prod database by hand. Every structure change goes through a migration, reviewed in a merge request. There is no automatic backup yet: see Limits.
Migrations
A describes a structure change. There is only one so far: products/migrations/0001_initial.py, which creates the table.
They are applied automatically:
| Environment | Who runs migrate |
|---|---|
| dev | the api service command in infra/docker-compose.dev.yml |
| prod | api/entrypoint.sh, when each container starts |
To change the table:
cd api
# 1. edit products/models.py
python manage.py makemigrations # 2. writes products/migrations/0002_….py
DB_HOST= python manage.py migrate # 3. test locally
# 4. commit the model AND the migration togetherMigrations do not roll back on their own
Rolling back the image in prod does not undo a migration already applied. A migration that drops a column breaks the old API version. Add first, remove later, in another release.