-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename GUID to UUID and stop supporting databases other than Postgres…
…ql with it
- Loading branch information
Denis Krienbühl
committed
Feb 6, 2015
1 parent
c658564
commit cd3e44c
Showing
7 changed files
with
66 additions
and
84 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
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,5 +1,5 @@ | ||
from .guid import GUID | ||
from .utcdatetime import UTCDateTime | ||
from .json_type import JSON | ||
from .utcdatetime import UTCDateTime | ||
from .uuid_type import UUID | ||
|
||
__all__ = ['GUID', 'UTCDateTime', 'JSON'] | ||
__all__ = ['UUID', 'UTCDateTime', 'JSON'] |
This file was deleted.
Oops, something went wrong.
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,46 @@ | ||
import uuid | ||
|
||
from libres.modules.compat import string_types | ||
|
||
from sqlalchemy.types import TypeDecorator | ||
from sqlalchemy.dialects import postgresql | ||
|
||
|
||
class SoftUUID(uuid.UUID): | ||
""" Behaves just like the UUID class, but allows strings to be compared | ||
with it, so that SoftUUID('my-uuid') == 'my-uuid' equals True. | ||
""" | ||
|
||
def __eq__(self, other): | ||
|
||
if isinstance(other, string_types): | ||
return self.hex == other.replace('-', '').strip() | ||
|
||
if isinstance(other, uuid.UUID): | ||
return self.__dict__ == other.__dict__ | ||
|
||
return False | ||
|
||
def __ne__(self, other): | ||
return not self.__eq__(other) | ||
|
||
def __hash__(self): | ||
# this function is not inherited in python 2 | ||
return hash(self.int) | ||
|
||
|
||
class UUID(TypeDecorator): | ||
""" Same as the Postgres UUID type, but returning SoftUUIDs instead | ||
of UUIDs on bind. | ||
""" | ||
impl = postgresql.UUID | ||
|
||
def process_bind_param(self, value, dialect): | ||
if value is not None: | ||
return str(value) | ||
|
||
def process_result_value(self, value, dialect): | ||
if value is not None: | ||
return SoftUUID(value) |
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,16 +1,16 @@ | ||
from libres.db.models.types.guid import StringEqualUUID | ||
from libres.db.models.types.uuid_type import SoftUUID | ||
from uuid import uuid4 | ||
|
||
|
||
def test_string_equal_uuid(): | ||
uuid = uuid4() | ||
|
||
assert uuid == StringEqualUUID(uuid.hex) | ||
assert uuid.hex == StringEqualUUID(uuid.hex) | ||
assert str(uuid) == StringEqualUUID(uuid.hex) | ||
assert uuid == SoftUUID(uuid.hex) | ||
assert uuid.hex == SoftUUID(uuid.hex) | ||
assert str(uuid) == SoftUUID(uuid.hex) | ||
|
||
|
||
def test_hashable_uuid(): | ||
uuid = uuid4() | ||
|
||
assert hash(StringEqualUUID(uuid.hex)) | ||
assert hash(SoftUUID(uuid.hex)) |