You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Next, migrate and upgrade the database migrations.
32
+
Next, apply the existing migrations (they are the canonical schema definition):
33
33
34
-
docker-compose exec compose \
35
-
bash -c \
36
-
"flask db merge heads && \
37
-
flask db stamp head && \
38
-
flask db migrate && \
39
-
flask db upgrade"
40
-
41
-
**Note**: `flask db merge heads` is not strictly necessary
42
-
unless you have multiple schema versions that are not from the same history
43
-
(e.g., multiple files in the `versions` directory).
44
-
However, `flask db merge heads` makes the migration more robust
45
-
when there are multiple versions from different histories.
34
+
docker-compose exec compose flask db upgrade
46
35
47
36
48
37
## Maintaining docker image and db
49
38
If you make a change to compose, you should be able to simply restart the server.
50
39
51
40
docker-compose restart compose
52
41
53
-
If you need to upgrade the db after changing any models:
42
+
If you change any models, generate a new Alembic migration and migrate the database (commit the generated revision file so it becomes the new source of truth):
54
43
55
44
docker-compose exec compose flask db migrate
56
45
docker-compose exec compose flask db upgrade
57
46
58
47
48
+
## Database migrations
49
+
50
+
The migrations stored in `backend/migrations` are the **only** source of truth for the schema—avoid merging heads, stamping, or manually altering the history. Always move the database forward (or rebuild from scratch) by applying the tracked revisions.
51
+
52
+
### Applying migrations after pulling a branch
53
+
54
+
Any time you start the backend or pull the latest changes, bring the database to the expected state with:
55
+
56
+
```sh
57
+
docker-compose exec compose flask db upgrade
58
+
```
59
+
60
+
`upgrade` is idempotent, so rerunning it is harmless; it only applies migrations that have not been run yet.
61
+
62
+
### Resetting the database when switching branches
63
+
64
+
Because each branch might change the schema independently, recreate the database before starting work on a different branch so that Alembic can replay only the migrations that exist on that branch.
0 commit comments