aiida.storage.psql_dos package#

Module with implementation of the storage backend using PostGreSQL and the disk-objectstore.

Subpackages#

Submodules#

Simple wrapper around the alembic command line tool that first loads an AiiDA profile.

class aiida.storage.psql_dos.alembic_cli.AlembicRunner[源代码]#

基类:object

Wrapper around the alembic command line tool that first loads an AiiDA profile.

__dict__ = mappingproxy({'__module__': 'aiida.storage.psql_dos.alembic_cli', '__doc__': 'Wrapper around the alembic command line tool that first loads an AiiDA profile.', '__init__': <function AlembicRunner.__init__>, 'execute_alembic_command': <function AlembicRunner.execute_alembic_command>, '__dict__': <attribute '__dict__' of 'AlembicRunner' objects>, '__weakref__': <attribute '__weakref__' of 'AlembicRunner' objects>, '__annotations__': {'profile': 'Profile | None'}})#
__init__() None[源代码]#
__module__ = 'aiida.storage.psql_dos.alembic_cli'#
__weakref__#

list of weak references to the object (if defined)

execute_alembic_command(command_name, connect=True, **kwargs)[源代码]#

Execute an Alembic CLI command.

参数:
  • command_name – the sub command name

  • kwargs – parameters to pass to the command

SqlAlchemy implementation of aiida.orm.implementation.backends.Backend.

class aiida.storage.psql_dos.backend.PsqlDosBackend(profile: Profile)[源代码]#

基类:StorageBackend

An AiiDA storage backend that stores data in a PostgreSQL database and disk-objectstore repository.

Note, there were originally two such backends, sqlalchemy and django. The django backend was removed, to consolidate access to this storage.

class Model(*, database_engine: str = 'postgresql_psycopg2', database_hostname: str = 'localhost', database_port: int = 5432, database_username: str, database_password: str, database_name: str, repository_uri: str)[源代码]#

基类:BaseModel

Model describing required information to configure an instance of the storage.

