Skip to content

Commit

Permalink
PsqlDos: Make hash reset migrations more explicit
Browse files Browse the repository at this point in the history
The `django_0039_reset_hash.py` and `e797afa09270_reset_hash.py`
migrations reset the hash for all nodes by deleting the extra in which
it is stored. The migrations use the `drop_hashes` function from the
`aiida/storage/psql_dos/migrations/utils/integrity.py` module. At the
time, and currently, the key used to store the hash in the extras is
`_aiida_hash`. This key is hard-coded in the `integrity.py` module but
it should really be part of the migrations themselves if the function is
to be reused. If in the future the key is changed, the migration should
specify which key should be used.
  • Loading branch information
sphuber committed May 15, 2023
1 parent 2fd0751 commit c447a1a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
10 changes: 4 additions & 6 deletions aiida/storage/psql_dos/migrations/utils/integrity.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,12 @@ def write_database_integrity_violation(results, headers, reason_message, action_
handle.write(tabulate(results, headers))


# Currently valid hash key
_HASH_EXTRA_KEY = '_aiida_hash'


def drop_hashes(conn):
def drop_hashes(conn, hash_extra_key):
"""Drop hashes of nodes.
Print warning only if the DB actually contains nodes.
:param hash_extra_key: The key in the extras used to store the hash at the time of this migration.
"""
# Remove when https://github.com/PyCQA/pylint/issues/1931 is fixed
# pylint: disable=no-name-in-module,import-error
Expand All @@ -204,5 +202,5 @@ def drop_hashes(conn):
if n_nodes > 0:
echo.echo_warning('Invalidating the hashes of all nodes. Please run "verdi rehash".', bold=True)

statement = text(f"UPDATE db_dbnode SET extras = extras #- '{{{_HASH_EXTRA_KEY}}}'::text[];")
statement = text(f"UPDATE db_dbnode SET extras = extras #- '{{{hash_extra_key}}}'::text[];")
conn.execute(statement)
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

def upgrade():
"""Migrations for the upgrade."""
drop_hashes(op.get_bind()) # pylint: disable=no-member
drop_hashes(op.get_bind(), hash_extra_key='_aiida_hash') # pylint: disable=no-member


def downgrade():
"""Migrations for the downgrade."""
drop_hashes(op.get_bind()) # pylint: disable=no-member
drop_hashes(op.get_bind(), hash_extra_key='_aiida_hash') # pylint: disable=no-member
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

def upgrade():
"""drop the hashes when upgrading"""
drop_hashes(op.get_bind()) # pylint: disable=no-member
drop_hashes(op.get_bind(), hash_extra_key='_aiida_hash') # pylint: disable=no-member


def downgrade():
"""drop the hashes also when downgrading"""
drop_hashes(op.get_bind()) # pylint: disable=no-member
drop_hashes(op.get_bind(), hash_extra_key='_aiida_hash') # pylint: disable=no-member

0 comments on commit c447a1a

Please sign in to comment.