Move uuid column type to models.py
This commit is contained in:
parent
fbf9b59456
commit
83e5e210a5
|
@ -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.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import scoped_session
|
from sqlalchemy.orm import scoped_session
|
||||||
from sqlalchemy.orm import sessionmaker
|
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
|
# Define naming conventions for generated constraints
|
||||||
|
|
|
@ -2,6 +2,7 @@ import enum
|
||||||
from sqlalchemy import (
|
from sqlalchemy import (
|
||||||
Boolean,
|
Boolean,
|
||||||
Column,
|
Column,
|
||||||
|
CHAR,
|
||||||
DateTime,
|
DateTime,
|
||||||
Enum,
|
Enum,
|
||||||
ForeignKey,
|
ForeignKey,
|
||||||
|
@ -10,11 +11,35 @@ from sqlalchemy import (
|
||||||
Table,
|
Table,
|
||||||
Text,
|
Text,
|
||||||
text,
|
text,
|
||||||
|
TypeDecorator,
|
||||||
)
|
)
|
||||||
from sqlalchemy.orm import relationship, backref
|
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):
|
class User(ModelBase):
|
||||||
|
@ -269,7 +294,7 @@ class Character(ModelBase):
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
|
|
||||||
# Public-facing character id
|
# 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
|
# The lexicon to which this character belongs
|
||||||
lexicon_id = Column(Integer, ForeignKey('lexicon.id'), nullable=False)
|
lexicon_id = Column(Integer, ForeignKey('lexicon.id'), nullable=False)
|
||||||
|
@ -316,7 +341,7 @@ class Article(ModelBase):
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
|
|
||||||
# Public-facing article id
|
# 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
|
# The lexicon to which this article belongs
|
||||||
lexicon_id = Column(Integer, ForeignKey('lexicon.id'), nullable=False)
|
lexicon_id = Column(Integer, ForeignKey('lexicon.id'), nullable=False)
|
||||||
|
|
Loading…
Reference in New Issue