aiida.backends package

Module for implementations of database backends.

aiida.backends.get_backend_manager(backend)[source]

Get an instance of the BackendManager for the current backend.

Parameters

backend – the type of the database backend

Returns

BackendManager

Submodules

Module for settings and utilities to determine and set the database schema versions.

class aiida.backends.manager.BackendManager[source]

Bases: object

Class to manage the database schema and environment.

__dict__ = mappingproxy({'__module__': 'aiida.backends.manager', '__doc__': 'Class to manage the database schema and environment.', '_settings_manager': None, 'get_settings_manager': <function BackendManager.get_settings_manager>, 'load_backend_environment': <function BackendManager.load_backend_environment>, '_load_backend_environment': <function BackendManager._load_backend_environment>, 'reset_backend_environment': <function BackendManager.reset_backend_environment>, 'migrate': <function BackendManager.migrate>, '_migrate_database_generation': <function BackendManager._migrate_database_generation>, '_migrate_database_version': <function BackendManager._migrate_database_version>, 'is_database_schema_ahead': <function BackendManager.is_database_schema_ahead>, 'get_schema_version_code': <function BackendManager.get_schema_version_code>, 'get_schema_version_reset': <function BackendManager.get_schema_version_reset>, 'get_schema_version_database': <function BackendManager.get_schema_version_database>, 'set_schema_version_database': <function BackendManager.set_schema_version_database>, 'get_schema_generation_database': <function BackendManager.get_schema_generation_database>, 'set_schema_generation_database': <function BackendManager.set_schema_generation_database>, 'validate_schema': <function BackendManager.validate_schema>, 'validate_schema_generation_for_migration': <function BackendManager.validate_schema_generation_for_migration>, 'validate_schema_version_for_migration': <function BackendManager.validate_schema_version_for_migration>, 'validate_schema_generation': <function BackendManager.validate_schema_generation>, 'validate_schema_version': <function BackendManager.validate_schema_version>, '__dict__': <attribute '__dict__' of 'BackendManager' objects>, '__weakref__': <attribute '__weakref__' of 'BackendManager' objects>})
__module__ = 'aiida.backends.manager'
__weakref__

list of weak references to the object (if defined)

abstract _load_backend_environment(**kwargs)[source]

Load the backend environment.

Parameters

kwargs – keyword arguments that will be passed on to the backend specific scoped session getter function.

_migrate_database_generation()[source]

Migrate the database schema generation.

Warning

this should NEVER be called directly because there is no validation performed on whether the current database schema generation and version can actually be migrated.

This normally just consists out of setting the schema generation value, but depending on the backend more might be needed. In that case, this method should be overridden and call super first, followed by the additional logic that is required.

_migrate_database_version()[source]

Migrate the database to the current schema version.

Warning

this should NEVER be called directly because there is no validation performed on whether the current database schema generation and version can actually be migrated.

_settings_manager = None
get_schema_generation_database()[source]

Return the database schema generation.

Returns

distutils.version.LooseVersion with schema generation of the database

abstract get_schema_version_code()[source]

Return the code schema version.

abstract get_schema_version_database()[source]

Return the database schema version.

Returns

distutils.version.LooseVersion with schema version of the database

abstract get_schema_version_reset(schema_generation_code)[source]

Return schema version the database should have to be able to automatically reset to code schema generation.

Parameters

schema_generation_code – the schema generation of the code.

Returns

schema version

abstract get_settings_manager()[source]

Return an instance of the SettingsManager.

Returns

SettingsManager

abstract is_database_schema_ahead()[source]

Determine whether the database schema version is ahead of the code schema version.

Warning

this will not check whether the schema generations are equal

Returns

boolean, True if the database schema version is ahead of the code schema version.

load_backend_environment(profile, validate_schema=True, **kwargs)[source]

Load the backend environment.

Parameters
  • profile – the profile whose backend environment to load

  • validate_schema – boolean, if True, validate the schema first before loading the environment.

  • kwargs – keyword arguments that will be passed on to the backend specific scoped session getter function.

