def test_es_only_all_flags(self): movies_index_model, _, _ = self.check_basic_setup_and_get_models( "movies") new_index_name = "moviez" with get_new_search_index(new_index_name) as index_info: moviez_index, moviez_doctype = index_info moviez_index_model, _, _ = self.check_basic_setup_and_get_models( new_index_name) call_command('es_update', new_index_name) num_docs = moviez_index.get_num_docs() expected_msg = "After updating the index, '{}' index should have had two documents in elasticsearch".format( new_index_name) self.assertEqual(num_docs, 2, expected_msg) # at this point we have two indexes, and the one called moviez has two docs in it call_command('es_drop', all=True, force=True, es_only=True) # test that none of our indexes are available es_indexes = DEMIndexManager.list_es_created_indexes() expected_msg = "After calling ./manage.py es_drop --all --force --es-only, es shouldn't have any indexes in it" self.assertEqual(len(es_indexes), 0, expected_msg) for index_model_instance in [ movies_index_model, moviez_index_model ]: available_version_ids = moviez_index_model.indexversion_set.all( ).values_list('id', flat=True) expected_msg = "After es_drop, the {} index should still have one version".format( index_model_instance.name) self.assertEqual(len(available_version_ids), 1, expected_msg) deleted_model = IndexVersion.objects.get( id=available_version_ids[0]) expected_msg = "After es_drop, the soft delete flag on the IndexVersion model id {} should be False".format( deleted_model.id) self.assertFalse(deleted_model.is_deleted, expected_msg)
def handle(self, *args, **options): indexes, exact_mode, apply_all, older_mode, _ = self.get_index_specifying_options( options, require_one_include_list=['es_only']) es_only = options.get('es_only', False) force = options.get('force', False) just_prefix = options.get('just-prefix', None) if es_only: if not indexes and apply_all: if not force: raise CannotDropAllIndexesWithoutForceArg( "When using --es-only, cannot use --all without --force" ) indexes = DEMIndexManager.list_es_created_indexes() count = 0 for index_name in indexes: if just_prefix and not index_name.startswith(just_prefix): continue logger.warning( "Dropping index {} from Elasticsearch only".format( index_name)) DEMIndexManager.delete_es_created_index(index_name, ignore=[404]) count += 1 logger.info( "Completed dropping {} indexes from Elasticsearch only".format( count)) elif apply_all: DEMIndexManager.drop_index('all', exact_mode=exact_mode, force=force, just_prefix=just_prefix, older_mode=older_mode) elif indexes: for index_name in indexes: DEMIndexManager.drop_index(index_name, exact_mode=exact_mode, force=force, just_prefix=just_prefix, older_mode=older_mode)
def test_newer_flag(self): """ Test that when two versions of the "movies" index are available, and the older one is activated, `./manage.py es_update movies --newer` indexes into the newer, unactivated version """ index_model, version_model, dem_index = self.check_basic_setup_and_get_models( ) # create two newer versions of the movies index call_command(*"es_create movies --force".split()) call_command(*"es_create movies --force".split()) avail_versions = self.check_num_available_versions( index_model, "After 2x './manage.py es_create movies --force',", 3) command = "es_update movies --newer" call_command(*command.split()) after_phrase = "After `{}`,".format(command) last_actions = self.check_last_index_actions( index_model, after_phrase, 5, expected_actions=[ # the parent update index action IndexAction.ACTION_UPDATE_INDEX, # 1st newer index update index action IndexAction.ACTION_UPDATE_INDEX, IndexAction.ACTION_PARTIAL_UPDATE_INDEX, # 2nd newer index update index actions IndexAction.ACTION_UPDATE_INDEX, IndexAction.ACTION_PARTIAL_UPDATE_INDEX, ]) first_action = last_actions[0] first_action_version = first_action.index_version self.assertIsNone( first_action_version, "{} expected parent UpdateIndexAction to be None, " "but was {}".format(after_phrase, str(first_action_version))) self.assertEqual( first_action.docs_affected, 4, "{} expected the parent UpdateIndexAction to have " "4 docs affected, but was {}".format(after_phrase, first_action.docs_affected)) actual_num_docs = dem_index.get_num_docs() self.assertEqual( actual_num_docs, 0, "{after_phrase} " "The original IndexVersion {index_name} was expected " "to have 0 docs, instead, it had {actual_num}".format( after_phrase=after_phrase, index_name=version_model.name, actual_num=actual_num_docs)) for i in [1, 3]: action = last_actions[i] self.assertEqual(action.docs_affected, 2) new_version_model = last_actions[i].index_version new_dem_index = DEMIndexManager.get_dem_index( new_version_model.name, exact_mode=True) actual_num_docs = new_dem_index.get_num_docs() self.assertEqual( actual_num_docs, 2, "{after_phrase} " "{index_name} was expected to have " "2 docs, instead, it had {actual_num}".format( after_phrase=after_phrase, index_name=new_version_model, actual_num=actual_num_docs))
def handle(self, *args, **options): logger.info("Available Index Definitions:") indexes, _, apply_all, _, _ = self.get_index_specifying_options( options, require_one_include_list=['es_only']) es_only = options.get('es_only', False) table = Texttable(max_width=85) if es_only: table.header(["Name", "Count"]) es_indexes = DEMIndexManager.list_es_created_indexes() for index in es_indexes: count = DEMIndexManager.get_es_index_doc_count(index) table.add_row([ index, count ]) else: table.header([ "Index Base Name", "Index Version Name", "Created", "Active", "Docs", "Tag"]) table.set_cols_width([20, 35, 7, 6, 5, 9]) indexes = DEMIndexManager.get_indexes() if indexes and not apply_all: new_indexes = [] for index in indexes: if index.get_base_name() in options['index']: new_indexes.append(index) indexes = new_indexes try: for dem_index in indexes: dem_index_model = dem_index.get_index_model() index_versions = dem_index_model.get_available_versions_with_prefix() if index_versions: for index_version in index_versions: num_docs = DEMIndexManager.get_es_index_doc_count(index_version.name) table.add_row([ dem_index_model.name, index_version.name, not (index_version.is_deleted is None), index_version.is_active or 0, num_docs, index_version.tag]) else: table.add_row([ dem_index.get_base_name(), "", False, False, dem_index.get_num_docs(), "Current (not created)"]) except AttributeError: raise FirstMigrationNotRunError() logger.info(table.draw()) logger.info( "An index version name is: \n" "{environment prefix}{index name}-{version primary key id}. \n" "Most Django Elastic Migrations management commands take the \n" "base name (in which case the activated version is used) or \n" "the specific index version name." )
def setUp(self): super(DEMTestCaseMixin, self).setUp() DEMIndexManager.test_post_setup()
def tearDownClass(cls): DEMIndexManager.test_post_teardown() super().tearDownClass()
def tearDown(self): DEMIndexManager.test_pre_teardown() super(DEMTestCaseMixin, self).tearDown()