I'm trying to figure out how to add RLS to tables using sqlalchemy and alembic.
I'm changing from schema per tenant to row per tenant in a multi-tenant db.
My approach is:
- use pg setting '
tenant.id' to identify the current tenant
- add tenant column to tables with server_default=func.current_setting('
tenant.id') to automatically set the tenant on insert.
- enable rls on tables: ALTER TABLE table ENABLE ROW LEVEL SECURITY
- create rls policy on tables: CREATE POLICY xxx ON table USING (tenant = current_setting('
tenant.id'))
- set
tenant.id at start of transaction: SET LOCAL
tenant.id = current_tenant;
I don't know how to enable rls or create rls policy on the tables without adding custom sql to the alembic version file.
Is there another/better way to do this?
Thanks,
Brian Hill