alembic icon indicating copy to clipboard operation
alembic copied to clipboard

add "recursive_version_locations = [false|true]" alembic.ini config

Open PMLP-novo opened this issue 4 years ago • 5 comments

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

PMLP-novo avatar Nov 27 '20 10:11 PMLP-novo

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

zzzeek avatar Nov 27 '20 17:11 zzzeek

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.

PMLP-novo avatar Dec 02 '20 08:12 PMLP-novo

where would the file go when one says "alembic revision" ?

zzzeek avatar Dec 02 '20 13:12 zzzeek

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.

PMLP-novo avatar Dec 11 '20 13:12 PMLP-novo

new files will just go into the folder as they do now. the option indicates to read the directories recursively and that's it.

zzzeek avatar Dec 11 '20 13:12 zzzeek

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

sqla-tester avatar Feb 21 '23 14:02 sqla-tester

A better solution for this problem would have been to add support for squashing migrations

TurnrDev avatar Mar 24 '23 10:03 TurnrDev

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

CaselIT avatar Mar 24 '23 11:03 CaselIT