__abstractmethods__ = frozenset({})#
__annotations__ = {'__class_vars__': 'ClassVar[set[str]]', '__private_attributes__': 'ClassVar[dict[str, ModelPrivateAttr]]', '__pydantic_complete__': 'ClassVar[bool]', '__pydantic_core_schema__': 'ClassVar[CoreSchema]', '__pydantic_custom_init__': 'ClassVar[bool]', '__pydantic_decorators__': 'ClassVar[_decorators.DecoratorInfos]', '__pydantic_extra__': 'dict[str, Any] | None', '__pydantic_fields_set__': 'set[str]', '__pydantic_generic_metadata__': 'ClassVar[_generics.PydanticGenericMetadata]', '__pydantic_parent_namespace__': 'ClassVar[dict[str, Any] | None]', '__pydantic_post_init__': "ClassVar[None | typing_extensions.Literal['model_post_init']]", '__pydantic_private__': 'dict[str, Any] | None', '__pydantic_root_model__': 'ClassVar[bool]', '__pydantic_serializer__': 'ClassVar[SchemaSerializer]', '__pydantic_validator__': 'ClassVar[SchemaValidator]', '__signature__': 'ClassVar[Signature]', 'database_engine': <class 'str'>, 'database_hostname': <class 'str'>, 'database_name': <class 'str'>, 'database_password': <class 'str'>, 'database_port': <class 'int'>, 'database_username': <class 'str'>, 'model_computed_fields': 'ClassVar[dict[str, ComputedFieldInfo]]', 'model_config': 'ClassVar[ConfigDict]', 'model_fields': 'ClassVar[dict[str, FieldInfo]]', 'repository_uri': <class 'str'>}#
__class_vars__: ClassVar[set[str]] = {}#
__dict__#
__module__ = 'aiida.storage.psql_dos.backend'#
__private_attributes__: ClassVar[dict[str, ModelPrivateAttr]] = {}#
__pydantic_complete__: ClassVar[bool] = True#
__pydantic_core_schema__: ClassVar[CoreSchema] = {'cls': <class 'aiida.storage.psql_dos.backend.PsqlDosBackend.Model'>, 'config': {'title': 'Model'}, 'custom_init': False, 'metadata': {'pydantic_js_annotation_functions': [], 'pydantic_js_functions': [functools.partial(<function modify_model_json_schema>, cls=<class 'aiida.storage.psql_dos.backend.PsqlDosBackend.Model'>), <bound method BaseModel.__get_pydantic_json_schema__ of <class 'aiida.storage.psql_dos.backend.PsqlDosBackend.Model'>>]}, 'ref': 'aiida.storage.psql_dos.backend.PsqlDosBackend.Model:94461381453840', 'root_model': False, 'schema': {'computed_fields': [], 'fields': {'database_engine': {'metadata': {'pydantic_js_annotation_functions': [<function get_json_schema_update_func.<locals>.json_schema_update_func>], 'pydantic_js_functions': []}, 'schema': {'default': 'postgresql_psycopg2', 'schema': {'type': 'str'}, 'type': 'default'}, 'type': 'model-field'}, 'database_hostname': {'metadata': {'pydantic_js_annotation_functions': [<function get_json_schema_update_func.<locals>.json_schema_update_func>], 'pydantic_js_functions': []}, 'schema': {'default': 'localhost', 'schema': {'type': 'str'}, 'type': 'default'}, 'type': 'model-field'}, 'database_name': {'metadata': {'pydantic_js_annotation_functions': [<function get_json_schema_update_func.<locals>.json_schema_update_func>], 'pydantic_js_functions': []}, 'schema': {'type': 'str'}, 'type': 'model-field'}, 'database_password': {'metadata': {'pydantic_js_annotation_functions': [<function get_json_schema_update_func.<locals>.json_schema_update_func>], 'pydantic_js_functions': []}, 'schema': {'type': 'str'}, 'type': 'model-field'}, 'database_port': {'metadata': {'pydantic_js_annotation_functions': [<function get_json_schema_update_func.<locals>.json_schema_update_func>], 'pydantic_js_functions': []}, 'schema': {'default': 5432, 'schema': {'type': 'int'}, 'type': 'default'}, 'type': 'model-field'}, 'database_username': {'metadata': {'pydantic_js_annotation_functions': [<function get_json_schema_update_func.<locals>.json_schema_update_func>], 'pydantic_js_functions': []}, 'schema': {'type': 'str'}, 'type': 'model-field'}, 'repository_uri': {'metadata': {'pydantic_js_annotation_functions': [<function get_json_schema_update_func.<locals>.json_schema_update_func>], 'pydantic_js_functions': []}, 'schema': {'type': 'str'}, 'type': 'model-field'}}, 'model_name': 'Model', 'type': 'model-fields'}, 'type': 'model'}#
__pydantic_custom_init__: ClassVar[bool] = False#
__pydantic_decorators__: ClassVar[_decorators.DecoratorInfos] = DecoratorInfos(validators={}, field_validators={}, root_validators={}, field_serializers={}, model_serializers={}, model_validators={}, computed_fields={})#
__pydantic_extra__: dict[str, Any] | None#
__pydantic_fields_set__: set[str]#
__pydantic_generic_metadata__: ClassVar[_generics.PydanticGenericMetadata] = {'args': (), 'origin': None, 'parameters': ()}#
__pydantic_parent_namespace__: ClassVar[dict[str, Any] | None] = {'__doc__': 'An AiiDA storage backend that stores data in a PostgreSQL database and disk-objectstore repository.\n\n    Note, there were originally two such backends, `sqlalchemy` and `django`.\n    The `django` backend was removed, to consolidate access to this storage.\n    ', '__module__': 'aiida.storage.psql_dos.backend', '__qualname__': 'PsqlDosBackend'}#
__pydantic_post_init__: ClassVar[None | typing_extensions.Literal['model_post_init']] = None#
__pydantic_private__: dict[str, Any] | None#
__pydantic_serializer__: ClassVar[SchemaSerializer] = SchemaSerializer(serializer=Model(     ModelSerializer {         class: Py(             0x000055e981162810,         ),         serializer: Fields(             GeneralFieldsSerializer {                 fields: {                     "database_password": SerField {                         key_py: Py(                             0x00007fc9fe3a4df0,                         ),                         alias: None,                         alias_py: None,                         serializer: Some(                             Str(                                 StrSerializer,                             ),                         ),                         required: true,                     },                     "database_engine": SerField {                         key_py: Py(                             0x00007fc9fe3a0ab0,                         ),                         alias: None,                         alias_py: None,                         serializer: Some(                             WithDefault(                                 WithDefaultSerializer {                                     default: Default(                                         Py(                                             0x00007fc9fe104ee0,                                         ),                                     ),                                     serializer: Str(                                         StrSerializer,                                     ),                                 },                             ),                         ),                         required: true,                     },                     "repository_uri": SerField {                         key_py: Py(                             0x00007fc9fe3a0cb0,                         ),                         alias: None,                         alias_py: None,                         serializer: Some(                             Str(                                 StrSerializer,                             ),                         ),                         required: true,                     },                     "database_name": SerField {                         key_py: Py(                             0x00007fc9fe3a0c70,                         ),                         alias: None,                         alias_py: None,                         serializer: Some(                             Str(                                 StrSerializer,                             ),                         ),                         required: true,                     },                     "database_port": SerField {                         key_py: Py(                             0x00007fc9fe3a0b70,                         ),                         alias: None,                         alias_py: None,                         serializer: Some(                             WithDefault(                                 WithDefaultSerializer {                                     default: Default(                                         Py(                                             0x00007fc9f6c012b0,                                         ),                                     ),                                     serializer: Int(                                         IntSerializer,                                     ),                                 },                             ),                         ),                         required: true,                     },                     "database_hostname": SerField {                         key_py: Py(                             0x00007fc9fe3a4d50,                         ),                         alias: None,                         alias_py: None,                         serializer: Some(                             WithDefault(                                 WithDefaultSerializer {                                     default: Default(                                         Py(                                             0x00007fc9ffa03ab0,                                         ),                                     ),                                     serializer: Str(                                         StrSerializer,                                     ),                                 },                             ),                         ),                         required: true,                     },                     "database_username": SerField {                         key_py: Py(                             0x00007fc9fe3a4da0,                         ),                         alias: None,                         alias_py: None,                         serializer: Some(                             Str(                                 StrSerializer,                             ),                         ),                         required: true,                     },                 },                 computed_fields: Some(                     ComputedFields(                         [],                     ),                 ),                 mode: SimpleDict,                 extra_serializer: None,                 filter: SchemaFilter {                     include: None,                     exclude: None,                 },                 required_fields: 7,             },         ),         has_extra: false,         root_model: false,         name: "Model",     }, ), definitions=[])#
__pydantic_validator__: ClassVar[SchemaValidator] = SchemaValidator(title="Model", validator=Model(     ModelValidator {         revalidate: Never,         validator: ModelFields(             ModelFieldsValidator {                 fields: [                     Field {                         name: "database_engine",                         lookup_key: Simple {                             key: "database_engine",                             py_key: Py(                                 0x00007fc9f6def7b0,                             ),                             path: LookupPath(                                 [                                     S(                                         "database_engine",                                         Py(                                             0x00007fc9f5e88170,                                         ),                                     ),                                 ],                             ),                         },                         name_py: Py(                             0x00007fc9fe3a0ab0,                         ),                         validator: WithDefault(                             WithDefaultValidator {                                 default: Default(                                     Py(                                         0x00007fc9fe104ee0,                                     ),                                 ),                                 on_error: Raise,                                 validator: Str(                                     StrValidator {                                         strict: false,                                         coerce_numbers_to_str: false,                                     },                                 ),                                 validate_default: false,                                 copy_default: false,                                 name: "default[str]",                                 undefined: Py(                                     0x00007fc9fe69f560,                                 ),                             },                         ),                         frozen: false,                     },                     Field {                         name: "database_hostname",                         lookup_key: Simple {                             key: "database_hostname",                             py_key: Py(                                 0x00007fc9f5ec6240,                             ),                             path: LookupPath(                                 [                                     S(                                         "database_hostname",                                         Py(                                             0x00007fc9f5e9e4c0,                                         ),                                     ),                                 ],                             ),                         },                         name_py: Py(                             0x00007fc9fe3a4d50,                         ),                         validator: WithDefault(                             WithDefaultValidator {                                 default: Default(                                     Py(                                         0x00007fc9ffa03ab0,                                     ),                                 ),                                 on_error: Raise,                                 validator: Str(                                     StrValidator {                                         strict: false,                                         coerce_numbers_to_str: false,                                     },                                 ),                                 validate_default: false,                                 copy_default: false,                                 name: "default[str]",                                 undefined: Py(                                     0x00007fc9fe69f560,                                 ),                             },                         ),                         frozen: false,                     },                     Field {                         name: "database_port",                         lookup_key: Simple {                             key: "database_port",                             py_key: Py(                                 0x00007fc9f5d4bfb0,                             ),                             path: LookupPath(                                 [                                     S(                                         "database_port",                                         Py(                                             0x00007fc9f5d4b2b0,                                         ),                                     ),                                 ],                             ),                         },                         name_py: Py(                             0x00007fc9fe3a0b70,                         ),                         validator: WithDefault(                             WithDefaultValidator {                                 default: Default(                                     Py(                                         0x00007fc9f6c012b0,                                     ),                                 ),                                 on_error: Raise,                                 validator: Int(                                     IntValidator {                                         strict: false,                                     },                                 ),                                 validate_default: false,                                 copy_default: false,                                 name: "default[int]",                                 undefined: Py(                                     0x00007fc9fe69f560,                                 ),                             },                         ),                         frozen: false,                     },                     Field {                         name: "database_username",                         lookup_key: Simple {                             key: "database_username",                             py_key: Py(                                 0x00007fc9f5e9cfd0,                             ),                             path: LookupPath(                                 [                                     S(                                         "database_username",                                         Py(                                             0x00007fc9f5e9dcf0,                                         ),                                     ),                                 ],                             ),                         },                         name_py: Py(                             0x00007fc9fe3a4da0,                         ),                         validator: Str(                             StrValidator {                                 strict: false,                                 coerce_numbers_to_str: false,                             },                         ),                         frozen: false,                     },                     Field {                         name: "database_password",                         lookup_key: Simple {                             key: "database_password",                             py_key: Py(                                 0x00007fc9f5e9f820,                             ),                             path: LookupPath(                                 [                                     S(                                         "database_password",                                         Py(                                             0x00007fc9f5e9e470,                                         ),                                     ),                                 ],                             ),                         },                         name_py: Py(                             0x00007fc9fe3a4df0,                         ),                         validator: Str(                             StrValidator {                                 strict: false,                                 coerce_numbers_to_str: false,                             },                         ),                         frozen: false,                     },                     Field {                         name: "database_name",                         lookup_key: Simple {                             key: "database_name",                             py_key: Py(                                 0x00007fc9f5d49570,                             ),                             path: LookupPath(                                 [                                     S(                                         "database_name",                                         Py(                                             0x00007fc9f5e86870,                                         ),                                     ),                                 ],                             ),                         },                         name_py: Py(                             0x00007fc9fe3a0c70,                         ),                         validator: Str(                             StrValidator {                                 strict: false,                                 coerce_numbers_to_str: false,                             },                         ),                         frozen: false,                     },                     Field {                         name: "repository_uri",                         lookup_key: Simple {                             key: "repository_uri",                             py_key: Py(                                 0x00007fc9f5e87cb0,                             ),                             path: LookupPath(                                 [                                     S(                                         "repository_uri",                                         Py(                                             0x00007fc9f5e86db0,                                         ),                                     ),                                 ],                             ),                         },                         name_py: Py(                             0x00007fc9fe3a0cb0,                         ),                         validator: Str(                             StrValidator {                                 strict: false,                                 coerce_numbers_to_str: false,                             },                         ),                         frozen: false,                     },                 ],                 model_name: "Model",                 extra_behavior: Ignore,                 extras_validator: None,                 strict: false,                 from_attributes: false,                 loc_by_alias: true,             },         ),         class: Py(             0x000055e981162810,         ),         post_init: None,         frozen: false,         custom_init: false,         root_model: false,         undefined: Py(             0x00007fc9fe69f560,         ),         name: "Model",     }, ), definitions=[], cache_strings=True)#
__signature__: ClassVar[Signature] = <Signature (*, database_engine: str = 'postgresql_psycopg2', database_hostname: str = 'localhost', database_port: int = 5432, database_username: str, database_password: str, database_name: str, repository_uri: str) -> None>#
__weakref__#

