From 041f64c80fe8965920073e05e028197c681859ff Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 10 Oct 2022 01:38:15 +0200 Subject: [PATCH] Dirty workaround to prevent redefinition of SQLAlchemy ORM model classes --- platypush/entities/_base.py | 5 +-- platypush/entities/devices.py | 24 ++++++++++----- platypush/entities/lights.py | 56 +++++++++++++++++++--------------- platypush/entities/switches.py | 24 ++++++++++----- 4 files changed, 67 insertions(+), 42 deletions(-) diff --git a/platypush/entities/_base.py b/platypush/entities/_base.py index c2a1a16cb..ab115bba4 100644 --- a/platypush/entities/_base.py +++ b/platypush/entities/_base.py @@ -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__ = { diff --git a/platypush/entities/devices.py b/platypush/entities/devices.py index 8d3d45230..a88073dfd 100644 --- a/platypush/entities/devices.py +++ b/platypush/entities/devices.py @@ -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'] diff --git a/platypush/entities/lights.py b/platypush/entities/lights.py index ae65eae6e..b2179a330 100644 --- a/platypush/entities/lights.py +++ b/platypush/entities/lights.py @@ -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'] diff --git a/platypush/entities/switches.py b/platypush/entities/switches.py index 3c7d6d100..86a72e908 100644 --- a/platypush/entities/switches.py +++ b/platypush/entities/switches.py @@ -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']