Skip to content

Commit

Permalink
Merge pull request #87 from ecmwf-projects/revert-85-revert-84-adapto…
Browse files Browse the repository at this point in the history
…r_properties_hash

Revert "Revert "Adaptor properties hash""
  • Loading branch information
francesconazzaro authored Jul 27, 2023
2 parents 3ba9c50 + eae2cc4 commit ad40059
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""add new field adaptor_properties_hash.
Revision ID: e5e9a8a41828
Revises: ffb034573c96
Create Date: 2023-07-25 16:07:47.059717
"""
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "e5e9a8a41828"
down_revision = "ffb034573c96"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.add_column("resources", sa.Column("adaptor_properties_hash", sa.String))


def downgrade() -> None:
op.drop_column("resources", "adaptor_properties_hash")
1 change: 1 addition & 0 deletions cads_catalogue/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class Resource(BaseModel):
# internal functionality related
adaptor = sa.Column(sa.String)
adaptor_configuration: Any = sa.Column(dialect_postgresql.JSONB)
adaptor_properties_hash = sa.Column(sa.String)
constraints_data: Any = sa.Column(dialect_postgresql.JSONB)
form_data: Any = sa.Column(dialect_postgresql.JSONB)
sources_hash = sa.Column(sa.String)
Expand Down
23 changes: 23 additions & 0 deletions cads_catalogue/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.

import glob
import hashlib
import itertools
import json
import os
Expand Down Expand Up @@ -45,6 +46,27 @@
}


def compute_config_hash(resource: dict[str, Any]) -> str:
"""Compute a configuration hash on the basis of some other fields.
Parameters
----------
resource: dictionary of the resource metadata
"""
source_fields = [
"constraints_data",
"mapping",
"form_data",
"adaptor_configuration",
]
ret_value = hashlib.md5()
for source_field in source_fields:
field_data = resource.get(source_field)
if field_data:
ret_value.update(json.dumps(field_data, sort_keys=True).encode("utf-8"))
return ret_value.hexdigest() # type: ignore


def is_db_to_update(
session: sa.orm.session.Session,
resources_folder_path: str | pathlib.Path,
Expand Down Expand Up @@ -597,6 +619,7 @@ def update_catalogue_resources(
resource = form_manager.transform_form(
session, resource_folder_path, resource, storage_settings
)
resource["adaptor_properties_hash"] = compute_config_hash(resource)
resource_sync(session, resource, storage_settings)
logger.info("resource %s db sync successful" % resource_uid)
except Exception: # noqa
Expand Down
Loading

0 comments on commit ad40059

Please sign in to comment.