I have multiple databases, I am using alembic to maintain the migrations for these database.
However I am currently using older version of alembic (Version 1.6.5)
I am trying to upgrade to a latest version of alembic as it supports multi-tenancy.
This is what my env.py file looks like
from __future__ import with_statement
import logging
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
from new_models import managedObjects
from new_models import ignoreObjects
# 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)
logger = logging.getLogger('alembic.env')
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from flask import current_app
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
target_metadata = current_app.extensions['migrate'].db.metadata
print("target", target_metadata)
# print('printing metadata', target_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.
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.
"""
def include_object(object, name, type_, reflected, compare_to):
# print('compare to', compare_to)
# print('object', object)
# print('type', type_)
# if hasattr(object, 'schema'):
# print(object.schema)
print('_________________')
print('obj', str(object), '--', str(object).split('.')[0], '--', obj)
if any(((obj == str(object)) | (obj == str(object).split('.')[0])) for obj in managedObjects):
if any((iobj == str(object)) for iobj in ignoreObjects):
print('Ignoring Object')
return False
# print('including', object.schema)
print('including')
return True
print('not including')
return False
url = config.get_main_option("sqlalchemy.url")
print("Got here!")
context.configure(
include_object=include_object,
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.
"""
# this callback is used to prevent an auto-migration from being generated
# when there are no changes to the schema
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
def process_revision_directives(context, revision, directives):
if getattr(config.cmd_opts, 'autogenerate', False):
script = directives[0]
if script.upgrade_ops.is_empty():
directives[:] = []
logger.info('No changes in schema detected.')
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool,
)
def include_object(object, name, type_, reflected, compare_to):
print('compare to', compare_to)
print('object', object)
print('type', type_)
print("managed objects", managedObjects)
if hasattr(object, 'schema'):
print(object.schema)
print('_________________')
print('obj', str(object), '|', str(object).split('.')[0])
if any(((obj == str(object)) | (obj == str(object).split('.')[0])) for obj in managedObjects):
if any((iobj == str(object)) for iobj in ignoreObjects):
print('Ignoring Object')
return False
# print('including', object.schema)
print('including')
return True
print('not including')
return False
with connectable.connect() as connection:
context.configure(
include_schemas=False,
# include_object=include_object,
connection=connection,
target_metadata=target_metadata,
process_revision_directives=process_revision_directives,
**current_app.extensions['migrate'].configure_args
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
When I run the following code it takes forever to complete as it's comparing against every single schema in my DB.
I.e the function include_object
seems to be comparing against every single schema in my DB.
How can I make it so at least for testing purposes so that alembic doesn't look into every single schema.
I.e
I have a table called 'User', 'Roles'
I want alembic to be only comparing changes against User table only.
as of now it appears to scan against User and Role both tables. My practical example includes tons of other tables, hence running this script takes forever.
The parameter object
in function include_object
is what I am referring to. How can I make sure it only does it against User table?