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

View File

@ -1,14 +1,22 @@
from sqlalchemy import Column, Integer, Boolean, ForeignKey from sqlalchemy import Column, Integer, Boolean, ForeignKey
from ._base import Entity from ._base import Entity, entity_types_registry
class Device(Entity): if not entity_types_registry.get('Device'):
__tablename__ = 'device'
id = Column(Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True) class Device(Entity):
reachable = Column(Boolean, default=True) __tablename__ = 'device'
__mapper_args__ = { id = Column(
'polymorphic_identity': __tablename__, 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 sqlalchemy import Column, Integer, String, ForeignKey, Boolean, Float
from .devices import Device from .devices import Device, entity_types_registry
class Light(Device): if not entity_types_registry.get('Light'):
__tablename__ = 'light'
id = Column(Integer, ForeignKey(Device.id, ondelete='CASCADE'), primary_key=True) class Light(Device):
on = Column(Boolean) __tablename__ = 'light'
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__ = { id = Column(
'polymorphic_identity': __tablename__, 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 sqlalchemy import Column, Integer, ForeignKey, Boolean
from .devices import Device from .devices import Device, entity_types_registry
class Switch(Device): if not entity_types_registry.get('Switch'):
__tablename__ = 'switch'
id = Column(Integer, ForeignKey(Device.id, ondelete='CASCADE'), primary_key=True) class Switch(Device):
state = Column(Boolean) __tablename__ = 'switch'
__mapper_args__ = { id = Column(
'polymorphic_identity': __tablename__, 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']