-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from tarsil/feature/add_index
Feature/add index
- Loading branch information
Showing
14 changed files
with
352 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import saffier | ||
from saffier import Database, Index, Registry | ||
|
||
database = Database("sqlite:///db.sqlite") | ||
models = Registry(database=database) | ||
|
||
|
||
class User(saffier.Model): | ||
name = saffier.CharField(max_length=255) | ||
email = saffier.EmailField(max_length=70) | ||
is_active = saffier.BooleanField(default=True) | ||
status = saffier.CharField(max_length=255) | ||
|
||
class Meta: | ||
registry = models | ||
unique_together = [ | ||
Index(fields=["name", "email"]), | ||
Index(fields=["is_active", "statux"], name="active_status_idx"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import saffier | ||
from saffier import Database, Registry | ||
|
||
database = Database("sqlite:///db.sqlite") | ||
models = Registry(database=database) | ||
|
||
|
||
class User(saffier.Model): | ||
name = saffier.CharField(max_length=255) | ||
email = saffier.EmailField(max_length=70, index=True) | ||
is_active = saffier.BooleanField(default=True) | ||
|
||
class Meta: | ||
registry = models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import saffier | ||
from saffier import Database, Index, Registry | ||
|
||
database = Database("sqlite:///db.sqlite") | ||
models = Registry(database=database) | ||
|
||
|
||
class User(saffier.Model): | ||
name = saffier.CharField(max_length=255) | ||
email = saffier.EmailField(max_length=70) | ||
is_active = saffier.BooleanField(default=True) | ||
|
||
class Meta: | ||
registry = models | ||
indexes = [Index(fields=["email"])] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import typing | ||
|
||
from pydantic import root_validator | ||
from pydantic.dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Index: | ||
""" | ||
Class responsible for handling and declaring the database indexes. | ||
""" | ||
|
||
suffix: str = "idx" | ||
max_name_length: int = 30 | ||
name: typing.Optional[str] = None | ||
fields: typing.Optional[typing.List[str]] = None | ||
|
||
@root_validator | ||
def validate_data(cls, values): | ||
name = values.get("name") | ||
|
||
if name is not None and len(name) > cls.max_name_length: | ||
raise ValueError(f"The max length of the index name must be 30. Got {len(name)}") | ||
|
||
fields = values.get("fields") | ||
if not isinstance(fields, (tuple, list)): | ||
raise ValueError("Index.fields must be a list or a tuple.") | ||
|
||
if fields and not all(isinstance(field, str) for field in fields): | ||
raise ValueError("Index.fields must contain only strings with field names.") | ||
|
||
if name is None: | ||
suffix = values.get("suffix", cls.suffix) | ||
values["name"] = f"{'_'.join(fields)}_{suffix}" | ||
|
||
return values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.