influitive / apartment

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

persistent_schemas concept not understandable #613

Open estani opened 4 years ago

estani commented 4 years ago

I had this config line:

 config.persistent_schemas = %w{ mng }

My expectation was that the mng schema will be added to the search path and nothing more.

What happens ist that just that line caused that when a new tenant schema is created, the tables in mng get wiped. (I think I saw them in the user schema, but I might be wrong).

I've remove it and now everything works as expected. Am I misunderstanding the docs?

...Enter persistent_schemas. You can configure a list of other schemas that will always remain in the search path, while the default gets swapped out

byted commented 4 years ago

Had the same issue, looked into it and it functions as expected: config.persistent_schemas values are added to the search_path, so the path looks like search_path = <new-tenant>,mng. During tenant creation, a DROP TABLE ... command is issued after the new schema was created and before the tables are created in the new schema. Since the table to drop is not yet in the new schema, the next schema is checked for a table with this name and voila, it drops the tables with the same name in mng

raysuelzer commented 4 years ago

That's really bad. How did you get around it deleting your database?

byted commented 4 years ago

We switched to using config.use_sql = true which uses a different approach on creating the schema and doesn't have the drop table issue.