Alembic migrations for multiple schemas with identical models #1713
-
I have a PostgreSQL database with multiple schemas, and each schema has the same set of models (same tables/columns). In my application, I generate Alembic migrations from these models. Since the models are the same across schemas, I want the migration files to contain a placeholder for the schema name instead of hardcoding one schema. At runtime, when I run alembic upgrade, I want to pass the target schema name through an environment variable (e.g., TENANT_SCHEMA=mytenant) so the migration is applied to that specific schema. So the workflow I’m looking for is: Question: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi, To provide the arguments to alembic, you can use either of
You can also refer to https://alembic.sqlalchemy.org/en/latest/cookbook.html#rudimental-schema-level-multi-tenancy-for-postgresql-mysql-other-databases for an example |
Beta Was this translation helpful? Give feedback.
-
Thanks for reply . |
Beta Was this translation helpful? Give feedback.
Hi,
To provide the arguments to alembic, you can use either of
You can also refer to https://alembic.sqlalchemy.org/en/latest/cookbook.html#rudimental-schema-level-multi-tenancy-for-postgresql-mysql-other-databases for an example
and to the multidb template https://github.com/sqlalchemy/alembic/tree/main/alembic/templates/multidb for an example of how to pass customer parameter to upgrade and downgrade functions (that in your case could the schema, …