influitive / apartment

Database multi-tenancy for Rack (and Rails) applications
2.66k stars 464 forks source link

[feature suggestion] automatically generate reporting views with all tenants data #616

Open DanielHeath opened 4 years ago

DanielHeath commented 4 years ago

Happy to do the work on this if it's a desirable feature, but not if it's unlikely to get merged.

I need to run some reporting over all tenants. A feature that might make this easier would be to generate a view which produces a view like:

select *, 'tenant_one' as tenant from tenant_one.table_name 
UNION 
select *, 'tenant_two' as tenant from tenant_two.table_name
UNION
...

If I can get my tenants to all share one sequence for ID generation, the 'tenant' column probably isn't needed.

The goal would be to automatically update each of these views whenever either the schema or the list of tenants are modified, so you can always query all_tenants.table_name to see unified data.