Skip to content

Commit

Permalink
Merge pull request #34633 from dimagi/ap/add-delete-only-if-index-exs…
Browse files Browse the repository at this point in the history
…ists

Make existing es migrations more tolerant to errors
  • Loading branch information
AmitPhulera authored May 17, 2024
2 parents 94e8bf6 + 007623b commit cd6b2c0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 16 deletions.
27 changes: 27 additions & 0 deletions corehq/apps/es/migration_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,32 @@ def reverse_run(self, *args, **kw):
return None


class DeleteOnlyIfIndexExists(DeleteIndex):
"""
The class will not error out if trying to delete the indices that don't exist.
The utility of this class would generally be in case of exceptions where a migration
has to be rolled back which were already applied on some of the environments.
Because of the nature of the operation, this class is not integrated into `make_elastic_migration` command.
"""
def __init__(self, name, es_versions=[]):
# To satisfy parent class signature adding dummy reverse params
# Reverse for this class would be no-op
dummy_reverse_params = ('type', {}, {}, 'setting_key')
super().__init__(name, dummy_reverse_params, es_versions)

def run(self, *args, **kwargs):
if self.es_versions and self._should_skip_operation(self.es_versions):
return
from corehq.apps.es.client import manager
if manager.index_exists(self.name):
return super().run(*args, **kwargs)
log.info(f"ElasticSearch index {self.name} does not exist. Skipping delete index operation.")

def reverse_run(self, *args, **kw):
return None


class MappingUpdateFailed(Exception):
"""The mapping update operation failed."""
16 changes: 8 additions & 8 deletions corehq/apps/es/migrations/0009_add_indices_for_reindex_in_es5.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Migration(migrations.Migration):
]