list of weak references to the object (if defined)

_abc_impl = <_abc._abc_data object>#
database_engine: str#
database_hostname: str#
database_name: str#
database_password: str#
database_port: int#
database_username: str#
model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}#

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_fields: ClassVar[dict[str, FieldInfo]] = {'database_engine': FieldInfo(annotation=str, required=False, default='postgresql_psycopg2', title='PostgreSQL engine', description='The engine to use to connect to the database.'), 'database_hostname': FieldInfo(annotation=str, required=False, default='localhost', title='PostgreSQL hostname', description='The hostname of the PostgreSQL server.'), 'database_name': FieldInfo(annotation=str, required=True, title='PostgreSQL database name', description='The name of the database in the PostgreSQL server.'), 'database_password': FieldInfo(annotation=str, required=True, title='PostgreSQL password', description='The password with which to connect to the PostgreSQL server.'), 'database_port': FieldInfo(annotation=int, required=False, default=5432, title='PostgreSQL port', description='The port of the PostgreSQL server.'), 'database_username': FieldInfo(annotation=str, required=True, title='PostgreSQL username', description='The username with which to connect to the PostgreSQL server.'), 'repository_uri': FieldInfo(annotation=str, required=True, title='File repository URI', description='URI to the file repository.')}#

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].

This replaces Model.__fields__ from Pydantic V1.

