Dirty workaround to prevent redefinition of SQLAlchemy ORM model classes

This commit is contained in:
Fabio Manganiello 2022-10-10 01:38:15 +02:00
parent aa5b52db2f
commit 041f64c80f
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
4 changed files with 67 additions and 42 deletions

View file

@ -2,7 +2,7 @@ import inspect
import pathlib
import types
from datetime import datetime
from typing import Mapping, Type, Tuple, Any
from typing import Dict, Mapping, Type, Tuple, Any
import pkgutil
from sqlalchemy import (
@ -22,6 +22,7 @@ from platypush.message import JSONAble
Base = declarative_base()
entities_registry: Mapping[Type['Entity'], Mapping] = {}
entity_types_registry: Dict[str, Type['Entity']] = {}
class Entity(Base):
@ -30,7 +31,6 @@ class Entity(Base):
"""
__tablename__ = 'entity'
__table_args__ = {'extend_existing': True}
id = Column(Integer, autoincrement=True, primary_key=True)
external_id = Column(String, nullable=True)
@ -54,6 +54,7 @@ class Entity(Base):
__table_args__ = (
Index('name_and_plugin_index', name, plugin),
Index('name_type_and_plugin_index', name, type, plugin),
{'extend_existing': True},
)
__mapper_args__ = {

View file

@ -1,14 +1,22 @@
from sqlalchemy import Column, Integer, Boolean, ForeignKey
from ._base import Entity
from ._base import Entity, entity_types_registry
class Device(Entity):
__tablename__ = 'device'
if not entity_types_registry.get('Device'):
id = Column(Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True)
reachable = Column(Boolean, default=True)
class Device(Entity):
__tablename__ = 'device'
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
id = Column(
Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True
)
reachable = Column(Boolean, default=True)
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
entity_types_registry['Device'] = Device
else:
Device = entity_types_registry['Device']

View file

@ -1,30 +1,38 @@
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, Float
from .devices import Device
from .devices import Device, entity_types_registry
class Light(Device):
__tablename__ = 'light'
if not entity_types_registry.get('Light'):
id = Column(Integer, ForeignKey(Device.id, ondelete='CASCADE'), primary_key=True)
on = Column(Boolean)
brightness = Column(Float)
saturation = Column(Float)
hue = Column(Float)
temperature = Column(Float)
x = Column(Float)
y = Column(Float)
colormode = Column(String)
effect = Column(String)
hue_min = Column(Float)
hue_max = Column(Float)
saturation_min = Column(Float)
saturation_max = Column(Float)
brightness_min = Column(Float)
brightness_max = Column(Float)
temperature_min = Column(Float)
temperature_max = Column(Float)
class Light(Device):
__tablename__ = 'light'
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
id = Column(
Integer, ForeignKey(Device.id, ondelete='CASCADE'), primary_key=True
)
on = Column(Boolean)
brightness = Column(Float)
saturation = Column(Float)
hue = Column(Float)
temperature = Column(Float)
x = Column(Float)
y = Column(Float)
colormode = Column(String)
effect = Column(String)
hue_min = Column(Float)
hue_max = Column(Float)
saturation_min = Column(Float)
saturation_max = Column(Float)
brightness_min = Column(Float)
brightness_max = Column(Float)
temperature_min = Column(Float)
temperature_max = Column(Float)
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
entity_types_registry['Light'] = Light
else:
Light = entity_types_registry['Light']

View file

@ -1,14 +1,22 @@
from sqlalchemy import Column, Integer, ForeignKey, Boolean
from .devices import Device
from .devices import Device, entity_types_registry
class Switch(Device):
__tablename__ = 'switch'
if not entity_types_registry.get('Switch'):
id = Column(Integer, ForeignKey(Device.id, ondelete='CASCADE'), primary_key=True)
state = Column(Boolean)
class Switch(Device):
__tablename__ = 'switch'
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
id = Column(
Integer, ForeignKey(Device.id, ondelete='CASCADE'), primary_key=True
)
state = Column(Boolean)
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
entity_types_registry['Switch'] = Switch
else:
Switch = entity_types_registry['Switch']