hi there -

usually programmatic customization of Alembic is done via the env.py script 
directly, that is, you would run "alembic revision" normally and it's within 
your local env.py that you can control how target_metadata is achieved.  you 
can also pass custom options to "alembic revision" using the -x argument : 
https://alembic.sqlalchemy.org/en/latest/api/runtime.html#alembic.runtime.environment.EnvironmentContext.get_x_argument
 .   (call this option one)

all of that said, what you are describing is in fact a custom front-end to 
Alembic.    These are also commonplace, and in these cases people usually call 
the command functions directly, as in the examples at 
https://alembic.sqlalchemy.org/en/latest/api/commands.html .

In this latter case, you can pass custom arguments and/or your target_metadata 
into the command using the config.attributes dictionary.  but again, you would 
need to have your env.py script coordinate with this state so that your front 
end creates an alembic.Config(), puts things inside of its .attributes, runs 
command.revision(config), then your env.py pulls data out of config.attributes. 
  (Call this option two).

Finally, the third option where you really don't want env.py to be involved at 
all and you want to directly invoke "revision with autogenerate" is to use the 
autogenerate API directly.   This is more work to implement and has more chance 
of there being backwards incompatible changes at a future date, but you could 
use the API as demonstrated in the source code to command.revision itself: 
https://github.com/sqlalchemy/alembic/blob/master/alembic/command.py#L159 

I would advise sticking with option one or option two, however feel free to 
experiment with any one of them to learn the general flow of things.




On Tue, Jan 5, 2021, at 10:56 AM, Nikola Radovanovic wrote:
> Hi,
> I would like to create set of thin wrappers on top of the alembic since we 
> need some customization. So first thing for me to solve is how to set 
> *target_metadata* programatically. What I realized by reading documentation 
> and code is that I need something like:
> 
> alembic_cfg = Config("alembic.ini")
> alembic_cfg.set_main_option("sqlalchemy.url", 
> "postgres://user:pass@localhost/testdb")
> script = ScriptDirectory.from_config(alembic_cfg)
> 
> engine = create_engine(config.db_uri)
> with engine.connect() as connection:
>     env_ctx = EnvironmentContext(config=alembic_cfg, script=script)
>     env_ctx.configure(connection=connection, target_metadata=Base.metadata)
>     ctx = env_ctx.get_context()
> 
> Next, I want to call *revision* with autogenerate set to True, but I am not 
> sure how?
> 
> Thank you in advance.
> 
> Kindest regards
> 

> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
>  
> http://www.sqlalchemy.org/
>  
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/bed8e0d1-f6c9-4ada-97eb-efc05936d5fan%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/bed8e0d1-f6c9-4ada-97eb-efc05936d5fan%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/2922a3ea-ccb1-40aa-b1ef-7726b5426207%40www.fastmail.com.

Reply via email to