-
Notifications
You must be signed in to change notification settings - Fork 54
Tips for SQLAlchemy
Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database Toolkit for Python. When making changes to the database, it's essential to follow these specific steps to auto-generate and test an Alembic migration:
-
Undeploy the current cluster:
reana-dev cluster-undeploy
-
Stash your
reana_db/models.py
changes:git stash
-
Deploy the old version of the cluster in debug mode:
reana-dev cluster-deploy --admin-email [email protected] --admin-password mysecretpassword --mode debug
-
Pop your stash:
git stash pop
-
Propagate changes to all other components:
reana-dev git-submodule --update
-
Generate the migration:
kubectl exec deployment/reana-server -- reana-db alembic revision --autogenerate -m "{{{description_of_your_change}}}"
-
Copy your migration from
reana-server
toreana-db
:From:
reana-server/modules/reana-db/reana_db/alembic/versions/12345678_9123_4a5b6c7d8e9f_{{{description_of_your_change}}}.py
To:
reana-db/reana_db/alembic/versions/12345678_9123_4a5b6c7d8e9f_{{{description_of_your_change}}}.py
You're done! With the migration part... After generating the initial migration, it's crucial to test it since the initial migration often leads to unexpected errors. Follow these steps to ensure the migration works as expected:
The testing example was taken from this issue. To thoroughly ensure the migration works, you should perform the downgrade and upgrade steps.
-
Check the schema of the table you are changing, in this case it is
__reana.job
.kubectl exec -ti deployment/reana-db -- psql -U reana
psql (9.6.2) Type "help" for help. reana=# \d __reana.job Table "__reana.job" Column | Type | Modifiers --------------------+-----------------------------+----------- ... finished_at | timestamp without time zone | started_at | timestamp without time zone | ... reana=# \q
-
View the Alembic migration history.
kubectl exec deployment/reana-server -- reana-db alembic history
ad93dae04483 -> 4801b98f6408 (head), Job started and finished times. c912d4f1e1cc -> ad93dae04483, Interactive sessions. <base> -> c912d4f1e1cc, Quota tables.
-
Check the current Alembic migration version.
kubectl exec deployment/reana-server -- reana-db alembic current
INFO [alembic.runtime.migration] Context impl PostgresqlImpl. INFO [alembic.runtime.migration] Will assume transactional DDL. 4801b98f6408 (head)
-
Downgrade the Alembic migration to a previous version (e.g.,
ad93dae04483
).kubectl exec deployment/reana-server -- reana-db alembic downgrade ad93dae04483
INFO [alembic.runtime.migration] Context impl PostgresqlImpl. INFO [alembic.runtime.migration] Will assume transactional DDL. INFO [alembic.runtime.migration] Running downgrade 4801b98f6408 -> ad93dae04483, Job started and finished times.
-
Verify the
__reana.job
table schema after downgrading the migration.kubectl exec -ti deployment/reana-db -- psql -U reana
psql (9.6.2) Type "help" for help. reana=# \d __reana.job Table "__reana.job" Column | Type | Modifiers --------------------+-----------------------------+----------- ... 👀 NO FINISHED_AT AND STARTED_AT! ... reana=# \q
-
Upgrade the Alembic migration back to the head version (
4801b98f6408
).kubectl exec deployment/reana-server -- reana-db alembic upgrade
INFO [alembic.runtime.migration] Context impl PostgresqlImpl. INFO [alembic.runtime.migration] Will assume transactional DDL. INFO [alembic.runtime.migration] Running upgrade ad93dae04483 -> 4801b98f6408, Job started and finished times.
-
Verify the
__reana.job
table schema after upgrading the migration.kubectl exec -ti deployment/reana-db -- psql -U reana
psql (9.6.2) Type "help" for help. reana=# \d __reana.job Table "__reana.job" Column | Type | Modifiers --------------------+-----------------------------+----------- ... finished_at | timestamp without time zone | started_at | timestamp without time zone | ... reana=# \q
Congratulations! You've successfully generated an Alembic migration for your database changes and thoroughly tested it to ensure everything works as expected.
REANA reproducible analysis platform
blog.reana.io | docs.reana.io | forum.reana.io | www.reana.io |
@gitter | @mattermost | @twitter
Introduction
Getting started
- Setting up your system
- Cloning sources
- Using production-like development mode
- Using live-code-reload and debug mode
Issue lifecycle
Understanding code base
Technology tips and tricks
- Tips for Docker
- Tips for Git
- Tips for GitLab
- Tips for Keycloak
- Tips for Kind
- Tips for Kubernetes
- Tips for OpenAPI
- Tips for PostgreSQL
- Tips for Python
- Tips for RabbitMQ
- Tips for SQLAlchemy