Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove duplicated code in postgres indexes #1805

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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