Skip to content

Commit

Permalink
feat: remove missing packages from index during verification
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jul 28, 2024
1 parent 2aca241 commit da5c436
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
16 changes: 15 additions & 1 deletion ckanext/federated_index/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ckan.plugins.toolkit as tk
from ckan import model
from ckan.lib import search
from ckan.lib.search.query import solr_literal
from ckan.logic import validate

from ckanext.federated_index import config, interfaces, shared, storage
Expand Down Expand Up @@ -56,6 +57,17 @@ def federated_index_profile_refresh(

if data_dict["verify"]:
for pkg_id in profile.check_ids(p["id"] for p in db.scan()):
mangled = db.get(pkg_id)
if not mangled:
continue

for plugin in p.PluginImplementations(interfaces.IFederatedIndex):
mangled = plugin.federated_index_mangle_package(mangled, profile) # noqa: PLW2901
tk.get_action("federated_index_profile_clear")(
tk.fresh_context(context),
{"profile": profile, "ids": [mangled["id"]]},
)

db.remove(pkg_id)

for pkg in profile.fetch_packages(payload):
Expand Down Expand Up @@ -119,7 +131,6 @@ def federated_index_profile_index(
for pkg_dict in packages:
for plugin in p.PluginImplementations(interfaces.IFederatedIndex):
pkg_dict = plugin.federated_index_mangle_package(pkg_dict, profile) # noqa: PLW2901

if model.Package.get(pkg_dict["name"]):
log.warning("Package with name %s already exists", pkg_dict["name"])
continue
Expand Down Expand Up @@ -184,6 +195,9 @@ def federated_index_profile_clear(
conn = search.make_connection()
query = f"+{config.profile_field()}:{profile.id}"

if ids := data_dict.get("ids"):
query += " id:({})".format(" OR ".join(map(solr_literal, ids)))

resp = conn.search(q=query, rows=0)

conn.delete(q=query)
Expand Down
3 changes: 3 additions & 0 deletions ckanext/federated_index/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ def profile_index(
def profile_clear(
not_empty: types.Validator,
federated_index_profile: types.Validator,
json_list_or_string: types.Validator,
ignore_missing: types.Validator,
) -> types.Schema:
return {
"profile": [not_empty, federated_index_profile],
"ids": [ignore_missing, json_list_or_string],
}


Expand Down
5 changes: 4 additions & 1 deletion ckanext/federated_index/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def call_action( # noqa: PLR0913
return super().call_action(action, data_dict, context, apikey, files)


class TestFederatedIndexPlugin(p.Plugin):
class TestFederatedIndexPlugin(p.SingletonPlugin):
p.implements(interfaces.IFederatedIndex, inherit=True)

def federated_index_mangle_package(
Expand All @@ -50,6 +50,9 @@ class TestProfile(shared.Profile):
id: str = "test"
url: str = "http://test.ckan.net"
test_app: Any = None
extras: dict[str, Any] = dataclasses.field(
default_factory=lambda: {"search_payload": {"q": "-id:test-*"}}
)

def get_client(self):
return TestAppCKAN(self.test_app)
Expand Down
17 changes: 13 additions & 4 deletions ckanext/federated_index/tests/logic/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,27 @@
class TestRefresh:
def test_verification(self, profile: shared.Profile, package_factory: Any):
"""Removed packages dropped from storage."""

pkgs = package_factory.create_batch(3)

result = call_action("federated_index_profile_refresh", profile=profile)
result = call_action(
"federated_index_profile_refresh", profile=profile, index=True
)
assert result == {"count": 3, "profile": profile.id}
available = call_action("package_search", rows=0)["count"]
assert available == 6

call_action("package_delete", id=pkgs[-1]["id"])

result = call_action(
"federated_index_profile_refresh", profile=profile, verify=False
"federated_index_profile_refresh", profile=profile, verify=False, index=True
)
assert result["count"] == 3
available = call_action("package_search", rows=0)["count"]
assert available == 5

result = call_action("federated_index_profile_refresh", profile=profile)
result = call_action(
"federated_index_profile_refresh", profile=profile, index=True
)
assert result["count"] == 2
available = call_action("package_search", rows=0)["count"]
assert available == 4
6 changes: 2 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ckanext-federated-index
version = 0.1.0
version = 0.1.1
description =
long_description = file: README.md
long_description_content_type = text/markdown
Expand All @@ -27,9 +27,7 @@ include_package_data = True
[options.entry_points]
ckan.plugins =
federated_index = ckanext.federated_index.plugin:FederatedIndexPlugin

ckan.test_plugins =
test_federated_index = ckanext.federated_index.tests.conftest:TestFederatedIndexPlugin
test_federated_index = ckanext.federated_index.tests.conftest:TestFederatedIndexPlugin

babel.extractors =
ckan = ckan.lib.extract:extract_ckan
Expand Down

0 comments on commit da5c436

Please sign in to comment.