Module with implementation of the database backend using SqlAlchemy.
aiida.backends.sqlalchemy.
get_scoped_session
Return a scoped session
According to SQLAlchemy docs, this returns always the same object within a thread, and a different object in a different thread. Moreover, since we update the session class upon forking, different session objects will be used.
kwargs – keyword argument 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()
reset_session
Reset the session which means setting the global engine and session factory instances to None.
Simple wrapper around the alembic command line tool that first loads an AiiDA profile.
aiida.backends.sqlalchemy.manage.
execute_alembic_command
Execute an Alembic CLI command.
command_name – the sub command name
kwargs – parameters to pass to the command
Utilities and configuration of the SqlAlchemy database schema.
aiida.backends.sqlalchemy.manager.
SqlaBackendManager
Bases: aiida.backends.manager.BackendManager
aiida.backends.manager.BackendManager
Class to manage the database schema.
__module__
_load_backend_environment
Load the backend environment.
kwargs – keyword arguments that will be passed on to aiida.backends.sqlalchemy.get_scoped_session().
aiida.backends.sqlalchemy.get_scoped_session()
_migrate_database_version
Migrate the database to the current schema version.
alembic_config
Context manager to return an instance of an Alembic configuration.
The current database connection is added in the attributes property, through which it can then also be retrieved, also in the env.py file, which is run when the database is migrated.
alembic_script
Context manager to return an instance of an Alembic ScriptDirectory.
get_schema_version_code
Return the code schema version.
get_schema_version_database
Return the database schema version.
distutils.version.StrictVersion with schema version of the 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.
migration_context
Context manager to return an instance of an Alembic migration context.
This migration context will have been configured with the current database connection, which allows this context to be used to inspect the contents of the database, such as the current revision.
reset_backend_environment
Reset the backend environment.
set_schema_version_database
Set the database schema version.
version – string with schema version to set
SqlaSettingsManager
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
Module to manage custom queries under SQLA backend.
aiida.backends.sqlalchemy.queries.
SqlaQueryManager
Bases: aiida.backends.general.abstractqueries.AbstractQueryManager
aiida.backends.general.abstractqueries.AbstractQueryManager
SQLAlchemy implementation of custom queries, for efficiency reasons.
__abstractmethods__
_abc_impl
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.
This module contains the codebase for the setUpClass and tearDown methods used internally by the AiidaTestCase. This inherits only from ‘object’ to avoid that it is picked up by the automatic discovery of tests (It shouldn’t, as it risks to destroy the DB if there are not the checks in place, and these are implemented in the AiidaTestCase.
aiida.backends.sqlalchemy.testbase.
SqlAlchemyTests
Bases: aiida.backends.testimplbase.AiidaTestImplementation
aiida.backends.testimplbase.AiidaTestImplementation
Base class to test SQLA-related functionalities.
clean_db
This method fully cleans the DB.
connection
Utility functions specific to the SqlAlchemy backend.
aiida.backends.sqlalchemy.utils.
delete_nodes_and_connections_sqla
Delete all nodes corresponding to pks in the input. :param pks_to_delete: A list, tuple or set of pks that should be deleted.
flag_modified
Wrapper around sqlalchemy.orm.attributes.flag_modified to correctly dereference utils.ModelWrapper
Since SqlAlchemy 1.2.12 (and maybe earlier but not in 1.0.19) the flag_modified function will check that the key is actually present in the instance or it will except. If we pass a model instance, wrapped in the ModelWrapper the call will raise an InvalidRequestError. In this function that wraps the flag_modified of SqlAlchemy, we derefence the model instance if the passed instance is actually wrapped in the ModelWrapper.
get_pg_tc
Return the transitive closure table template
install_tc
Install the transitive closure table with SqlAlchemy.