Skip to content

Commit b44bce9

Browse files
committed
doc on database migration
1 parent 8771bd3 commit b44bce9

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,89 @@ curl -v http://0.0.0.0:8000/docs
5858
```shell
5959
docker exec -it fastapi_expense_backend alembic current
6060
```
61+
62+
### Migrating changes made to the database
63+
64+
#### Important notes about containers
65+
66+
If your Alembic setup is running inside a container (e.g., a Docker container), you can still check the Alembic state by executing commands inside the container:
67+
68+
```shell
69+
docker exec -it fastapi_expense_backend sh
70+
```
71+
72+
> [!CAUTION]
73+
> Trying to execute alembic from the host machine instead of inside the container will result in an error:
74+
> `sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "db" to address: Name or service not known`
75+
>
76+
> This error is due to the fact that db is the name of the docker-compose service and also the host name.
77+
> What would work is: "localhost" instead of "db", however the backend container would not be able to connect to the database url.
78+
79+
80+
#### Generate a migration script
81+
82+
After making changes to your database models (schemas), you need to generate a migration script.
83+
84+
```shell
85+
alembic revision --autogenerate -m "Changed expense to transaction table name"
86+
87+
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
88+
INFO [alembic.runtime.migration] Will assume transactional DDL.
89+
INFO [alembic.autogenerate.compare] Detected removed index 'ix_transactions_id' on 'transactions'
90+
INFO [alembic.autogenerate.compare] Detected removed table 'transactions'
91+
INFO [alembic.ddl.postgresql] Detected sequence named 'expenses_id_seq' as owned by integer column 'expenses(id)', assuming SERIAL and omitting
92+
INFO [alembic.autogenerate.compare] Detected removed index 'ix_expenses_description' on 'expenses'
93+
INFO [alembic.autogenerate.compare] Detected removed index 'ix_expenses_id' on 'expenses'
94+
INFO [alembic.autogenerate.compare] Detected removed table 'expenses'
95+
Generating /app/alembic/versions/08290ef9350a_changed_expense_to_transaction_table_.py ... done
96+
```
97+
98+
- The --autogenerate flag will automatically compare your models with the current database schema and generate the necessary migration.
99+
- The `-m` flag allows you to provide a message describing the migration (e.g., "Changed expense to transaction table name").
100+
101+
Alembic will generate a migration script in the `alembic/versions/` folder. Review the generated script to ensure it properly represents your intended changes.
102+
103+
For example, if you added a new field to your Transaction model, you should see something like this in the migration script:
104+
105+
```python
106+
def upgrade():
107+
op.add_column('transactions', sa.Column('new_field', sa.String(), nullable=True))
108+
109+
def downgrade():
110+
op.drop_column('transactions', 'new_field')
111+
112+
```
113+
114+
#### Apply the migration script to the database
115+
116+
To apply the migration to your database, run the following command:
117+
118+
```shell
119+
alembic upgrade head
120+
121+
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
122+
INFO [alembic.runtime.migration] Will assume transactional DDL.
123+
INFO [alembic.runtime.migration] Running upgrade 65dd6b98e39b -> 08290ef9350a, Changed expense to transaction table name
124+
```
125+
126+
After running the migration, you can verify the changes in your database by inspecting the tables or running queries.
127+
128+
#### Rollback if needed
129+
130+
Rollback the migration: If something goes wrong, you can downgrade to a previous revision:
131+
132+
```shell
133+
alembic downgrade -1 # Go back 1 revision
134+
```
135+
136+
#### Check the current version of the database schema
137+
138+
Check the current version: To see the current state of the database schema:
139+
140+
```shell
141+
alembic current
142+
143+
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
144+
INFO [alembic.runtime.migration] Will assume transactional DDL.
145+
08290ef9350a (head)
146+
```

0 commit comments

Comments
 (0)