Replies: 7 comments
-
What database URL is in the alembic.ini and how is that hardcoded like that? Does your application have its own config file that users are configuring where the database goes ? If your application needs to run alembic upgrades I'd assume your users configure the path to their database in some other config file, so assuming that's the case you can put the alembic config there as well, however, in general what applications usually do is they make their own front-end script or command instead of having users run alembic directly. that's what the command api is for. |
Beta Was this translation helpful? Give feedback.
-
The DB URL is stored by the user in an environment variable read in env.py. I think I got this from a tutorial from Miguel Grinberg. I may have modifed things so if anything there is fishy, blame me first. # this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
logger = logging.getLogger("alembic")
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
DB_URL = os.getenv("SQLALCHEMY_DATABASE_URI")
if DB_URL is None:
logger.error("SQLALCHEMY_DATABASE_URI environment variable not set.")
sys.exit()
db.set_db_url(DB_URL)
config.set_main_option("sqlalchemy.url", str(db.url).replace("%", "%%"))
target_metadata = Base.metadata (The sqlalchemy.url contains an unmodified dummy URL I could probably remove. Honestly I copied the tutorial and was happy to get things working with the modification above in env.py.)
Indeed, rather than asking users to set up an env var with the DB URL, I could provide a template config file and ask them to edit it and set the path to that file in an env var. I'd do this already if I had several configuration parameters. I could try to add logic in that config file to define the path to the alembic.ini file, but I don't think that will help with calling the alembic command directly.
Oh, so that's the way people do. I assumed it was better to expose alembic (widely used and documented command) rather than add a custom layer above it to expose (even a chosen subset of) its features. Now I'm afraid I chose an unbeaten path that may lead to trouble... One thing I could do as a workaround is just write a command the user would execute in its virtualenv that would export the absolute file path to my_app.alembic.ini into e.g.
It is just one more step. I think the change I propose in OP would solve this but I won't insist about it, especially if it is useful only to cover my corner case. Thank you for your quick reply. Much appreciated. |
Beta Was this translation helpful? Give feedback.
-
I would think users using your library jut want to "myapp upgrade" and not really get into all the other alembic revision commands, so in that sense it may be seen as a more friendly facade |
Beta Was this translation helpful? Give feedback.
-
You're probably right. In practice I don't expect many commands to be needed indeed. I could expose I guess I wanted to make things simple for myself avoiding adding a layer that seemed unnecessary but now I get your point of view. Besides it's only a few commands to add. Thanks again. Feel free to close if you think this proposal is not useful. I still think it might make sense if only for me as a dev to use alembic on our deployments without the need for a custom command. But I'm not the one coding and maintaining the feature so I won't complain. |
Beta Was this translation helpful? Give feedback.
-
oh you made it an issue. will put it in "ideas" |
Beta Was this translation helpful? Give feedback.
-
@zzzeek @lafrech I am having the same problem,
above is my directory structure. they would provide their own database credentials in .env file and i want to pass it to alembic and run the migration. When i do db_current
db_downgrade or db_upgrade nothing happens My code in exactly like @lafrech
I set my db url from os.getenv() inside env.py can you please give me hint what could be wrong here. |
Beta Was this translation helpful? Give feedback.
-
@zzzeek @lafrech Finally I have solved it thank you very much for this awesome package, and @lafrech for hinting the clear way to do this. you guys are absolute rock star. |
Beta Was this translation helpful? Give feedback.
-
Hi.
I'm trying to distribute alembic migration files with my application. I want the user to basically
pip install my_app
thenalembic upgrade head
.I can put the migration files directory inside the app along with alembic.ini (and modify setup.py and MANIFEST.in accordingly), but then, AFAIU, the user will need to
We could improve this by allowing the user to do
The package:file syntax is inspired by Flask but with different semantics. Well, perhaps:
is wrong since it is a valid character in a filename. The point is we need to be able to discriminate a file system path and a package path. We could even use a different config flag.I think this is how
script_location
accepts package resources (tutorial).The option could accept a package and assume alembic.ini in package root. But then again, to discriminate a package name from a relative file name, we might have to use a different config flag.
I've seen people discussing on SO about distributing migration files, but I couldn't find the answer to the alembic.ini path issue. I'm pretty sure there's an already existing and recommended way to do this. In this case, I'd be more than happy to get a pointer, as I've been searching for hours. But I don't mean to turn this into a support request.
In any case, I figured the idea above could be worth sharing.
Have a nice day!
Beta Was this translation helpful? Give feedback.
All reactions