Skip to content Skip to sidebar Skip to footer

Perform Alembic Upgrade In Multiple Schemas

I am using flask + sqlalchemy + alembic + postgresql, and am trying to find a simple architecture to solve the following problem. I have a simple database structure, lets say two t

Solution 1:

What we ended up doing was use sqlalchemy's event mechanism to catch the queries before they are executed and add a prefix to alter the schema :

def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
    schema_name = <Logic to resolve schema name>
    statement = "SET search_path TO '%s'; %s" % (schema_name, statement)
    return statement, parameters
    ......
    (later in the code)
    listen(Engine, 'before_cursor_execute', before_cursor_execute, retval=True)

This way, we can run alembic's migrate several time, making sure the schema_name is resolved correctly each time, and everything works smoothly

Post a Comment for "Perform Alembic Upgrade In Multiple Schemas"