0

Let's say I want to build a data warehouse using these two tools. I was thinking of something like

root
- database_schema
-- table1.py (SQLAlchemy model)
-- table2.py
...
- database_schema2
-- table1.py
...
- alembic

However, alembic is creating all migrations in one folder (versions). Is it possible to "attach" migrations to the corresponding model? So e.g. I would have something like this:

root
- database_schema
-- table1
--- migrations
---- migration1.py (Alembic migration)
--- table1.py (SQLAlchemy model)
-- table2
--- migrations
--- table2.py
...
- database_schema2
-- table1.py
...
- alembic
TylerH
  • 20,799
  • 66
  • 75
  • 101
romanzdk
  • 930
  • 11
  • 30
  • Mixing in your concrete question ("is it possible to attach migrations to the corresponding model?") with the abstract one ("what is the best practice for...?") is liable to get what would otherwise be an answerable and on-topic post if it _only_ included the concrete question closed as overbroad or opinion-based. – Charles Duffy Jan 16 '23 at 17:20
  • Alright, edited – romanzdk Jan 16 '23 at 17:34

1 Answers1

1

It seems there is support for something like this but it looks meant more for large submodules: https://alembic.sqlalchemy.org/en/latest/branches.html#working-with-multiple-bases

Also I wouldn't mix the migrations in with your app code like that. You might want to run different linters/formatters or have other scanning tools that are going to keep getting hung up on the migration files. I would keep them under version control but in a directory parallel to your source code. Also sometimes migrations involve more than 1 table and you'd have this weird ambiguity if you are trying to make them 1-to-1.

You could also apply some crude labeling to the suffix of the revisions, ie. versions/{revision_id}_table1.py. Just do alembic revision -m table1 --autogenerate. I think you can make that suffix whatever you want, and still set the message if you wanted to.

Ian Wilson
  • 6,223
  • 1
  • 16
  • 24