2018-05-10 19:13:36 +02:00
|
|
|
from typing import Any, Callable, Dict, List, Tuple
|
2016-03-27 11:28:43 +02:00
|
|
|
from django.db.models.query import QuerySet
|
2014-02-26 21:50:36 +01:00
|
|
|
import re
|
|
|
|
import time
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def create_index_if_not_exist(index_name: str, table_name: str, column_string: str,
|
|
|
|
where_clause: str) -> str:
|
2017-06-05 15:10:08 +02:00
|
|
|
#
|
|
|
|
# FUTURE TODO: When we no longer need to support postgres 9.3 for Trusty,
|
|
|
|
# we can use "IF NOT EXISTS", which is part of postgres 9.5
|
|
|
|
# (and which already is supported on Xenial systems).
|
|
|
|
stmt = '''
|
|
|
|
DO $$
|
|
|
|
BEGIN
|
|
|
|
IF NOT EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM pg_class
|
|
|
|
where relname = '%s'
|
|
|
|
) THEN
|
|
|
|
CREATE INDEX
|
|
|
|
%s
|
|
|
|
ON %s (%s)
|
|
|
|
%s;
|
|
|
|
END IF;
|
|
|
|
END$$;
|
|
|
|
''' % (index_name, index_name, table_name, column_string, where_clause)
|
|
|
|
return stmt
|
2014-03-11 18:17:52 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def act_on_message_ranges(db: Any,
|
|
|
|
orm: Dict[str, Any],
|
|
|
|
tasks: List[Tuple[Callable[[QuerySet], QuerySet], Callable[[QuerySet], None]]],
|
|
|
|
batch_size: int=5000,
|
|
|
|
sleep: float=0.5) -> None:
|
2014-03-11 18:17:52 +01:00
|
|
|
# tasks should be an array of (filterer, action) tuples
|
|
|
|
# where filterer is a function that returns a filtered QuerySet
|
|
|
|
# and action is a function that acts on a QuerySet
|
|
|
|
|
|
|
|
all_objects = orm['zerver.Message'].objects
|
2014-03-12 18:39:24 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
min_id = all_objects.all().order_by('id')[0].id
|
|
|
|
except IndexError:
|
2015-11-01 17:11:06 +01:00
|
|
|
print('There is no work to do')
|
2014-03-12 18:39:24 +01:00
|
|
|
return
|
|
|
|
|
2014-03-11 18:17:52 +01:00
|
|
|
max_id = all_objects.all().order_by('-id')[0].id
|
2015-11-01 17:11:06 +01:00
|
|
|
print("max_id = %d" % (max_id,))
|
2016-11-28 23:29:01 +01:00
|
|
|
overhead = int((max_id + 1 - min_id) / batch_size * sleep / 60)
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Expect this to take at least %d minutes, just due to sleeps alone." % (overhead,))
|
2014-03-11 18:17:52 +01:00
|
|
|
|
|
|
|
while min_id <= max_id:
|
|
|
|
lower = min_id
|
|
|
|
upper = min_id + batch_size - 1
|
|
|
|
if upper > max_id:
|
|
|
|
upper = max_id
|
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print('%s about to update range %s to %s' % (time.asctime(), lower, upper))
|
2014-03-11 18:17:52 +01:00
|
|
|
|
|
|
|
db.start_transaction()
|
|
|
|
for filterer, action in tasks:
|
|
|
|
objects = all_objects.filter(id__range=(lower, upper))
|
|
|
|
targets = filterer(objects)
|
|
|
|
action(targets)
|
|
|
|
db.commit_transaction()
|
|
|
|
|
|
|
|
min_id = upper + 1
|
|
|
|
time.sleep(sleep)
|