Skip to content

Commit

Permalink
[IMP] util/pg: deduplicate "{parallel_filter}"
Browse files Browse the repository at this point in the history
As usual, it reduces chances of stupid mistakes (e.g. typos).
  • Loading branch information
Pirols committed Sep 26, 2024
1 parent e800de2 commit 2b341f2
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/util/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

ON_DELETE_ACTIONS = frozenset(("SET NULL", "CASCADE", "RESTRICT", "NO ACTION", "SET DEFAULT"))
MAX_BUCKETS = int(os.getenv("MAX_BUCKETS", "150000"))
PARALLEL_FILTER = "{parallel_filter}"


class PGRegexp(str):
Expand Down Expand Up @@ -252,9 +253,9 @@ def explode_query_range(cr, query, table, alias=None, bucket_size=10000, prefix=

alias = alias or table

if "{parallel_filter}" not in query:
if PARALLEL_FILTER not in query:
sep_kw = " AND " if re.search(r"\sWHERE\s", query, re.M | re.I) else " WHERE "
query += sep_kw + "{parallel_filter}"
query += sep_kw + PARALLEL_FILTER

cr.execute(format_query(cr, "SELECT min(id), max(id) FROM {}", table))
min_id, max_id = cr.fetchone()
Expand Down Expand Up @@ -300,7 +301,7 @@ def explode_query_range(cr, query, table, alias=None, bucket_size=10000, prefix=
return [query.replace("{parallel_filter}", parallel_filter)]

parallel_filter = "{alias}.id BETWEEN %(lower-bound)s AND %(upper-bound)s".format(alias=alias)
query = query.replace("%", "%%").replace("{parallel_filter}", parallel_filter)
query = query.replace("%", "%%").replace(PARALLEL_FILTER, parallel_filter)
return [
cr.mogrify(query, {"lower-bound": ids[i], "upper-bound": ids[i + 1] - 1}).decode() for i in range(len(ids) - 1)
]
Expand Down

0 comments on commit 2b341f2

Please sign in to comment.