Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alembic - Example of using package resource as script_location

I am attempting to include Alembic migrations as part of a python package distribution. Since this distribution will be installed, the Alembic scripting directory (which holds the migrations) will end up being copied to the python packages folders. In this case, how can I tell Alembic where to find this directory?

In the Alembic documentation, it says that the migration directory can be specified in the config.ini file as a package reference:

  • script_location - this is the location of the Alembic environment. It is normally specified as a filesystem location, either relative or absolute. If the location is a relative path, it’s interpreted as relative to the current directory.

(snip)

For support of applications that package themselves into .egg files, the value can also be specified as a package resource, in which case resource_filename() is used to find the file (new in 0.2.2). Any non-absolute URI which contains colons is interpreted here as a resource name, rather than a straight filename.

The documentation gives no further information or examples.

Has anyone successfully implemented this? How exactly do you make your migration scripting_folder into a "package resource"? How then do you tell alembic where to find it?

like image 255
David H Avatar asked Sep 11 '14 23:09

David H


People also ask

How do I downgrade my alembic?

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. Hope it's clear enough.

How do I change my alembic head?

Delete (or move to another folder) the specific migration file (in migrations/versions folder). The head will automatically revert to the most recent remaining migration. Using stamp will set the db version value to the specified revision; not alter the head revision number.

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

For instance:

script_location = mypackage:somewhere/inside/it

This requires that you include the migrations inside your distribution: you should look at your MANIFEST.in file, etc.

like image 109
manu Avatar answered Oct 31 '22 22:10

manu