romanzdk
romanzdk

Reputation: 1332

Can I attach migrations to a corresponding data warehouse model using SQLAlchemy and Alembic?

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

Upvotes: 0

Views: 113

Answers (1)

Ian Wilson
Ian Wilson

Reputation: 9099

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.

Upvotes: 1

Related Questions