Replies: 4 comments 20 replies
-
the stack overflow question refers to someone who doesn't know if their current migration target is an upgrade or a downgrade, relative to the current state of the database. they of course should look at "alembic history" to see where the database's current version is relative to history, but downgrades are not really a normal part of automated deployments and they are very much separate from "upgrades". your issue seems to be related to deployment, so I dont see how that question applies here. if you have an A->B A->C kind of problem, those sound like branches. if two branches of code have diverging alembic trees, you first merge them into a common merge point and alembic can move to that point. I would check out https://alembic.sqlalchemy.org/en/latest/branches.html |
Beta Was this translation helpful? Give feedback.
-
I hoped it would be obvious from my description, that in my situation, deployment pipeline, which I intend to be the place where alembic migrations are executed, doesn't know whether it is "upgrade" or "downgrade". It just knows the version of our application it needs to deploy and also the version that is currently deployed. So I am looking for a way to be able to apply a necessary sequence of migrations that would bring the database from "old" to "new" version. One of the situations is the need to switch from "A->B" to "A->C", which would involve "downgrade B->A" and then "upgrade A->C". As an extra complication, one image, containing alembic knows only of ["A", "B"] and another one knows only of ["A", "C"]. For prod I mostly expect regular upgrades, however, there is a possibility that we would need to do a roll-back to previous version which would imply doing downgrade. But for dev we need to be able to conveniently switch our single dev cluster between different branches of our app, which would also possibly involve abovementioned db migrations. This seems to be remotely related. |
Beta Was this translation helpful? Give feedback.
-
Digging through Alembic issues found this one: so it seems like this is a recurring idea. It's a shame that it doesn't get traction with Alembic's authors. P.S. Looking at this PR probably gives an idea of complexity of proper implementation of this feature. |
Beta Was this translation helpful? Give feedback.
-
The thing is that deployments of versions that introduce schema changes may not always be done by people developing those feature branches. Let me remind that we have a single shared dev environment where all the devs can deploy. They do not step on each others toes by communicating via messenger. Consider the following scenario:
Similar scenario can be with A, B and C db migrations and a need to switch between feature branches containing B and C (and possibly back). And that switch can be done by Daniel, who just knows of feature branches and knows nothing of migrations. He just knows how to activate the deployment pipeline specifying desired app version. |
Beta Was this translation helpful? Give feedback.
-
In our project, the "D" in "CI/CD" stands for "delivery". The build pipeline produces Docker images which, per this reasonable suggestion also include Alembic migrations. Deployment is managed in GitOps-y way with a dedicated repository describing which image versions are deployed to which environments. It is my intent to perform DB migrations as one of the steps in deployment pipeline. While migrations in production environment are expected to be linear upgrades, for our dev/sandbox environment I expect situations with having to first deploy image version containing "A->B" (let's call it image
v1.feat1
) and then that containing "A->C" (lets call it imagev1.feat2
) migrations. The problem here is thatv1.feat2
knows nothing of migrationB
andv1.feat1
knows nothing of migrationC
, making it impossible to use either of those two images to perform migrations necessary to switch between these two images. Therefore one possible solution I see is the following three-step procedure:The drawback of this solution is that migrations are not going to be performed in a single DB transaction. Other possible solutions are:
alembic/versions/*.py
files are copied into the same directory of the new image.I am not sure whether these alternative solutions would not have any issues with different versions of Alembic (or some other library, or even version of Python) in the "old" and "new" images. These solutions have the benefit of performing all migration steps in a single DB transaction.
Of course, regardless of the approach taken, Alembic would still need a "deploy" (tentative name) command instead of or in addition to "upgrade"/"downgrade".
While targeted at dev environment, the same mechanism would also work for downgrades in production (which are hoped to be exceptional situations).
This may be already a solved problem, but questions like this make me doubt it, and I would also expect to find something in the cookbook.
Beta Was this translation helpful? Give feedback.
All reactions