operations = [
corehq.apps.es.migration_operations.CreateIndex(
corehq.apps.es.migration_operations.CreateIndexIfNotExists(
name=index_runtime_name('apps-2024-03-09'),
type_='app',
mapping={
Expand All @@ -32,7 +32,7 @@ class Migration(migrations.Migration):
settings_key='hqapps',
es_versions=[5],
),
corehq.apps.es.migration_operations.CreateIndex(
corehq.apps.es.migration_operations.CreateIndexIfNotExists(
name=index_runtime_name('case-search-2024-03-09'),
type_='case',
mapping={
Expand All @@ -49,7 +49,7 @@ class Migration(migrations.Migration):
settings_key='case_search',
es_versions=[5],
),
corehq.apps.es.migration_operations.CreateIndex(
corehq.apps.es.migration_operations.CreateIndexIfNotExists(
name=index_runtime_name('cases-2024-03-09'),
type_='case',
mapping={
Expand All @@ -64,7 +64,7 @@ class Migration(migrations.Migration):
settings_key='hqcases',
es_versions=[5],
),
corehq.apps.es.migration_operations.CreateIndex(
corehq.apps.es.migration_operations.CreateIndexIfNotExists(
name=index_runtime_name('domains-2024-03-09'),
type_='hqdomain',
mapping={
Expand All @@ -80,7 +80,7 @@ class Migration(migrations.Migration):
settings_key='hqdomains',
es_versions=[5],
),
corehq.apps.es.migration_operations.CreateIndex(
corehq.apps.es.migration_operations.CreateIndexIfNotExists(
name=index_runtime_name('forms-2024-03-09'),
type_='xform',
mapping={
Expand All @@ -95,7 +95,7 @@ class Migration(migrations.Migration):
settings_key='xforms',
es_versions=[5],
),
corehq.apps.es.migration_operations.CreateIndex(
corehq.apps.es.migration_operations.CreateIndexIfNotExists(
name=index_runtime_name('groups-2024-03-09'),
type_='group',
mapping={
Expand All @@ -110,7 +110,7 @@ class Migration(migrations.Migration):
settings_key='hqgroups',
es_versions=[5],
),
corehq.apps.es.migration_operations.CreateIndex(
corehq.apps.es.migration_operations.CreateIndexIfNotExists(
name=index_runtime_name('sms-2024-03-09'),
type_='sms',
mapping={
Expand All @@ -126,7 +126,7 @@ class Migration(migrations.Migration):
settings_key='smslogs',
es_versions=[5],
),
corehq.apps.es.migration_operations.CreateIndex(
corehq.apps.es.migration_operations.CreateIndexIfNotExists(
name=index_runtime_name('users-2024-03-09'),
type_='user',
mapping={
Expand Down
16 changes: 8 additions & 8 deletions corehq/apps/es/migrations/0010_delete_reverted_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@ class Migration(migrations.Migration):
]

operations = [
corehq.apps.es.migration_operations.DeleteIndex(
corehq.apps.es.migration_operations.DeleteOnlyIfIndexExists(
name=index_runtime_name("apps-2024-03-09"),
es_versions=[5],
),
corehq.apps.es.migration_operations.DeleteIndex(
corehq.apps.es.migration_operations.DeleteOnlyIfIndexExists(
name=index_runtime_name("case-search-2024-03-09"),
es_versions=[5],
),
corehq.apps.es.migration_operations.DeleteIndex(
corehq.apps.es.migration_operations.DeleteOnlyIfIndexExists(
name=index_runtime_name("cases-2024-03-09"),
es_versions=[5],
),
corehq.apps.es.migration_operations.DeleteIndex(
corehq.apps.es.migration_operations.DeleteOnlyIfIndexExists(
name=index_runtime_name("domains-2024-03-09"),
es_versions=[5],
),
corehq.apps.es.migration_operations.DeleteIndex(
corehq.apps.es.migration_operations.DeleteOnlyIfIndexExists(
name=index_runtime_name("forms-2024-03-09"),
es_versions=[5],
),
corehq.apps.es.migration_operations.DeleteIndex(
corehq.apps.es.migration_operations.DeleteOnlyIfIndexExists(
name=index_runtime_name("groups-2024-03-09"),
es_versions=[5],
),
corehq.apps.es.migration_operations.DeleteIndex(
corehq.apps.es.migration_operations.DeleteOnlyIfIndexExists(
name=index_runtime_name("sms-2024-03-09"),
es_versions=[5],
),
corehq.apps.es.migration_operations.DeleteIndex(
corehq.apps.es.migration_operations.DeleteOnlyIfIndexExists(
name=index_runtime_name("users-2024-03-09"),
es_versions=[5],
),
Expand Down
33 changes: 33 additions & 0 deletions corehq/apps/es/tests/test_migration_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
CreateIndex,
CreateIndexIfNotExists,
DeleteIndex,
DeleteOnlyIfIndexExists,
MappingUpdateFailed,
UpdateIndexMapping,
make_mapping_meta,
Expand Down Expand Up @@ -374,6 +375,38 @@ def test_reverse_is_noop(self):
self.assertIndexExists(self.index)


@es_test
class TestDeleteOnlyIfIndexExists(BaseCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.DeleteOnlyIfIndexExists = DeleteOnlyIfIndexExists

def test_deletes_index_if_exists(self):
migration = TestMigration(self.DeleteOnlyIfIndexExists(self.index))
manager.index_create(self.index)
self.assertIndexExists(self.index)
migration.apply()
# Index is deleted after running the migrations
self.assertIndexDoesNotExist(self.index)

def test_does_not_fail_if_index_does_not_exists(self):
self.assertIndexDoesNotExist(self.index)
migration = TestMigration(self.DeleteOnlyIfIndexExists(self.index))
migration.apply()
# Index still does not exist and no errors were raised in the tests.
self.assertIndexDoesNotExist(self.index)

def test_reverse_is_noop(self):
manager.index_create(self.index)
migration = TestMigration(self.DeleteOnlyIfIndexExists(self.index))
migration.apply()
self.assertIndexDoesNotExist(self.index)
migration.unapply()
self.assertIndexDoesNotExist(self.index)


@es_test
class TestDeleteIndex(BaseCase):

Expand Down

0 comments on commit cd6b2c0

Please sign in to comment.