Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adaptor properties hash #84

Merged
merged 6 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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