repository_uri: str#
__abstractmethods__ = frozenset({})#
__init__(profile: Profile) None[源代码]#

Initialize the backend, for this profile.

Raises:

~aiida.common.exceptions.UnreachableStorage if the storage cannot be accessed

Raises:

~aiida.common.exceptions.IncompatibleStorageSchema if the profile’s storage schema is not at the latest version (and thus should be migrated)

Raises:
raises:

aiida.common.exceptions.CorruptStorage if the storage is internally inconsistent

__module__ = 'aiida.storage.psql_dos.backend'#
__str__() str[源代码]#

Return a string showing connection details for this instance.

_abc_impl = <_abc._abc_data object>#
_backup(dest: str, keep: int | None = None)[源代码]#
_backup_storage(manager: BackupManager, path: Path, prev_backup: Path | None = None) None[源代码]#

Create a backup of the postgres database and disk-objectstore to the provided path.

参数:
  • manager – BackupManager from backup_utils containing utilities such as for calling the rsync.

  • path – Path to where the backup will be created.

  • prev_backup – Path to the previous backup. Rsync calls will be hard-linked to this path, making the backup incremental and efficient.

_clear() None[源代码]#

Clear the storage, removing all data.

警告

This is a destructive operation, and should only be used for testing purposes.

_default_user: 'User' | None#
static _get_mapper_from_entity(entity_type: EntityTypes, with_pk: bool)[源代码]#

