-
Notifications
You must be signed in to change notification settings - Fork 90
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
Multiple Unique Constraints with auto migrations #582
Open
northpowered
wants to merge
6
commits into
piccolo-orm:master
Choose a base branch
from
northpowered:dev_multiple_unique_constraints
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ htmlcov/ | |
prof/ | ||
.env/ | ||
.venv/ | ||
result.json | ||
result.json |
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 |
---|---|---|
@@ -1 +1 @@ | ||
__VERSION__ = "0.82.0" | ||
__VERSION__ = "0.82.0" |
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
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,42 @@ | ||
from .base import ColumnMeta, Column | ||
|
||
class Constraint(Column): | ||
def __init__(self) -> None: | ||
pass | ||
|
||
class UniqueConstraint(Constraint): | ||
""" | ||
This class represents UNIQUE CONSTRAINT, which can be use to create | ||
many complex (multi-field) constraints for the Table | ||
All manipulations with the Constraint are like anything about Columns | ||
|
||
Usage: | ||
class FooTable(Table): | ||
foo_field = Text() | ||
bar_field = Text() | ||
my_constraint_1 = UniqueConstraint(['foo_field','bar_field']) | ||
|
||
SQL queries for creating and dropping constrains are similar to: | ||
ALTER TABLE foo_table ADD CONSTRAINT my_constraint_1 UNIQUE (foo_field, bar_field); | ||
ALTER TABLE foo_table DROP IF EXIST CONSTRAINT my_constraint_1; | ||
""" | ||
def __init__(self, unique_columns: list[str]) -> None: | ||
super().__init__() | ||
self._meta = ColumnMeta() | ||
self.unique_columns = unique_columns | ||
self._meta.params.update({ | ||
'unique_columns':self.unique_columns | ||
}) | ||
|
||
@property | ||
def column_type(self): | ||
return "CONSTRAINT" | ||
|
||
@property | ||
def ddl(self) -> str: | ||
""" | ||
Used when creating tables. | ||
""" | ||
unique_columns_string = ",".join(self.unique_columns) | ||
query = f'{self.column_type} "{self._meta.db_column_name}" UNIQUE ({unique_columns_string})' | ||
return query |
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 |
---|---|---|
|
@@ -176,6 +176,17 @@ class SetLength(AlterColumnStatement): | |
def ddl(self) -> str: | ||
return f'ALTER COLUMN "{self.column_name}" TYPE VARCHAR({self.length})' | ||
|
||
@dataclass | ||
class AddUniqueConstraint(AlterStatement): | ||
__slots__ = ("constraint_name","columns") | ||
|
||
constraint_name: str | ||
columns: list[str] | ||
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. Same as above -> |
||
|
||
@property | ||
def ddl(self) -> str: | ||
columns_str: str = ",".join(self.columns) | ||
return f"ADD CONSTRAINT {self.constraint_name} UNIQUE ({columns_str})" | ||
|
||
@dataclass | ||
class DropConstraint(AlterStatement): | ||
|
@@ -264,6 +275,7 @@ class Alter(DDL): | |
__slots__ = ( | ||
"_add_foreign_key_constraint", | ||
"_add", | ||
"_add_unique_constraint", | ||
"_drop_constraint", | ||
"_drop_default", | ||
"_drop_table", | ||
|
@@ -282,6 +294,7 @@ def __init__(self, table: t.Type[Table], **kwargs): | |
super().__init__(table, **kwargs) | ||
self._add_foreign_key_constraint: t.List[AddForeignKeyConstraint] = [] | ||
self._add: t.List[AddColumn] = [] | ||
self._add_unique_constraint: t.List[AddUniqueConstraint] = [] | ||
self._drop_constraint: t.List[DropConstraint] = [] | ||
self._drop_default: t.List[DropDefault] = [] | ||
self._drop_table: t.Optional[DropTable] = None | ||
|
@@ -433,6 +446,12 @@ def _get_constraint_name(self, column: t.Union[str, ForeignKey]) -> str: | |
tablename = self.table._meta.tablename | ||
return f"{tablename}_{column_name}_fk" | ||
|
||
def add_unique_constraint(self, constraint_name: str, columns: list[str]): | ||
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. See above. |
||
self._add_unique_constraint.append( | ||
AddUniqueConstraint(constraint_name=constraint_name,columns=columns) | ||
) | ||
return self | ||
|
||
def drop_constraint(self, constraint_name: str) -> Alter: | ||
self._drop_constraint.append( | ||
DropConstraint(constraint_name=constraint_name) | ||
|
@@ -520,6 +539,8 @@ def default_ddl(self) -> t.Sequence[str]: | |
self._set_length, | ||
self._set_default, | ||
self._set_digits, | ||
self._add_unique_constraint, | ||
self._drop_constraint, | ||
) | ||
] | ||
|
||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this is why the tests are failing.
We can't use this newer syntax yet, because we're keeping backwards compatibility with Python 3.7.
So it'll have to be
typing.List[str]
instead.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 used to Python3.10, so I forgot about older versions