1

I have simple case i.e, one database. At times, I want to generate a revision using different script.py.mako file (or different content). Is it possible to support more that one script.py.mako file and specify in command not to use default script.py.mako file? Or any other suggestion to generate a revision but with content other than one in script.py.mako file?

In Normal case, when I run alembic revision --autogenerate --message "message" command, this generates a revision based on script.py.mako template.

But in some cases I want to use the same command, but with some flag or some indication to generate a revision with different content. And content can be as simple as writing something in database or making an api call.

So how can I use alembic revision --autogenerate command to generate revision based on supplied flag?

Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
  • Thanks for clarifying your question. It's still not perfectly clear what you are trying to accomplish with your flag/modifier. Can you share specific details – for example, normally, add new columns, tables, etc. But with modifier you want to... _(add columns and tables, but with a prefix?)_ – Yaakov Bressler Oct 19 '22 at 13:42

1 Answers1

1

Yes, this is possible, though not recommended. Let me explain. The alembic CLI is designed to point to an .ini file which specifies a configuration, importantly therein, a script_location which points to a directory with migration scripts and a script.py.mako file (which generates these migration scripts).

If you want a 2nd but different script.py.mako file, you have 2 complicated options (neither of which I recommend):

  1. You'll either need to have a separate directory for migrations – which is useful if you have 2 separate databases – but very challenging if you have a single database.
  2. You can execute migrations without the CLI and interact with alembic's API directory, as shown here: Using Alembic API from inside application code and here: Rudimental Schema-Level Multi Tenancy for PostgreSQL Databases. Be warned, this is a daunting task in of itself and requires overwriting default alembic configurations, which goes against how the alembic API is designed. It is possible, however, and for advanced users, perhaps preferred.

Instead, I recommend you utilize conditional migration elements in your migration script (within script.py.mako). This will allow you to perform certain migrations when certain arguments are passed through the CLI.

Lastly, it is possible your question meant to ask how to perform a single migration for a single database within an application which utilizes multiple databases. If this is so, ask an additional Q on SO and tag me in it and I'd be happy to share how to execute that there.

Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
  • Thanks Yaakov for quick & useful tips. Appreciated! With your recommended approach the problem is -x flag is available for "alembic -x data=true upgrade head", not for "alembic revision --autogenerate" command. What I am looking for is to generate revision content based on passed argument during autogenerate step. Any idea? – anonymous anonymous Oct 16 '22 at 15:58
  • Certainly doable. Why don't you take a few minutes to update your Q and add very specific details about what you're trying to do. Note, migrations are complex tasks in of themselves, so extra information is going to be super helpful. @anonymousanonymous – Yaakov Bressler Oct 16 '22 at 17:08
  • @Yaakov...added more details in question. Thx! – anonymous anonymous Oct 16 '22 at 18:24
  • 1
    Hi @Yaakov Bressler, any idea? – anonymous anonymous Oct 17 '22 at 15:24
  • I moved this thread to the comments in your Q. :) @anonymousanonymous – Yaakov Bressler Oct 19 '22 at 13:42