Return the Sqlalchemy mapper and fields corresponding to the given entity.

参数:

with_pk – if True, the fields returned will include the primary key

_initialise_session()[源代码]#

Initialise the SQLAlchemy session factory.

Only one session factory is ever associated with a given class instance, i.e. once the instance is closed, it cannot be reopened.

The session factory, returns a session that is bound to the current thread. Multi-thread support is currently required by the REST API. Although, in the future, we may want to move the multi-thread handling to higher in the AiiDA stack.

property authinfos#

Return the collection of authorisation information objects

bulk_insert(entity_type: EntityTypes, rows: List[dict], allow_defaults: bool = False) List[int][源代码]#

Insert a list of entities into the database, directly into a backend transaction.

参数:
  • entity_type – The type of the entity

  • data – A list of dictionaries, containing all fields of the backend model, except the id field (a.k.a primary key), which will be generated dynamically

  • allow_defaults – If False, assert that each row contains all fields (except primary key(s)), otherwise, allow default values for missing fields.

Raises:

IntegrityError if the keys in a row are not a subset of the columns in the table

返回:

The list of generated primary keys for the entities

bulk_update(entity_type: EntityTypes, rows: List[dict]) None[源代码]#

Update a list of entities in the database, directly with a backend transaction.

参数:
  • entity_type – The type of the entity

  • data – A list of dictionaries, containing fields of the backend model to update, and the id field (a.k.a primary key)

Raises:

IntegrityError if the keys in a row are not a subset of the columns in the table

close() None[源代码]#

Close the storage access.

property comments#

Return the collection of comments

property computers#

Return the collection of computers

delete(delete_database_user: bool = False) None[源代码]#

Delete the storage and all the data.

参数:

delete_database_user – Also delete the database user. This is False by default because the user may be used by other databases.

delete_nodes_and_connections(pks_to_delete: Sequence[int]) None[源代码]#

Delete all nodes corresponding to pks in the input and any links to/from them.

This method is intended to be used within a transaction context.

参数:

pks_to_delete – a sequence of node pks to delete

Raises:

AssertionError if a transaction is not active

get_backend_entity(model: Model) BackendEntity[源代码]#

Return the backend entity that corresponds to the given Model instance

参数:

model – the ORM model instance to promote to a backend instance

返回:

the backend entity corresponding to the given model

get_global_variable(key: str) None | str | int | float[源代码]#

Return a global variable from the storage.

参数:

key – the key of the setting

Raises:

KeyError if the setting does not exist

get_info(detailed: bool = False) dict[源代码]#

Return general information on the storage.

参数:

detailed – flag to request more detailed information about the content of the storage.

返回:

a nested dict with the relevant information.

get_repository() DiskObjectStoreRepositoryBackend[源代码]#

Return the object repository configured for this backend.

get_session() Session[源代码]#

Return an SQLAlchemy session bound to the current thread.

