Replies: 21 comments 14 replies
-
This is a great idea. Although the Database allows to have the connections and manage them as well, having this seemless integrated is a good idea. While I investigate how can this happen it would be nice to have a PR with a possible solution and here some possible use cases as example (codewise) too |
Beta Was this translation helpful? Give feedback.
-
So as per my initial investigation, SQLAlchemy Unless you specify the schema in the metadata (via registry = Registry(database=...., schema="my_schema")
registry_public = Registry(database=....) This should also work out of the box for alembic since to run the migrations wou would need to use the Now we can try to simplify this process somehow but for now I hope this helps. |
Beta Was this translation helpful? Give feedback.
-
@rorik302 I am currently testing something that so far looks good. Allowing the queries into different schemas and even different databases is the database object is provided. I have some concerns though. How are you going to manage the migrations of multiple schemas? You can query but I'm actually curious about that. Also, how is Edgy so far working for you? |
Beta Was this translation helpful? Give feedback.
-
I agree. SQLA is very complex and can be not very user friendly. Hence Edgy and Saffier. The good news is that I'm currently testing something and might need some work. This PR #15 already has the "using" and the "using_with_db" So the first is what you initially thought. Example User.query.using("my-schema").all() The second also allows to pass a different database object with the schema name. Example: new_database = Database (...)
User.query.using_with_db(database=database, schema="my-schema") @rorik302 Do you think you can fork edgy and checkout this branch #15 and see if this does what you need? I haven't updated the docs because I'm running some other tests and analysis but the tests I ran, it was working but it would be nice to have this on your side to check as well? |
Beta Was this translation helpful? Give feedback.
-
I wonder how you have so much time to do so much work )). Thank you. I will check the code later |
Beta Was this translation helpful? Give feedback.
-
Well it's in my free time + I also use Saffier and Edgy and Esmerald with my clients :) |
Beta Was this translation helpful? Give feedback.
-
Should migrations work at this stage? |
Beta Was this translation helpful? Give feedback.
-
The migrations are something else. There is another branch that I’m working on. Something that will be similar to this https://django-tenants.readthedocs.io/en/latest/ since I was the one making this https://django-tenants-url.tarsild.io/ (the error is related to the CI which I will fix soon). This is the reason why I asked you how would you manage your migration side. For Edgy will be tricky making one migration per tenant because you might have 3000 tomorrow and 50000 the day after (hence my other PR trying to create a contrib (not core) in Edgy that does something similar to the links shared above). this is very powerful feature like you said that you don’t see anywhere so I might take some time to get there but eventually we will 🙂 |
Beta Was this translation helpful? Give feedback.
-
I suppose that when using |
Beta Was this translation helpful? Give feedback.
-
Ok, can you provide me with more concrete examples like folder structure and a file example? |
Beta Was this translation helpful? Give feedback.
-
@rorik302 I think I'm understanding what you are sugesting. I need to have a deep look. Btw I merged something into the PR I told you to have a look. Its cleaner and simpler but I also added a "contrib", which means its not the core, just a suggestion from edgy that is similar to https://github.com/django-tenants/django-tenants/ . Which is the approach you and I are talking. See the way I did the tests -> https://github.com/tarsil/edgy/pull/15/files#diff-7a5a06491a50457971419953b7a6b736aaf34bf8a5053163514ab916e6214734 Proves that the schemas if generated via contrib, work as expected. Of course the Edgy core with the `Model.query.using(...)' its agnostic to the contrib which is the introduction of the support to multi tenancy. The contrib is a nice to have in general :) Still a lot to do |
Beta Was this translation helpful? Give feedback.
-
I found an issue. Seems edgy uses public schema by default. When I define schema in Registry, edgy uses public schema. If I delete public schema and call |
Beta Was this translation helpful? Give feedback.
-
So, no, that is not an issue. By default of no schema is specified, ok n the registry (Param schema), SQLAlchemy will use the default schema of each supported database. In case of postgres it's called public, in SQL server its called dbo and so on so forth. The public is what you called before the "shared". Unless I'm not understanding what you meant without an example. Registry with a schema its like this Registry(database=database, schema="another") |
Beta Was this translation helpful? Give feedback.
-
Another good news it's that will be the availablity of a set_tenant() that will make your queries global to the tenant. Meaning? If you do a set_tenant you won't need to use the using(). This can be particularly useful if you have a Middleware Somewhere in your application that sets the tenant before reaching the API and therefore queries only the tenant data. It's now in development and should be in branch soon |
Beta Was this translation helpful? Give feedback.
-
Ok, great progress. Now pushed to the repo but basically the |
Beta Was this translation helpful? Give feedback.
-
One more question. I have models:
How can I get list of tenants with related users? |
Beta Was this translation helpful? Give feedback.
-
Oh, Do you mean all the tenants and all the users of each tenant? Oh that is model logic. I would add a function in the Tenant like this: async get_tenant_users(self) -> List[User]:
return await User.query.filter(tenant=self) This should do it nicely. The using is now working nicely right? Also the issue you had with the schema is now solved? Btw, thank you for this collaboration. Helped a lot with the design. It's almost done and ready I believe. BUT because you have a FK in the user to the tenant, you can actually use the related field 😎. In the FK, add like this: tenant: Tenant = edgy.ForeignKey(Tenant, on_delete=edgy.CASCADE, related_name="users") Because now you have a related name users inside the FK to the tenant, you can query directly like this: await Tenant.query.filter(users__tenant=self) Nice right? Have a look at the docs. It's covered there :) |
Beta Was this translation helpful? Give feedback.
-
Thank you. Awesome work. |
Beta Was this translation helpful? Give feedback.
-
@rorik302 the branch is now merged into main and this is exciting!! You can get the main as well and continue. I wont yet release because I want to add some extra docs as a nice to give from Edgy. So far, is edgy treating you well? Multi tenancy is coming! :D |
Beta Was this translation helpful? Give feedback.
-
@rorik302 I'm happy to announce that the new release is now out! https://edgy.tarsild.io/tenancy/edgy/ https://edgy.tarsild.io/release-notes/ There is no native support for multi tenant migrations yet because that needs to be properly planned but for now the full multi tenancy support on Edgy is out. Thank you for your help on this and if you are using Saffier. Saffier 0.17.0 is also out also with support for multi tenancy in the same fashion 👍🏼 |
Beta Was this translation helpful? Give feedback.
-
I will be closing this discussion @rorik302 as I think we got what it was initially suggested working well but feel free to tell me otherwise. Good news is that I might have found a simpler way to give support to migrations with Edgy and Saffier and I’m currently working in that as we speak and should be out soon. Awesome collaboration with you @rorik302 . Thank you very much. |
Beta Was this translation helpful? Give feedback.
-
I suggest making it possible for a queryset to change database schemas. For example,
Model.with_db_schema(schema_name).query.all()
. This would be great for multi-tenant applicationsBeta Was this translation helpful? Give feedback.
All reactions