from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from epsilon.models.meta import metadata
target_metadata = metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
schema_names = config.get_main_option('schemas')
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
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 schema_names.split():
conn = connection.execution_options(schema_translate_map={None:
schema_name})
print("Migrating schema %s" % schema_name)
context.configure(
connection=conn,
target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
четверг, 30 августа 2018 г., 16:09:35 UTC+3 пользователь Mike Bayer написал:
>
> On Thu, Aug 30, 2018 at 7:11 AM, sector119 <[email protected]
> <javascript:>> wrote:
> > Mike, but in run_migrations_online() I use conn =
> > connection.execution_options(schema_translate_map={None: schema_name})
> > But I get no schemas at resulting alembic/versions/file.py
>
> can you share your env.py
>
> >
> >
> > среда, 29 августа 2018 г., 20:46:07 UTC+3 пользователь Mike Bayer
> написал:
> >>
> >> On Wed, Aug 29, 2018 at 5:12 AM, sector119 <[email protected]> wrote:
> >> > Hello
> >> >
> >> > I have N schemas with the same set of tables, 1 system schema with
> >> > users,
> >> > groups, ... tables and 6 schemas with streets, organizations,
> >> > transactions,
> >> > ... tables.
> >> > On those schemas tables I don't set __table_args__ = ({'schema':
> >> > SCHEMA},)
> >> > I just call dbsession.execute('SET search_path TO system, %s' %
> SCHEMA)
> >> > before sql queries.
> >> >
> >> > When I make some changes in my model structures I want to refactor
> table
> >> > in
> >> > all schemas using Alembic, how can I do that?
> >> > Maybe I can make some loop over my schemas somewhere?
> >>
> >> setting the search path is going to confuse SQLAlchemy's table
> >> reflection process, such that it assumes a Table of a certain schema
> >> does not require a "schema" argument, because it is already in the
> >> search path.
> >>
> >> Keep the search path set to "public", see
> >>
> >>
> http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#remote-schema-table-introspection-and-postgresql-search-path.
>
>
> >> There is an option to change this behavior mentioned in that
> >> section called postgresql_ignore_search_path, however it isn't
> >> guaranteed to suit all use cases. if that makes your case work, then
> >> that would be all you need. if not, then read on...
> >>
> >> For the officially supported way to do this, you want to have the
> >> explicit schema name inside the SQL - but this can be automated for a
> >> multi-tenancy application. Use the schema translation map feature:
> >>
> >>
> http://docs.sqlalchemy.org/en/latest/core/connections.html?highlight=execution_options#schema-translating.
>
>
> >>
> >>
> >> >
> >> >
> >> > Thanks
> >> >
> >> > --
> >> > 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 post to this group, send email to [email protected].
> >> > Visit this group at https://groups.google.com/group/sqlalchemy.
> >> > For more options, visit https://groups.google.com/d/optout.
> >
> > --
> > 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] <javascript:>.
> > To post to this group, send email to [email protected]
> <javascript:>.
> > Visit this group at https://groups.google.com/group/sqlalchemy.
> > For more options, visit https://groups.google.com/d/optout.
>
--
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 post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.