migrate()[source]

Migrate the database to the latest schema generation or version.

abstract reset_backend_environment()[source]

Reset the backend environment.

set_schema_generation_database(generation)[source]

Set the database schema generation.

Parameters

generation – string with schema generation to set

abstract set_schema_version_database(version)[source]

Set the database schema version.

Parameters

version – string with schema version to set

validate_schema(profile)[source]

Validate that the current database generation and schema are up-to-date with that of the code.

Parameters

profile – the profile for which to validate the database schema

Raises

aiida.common.exceptions.ConfigurationError – if database schema version or generation is not up-to-date

validate_schema_generation(profile)[source]

Validate that the current database schema generation is up-to-date with that of the code.

Raises

aiida.common.exceptions.IncompatibleDatabaseSchema – if database schema generation is not up-to-date

validate_schema_generation_for_migration()[source]

Validate whether the current database schema generation can be migrated.

Raises

aiida.common.exceptions.IncompatibleDatabaseSchema – if database schema generation cannot be migrated

validate_schema_version(profile)[source]

Validate that the current database schema version is up-to-date with that of the code.

Parameters

profile – the profile for which to validate the database schema

Raises

aiida.common.exceptions.IncompatibleDatabaseSchema – if database schema version is not up-to-date

validate_schema_version_for_migration()[source]

Validate whether the current database schema version can be migrated.

Warning

this will not validate that the schema generation is correct.

Raises

aiida.common.exceptions.IncompatibleDatabaseSchema – if database schema version cannot be migrated

class aiida.backends.manager.Setting(key, value, description, time)

Bases: tuple

__getnewargs__()

Return self as a plain tuple. Used by copy and pickle.

__module__ = 'aiida.backends.manager'
static __new__(_cls, key, value, description, time)

Create new instance of Setting(key, value, description, time)

__repr__()

Return a nicely formatted representation string

__slots__ = ()
_asdict()

Return a new dict which maps field names to their values.

_field_defaults = {}
_fields = ('key', 'value', 'description', 'time')
_fields_defaults = {}
classmethod _make(iterable)

Make a new Setting object from a sequence or iterable

_replace(**kwds)

Return a new Setting object replacing specified fields with new values

description

Alias for field number 2

key

Alias for field number 0

time

Alias for field number 3

value

Alias for field number 1

class aiida.backends.manager.SettingsManager[source]

Bases: object

Class to get, set and delete settings from the DbSettings table.

__dict__ = mappingproxy({'__module__': 'aiida.backends.manager', '__doc__': 'Class to get, set and delete settings from the `DbSettings` table.', 'get': <function SettingsManager.get>, 'set': <function SettingsManager.set>, 'delete': <function SettingsManager.delete>, '__dict__': <attribute '__dict__' of 'SettingsManager' objects>, '__weakref__': <attribute '__weakref__' of 'SettingsManager' objects>})
__module__ = 'aiida.backends.manager'
__weakref__

list of weak references to the object (if defined)

abstract delete(key)[source]

Delete the setting with the given key.

Parameters

key – the key identifying the setting

Raises

~aiida.common.exceptions.NotExistent if the settings does not exist

abstract get(key)[source]

Return the setting with the given key.

Parameters

key – the key identifying the setting

Returns

Setting

Raises

~aiida.common.exceptions.NotExistent if the settings does not exist

abstract set(key, value, description=None)[source]

Return the settings with the given key.

Parameters
  • key – the key identifying the setting

  • value – the value for the setting

  • description – optional setting description

Basic test classes.

class aiida.backends.testbase.AiidaPostgresTestCase(methodName='runTest')[source]

Bases: aiida.backends.testbase.AiidaTestCase

Setup postgres tests.

__module__ = 'aiida.backends.testbase'
classmethod setUpClass(*args, **kwargs)[source]

Setup the PGTest postgres test cluster.

classmethod tearDownClass(*args, **kwargs)[source]

Close the PGTest postgres test cluster.

class aiida.backends.testbase.AiidaTestCase(methodName='runTest')[source]

