Skip to content
Snippets Groups Projects
Commit 4198eba6 authored by Sid Verma's avatar Sid Verma Committed by Kyle McCormick
Browse files

Add schema version in library indexes, improve reindexing command

parent b56f3d60
Branches
Tags
No related merge requests found
...@@ -39,6 +39,8 @@ class ContentLibraryIndexer: ...@@ -39,6 +39,8 @@ class ContentLibraryIndexer:
INDEX_NAME = "content_library_index" INDEX_NAME = "content_library_index"
LIBRARY_DOCUMENT_TYPE = "content_library" LIBRARY_DOCUMENT_TYPE = "content_library"
SCHEMA_VERSION = 0
@classmethod @classmethod
def index_libraries(cls, library_keys): def index_libraries(cls, library_keys):
""" """
...@@ -60,7 +62,10 @@ class ContentLibraryIndexer: ...@@ -60,7 +62,10 @@ class ContentLibraryIndexer:
bundle_metadata = get_bundle(ref.bundle_uuid) bundle_metadata = get_bundle(ref.bundle_uuid)
# NOTE: Increment ContentLibraryIndexer.SCHEMA_VERSION if the following schema is updated to avoid dealing
# with outdated indexes which might cause errors due to missing/invalid attributes.
library_dict = { library_dict = {
"schema_version": ContentLibraryIndexer.SCHEMA_VERSION,
"id": str(library_key), "id": str(library_key),
"uuid": str(bundle_metadata.uuid), "uuid": str(bundle_metadata.uuid),
"title": bundle_metadata.title, "title": bundle_metadata.title,
...@@ -84,7 +89,10 @@ class ContentLibraryIndexer: ...@@ -84,7 +89,10 @@ class ContentLibraryIndexer:
library_keys_str = [str(key) for key in library_keys] library_keys_str = [str(key) for key in library_keys]
response = searcher.search( response = searcher.search(
doc_type=cls.LIBRARY_DOCUMENT_TYPE, doc_type=cls.LIBRARY_DOCUMENT_TYPE,
field_dictionary={"id": library_keys_str}, field_dictionary={
"id": library_keys_str,
"schema_version": ContentLibraryIndexer.SCHEMA_VERSION
},
size=MAX_SIZE, size=MAX_SIZE,
) )
......
""" Management command to update content libraries' search index """ """ Management command to update content libraries' search index """
import logging
from textwrap import dedent from textwrap import dedent
from django.core.management import BaseCommand from django.core.management import BaseCommand
...@@ -43,20 +45,29 @@ class Command(BaseCommand): ...@@ -43,20 +45,29 @@ class Command(BaseCommand):
dest='all', dest='all',
help='Reindex all libraries' help='Reindex all libraries'
) )
parser.add_argument(
'--force',
action='store_true',
dest='force',
help='Run command without user prompt for confirmation'
)
parser.add_argument('library_ids', nargs='*') parser.add_argument('library_ids', nargs='*')
def handle(self, *args, **options): def handle(self, *args, **options):
if options['clear-all']: if options['clear-all']:
if query_yes_no(self.CONFIRMATION_PROMPT_CLEAR, default="no"): if options['force'] or query_yes_no(self.CONFIRMATION_PROMPT_CLEAR, default="no"):
logging.info("Removing all libraries from the index")
ContentLibraryIndexer.remove_all_libraries() ContentLibraryIndexer.remove_all_libraries()
return return
if options['all']: if options['all']:
if query_yes_no(self.CONFIRMATION_PROMPT_ALL, default="no"): if options['force'] or query_yes_no(self.CONFIRMATION_PROMPT_ALL, default="no"):
logging.info("Indexing all libraries")
library_keys = [library.library_key for library in ContentLibrary.objects.all()] library_keys = [library.library_key for library in ContentLibrary.objects.all()]
else: else:
return return
else: else:
logging.info("Indexing libraries: {}".format(options['library_ids']))
library_keys = list(map(LibraryLocatorV2.from_string, options['library_ids'])) library_keys = list(map(LibraryLocatorV2.from_string, options['library_ids']))
ContentLibraryIndexer.index_libraries(library_keys) ContentLibraryIndexer.index_libraries(library_keys)
...@@ -3,9 +3,11 @@ Testing indexing of blockstore based content libraries ...@@ -3,9 +3,11 @@ Testing indexing of blockstore based content libraries
""" """
from django.conf import settings from django.conf import settings
from django.core.management import call_command
from django.test.utils import override_settings from django.test.utils import override_settings
from search.search_engine_base import SearchEngine from mock import patch
from opaque_keys.edx.locator import LibraryLocatorV2 from opaque_keys.edx.locator import LibraryLocatorV2
from search.search_engine_base import SearchEngine
from openedx.core.djangoapps.content_libraries.libraries_index import ContentLibraryIndexer, LibraryNotIndexedException from openedx.core.djangoapps.content_libraries.libraries_index import ContentLibraryIndexer, LibraryNotIndexedException
from openedx.core.djangoapps.content_libraries.tests.base import ContentLibrariesRestApiTest from openedx.core.djangoapps.content_libraries.tests.base import ContentLibrariesRestApiTest
...@@ -47,6 +49,24 @@ class ContentLibraryIndexerIndexer(ContentLibrariesRestApiTest): ...@@ -47,6 +49,24 @@ class ContentLibraryIndexerIndexer(ContentLibrariesRestApiTest):
self.assertEqual(response['has_unpublished_changes'], False) self.assertEqual(response['has_unpublished_changes'], False)
self.assertEqual(response['has_unpublished_deletes'], False) self.assertEqual(response['has_unpublished_deletes'], False)
def test_schema_updates(self):
"""
Test that outdated indexes aren't retrieved
"""
result = self._create_library(slug="test-lib-schemaupdates-1", title="Title 1", description="Description")
library_key = LibraryLocatorV2.from_string(result['id'])
ContentLibraryIndexer.get_libraries([library_key])
with patch("openedx.core.djangoapps.content_libraries.libraries_index.ContentLibraryIndexer.SCHEMA_VERSION",
new=1):
with self.assertRaises(LibraryNotIndexedException):
ContentLibraryIndexer.get_libraries([library_key])
call_command("reindex_content_library", all=True, quiet=True)
ContentLibraryIndexer.get_libraries([library_key])
def test_remove_all_libraries(self): def test_remove_all_libraries(self):
""" """
Test if remove_all_libraries() deletes all libraries Test if remove_all_libraries() deletes all libraries
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment