Skip to content

Commit

Permalink
Rename GUID to UUID and stop supporting databases other than Postgres…
Browse files Browse the repository at this point in the history
…ql with it
  • Loading branch information
Denis Krienbühl committed Feb 6, 2015
1 parent c658564 commit cd3e44c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 84 deletions.
8 changes: 4 additions & 4 deletions libres/db/models/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)

from libres.db.models import ORMBase
from libres.db.models.types import GUID, UTCDateTime, JSON
from libres.db.models.types import UUID, UTCDateTime, JSON
from libres.db.models.other import OtherModels
from libres.db.models.timestamp import TimestampMixin

Expand Down Expand Up @@ -52,16 +52,16 @@ class Allocation(TimestampMixin, ORMBase, OtherModels):

#: the resource uuid of the allocation, may not be an actual resource
#: see :class:`.models.Allocation` for more information
resource = Column(GUID(), nullable=False)
resource = Column(UUID(), nullable=False)

#: resource of which this allocation is a mirror. If the mirror_of
#: attribute equals the resource, this is a real resource
#: see :class:`.models.Allocation` for more information
mirror_of = Column(GUID(), nullable=False)
mirror_of = Column(UUID(), nullable=False)

#: Group uuid to which this allocation belongs to. Every allocation has a
#: group but some allcations may be the only one in their group.
group = Column(GUID(), nullable=False)
group = Column(UUID(), nullable=False)

#: Number of times this allocation may be reserved
quota = Column(types.Integer(), default=1)
Expand Down
10 changes: 5 additions & 5 deletions libres/db/models/reservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sqlalchemy.schema import Index

from libres.db.models import ORMBase
from libres.db.models.types import GUID, UTCDateTime, JSON
from libres.db.models.types import UUID, UTCDateTime, JSON
from libres.db.models.other import OtherModels
from libres.db.models.timestamp import TimestampMixin
from libres.modules import calendar
Expand All @@ -32,12 +32,12 @@ class Reservation(TimestampMixin, ORMBase, OtherModels):
)

token = Column(
GUID(),
UUID(),
nullable=False,
)

target = Column(
GUID(),
UUID(),
nullable=False,
)

Expand All @@ -47,7 +47,7 @@ class Reservation(TimestampMixin, ORMBase, OtherModels):
)

resource = Column(
GUID(),
UUID(),
nullable=False
)

Expand Down Expand Up @@ -84,7 +84,7 @@ class Reservation(TimestampMixin, ORMBase, OtherModels):
)

session_id = Column(
GUID()
UUID()
)

quota = Column(
Expand Down
6 changes: 3 additions & 3 deletions libres/db/models/reserved_slot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)

from libres.db.models import ORMBase, Allocation
from libres.db.models.types import GUID, UTCDateTime
from libres.db.models.types import UUID, UTCDateTime
from libres.db.models.timestamp import TimestampMixin
from libres.modules import calendar

Expand All @@ -23,7 +23,7 @@ class ReservedSlot(TimestampMixin, ORMBase):
__tablename__ = 'reserved_slots'

resource = Column(
GUID(),
UUID(),
primary_key=True,
nullable=False,
autoincrement=False
Expand Down Expand Up @@ -62,7 +62,7 @@ class ReservedSlot(TimestampMixin, ORMBase):
)

reservation_token = Column(
GUID(),
UUID(),
nullable=False
)

Expand Down
6 changes: 3 additions & 3 deletions libres/db/models/types/__init__.py
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']
64 changes: 0 additions & 64 deletions libres/db/models/types/guid.py

This file was deleted.

46 changes: 46 additions & 0 deletions libres/db/models/types/uuid_type.py
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)
10 changes: 5 additions & 5 deletions libres/tests/test_types.py
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))

0 comments on commit cd3e44c

Please sign in to comment.