Bases: unittest.case.TestCase

This is the base class for AiiDA tests, independent of the backend.

Internally it loads the AiidaTestImplementation subclass according to the current backend.

__backend_instance = None
__module__ = 'aiida.backends.testbase'
_class_was_setup = False
_computer: aiida.orm.Computer = None
_user: aiida.orm.User = None
assertClickResultNoException(cli_result)[source]
assertClickSuccess(cli_result)[source]
backend: aiida.orm.implementation.Backend = None
classmethod clean_db()[source]

Clean up database and reset caches.

Resets AiiDA manager cache, which could otherwise be left in an inconsistent state when cleaning the database.

classmethod clean_repository()[source]

Cleans up file repository.

computer

A class that, when used as a decorator, works as if the two decorators @property and @classmethod where applied together (i.e., the object works as a property, both for the Class and for any of its instance; and is called with the class cls rather than with the instance as its first argument).

classmethod get_backend_class()[source]

Get backend class.

classmethod insert_data()[source]

This method setups the database (by creating a default user) and inserts default data into the database (which is for the moment a default computer).

classmethod refurbish_db()[source]

Clean up database and repopulate with initial data.

Combines clean_db and insert_data.

classmethod setUpClass()[source]

Set up test class.

tearDown()[source]

Hook method for deconstructing the test fixture after testing it.

classmethod tearDownClass()[source]

Tear down test class.

Note: Also cleans file repository.

user

A class that, when used as a decorator, works as if the two decorators @property and @classmethod where applied together (i.e., the object works as a property, both for the Class and for any of its instance; and is called with the class cls rather than with the instance as its first argument).

user_email

A class that, when used as a decorator, works as if the two decorators @property and @classmethod where applied together (i.e., the object works as a property, both for the Class and for any of its instance; and is called with the class cls rather than with the instance as its first argument).

aiida.backends.testbase.check_if_tests_can_run()[source]

Verify that the currently loaded profile is a test profile, otherwise raise TestsNotAllowedError.

aiida.backends.testbase.get_default_user(**kwargs)[source]

Creates and stores the default user in the database.

Default user email is taken from current profile. No-op if user already exists. The same is done in verdi setup.

Parameters

kwargs – Additional information to use for new user, i.e. ‘first_name’, ‘last_name’ or ‘institution’.

Returns

the User

Implementation-dependednt base tests

class aiida.backends.testimplbase.AiidaTestImplementation[source]

Bases: abc.ABC

Backend-specific test implementations.

__abstractmethods__ = frozenset({'clean_db'})
__dict__ = mappingproxy({'__module__': 'aiida.backends.testimplbase', '__doc__': 'Backend-specific test implementations.', '_backend': None, 'backend': <property object>, 'clean_db': <function AiidaTestImplementation.clean_db>, '__dict__': <attribute '__dict__' of 'AiidaTestImplementation' objects>, '__weakref__': <attribute '__weakref__' of 'AiidaTestImplementation' objects>, '__abstractmethods__': frozenset({'clean_db'}), '_abc_impl': <_abc_data object>})
__module__ = 'aiida.backends.testimplbase'
__weakref__

list of weak references to the object (if defined)

_abc_impl = <_abc_data object>
_backend = None
property backend

Get the backend.

abstract clean_db()[source]

This method fully cleans the DB.

Backend-agnostic utility functions

aiida.backends.utils.create_scoped_session_factory(engine, **kwargs)[source]

Create scoped SQLAlchemy session factory

aiida.backends.utils.create_sqlalchemy_engine(profile, **kwargs)[source]

Create SQLAlchemy engine (to be used for QueryBuilder queries)

Parameters

kwargs – keyword arguments that will be passed on to sqlalchemy.create_engine. See https://docs.sqlalchemy.org/en/13/core/engines.html?highlight=create_engine#sqlalchemy.create_engine for more info.

aiida.backends.utils.delete_nodes_and_connections(pks)[source]

Backend-agnostic function to delete Nodes and connections