get_unreferenced_keyset(check_consistency: bool = True) Set[str][源代码]#

Returns the keyset of objects that exist in the repository but are not tracked by AiiDA.

This should be all the soft-deleted files.

参数:

check_consistency – toggle for a check that raises if there are references in the database with no actual object in the underlying repository.

返回:

a set with all the objects in the underlying repository that are not referenced in the database.

property groups#

Return the collection of groups

property in_transaction: bool#

Return whether a transaction is currently active.

classmethod initialise(profile: Profile, reset: bool = False) bool[源代码]#

Initialise the storage backend.

This is typically used once when a new storage backed is created. If this method returns without exceptions the storage backend is ready for use. If the backend already seems initialised, this method is a no-op.

参数:

reset – If true, destroy the backend if it already exists including all of its data before recreating and initialising it. This is useful for example for test profiles that need to be reset before or after tests having run.

返回:

True if the storage was initialised by the function call, False if it was already initialised.

property is_closed: bool#

Return whether the storage is closed.

property logs#

Return the collection of logs

maintain(full: bool = False, dry_run: bool = False, **kwargs) None[源代码]#

Perform maintenance tasks on the storage.

If full == True, then this method may attempt to block the profile associated with the storage to guarantee the safety of its procedures. This will not only prevent any other subsequent process from accessing that profile, but will also first check if there is already any process using it and raise if that is the case. The user will have to manually stop any processes that is currently accessing the profile themselves or wait for it to finish on its own.

参数:
  • full – flag to perform operations that require to stop using the profile to be maintained.

  • dry_run – flag to only print the actions that would be taken without actually executing them.

classmethod migrate(profile: Profile) None[源代码]#

Migrate the storage of a profile to the latest schema version.

If the schema version is already the latest version, this method does nothing. If the storage is uninitialised, this method will raise an exception.

Raises:

:class`~aiida.common.exceptions.UnreachableStorage` if the storage cannot be accessed.

Raises:

StorageMigrationError if the storage is not initialised.

migrator#

PsqlDosMigrator 的别名

classmethod migrator_context(profile: Profile)[源代码]#
property nodes#

Return the collection of nodes

query()[源代码]#

Return an instance of a query builder implementation for this backend

set_global_variable(key: str, value: None | str | int | float, description: str | None = None, overwrite=True) None[源代码]#

Set a global variable in the storage.

参数:
  • key – the key of the setting

  • value – the value of the setting

  • description – the description of the setting (optional)

  • overwrite – if True, overwrite the setting if it already exists

Raises:

ValueError if the key already exists and overwrite is False

transaction() Iterator[Session][源代码]#

Open a transaction to be used as a context manager.

If there is an exception within the context then the changes will be rolled back and the state will be as before entering. Transactions can be nested.

property users#

Return the collection of users

classmethod version_head() str[源代码]#

Return the head schema version of this storage backend type.

classmethod version_profile(profile: Profile) str | None[源代码]#

Return the schema version of the given profile’s storage, or None for empty/uninitialised storage.

Raises:

~aiida.common.exceptions.UnreachableStorage if the storage cannot be accessed

aiida.storage.psql_dos.backend.get_filepath_container(profile: Profile) Path[源代码]#

Return the filepath of the disk-object store container.

Schema validation and migration utilities.

This code interacts directly with the database, outside of the ORM, taking a Profile as input for the connection configuration.

重要

This code should only be accessed via the storage backend class, not directly!

class aiida.storage.psql_dos.migrator.PsqlDosMigrator(profile: Profile)[源代码]#

基类:object

Class for validating and migrating psql_dos storage instances.

重要

This class should only be accessed via the storage backend class (apart from for test purposes)

