I've spent a long time evaluating this use case and there are a lot of
ways to do it, but ultimately you are saying each schema is identical,
and originally, you just wanted to set "search_path" to each schema,
which is probably how this should be done if each schema is
independent of each other and has the identical tables.  This means
that when you autogenerate, you should only "autogenerate" for the
first schema since they are all identical.   Then when migrations run,
you don't need to have "schema" set in your create_table because you
are setting search path, that will be the "default" schema.

for that exact use case, this works:

def run_migrations_online():
    connectable = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix='sqlalchemy.',
        poolclass=pool.NullPool)

    with connectable.connect() as connection:
        for schema_name in ['test_schema', 'test_schema_2']:
            connection.execute("SET search_path TO %s" % schema_name)
            connection.dialect.default_schema_name = schema_name

            context.configure(
                connection=connection,
                target_metadata=target_metadata
            )

            with context.begin_transaction():
                context.run_migrations()

            if context.config.cmd_opts.cmd[0].__name__ == 'revision':
                break

note I had to set the default_schema_name on the dialect since we are
changing the search path without re-initializing an engine.

if you want individual migration sections for each schema, with or
without "schema" written in, there's ways to do all that also but that
doesn't seem necessary if you are sharing a single model with multiple
identical schemas.

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to