mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
Fixes #2665. Regenerated by tabbott with `lint --fix` after a rebase and change in parameters. Note from tabbott: In a few cases, this converts technical debt in the form of unsorted imports into different technical debt in the form of our largest files having very long, ugly import sequences at the start. I expect this change will increase pressure for us to split those files, which isn't a bad thing. Signed-off-by: Anders Kaseorg <anders@zulip.com>
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import time
|
|
from typing import List, TypeVar
|
|
|
|
from psycopg2.extensions import cursor
|
|
from psycopg2.sql import SQL, Composable, Identifier
|
|
|
|
CursorObj = TypeVar('CursorObj', bound=cursor)
|
|
|
|
|
|
def do_batch_update(cursor: CursorObj,
|
|
table: str,
|
|
assignments: List[Composable],
|
|
batch_size: int=10000,
|
|
sleep: float=0.1) -> None: # nocoverage
|
|
# The string substitution below is complicated by our need to
|
|
# support multiple postgres versions.
|
|
stmt = SQL('''
|
|
UPDATE {}
|
|
SET {}
|
|
WHERE id >= %s AND id < %s
|
|
''').format(
|
|
Identifier(table),
|
|
SQL(', ').join(assignments),
|
|
)
|
|
|
|
cursor.execute(SQL("SELECT MIN(id), MAX(id) FROM {}").format(Identifier(table)))
|
|
(min_id, max_id) = cursor.fetchone()
|
|
if min_id is None:
|
|
return
|
|
|
|
print(f"\n Range of rows to update: [{min_id}, {max_id}]")
|
|
while min_id <= max_id:
|
|
lower = min_id
|
|
upper = min_id + batch_size
|
|
print(f' Updating range [{lower},{upper})')
|
|
cursor.execute(stmt, [lower, upper])
|
|
|
|
min_id = upper
|
|
time.sleep(sleep)
|
|
|
|
# Once we've finished, check if any new rows were inserted to the table
|
|
if min_id > max_id:
|
|
cursor.execute(SQL("SELECT MAX(id) FROM {}").format(Identifier(table)))
|
|
(max_id,) = cursor.fetchone()
|
|
|
|
print(" Finishing...", end='')
|