Skip to content

Commit 6a14935

Browse files
committed
Adding script to enable clearing datastore.
1 parent 95e80e1 commit 6a14935

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

CONTRIBUTING.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ Running Regression Tests
209209

210210
$ python regression/populate_datastore.py
211211

212+
- If you make a mistake during development (i.e. a failing test that
213+
prevents clean-up) you can clear all regression data from your
214+
datastore instance via::
215+
216+
$ python regression//clear_datastore.py
217+
212218
Test Coverage
213219
-------------
214220

regression/clear_datastore.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Script to populate datastore with regression test data."""
2+
3+
4+
# This assumes the command is being run via tox hence the
5+
# repository root is the current directory.
6+
from regression import regression_utils
7+
8+
9+
FETCH_MAX = 20
10+
ALL_KINDS = [
11+
'Character',
12+
'Company',
13+
'Kind',
14+
'Person',
15+
'Post',
16+
]
17+
18+
19+
def remove_kind(dataset, kind):
20+
dataset_id = dataset.id()
21+
connection = dataset.connection()
22+
23+
with dataset.transaction():
24+
query = dataset.query(kind=kind).limit(
25+
FETCH_MAX).projection(['__key__'])
26+
results = []
27+
more_results = True
28+
while more_results:
29+
# Make new query.
30+
if query._cursor is not None:
31+
query = query.with_cursor(query._cursor)
32+
33+
curr_results = query.fetch()
34+
results.extend(curr_results)
35+
36+
more_results = len(curr_results) == FETCH_MAX
37+
38+
# Now that we have all results, we seek to delete.
39+
key_pbs = [entity.key().to_protobuf() for entity in results]
40+
connection.delete_entities(dataset_id, key_pbs)
41+
42+
43+
def remove_all_entities():
44+
print 'This command will remove all entities for the following kinds:'
45+
print '\n'.join(['- ' + val for val in ALL_KINDS])
46+
response = raw_input('Is this OK [y/n]? ')
47+
if response.lower() != 'y':
48+
print 'Doing nothing.'
49+
return
50+
51+
dataset = regression_utils.get_dataset()
52+
for kind in ALL_KINDS:
53+
remove_kind(dataset, kind)
54+
55+
56+
if __name__ == '__main__':
57+
remove_all_entities()

0 commit comments

Comments
 (0)