Using Alembic with multiple tenants, each being different version, but same DB #978
-
Hi team, first of all, thanks for the great product. I know that Alembic isn't designed for my use case, and generally the use case seems to be rare, but I wonder if there's a logical approach to such an issue. Use case: The problem arises when each tenant expects a different schema from the same DB. This mostly can happen when there's a long running instance of an outdated version of the client code, and a new instance is running with a newer code that has a new revision. Approach: Other approaches that I though about were over complicated and it seems like changing the architecture/environment would be a better solution. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
if these tenants are sharing the same tables, then in order to support tenants on older versions of the code that refers to older versions of the schema, you have to implement a gradual versioning approach to your whole application where you never drop any columns or tables until all tenants have been migrated. application code that is newer also needs to be able to apply data to the "old" columns at runtime if there is data between these tenants that needs to be shared. overall this is certainly a very major undertaking and it will severely limit how easily you can make schema changes to your database and to your application, as both will always need to be operating against a past version of the schema and a future one at the same time. Nevertheless this approach is done in the real world including for some Openstack projects like Openstack Nova, to suit the approach of being able to upgrade the software on multiple nodes while the whole thing keeps running. this is not quite the same thing as "tenants" though. if OTOH your multitenant approach involves each tenant using a different sub-schema, like a PostgreSQL schema, then you'd version the schemas separately. The usual approach for a true multi-"tenant" system with remote users accessing some common source of data is to not grant direct database access, and to instead provide RESTful API access with versioning. that way you just keep providing api/v1, api/v2, etc. and you can easily migrate your schema without any risk introduced by the multi-tenant aspect of it. |
Beta Was this translation helpful? Give feedback.
if these tenants are sharing the same tables, then in order to support tenants on older versions of the code that refers to older versions of the schema, you have to implement a gradual versioning approach to your whole application where you never drop any columns or tables until all tenants have been migrated. application code that is newer also needs to be able to apply data to the "old" columns at runtime if there is data between these tenants that needs to be shared.
overall this is certainly a very major undertaking and it will severely limit how easily you can make schema changes to your database and to your application, as both will always need to be operating against a past versio…