Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store the alembic connect string outside of alembic.ini?

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?

like image 533
Doug T. Avatar asked Mar 04 '14 17:03

Doug T.


People also ask

What is alembic ini?

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.

How do you use alembic for migration?

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.

How do I downgrade alembic?

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.

What is SQLAlchemy alembic?

¶ Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database Toolkit for Python. Front Matter. Project Homepage. Installation.


1 Answers

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.

like image 133
Tammo Heeren Avatar answered Oct 11 '22 17:10

Tammo Heeren