__dict__ = mappingproxy({'__module__': 'aiida.storage.psql_dos.migrator', '__doc__': 'Class for validating and migrating `psql_dos` storage instances.\n\n    .. important:: This class should only be accessed via the storage backend class (apart from for test purposes)\n    ', 'alembic_version_tbl_name': 'alembic_version', 'django_version_table': <sqlalchemy.sql.selectable.TableClause at 0x7fc9f5ec9d10; django_migrations>, '__init__': <function PsqlDosMigrator.__init__>, 'close': <function PsqlDosMigrator.close>, 'connection': <property object>, 'get_schema_versions': <classmethod(<function PsqlDosMigrator.get_schema_versions>)>, 'get_schema_version_head': <classmethod(<function PsqlDosMigrator.get_schema_version_head>)>, 'get_schema_version_profile': <function PsqlDosMigrator.get_schema_version_profile>, 'validate_storage': <function PsqlDosMigrator.validate_storage>, 'get_container': <function PsqlDosMigrator.get_container>, 'get_repository_uuid': <function PsqlDosMigrator.get_repository_uuid>, 'initialise': <function PsqlDosMigrator.initialise>, 'is_initialised': <property object>, 'is_repository_initialised': <property object>, 'is_database_initialised': <property object>, 'reset_repository': <function PsqlDosMigrator.reset_repository>, 'reset_database': <function PsqlDosMigrator.reset_database>, 'initialise_repository': <function PsqlDosMigrator.initialise_repository>, 'initialise_database': <function PsqlDosMigrator.initialise_database>, 'delete_all_tables': <function PsqlDosMigrator.delete_all_tables>, 'migrate': <function PsqlDosMigrator.migrate>, 'migrate_up': <function PsqlDosMigrator.migrate_up>, 'migrate_down': <function PsqlDosMigrator.migrate_down>, '_alembic_config': <staticmethod(<function PsqlDosMigrator._alembic_config>)>, '_alembic_script': <classmethod(<function PsqlDosMigrator._alembic_script>)>, '_alembic_connect': <function PsqlDosMigrator._alembic_connect>, '_migration_context': <function PsqlDosMigrator._migration_context>, 'session': <function PsqlDosMigrator.session>, 'get_current_table': <function PsqlDosMigrator.get_current_table>, '__dict__': <attribute '__dict__' of 'PsqlDosMigrator' objects>, '__weakref__': <attribute '__weakref__' of 'PsqlDosMigrator' objects>, '__annotations__': {}})#
__init__(profile: Profile) None[源代码]#
__module__ = 'aiida.storage.psql_dos.migrator'#
__weakref__#

list of weak references to the object (if defined)

static _alembic_config()[源代码]#

Return an instance of an Alembic Config.

_alembic_connect() Iterator[Config][源代码]#

Context manager to return an instance of an Alembic configuration.

The profiles’s 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.

classmethod _alembic_script()[源代码]#

Return an instance of an Alembic ScriptDirectory.

_migration_context() Iterator[MigrationContext][源代码]#

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.

alembic_version_tbl_name = 'alembic_version'#
close() None[源代码]#

Close the connection if it was opened and dispose of the engine.

property connection#

Return the connection to the database.

Will automatically create the engine and open an connection if not already opened in a previous call.

返回:

Open connection to the database.

Raises:

aiida.common.exceptions.UnreachableStorage if connecting to the database fails.

delete_all_tables(*, exclude_tables: list[str] | None = None) None[源代码]#

Delete all tables of the current database schema.

The tables are determined dynamically through reflection of the current schema version. Any other tables in the database that are not part of the schema should remain unaffected.

参数:

exclude_tables – Optional list of table names that should not be deleted.

django_version_table = <sqlalchemy.sql.selectable.TableClause at 0x7fc9f5ec9d10; django_migrations>#
get_container() Container[源代码]#

Return the disk-object store container.

返回:

The disk-object store container configured for the repository path of the current profile.

get_current_table(table_name: str) Any[源代码]#

Return a table instantiated at the correct migration.

Note that this is obtained by inspecting the database and not by looking into the models file. So, special methods possibly defined in the models files/classes are not present.

get_repository_uuid() str[源代码]#

Return the UUID of the repository.

返回:

The repository UUID.

Raises:

UnreachableStorage if the UUID cannot be retrieved, which probably means that the repository is not initialised.

classmethod get_schema_version_head() str[源代码]#

Return the head schema version for this storage, i.e. the latest schema this storage can be migrated to.

get_schema_version_profile(check_legacy=False) str | None[源代码]#

Return the schema version of the backend instance for this profile.

Note, the version will be None if the database is empty or is a legacy django database.

classmethod get_schema_versions() Dict[str, str][源代码]#

Return all available schema versions (oldest to latest).

返回:

schema version -> description

initialise(reset: bool = False) bool[源代码]#

Initialise the storage backend.

This is typically used once when a new storage backed is created. If this method returns without exceptions the storage backend is ready for use. If the backend already seems initialised, this method is a no-op.

参数:

reset – If true, destroy the backend if it already exists including all of its data before recreating and initialising it. This is useful for example for test profiles that need to be reset before or after tests having run.

返回:

True if the storage was initialised by the function call, False if it was already initialised.

initialise_database() None[源代码]#

Initialise the database.

This assumes that the database has no schema whatsoever and so the initial schema is created directly from the models at the current head version without migrating through all of them one by one.

