-
-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create command to remove unused data by checking existing code #493
Open
cristianowa
wants to merge
1
commit into
jazzband:master
Choose a base branch
from
cristianowa:create-waffle-delete-unused
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import os | ||
import pathlib | ||
|
||
from django.core.management.base import BaseCommand | ||
from waffle import ( | ||
get_waffle_flag_model, | ||
get_waffle_switch_model, | ||
get_waffle_sample_model, | ||
) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Delete flags, samples, and switches not present in the code from the Database" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--dry-run", | ||
action="store_true", | ||
help="Do not delete anything, just show what would be deleted", | ||
) | ||
parser.add_argument( | ||
"--no-input", | ||
action="store_true", | ||
help="Do not prompt for confirmation", | ||
) | ||
parser.add_argument( | ||
"--switches", | ||
action="store_true", | ||
help="Remove unused switches", | ||
) | ||
parser.add_argument( | ||
"--flags", | ||
action="store_true", | ||
help="Remove unused flags", | ||
) | ||
parser.add_argument( | ||
"--samples", | ||
action="store_true", | ||
help="Remove unused samples", | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
no_input = kwargs["no_input"] | ||
delete_switches = kwargs["switches"] | ||
delete_flags = kwargs["flags"] | ||
delete_samples = kwargs["samples"] | ||
if delete_switches: | ||
self.delete_model(get_waffle_switch_model(), no_input) | ||
if delete_flags: | ||
self.delete_model(get_waffle_flag_model(), no_input) | ||
if delete_samples: | ||
self.delete_model(get_waffle_sample_model(), no_input) | ||
|
||
def delete_model(self, model, no_input): | ||
items = model.objects.all() | ||
for item in items: | ||
if not expression_exists(item.name): | ||
self.stdout.write("%s %s not found in the code" % (model.__name__, item.name)) | ||
if no_input or self.confirm("Delete %s ?" % model.__name__): | ||
self.stdout.write("Deleting switch") | ||
item.delete() | ||
else: | ||
self.stdout.write("%s %s found in the code" % (model.__name__, item.name)) | ||
|
||
def confirm(self, question): | ||
answer = input(question + " [y/N] ").strip() | ||
return answer.lower() == "y" | ||
|
||
|
||
def expression_in_file(expression, filename): | ||
with open(filename) as file: | ||
content = file.read() | ||
return expression in content | ||
|
||
|
||
def expression_exists(expression): | ||
for root, dirs, files in os.walk(os.getcwd()): | ||
for file in files: | ||
if not (file.endswith(".py") or file.endswith(".html")): #TODO: make this a list of extensions | ||
continue | ||
filename = pathlib.Path(root) / file | ||
if expression_in_file(expression, filename): | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,7 +290,7 @@ def test_delete_flag(self): | |
call_command('waffle_delete', flag_names=[name]) | ||
self.assertEqual(get_waffle_flag_model().objects.count(), 0) | ||
|
||
def test_delete_swtich(self): | ||
def test_delete_switch(self): | ||
""" The command should delete a switch. """ | ||
name = 'test_switch' | ||
get_waffle_switch_model().objects.create(name=name) | ||
|
@@ -329,3 +329,44 @@ def test_delete_some_but_not_all_records(self): | |
|
||
call_command('waffle_delete', flag_names=[flag_1]) | ||
self.assertTrue(get_waffle_flag_model().objects.filter(name=flag_2).exists()) | ||
|
||
|
||
class WaffleDeleteUnused(TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests aren't really testing the desired behavior—deleting flags. Consider updating the command to take a file path of code to search, and passing that in the test calls to confirm items are deleted. |
||
def test_delete_switches(self): | ||
# we concat the strings so that the switch is not found in the code | ||
get_waffle_switch_model().objects.create(name="FIRST" + "NEVER_FOUND" + "SWITCH", active=True) | ||
get_waffle_switch_model().objects.create(name="SECOND" + "NEVER_FOUND" + "SWITCH", active=True) | ||
# this test is in the search directory, so this very instance will be found | ||
get_waffle_switch_model().objects.create(name="SWITCH_FOUND", active=True) | ||
call_command('waffle_delete_unused', "--no-input", "--switches") | ||
self.assertEqual(1, get_waffle_switch_model().objects.all().count()) | ||
|
||
def test_delete_samples(self): | ||
# we concat the strings so that the switch is not found in the code | ||
get_waffle_sample_model().objects.create(name="FIRST" + "NEVER_FOUND" + "SAMPLE", percent=0) | ||
get_waffle_sample_model().objects.create(name="SECOND" + "NEVER_FOUND" + "SAMPLE", percent=0) | ||
# this test is in the search directory, so this very instance will be found | ||
get_waffle_sample_model().objects.create(name="SAMPLE_FOUND", percent=0) | ||
call_command('waffle_delete_unused', "--no-input", "--samples") | ||
self.assertEqual(1, get_waffle_sample_model().objects.all().count()) | ||
|
||
def test_delete_flags(self): | ||
# we concat the strings so that the switch is not found in the code | ||
get_waffle_flag_model().objects.create(name="FIRST" + "NEVER_FOUND" + "FLAG") | ||
get_waffle_flag_model().objects.create(name="SECOND" + "NEVER_FOUND" + "FLAG") | ||
# this test is in the search directory, so this very instance will be found | ||
get_waffle_flag_model().objects.create(name="FLAG_FOUND") | ||
call_command('waffle_delete_unused', "--no-input", "--flags") | ||
self.assertEqual(1, get_waffle_flag_model().objects.all().count()) | ||
|
||
def test_deletion_confirmation(self): | ||
from unittest import mock | ||
mock.patch('builtins.input', side_effect=["N\n", "Y\n", "N\n"]).start() | ||
|
||
get_waffle_switch_model().objects.create(name="FIRST" + "NEVER_FOUND" + "SWITCH", active=True) | ||
get_waffle_sample_model().objects.create(name="FIRST" + "NEVER_FOUND" + "SAMPLE", percent=0) | ||
get_waffle_flag_model().objects.create(name="FIRST" + "NEVER_FOUND" + "FLAG") | ||
call_command('waffle_delete_unused', "--flags", "--samples", "--switches") | ||
self.assertEqual(0, get_waffle_flag_model().objects.all().count()) | ||
self.assertEqual(1, get_waffle_sample_model().objects.all().count()) | ||
self.assertEqual(1, get_waffle_switch_model().objects.all().count()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you going to do this? Perhaps allow them to be passed in as an optional argument?