I'm using Alembic with SQL Alchemy. With SQL Alchemy, I tend to follow a pattern where I don't store the connect string with the versioned code. Instead I have file secret.py
that contains any confidential information. I throw this filename in my .gitignore
so it doesn't end up on GitHub.
This pattern works fine, but now I'm getting into using Alembic for migrations. It appears that I cannot hide the connect string. Instead in alembic.ini, you place the connect string as a configuration parameter:
# the 'revision' command, regardless of autogenerate # revision_environment = false sqlalchemy.url = driver://user:pass@localhost/dbname # Logging configuration [loggers] keys = root,sqlalchemy,alembi
I fear I'm going to accidentally commit a file with username/password information for my database. I'd rather store this connect string in a single place and avoid the risk of accidentally committing it to version control.
What options do I have?
Alembic provides for the creation, management, and invocation of change management scripts for a relational database, using SQLAlchemy as the underlying engine. This tutorial will provide a full introduction to the theory and usage of this tool. To begin, make sure Alembic is installed as described at Installation.
Using the above command alembic generate our first migration commit file in versions folder. you can see the version file now in versions folder. Once this file generates we are ready for database migration. Once you run above command your tables will be generated in your database.
Just a note for the answer of Mark Amery: If you want to run to downgrade() of a version, you will need to run alembic downgrade the-version-before-it , which mean it will revert to the version after the version that you want to downgrade. Which is the version before the version that we want to revert.
¶ Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database Toolkit for Python. Front Matter. Project Homepage. Installation.
I had the very same problem yesterday and found a following solution to work. I do the following in alembic/env.py
:
# this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config # this will overwrite the ini-file sqlalchemy.url path # with the path given in the config of the main code import config as ems_config config.set_main_option('sqlalchemy.url', ems_config.config.get('sql', 'database'))
ems_config
is an external module that holds my configuration data.
config.set_main_option(...)
essentially overwrites the sqlalchemy.url
key in the [alembic]
section of the alembic.ini
file. In my configuration I simply leave it black.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With