-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Centralize get_session() for backend QueryBuilders #3676
[WIP] Centralize get_session() for backend QueryBuilders #3676
Conversation
Extend the current reset backend functionality to also be valid for the django backend. Centralize common static methods.
aiida/backends/manager.py
Outdated
@abc.abstractmethod | ||
def reset_backend_environment(self): | ||
@staticmethod | ||
def reset_backend_environment(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be removed from the django specific implementation. This method is for actually resetting the entire database environment, i.e. what is done by _load_backend_environment
. The fact that we use SqlAlchemy for the query builder even for the Django environment is an implementation detail of the query builder not the whole database backend as a whole. So this is also not the correct place to reset the SqlA session for the query builder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method never existed for django
, but was empty. But you're saying that having the method actually doing something for the django
backend is wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And whether I reset the session for the QB or not shouldn't matter? If I want to reset the session, for any reason, I guess the place to do this should indeed be on the backend level, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method is intended to reset the database environment for that specific backend. This is necessary if you want to switch profile in the same interpreter. When you load a profile, as soon as the database backend is needed the _load_backend_environment
is called. In the case of Django this will populate the aiida.backends.djsite.settings
module with the correct database connection parameters and call django.setup
. If you switch profile with another database, you have to "undo" i.e. reset this and do it again for the new profile settings. For SqlAlchemy this loading and resetting is simply the setting and resetting of the session.
Currently, the switching of profiles is not yet supported, which is why reset_backend_environment
is not yet implemented for Django, because I haven't looked into it yet on how to do it. However, that doesn't mean it should be used for resetting the SqlAlchemy session when the querybuilder wants to. This is an implementation detail of the query builder and so should be performed there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! Thank you for the detailed explanation. I will find a work-around, specifically for the QB.
Instead of resetting the backend, calling the function |
Just to be sure (I'm not sure it's clear): the sole purpose of
I don't think it should be (re)used for other purposes. By the way, I realise now that probably:
I'm adding this comments also to #2813 My other comment is: should we really reuse logic from the sqlalchemy backend, importing it from the django code? Is this what we do elsewhere? |
That makes sense. I also could not understand why you passed an engine without using it.
Yup.
For the QueryBuilder, yes. Since it is based on SQLA, this makes sense in that context. There is no django-specific thing that needs to be done, it is indeed only the QB-used session(s), and attachments to it that needs to be reset.
That's fine, but know that it will be the exact same as for the SQLA backend, since again, it is the SQLA that is used for the QB. |
The reason why this still may make sense, is for conceptual reasons. |
I think design wise we should take a step back and see what the various requirements are.
If the Now I remember that having the We can discuss and work this further out in person if you like in case I have missed any crucial limitations |
@sphuber, you're hitting some issues we have already turned over several times, and things both @szoupanos, @giovannipizzi and I have tried to consider and test. To me, it seems it indeed depends on a question of design. Having QB handle a session/having the user handle it, is not as easy as it seems, due to the intricacies of how AiiDA otherwise uses sessions. So in the end, I agree, let's sit down and go over how it may be done, meeting all of our various requirements. |
@CasperWA just FYI I have a working branch that tackles this problem and others. I haven't opened a PR yet because I am not sure yet about certain things, but maybe I should already open it to start discussion? |
Would be good. It would already supersede this PR and may also supersede @szoupanos' #3655. In any case, it would be good to have the diff online to be able to discuss. |
Closing in favor of #3708 |
Fixes #3645
Extend the current reset backend functionality to also be valid for the django backend.
Centralize common static methods in
Backend
andBackendQueryBuilder
.