Skip to content

Commit

Permalink
refactor: remove duplicated code in postgres indexes (#1805)
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng authored Dec 9, 2024
1 parent ec4e1a1 commit 3f0877d
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 23 deletions.
20 changes: 0 additions & 20 deletions tortoise/contrib/postgres/indexes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from typing import Optional, Tuple

from pypika.terms import Term, ValueWrapper

from tortoise.indexes import PartialIndex


Expand All @@ -10,22 +6,6 @@ class PostgreSQLIndex(PartialIndex):
"CREATE INDEX {exists}{index_name} ON {table_name} USING{index_type}({fields}){extra};"
)

def __init__(
self,
*expressions: Term,
fields: Optional[Tuple[str, ...]] = None,
name: Optional[str] = None,
condition: Optional[dict] = None,
) -> None:
super().__init__(*expressions, fields=fields, name=name)
if condition:
cond = " WHERE "
items = []
for k, v in condition.items():
items.append(f"{k} = {ValueWrapper(v)}")
cond += " AND ".join(items)
self.extra = cond


class BloomIndex(PostgreSQLIndex):
INDEX_TYPE = "BLOOM"
Expand Down
4 changes: 1 addition & 3 deletions tortoise/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ def __init__(
super().__init__(*expressions, fields=fields, name=name)
if condition:
cond = " WHERE "
items = []
for k, v in condition.items():
items.append(f"{k} = {ValueWrapper(v)}")
items = [f"{k} = {ValueWrapper(v)}" for k, v in condition.items()]
cond += " AND ".join(items)
self.extra = cond

0 comments on commit 3f0877d

Please sign in to comment.