From 83e5e210a59bb8eddc29f82196787a6710f6f3c4 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 5 May 2021 22:18:34 -0700 Subject: [PATCH] Move uuid column type to models.py --- amanuensis/database.py | 27 +-------------------------- amanuensis/models.py | 33 +++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/amanuensis/database.py b/amanuensis/database.py index 74ee911..8faa5f1 100644 --- a/amanuensis/database.py +++ b/amanuensis/database.py @@ -1,32 +1,7 @@ -from sqlalchemy import create_engine, MetaData, event, TypeDecorator, CHAR +from sqlalchemy import create_engine, MetaData, event from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import scoped_session from sqlalchemy.orm import sessionmaker -import sqlite3 -import uuid - - -class Uuid(TypeDecorator): - """ - A uuid backed by a char(32) field in sqlite. - """ - impl = CHAR(32) - - def process_bind_param(self, value, dialect): - if value is None: - return value - elif not isinstance(value, uuid.UUID): - return f'{uuid.UUID(value).int:32x}' - else: - return f'{value.int:32x}' - - def process_result_value(self, value, dialect): - if value is None: - return value - elif not isinstance(value, uuid.UUID): - return uuid.UUID(value) - else: - return value # Define naming conventions for generated constraints diff --git a/amanuensis/models.py b/amanuensis/models.py index 79a1429..d31f1b9 100644 --- a/amanuensis/models.py +++ b/amanuensis/models.py @@ -2,6 +2,7 @@ import enum from sqlalchemy import ( Boolean, Column, + CHAR, DateTime, Enum, ForeignKey, @@ -10,11 +11,35 @@ from sqlalchemy import ( Table, Text, text, + TypeDecorator, ) from sqlalchemy.orm import relationship, backref -from uuid import uuid4 +import uuid -from .database import ModelBase, Uuid +from .database import ModelBase + + +class Uuid(TypeDecorator): + """ + A uuid backed by a char(32) field in sqlite. + """ + impl = CHAR(32) + + def process_bind_param(self, value, dialect): + if value is None: + return value + elif not isinstance(value, uuid.UUID): + return f'{uuid.UUID(value).int:32x}' + else: + return f'{value.int:32x}' + + def process_result_value(self, value, dialect): + if value is None: + return value + elif not isinstance(value, uuid.UUID): + return uuid.UUID(value) + else: + return value class User(ModelBase): @@ -269,7 +294,7 @@ class Character(ModelBase): id = Column(Integer, primary_key=True) # Public-facing character id - public_id = Column(Uuid, nullable=False, unique=True, default=uuid4) + public_id = Column(Uuid, nullable=False, unique=True, default=uuid.uuid4) # The lexicon to which this character belongs lexicon_id = Column(Integer, ForeignKey('lexicon.id'), nullable=False) @@ -316,7 +341,7 @@ class Article(ModelBase): id = Column(Integer, primary_key=True) # Public-facing article id - public_id = Column(Uuid, nullable=False, unique=True, default=uuid4) + public_id = Column(Uuid, nullable=False, unique=True, default=uuid.uuid4) # The lexicon to which this article belongs lexicon_id = Column(Integer, ForeignKey('lexicon.id'), nullable=False)