alembic
alembic copied to clipboard
add "recursive_version_locations = [false|true]" alembic.ini config
Describe your question I have an applications where the number of migrations we have done goes past 50. So I would like to organize the migrations into a subfolder system inside the alembic versions. When I try to just move the files into subfolders it does not work out of the box any suggestions on how to do this or is this a feature request?
Example of a folder structure
alembic/versions └───version_0 │ └───relaease_1 │ │ 439873242343_feature_bla_bla.py │ │ 43987324fwaer_feature_blu_blu.py └───version_1 │ └───relaease_2 │ │ 439873243243_feature_bi_foo.py │ │ 43987324ewraer_feature_we_br.py
multiple directories can be set in the config file by naming them separated by a space with the version_locations
directive:
https://alembic.sqlalchemy.org/en/latest/branches.html#setting-up-multiple-version-directories
alembic.ini
version_locations = %(here)/alembic/versions/version_0/release_1 %(here)/alembic/versions/version_1/release/2
when you do this, the "alembic revision" command when asked to create a new file will now require that you also specify --version-path otherwise it won't know where to put the file.
in order to make the above scheme dynamic you would need to build a custom front-end that calls into the Alembic commands with a customized config object: https://alembic.sqlalchemy.org/en/latest/api/commands.html
Thank you very much for the answer. It make sense just wondered if it could be a potential feature to be able to specify an option in the alembic.ini
recursivly_search_for_versions=true
So that when set to true. You would search all subfolders for versions. I don't know if it is difficult to make or how much value it would give others. But atleast in my mind it seems like a prettier and easier to maintain solution than the suggested solution.
where would the file go when one says "alembic revision" ?
Either just add it to alembic_versions and then people could just move it manually. Or alternatively you could specify alembic revision -m "version_0/release_1/my new feature"
or as a new argument alembic revision -m "my new feature" -sub-dir "version_0/release_1"
but I don't know if it is posible or fits best in the current api.
new files will just go into the folder as they do now. the option indicates to read the directories recursively and that's it.
ostr00000 has proposed a fix for this issue in the main branch:
add recursive_version_locations option for searching revision files https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4457
A better solution for this problem would have been to add support for squashing migrations
I don't think they are equivalent, especially if transaction_per_migration
is used https://alembic.sqlalchemy.org/en/latest/api/runtime.html#alembic.runtime.environment.EnvironmentContext.configure.params.transaction_per_migration
also the normal order resolution apply, so each folder is not run in order