Module with implementation of the database backend using Django.
aiida.backends.djsite.
get_scoped_session
Return a scoped session for the given profile that is exclusively to be used for the QueryBuilder.
Since the QueryBuilder implementation uses SqlAlchemy to map the query onto the models in order to generate the SQL to be sent to the database, it requires a session, which is an sqlalchemy.orm.session.Session instance. The only purpose is for SqlAlchemy to be able to connect to the database perform the query and retrieve the results. Even the Django backend implementation will use SqlAlchemy for its QueryBuilder and so also needs an SqlA session. It is important that we do not reuse the scoped session factory in the SqlAlchemy implementation, because that runs the risk of cross-talk once profiles can be switched dynamically in a single python interpreter. Therefore the Django implementation of the QueryBuilder should keep its own SqlAlchemy engine and scoped session factory instances that are used to provide the query builder with a session.
sqlalchemy.orm.session.Session
kwargs – keyword arguments that will be passed on to aiida.backends.utils.create_sqlalchemy_engine(), opening the possibility to change QueuePool time outs and more. See https://docs.sqlalchemy.org/en/13/core/engines.html?highlight=create_engine#sqlalchemy.create_engine for more info.
aiida.backends.utils.create_sqlalchemy_engine()
sqlalchemy.orm.session.Session instance with engine configured for the given profile.
reset_session
Reset the session which means setting the global engine and session factory instances to None.
Simple wrapper around Django’s manage.py CLI script.
Utilities and configuration of the Django database schema.
aiida.backends.djsite.manager.
DjangoBackendManager
Bases: aiida.backends.manager.BackendManager
aiida.backends.manager.BackendManager
Class to manage the database schema.
__module__
_load_backend_environment
Load the backend environment.
The scoped session is needed for the QueryBuilder only.
kwargs – keyword arguments that will be passed on to aiida.backends.djsite.get_scoped_session().
aiida.backends.djsite.get_scoped_session()
_migrate_database_generation
Reset the database schema generation.
For Django we also have to clear the django_migrations table that contains a history of all applied migrations. After clearing it, we reinsert the name of the new initial schema .
_migrate_database_version
Migrate the database to the current schema version.
get_schema_generation_database
Return the database schema version.
distutils.version.StrictVersion with schema version of the database
get_schema_version_code
Return the code schema version.
get_schema_version_database
get_schema_version_reset
Return schema version the database should have to be able to automatically reset to code schema generation.
schema_generation_code – the schema generation of the code.
schema version
get_settings_manager
Return an instance of the SettingsManager.
SettingsManager
is_database_schema_ahead
Determine whether the database schema version is ahead of the code schema version.
Warning
this will not check whether the schema generations are equal
boolean, True if the database schema version is ahead of the code schema version.
reset_backend_environment
Reset the backend environment.
set_schema_version_database
Set the database schema version.
version – string with schema version to set
DjangoSettingsManager
Bases: aiida.backends.manager.SettingsManager
aiida.backends.manager.SettingsManager
Class to get, set and delete settings from the DbSettings table.
delete
Delete the setting with the given key.
key – the key identifying the setting
~aiida.common.exceptions.NotExistent if the settings does not exist
get
Return the setting with the given key.
Setting
set
Return the settings with the given key.
value – the value for the setting
description – optional setting description
table_name
validate_table_existence
Verify that the DbSetting table actually exists.
~aiida.common.exceptions.NotExistent if the settings table does not exist
Django query backend.
aiida.backends.djsite.queries.
DjangoQueryManager
Bases: aiida.backends.general.abstractqueries.AbstractQueryManager
aiida.backends.general.abstractqueries.AbstractQueryManager
Object that mananges the Django queries.
__abstractmethods__
_abc_impl
get_bands_and_parents_structure
Returns bands and closest parent structure.
get_creation_statistics
Return a dictionary with the statistics of node creation, summarized by day, optimized for the Django backend.
Days when no nodes were created are not present in the returned ctime_by_day dictionary.
user_pk – If None (default), return statistics for all users. If user pk is specified, return only the statistics for the given user.
a dictionary as follows:
{ "total": TOTAL_NUM_OF_NODES, "types": {TYPESTRING1: count, TYPESTRING2: count, ...}, "ctime_by_day": {'YYYY-MMM-DD': count, ...}
where in ctime_by_day the key is a string in the format ‘YYYY-MM-DD’ and the value is an integer with the number of nodes created that day.
query_group
Subselect to filter data nodes by their group.
q_object – a query object
args – a namespace with parsed command line parameters.
query_past_days
Subselect to filter data nodes by their age.
get_closest_parents
Get the closest parents dbnodes of a set of nodes.
pks – one pk or an iterable of pks of nodes
chunk_size – we chunk the pks into groups of this size, to optimize the speed (default=50)
print_progress – print the the progression if True (default=False).
args – additional query parameters
kwargs – additional query parameters
a dictionary of the form pk1: pk of closest parent of node with pk1, pk2: pk of closest parent of node with pk2
Note
It works also if pks is a list of nodes rather than their pks
Django settings for the AiiDA project.
Utility functions specific to the Django backend.
aiida.backends.djsite.utils.
delete_nodes_and_connections_django
Delete all nodes corresponding to pks in the input.
pks_to_delete – A list, tuple or set of pks that should be deleted.