-
-
Notifications
You must be signed in to change notification settings - Fork 374
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
create unqiue index in indexes and related children in contrib.postgres/sqlite.indexes #1642
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Dict, Optional, Tuple | ||
|
||
from pypika.terms import Term, ValueWrapper | ||
|
||
from tortoise.indexes import PartialIndex | ||
|
||
|
||
class SqliteIndex(PartialIndex): | ||
INDEX_CREATE_TEMPLATE = PartialIndex.INDEX_CREATE_TEMPLATE.replace("INDEX", "INDEX {exists} ") | ||
|
||
|
||
class SqliteUniqueIndex(SqliteIndex): | ||
INDEX_TYPE = "unique".upper() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just |
||
|
||
def __init__( | ||
self, | ||
*expressions: Term, | ||
fields: Optional[Tuple[str, ...]] = None, | ||
name: Optional[str] = None, | ||
condition: Optional[Dict[str, str]] = None, | ||
where_expre: Optional[str] = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a little at lost here |
||
): | ||
_condition = condition | ||
if condition: | ||
condition = None | ||
super().__init__(*expressions, fields=fields, name=name, condition=condition) | ||
if _condition: | ||
# TODO: what is the best practice to inject where expression? | ||
self.extra = where_expre or self._gen_condition(_condition) | ||
|
||
@classmethod | ||
def _gen_field_cond(cls, kv: tuple): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add return typing |
||
key, cond = kv | ||
op = ( | ||
"" | ||
if isinstance(cond, str) | ||
and cond.strip().lower().startswith(("is ", "isnot ", "<", ">", "=", "!=")) | ||
else "=" | ||
) | ||
if op == "=": | ||
cond = ValueWrapper(cond) | ||
return str(f"{key} {op} {cond}") | ||
|
||
def _gen_condition(self, conditions: Dict[str, str]): | ||
conditions = " AND ".join(tuple(map(self._gen_field_cond, conditions.items()))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use comprehensions
With that you will also be able to make better signature and typing for |
||
return f" where {conditions}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was wrong about this, I forgot that postgresql allows only default b-tree index to be unique, so it is something we don't need to support