summaryrefslogtreecommitdiffstats
path: root/nova/db
diff options
context:
space:
mode:
authorEric Day <eday@oddments.org>2010-12-27 11:22:15 -0800
committerEric Day <eday@oddments.org>2010-12-27 11:22:15 -0800
commit438197264ea5ddc8bf076100586af6c71b0bf58d (patch)
treece79a45f04ddbd29702e01d5dca8aa98ecda5ff6 /nova/db
parentd808c70dd5420daf90931cdf122d0937af0655b3 (diff)
downloadnova-438197264ea5ddc8bf076100586af6c71b0bf58d.tar.gz
nova-438197264ea5ddc8bf076100586af6c71b0bf58d.tar.xz
nova-438197264ea5ddc8bf076100586af6c71b0bf58d.zip
Added custom guid type so we can choose the most efficient backend DB type easily.
Diffstat (limited to 'nova/db')
-rw-r--r--nova/db/sqlalchemy/models.py40
1 files changed, 38 insertions, 2 deletions
diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py
index a7725ac3c..7ec96bc6d 100644
--- a/nova/db/sqlalchemy/models.py
+++ b/nova/db/sqlalchemy/models.py
@@ -22,12 +22,14 @@ SQLAlchemy models for nova data.
import datetime
import uuid
-from sqlalchemy.orm import relationship, backref, object_mapper
from sqlalchemy import Column, Integer, Float, String, schema
from sqlalchemy import ForeignKey, DateTime, Boolean, Text
+from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import relationship, backref, object_mapper
from sqlalchemy.schema import ForeignKeyConstraint
+from sqlalchemy.types import TypeDecorator, CHAR
from nova.db.sqlalchemy.session import get_session
@@ -44,6 +46,40 @@ def make_uuid():
return uuid.uuid1().hex
+class GUID(TypeDecorator):
+ """Platform-independent GUID type.
+
+ Uses Postgresql's UUID type, otherwise uses
+ CHAR(32), storing as stringified hex values.
+
+ """
+ impl = CHAR
+
+ def load_dialect_impl(self, dialect):
+ if dialect.name == 'postgresql':
+ return dialect.type_descriptor(UUID())
+ else:
+ return dialect.type_descriptor(CHAR(32))
+
+ def process_bind_param(self, value, dialect):
+ if value is None:
+ return value
+ elif dialect.name == 'postgresql':
+ return str(value)
+ else:
+ if not isinstance(value, uuid.UUID):
+ return "%.32x" % uuid.UUID(value)
+ else:
+ # hexstring
+ return "%.32x" % value
+
+ def process_result_value(self, value, dialect):
+ if value is None:
+ return value
+ else:
+ return uuid.UUID(value).hex
+
+
class NovaBase(object):
"""Base class for Nova Models."""
__table_args__ = {'mysql_engine': 'InnoDB'}
@@ -169,7 +205,7 @@ class Certificate(BASE, NovaBase):
class Instance(BASE, NovaBase):
"""Represents a guest vm."""
__tablename__ = 'instances'
- id = Column(String(32), primary_key=True, default=make_uuid)
+ id = Column(GUID, primary_key=True, default=make_uuid)
@property
def name(self):