I have a Postgres server which we have one database in. This one database is our data warehouse. We have a schema per software application in this database.
I'm working on a new project which I'm using sqlalchemy's alembic to create the schema migrations. However, because of the way my DB is setup... it looks like the --autogenerate option of the revision generator is scanning all the schemas in the database.
I can't find an option to restrict the inspection to only one schema. The only option I found was to create a function to pass into the inclue_object parameter in the alembic context. So alembic will scan all the schemas but will only use the schema/tables if that function returns true. This is less than ideal because I have hundreds of tables... so this process is slow.
def include_object(object, name, type_, reflected, compare_to):
print(object, name, type_, reflected, compare_to)
if type_ == 'table' and object.schema != 'leads_manager':
print('returning false')
return False
else:
print('returning true')
return True
def run_migrations_offline():
url = get_db_uri()
context.configure(
url=url,
target_metadata=target_metadata,
include_object=include_object,
)
with context.begin_transaction():
context.execute('SET search_path TO leads_manager')
context.run_migrations()
Anyone know how to restrict alembic autogenerate to only one schema in postgres?
I ended up creating a user that only has access to my schema of interest. Alembic --autogenerate then only inspects that schema since it doesn't have access to any others.