initialise_repository() None[源代码]#

Initialise the repository.

property is_database_initialised: bool#

Return whether the database is initialised.

This is the case if it contains the table that holds the schema version for alembic or Django.

返回:

True if the database is initialised, False otherwise.

property is_initialised: bool#

Return whether the storage is initialised.

This is the case if both the database and the repository are initialised.

返回:

True if the storage is initialised, False otherwise.

property is_repository_initialised: bool#

Return whether the repository is initialised.

返回:

True if the repository is initialised, False otherwise.

migrate() None[源代码]#

Migrate the storage for this profile to the head version.

Raises:

UnreachableStorage if the storage cannot be accessed.

Raises:

StorageMigrationError if the storage is not initialised.

migrate_down(version: str) None[源代码]#

Migrate the database down to a specific version.

参数:

version – string with schema version to migrate to

migrate_up(version: str) None[源代码]#

Migrate the database up to a specific version.

参数:

version – string with schema version to migrate to

reset_database() None[源代码]#

Reset the database by deleting all content from all tables.

This will also destroy the settings table and so in order to use it again, it will have to be reinitialised.

reset_repository() None[源代码]#

Reset the repository by deleting all of its contents.

This will also destroy the configuration and so in order to use it again, it will have to be reinitialised.

session() Iterator[Session][源代码]#

Context manager to return a session for the database.

validate_storage() None[源代码]#

Validate that the storage for this profile

  1. That the database schema is at the head version, i.e. is compatible with the code API.

  2. That the repository ID is equal to the UUID set in the database

Raises:

aiida.common.exceptions.UnreachableStorage if the storage cannot be connected to

Raises:

aiida.common.exceptions.IncompatibleStorageSchema if the storage is not compatible with the code API.

Raises:

aiida.common.exceptions.CorruptStorage if the repository ID is not equal to the UUID set in thedatabase.

Utility functions specific to the SqlAlchemy backend.

class aiida.storage.psql_dos.utils.PsqlConfig[源代码]#

基类:TypedDict

Configuration to connect to a PostgreSQL database.

__annotations__ = {'database_hostname': <class 'str'>, 'database_name': <class 'str'>, 'database_password': <class 'str'>, 'database_port': <class 'int'>, 'database_username': <class 'str'>, 'engine_kwargs': <class 'dict'>}#
__dict__ = mappingproxy({'__module__': 'aiida.storage.psql_dos.utils', '__annotations__': {'database_hostname': <class 'str'>, 'database_port': <class 'int'>, 'database_username': <class 'str'>, 'database_password': <class 'str'>, 'database_name': <class 'str'>, 'engine_kwargs': <class 'dict'>}, '__doc__': 'Configuration to connect to a PostgreSQL database.', '__orig_bases__': (<function TypedDict>,), '__dict__': <attribute '__dict__' of 'PsqlConfig' objects>, '__weakref__': <attribute '__weakref__' of 'PsqlConfig' objects>, '__required_keys__': frozenset(), '__optional_keys__': frozenset({'database_username', 'database_port', 'engine_kwargs', 'database_hostname', 'database_password', 'database_name'}), '__total__': False})#
__module__ = 'aiida.storage.psql_dos.utils'#
__optional_keys__ = frozenset({'database_hostname', 'database_name', 'database_password', 'database_port', 'database_username', 'engine_kwargs'})#
__orig_bases__ = (<function TypedDict>,)#
__required_keys__ = frozenset({})#
__total__ = False#
__weakref__#

list of weak references to the object (if defined)

database_hostname: str#
database_name: str#
database_password: str#
database_port: int#
database_username: str#
engine_kwargs: dict#

keyword argument that will be passed on to the SQLAlchemy engine.

aiida.storage.psql_dos.utils.create_scoped_session_factory(engine, **kwargs)[源代码]#

Create scoped SQLAlchemy session factory

aiida.storage.psql_dos.utils.create_sqlalchemy_engine(config: PsqlConfig)[源代码]#

Create SQLAlchemy engine (to be used for QueryBuilder queries)

参数:

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.storage.psql_dos.utils.flag_modified(instance, key)[源代码]#

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.

aiida.storage.psql_dos.utils.get_pg_tc(links_table_name, links_table_input_field, links_table_output_field, closure_table_name, closure_table_parent_field, closure_table_child_field)[源代码]#

Return the transitive closure table template

aiida.storage.psql_dos.utils.install_tc(session)[源代码]#

Install the transitive closure table with SqlAlchemy.