Replies: 2 comments
-
hi - I would be -1 on an env.py script that uses environment variables, options for how the upgrade should be run are more safely passed to the command line itself. For this purpose we provide two options:
if your build environment uses environment variables, I would connect these to the command line in the shell script where your command runs:
it seems like your use case above would be well suited using the |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for your reply
…On Thu, 30 Jan 2025, 16:56 Michael Bayer, ***@***.***> wrote:
hi -
I would be -1 on an env.py script that uses environment variables, options
for how the upgrade should be run are more safely passed to the command
line itself. For this purpose we provide two options:
1. the --name option
<https://alembic.sqlalchemy.org/en/latest/cookbook.html#run-multiple-alembic-environments-from-one-ini-file>
which can indicate different portions of the alembic.ini file
2. the -x argument
<https://alembic.sqlalchemy.org/en/latest/api/runtime.html#alembic.runtime.environment.EnvironmentContext.get_x_argument>
which can be used to send arbitrary arguments to env.py, such as in
this example where it's used for multi-tenancy
<https://alembic.sqlalchemy.org/en/latest/cookbook.html#rudimental-schema-level-multi-tenancy-for-postgresql-mysql-other-databases>
if your build environment uses environment variables, I would connect
these to the command line in the shell script where your command runs:
alembic -xdatabase_url=${DATABASE_URL} upgrade head
it seems like your use case above would be well suited using the -x
option and as far as examples is an alternative version of what we have for
https://alembic.sqlalchemy.org/en/latest/cookbook.html#run-multiple-alembic-environments-from-one-ini-file
so I can see an example section for "basic argument passing to env.py" if
this helps.
—
Reply to this email directly, view it on GitHub
<#1596 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIQNJAGXIG5U6SHFODBYAKL2NIR7JAVCNFSM6AAAAABWFDHTGGVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEMBQGY3DSOA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hi Alembic team and community,
I’ve been working on a project where I needed to manage database migrations for multiple environments (e.g., development and testing) using Alembic. To achieve this, I implemented a custom env.py script that dynamically selects the appropriate database configuration based on environment variables. I believe this approach could be useful for other developers and would like to propose adding it as an example or guide to the Alembic documentation.
Problem Statement
Many projects require different database configurations for development, testing, and production environments. However, the default env.py script in Alembic does not provide built-in support for switching between these configurations dynamically. This can lead to confusion or errors when running migrations in different environments.
solution
address this, I modified the env.py script to:
Use environment variables (DEV_DATABASE_URL, TEST_DATABASE_URL) to define database URLs for each environment.
Dynamically select the appropriate section (devdb, testdb) based on an ENVIRONMENT variable.
Validate the presence of required configuration options (e.g., sqlalchemy.url) and raise meaningful errors if they are missing.
Benefits
This approach provides several benefits:
env.py
script.Suggestions for Integration
I believe this solution could be valuable to the Alembic community in the following ways:
env.py
.Request for Feedback
I’d love to hear your thoughts on this proposal. Would this be a valuable addition to the Alembic ecosystem? Are there any improvements or alternative approaches you’d suggest?
Thank you for considering this proposal. I’m happy to contribute further if there’s interest in moving forward.
Beta Was this translation helpful? Give feedback.
All reactions