Skip to content

Database ​

The table ​

TableOne row =Main fields
products_producta product sold by the groceryid, 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 ​

WhereDatabaseWhy
Tests (CI and local)SQLite, created and destroyed by pytestFast, nothing to install
Your machineSQLite or PostgreSQLYour choice, with DB_HOST
devPostgreSQL 17, db containerSame as prod
prodPostgreSQL 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:

EnvironmentWho runs migrate
devthe api service command in infra/docker-compose.dev.yml
prodapi/entrypoint.sh, when each container starts

To change the table:

sh
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 together

Migrations 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.