Skip to content

Commit

Permalink
Merge branch 'master' into #45_remove_support_for_python_2
Browse files Browse the repository at this point in the history
# Conflicts:
#	django_elastic_migrations/management/commands/es_deactivate.py
  • Loading branch information
codekiln committed Nov 13, 2018
2 parents 48f67bf + 1d8ad6c commit 35d35da
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 136 deletions.
10 changes: 0 additions & 10 deletions django_elastic_migrations/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,6 @@ def activate_index(cls, index_name, exact_mode=False):
action = ActivateIndexAction()
return cls._start_action_for_indexes(action, index_name, exact_mode)

@classmethod
def deactivate_index(cls, index_name, exact_mode=False):
"""
Given the named index, activate the latest version of the index
"""
# avoid circular import
from django_elastic_migrations.models import DeactivateIndexAction
action = DeactivateIndexAction()
return cls._start_action_for_indexes(action, index_name, exact_mode)

@classmethod
def clear_index(cls, index_name, exact_mode=False, older_mode=False):
"""
Expand Down
1 change: 0 additions & 1 deletion django_elastic_migrations/management/commands/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
'create': 'Create indexes; calls es_create',
'activate': 'Activate indexes; calls es_activate',
'update': 'Update indexes; calls es_update',
'deactivate': 'Deactivate indexes; calls es_drop',
'clear': 'Clears indexes; calls es_clear',
'drop': 'Drop indexes; calls es_drop',
'dangerous_reset': 'Dangerously drops all indexes and recreates all indexes (!)'
Expand Down
24 changes: 0 additions & 24 deletions django_elastic_migrations/management/commands/es_deactivate.py
Original file line number Diff line number Diff line change
@@ -1,24 +0,0 @@
from django_elastic_migrations import DEMIndexManager
from django_elastic_migrations.management.commands.es import ESCommand


class Command(ESCommand):
help = "django-elastic-migrations: deactivate an index"

def add_arguments(self, parser):
self.get_index_specifying_arguments(parser)

def handle(self, *args, **options):
indexes, exact_mode, apply_all, _, _ = self.get_index_specifying_options(options)

if apply_all:
DEMIndexManager.deactivate_index(
'all',
exact_mode=exact_mode,
)
elif indexes:
for index_name in indexes:
DEMIndexManager.deactivate_index(
index_name,
exact_mode=exact_mode,
)
23 changes: 23 additions & 0 deletions django_elastic_migrations/migrations/0007_auto_20181112_0937.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2018-11-12 09:37
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('django_elastic_migrations', '0006_auto_20180709_2101'),
]

operations = [
migrations.DeleteModel(
name='DeactivateIndexAction',
),
migrations.AlterField(
model_name='indexaction',
name='action',
field=models.CharField(choices=[('create_index', 'create_index'), ('update_index', 'update_index'), ('activate_index', 'activate_index'), ('clear_index', 'clear_index'), ('drop_index', 'drop_index'), ('partial_update_index', 'partial_update_index')], max_length=64),
),
]
107 changes: 6 additions & 101 deletions django_elastic_migrations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ def get_new_version(self, dem_index=None):
version.save()
return version

def deactivate(self):
"""
Remove any active version from this index
"""
if self.active_version:
self.active_version = None
self.save()

def get_available_versions(self):
return self.indexversion_set.filter(deleted_time__isnull=True)

Expand Down Expand Up @@ -216,13 +208,12 @@ class IndexAction(models.Model):
ACTION_CREATE_INDEX = 'create_index'
ACTION_UPDATE_INDEX = 'update_index'
ACTION_ACTIVATE_INDEX = 'activate_index'
ACTION_DEACTIVATE_INDEX = 'deactivate_index'
ACTION_CLEAR_INDEX = 'clear_index'
ACTION_DROP_INDEX = 'drop_index'
ACTION_PARTIAL_UPDATE_INDEX = 'partial_update_index'
ACTIONS_ALL = [
ACTION_CREATE_INDEX, ACTION_UPDATE_INDEX, ACTION_ACTIVATE_INDEX,
ACTION_DEACTIVATE_INDEX, ACTION_CLEAR_INDEX, ACTION_DROP_INDEX, ACTION_PARTIAL_UPDATE_INDEX
ACTION_CLEAR_INDEX, ACTION_DROP_INDEX, ACTION_PARTIAL_UPDATE_INDEX
]
ACTIONS_ALL_CHOICES = [(i, i) for i in ACTIONS_ALL]

Expand Down Expand Up @@ -497,7 +488,6 @@ class ActivateIndexAction(IndexAction):
DEFAULT_ACTION = IndexAction.ACTION_ACTIVATE_INDEX

def __init__(self, *args, **kwargs):
self.deactivate = kwargs.pop('deactivate', False)
super(ActivateIndexAction, self).__init__(*args, **kwargs)

class Meta:
Expand All @@ -513,16 +503,10 @@ def perform_action(self, dem_index, *args, **kwargs):
self.index_version = version_model
index = self.index

if self.deactivate and index.active_version == version_model:
index.active_version = None
self.add_log(
"Deactivating index version '{index_version_name}' "
"because you said to do so.".format(**msg_params))
else:
index.active_version = version_model
self.add_log(
"Activating index version '{index_version_name}' "
"because you said to do so.".format(**msg_params))
index.active_version = version_model
self.add_log(
"Activating index version '{index_version_name}' "
"because you said to do so.".format(**msg_params))
index.save()
# by reinitializing, we ensure this worker knows about the update immediately
DEMIndexManager.initialize()
Expand All @@ -543,22 +527,7 @@ def perform_action(self, dem_index, *args, **kwargs):
active_version = self.index.active_version
msg_params.update({"index_version_name": latest_version.name})

if self.deactivate:
if self.index.active_version:
self.index.active_version = None
self.add_log(
"For index '{index_name}', DEactivating "
"'{index_version_name}' "
"because you said so.".format(
**msg_params))
self.index.save()
else:
self.add_log(
"For index '{index_name}', there is no active version; "
"so there is no version to deactivate. \n"
"No action performed.".format(**msg_params)
)
elif active_version != latest_version:
if active_version != latest_version:
self.index.active_version = latest_version
self.index.save()
# by reinitializing, we ensure this worker knows about the update immediately
Expand Down Expand Up @@ -712,70 +681,6 @@ def perform_action(self, dem_index, *args, **kwargs):
)


class DeactivateIndexAction(IndexAction):
DEFAULT_ACTION = IndexAction.ACTION_DEACTIVATE_INDEX

class Meta:
# https://docs.djangoproject.com/en/2.0/topics/db/models/#proxy-models
proxy = True

def perform_action(self, dem_index, *args, **kwargs):
msg_params = {"index_name": self.index.name}
if dem_index.get_version_id():
# we have instantiated this DEMIndex with a specific IndexVersion
version_model = dem_index.get_version_model()
msg_params.update({"index_version_name": version_model.name})
self.index_version = version_model
index = self.index

index.active_version = None
if index.active_version == version_model:
self.add_log(
"Deactivating formerly active index version "
"'{index_version_name}' "
"because you said to do so.".format(**msg_params))
index.save()
# re-initialize so as to ensure this worker gets the message
DEMIndexManager.initialize()
else:
self.add_log(
"There is no need to deactivate '{index_version_name}' "
"because is it not active.".format(**msg_params))

else:
# use the active version of the index if one exists.

# first, check if *any* version exists.
latest_version = self.index.get_latest_version()
if not latest_version:
raise NoCreatedIndexVersion(
"You must have created a version of the "
"'{index_name}' index to call es_deactivate "
"index.".format(**msg_params)
)

# at least one version is available.

msg_params.update({"index_version_name": latest_version.name})

if self.index.active_version:
self.index.active_version = None
self.add_log(
"For index '{index_name}', DEactivating "
"'{index_version_name}' "
"because you said so.".format(
**msg_params))
self.index.save()
# re-initialize so as to ensure this worker gets the message
DEMIndexManager.initialize()
else:
self.add_log(
"For index '{index_name}', there is no active version; "
"so there is no version to deactivate. \n"
"No action performed.".format(**msg_params)
)


class DropIndexAction(OlderModeMixin, IndexAction):
DEFAULT_ACTION = IndexAction.ACTION_DROP_INDEX

Expand Down

0 comments on commit 35d35da

Please sign in to comment.