Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #61 from Kinto/fix-reindex-command
Browse files Browse the repository at this point in the history
Stop as soon as we reached last page.
  • Loading branch information
Natim authored Apr 11, 2018
2 parents d5ebc54 + 3269de3 commit 7469383
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ Changelog
=========


0.4.0 (unreleased)
0.3.1 (unreleased)
------------------

- Nothing changed yet.
**Bug fixes**

- Fix the reindex get_paginated_records function. (fixes #61)


0.3.0 (2017-09-12)
Expand Down
5 changes: 3 additions & 2 deletions kinto_elasticsearch/command_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ def get_paginated_records(storage, bucket_id, collection_id, limit=5000):
pagination_rules=pagination_rules,
sorting=sorting,
limit=limit)
if len(records) == 0:
break # Done.

yield records

if len(records) < limit:
break # Done.

smallest_timestamp = records[-1]["last_modified"]
pagination_rules = [
[Filter("last_modified", smallest_timestamp, COMPARISON.LT)]
Expand Down
16 changes: 15 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import mock
import os
import unittest
from kinto_elasticsearch.command_reindex import main, reindex_records
from kinto_elasticsearch.command_reindex import main, reindex_records, get_paginated_records
from . import BaseWebTest

HERE = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -83,3 +83,17 @@ def test_cli_default_to_sys_argv(self):
with mock.patch('sys.argv', ['cli', '--ini', os.path.join(HERE, 'wrong_config.ini')]):
exit_code = main()
assert exit_code == 62

def test_get_paginated_records(self):
# Create collection or bucket
self.app.put("/buckets/bid", headers=self.headers)
body = {"data": {"index:schema": self.schema}}
self.app.put_json("/buckets/bid/collections/cid", body, headers=self.headers)
for i in range(5):
self.app.post_json("/buckets/bid/collections/cid/records",
{"data": {"build": {"id": "efg%d" % i, "date": "2017-02-01"}}},
headers=self.headers)
page_count = 0
for records in get_paginated_records(self.app.app.registry.storage, 'bid', 'cid', limit=3):
page_count += 1
assert page_count == 2

0 comments on commit 7469383

Please sign in to comment.