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 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
if not entity_types_registry.get('Device'):
class Device(Entity): class Device(Entity):
__tablename__ = 'device' __tablename__ = 'device'
id = Column(Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True) id = Column(
Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True
)
reachable = Column(Boolean, default=True) reachable = Column(Boolean, default=True)
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
entity_types_registry['Device'] = Device
else:
Device = entity_types_registry['Device']

View file

@ -1,12 +1,16 @@
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
if not entity_types_registry.get('Light'):
class Light(Device): class Light(Device):
__tablename__ = 'light' __tablename__ = 'light'
id = Column(Integer, ForeignKey(Device.id, ondelete='CASCADE'), primary_key=True) id = Column(
Integer, ForeignKey(Device.id, ondelete='CASCADE'), primary_key=True
)
on = Column(Boolean) on = Column(Boolean)
brightness = Column(Float) brightness = Column(Float)
saturation = Column(Float) saturation = Column(Float)
@ -28,3 +32,7 @@ class Light(Device):
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, '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
if not entity_types_registry.get('Switch'):
class Switch(Device): class Switch(Device):
__tablename__ = 'switch' __tablename__ = 'switch'
id = Column(Integer, ForeignKey(Device.id, ondelete='CASCADE'), primary_key=True) id = Column(
Integer, ForeignKey(Device.id, ondelete='CASCADE'), primary_key=True
)
state = Column(Boolean) state = Column(Boolean)
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
entity_types_registry['Switch'] = Switch
else:
Switch = entity_types_registry['Switch']