New API to check if a table class exists before defining it.

- Check if it's part of the metadata through a function call rather than
  checking `Base.metadata` in every single module.

- Make it possible to override them (mostly for doc generation logic
  that needs to be able to import those classes).

- Make it possible to extend them.
This commit is contained in:
Fabio Manganiello 2023-10-04 09:50:10 +02:00
parent 608844ca0c
commit d872835093
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
32 changed files with 170 additions and 82 deletions

View file

@ -1,3 +1,6 @@
from contextlib import contextmanager
from dataclasses import dataclass
from sqlalchemy import __version__ from sqlalchemy import __version__
sa_version = tuple(map(int, __version__.split('.'))) sa_version = tuple(map(int, __version__.split('.')))
@ -8,3 +11,38 @@ else:
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base() Base = declarative_base()
@dataclass
class DbContext:
"""
Context flags for the database session.
"""
override_definitions: bool = False
_ctx = DbContext()
@contextmanager
def override_definitions():
"""
Temporarily override the definitions of the entities in the entities
registry.
This is useful when the entities are being imported off-context, like
e.g. in the `inspect` or `alembic` modules.
"""
_ctx.override_definitions = True
yield
_ctx.override_definitions = False
def is_defined(table_name: str) -> bool:
"""
Check if the given entity class is defined in the entities registry.
:param table_name: Name of the table associated to the entity class.
"""
return not _ctx.override_definitions and table_name in Base.metadata

View file

@ -30,7 +30,7 @@ from sqlalchemy.orm.exc import ObjectDeletedError
import platypush import platypush
from platypush.config import Config from platypush.config import Config
from platypush.common.db import Base from platypush.common.db import Base, is_defined
from platypush.message import JSONAble, Message from platypush.message import JSONAble, Message
EntityRegistryType = Dict[str, Type['Entity']] EntityRegistryType = Dict[str, Type['Entity']]
@ -52,7 +52,7 @@ fail.
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
if 'entity' not in Base.metadata: if not is_defined('entity'):
class Entity(Base): class Entity(Base):
""" """

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .three_axis import ThreeAxisSensor from .three_axis import ThreeAxisSensor
if 'accelerometer' not in Base.metadata: if not is_defined('accelerometer'):
class Accelerometer(ThreeAxisSensor): class Accelerometer(ThreeAxisSensor):
""" """
@ -20,6 +20,7 @@ if 'accelerometer' not in Base.metadata:
primary_key=True, primary_key=True,
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,12 +1,12 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .dimmers import Dimmer from .dimmers import Dimmer
from .switches import Switch from .switches import Switch
if 'volume' not in Base.metadata: if not is_defined('volume'):
class Volume(Dimmer): class Volume(Dimmer):
__tablename__ = 'volume' __tablename__ = 'volume'
@ -15,12 +15,13 @@ if 'volume' not in Base.metadata:
Integer, ForeignKey(Dimmer.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(Dimmer.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'muted' not in Base.metadata: if not is_defined('muted'):
class Muted(Switch): class Muted(Switch):
__tablename__ = 'muted' __tablename__ = 'muted'
@ -29,6 +30,7 @@ if 'muted' not in Base.metadata:
Integer, ForeignKey(Switch.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(Switch.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'battery' not in Base.metadata: if not is_defined('battery'):
class Battery(NumericSensor): class Battery(NumericSensor):
__tablename__ = 'battery' __tablename__ = 'battery'
@ -19,6 +19,7 @@ if 'battery' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -9,13 +9,13 @@ from sqlalchemy import (
String, String,
) )
from platypush.common.db import Base from platypush.common.db import is_defined
from ..devices import Device from ..devices import Device
from ._service import BluetoothService from ._service import BluetoothService
if 'bluetooth_device' not in Base.metadata: if not is_defined('bluetooth_device'):
class BluetoothDevice(Device): class BluetoothDevice(Device):
""" """
@ -68,6 +68,7 @@ if 'bluetooth_device' not in Base.metadata:
model_id = Column(String, default=None) model_id = Column(String, default=None)
""" Device model ID. """ """ Device model ID. """
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -8,10 +8,10 @@ from sqlalchemy import (
String, String,
) )
from platypush.common.db import Base from platypush.common.db import is_defined
from platypush.entities import Entity from platypush.entities import Entity
if 'bluetooth_service' not in Base.metadata: if not is_defined('bluetooth_service'):
class BluetoothService(Entity): class BluetoothService(Entity):
""" """
@ -44,6 +44,7 @@ if 'bluetooth_service' not in Base.metadata:
connected = Column(Boolean, default=False) connected = Column(Boolean, default=False)
""" Whether an active connection exists to this service. """ """ Whether an active connection exists to this service. """
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -6,14 +6,14 @@ from sqlalchemy import (
Integer, Integer,
) )
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import EnumSensor from .sensors import EnumSensor
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
if 'button' not in Base.metadata: if not is_defined('button'):
class Button(EnumSensor): class Button(EnumSensor):
__tablename__ = 'button' __tablename__ = 'button'
@ -22,6 +22,7 @@ if 'button' not in Base.metadata:
Integer, ForeignKey(EnumSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(EnumSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -6,12 +6,12 @@ from sqlalchemy import (
String, String,
) )
from platypush.common.db import Base from platypush.common.db import is_defined
from .devices import Device from .devices import Device
if 'cloud_instance' not in Base.metadata: if not is_defined('cloud_instance'):
class CloudInstance(Device): class CloudInstance(Device):
""" """
@ -38,6 +38,7 @@ if 'cloud_instance' not in Base.metadata:
alerts = Column(JSON) alerts = Column(JSON)
backups = Column(JSON) backups = Column(JSON)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import BinarySensor from .sensors import BinarySensor
if 'contact_sensor' not in Base.metadata: if not is_defined('contact_sensor'):
class ContactSensor(BinarySensor): class ContactSensor(BinarySensor):
""" """
@ -18,6 +18,7 @@ if 'contact_sensor' not in Base.metadata:
Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, Boolean, ForeignKey from sqlalchemy import Column, Integer, Boolean, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from ._base import Entity from ._base import Entity
if 'device' not in Base.metadata: if not is_defined('device'):
class Device(Entity): class Device(Entity):
""" """
@ -19,6 +19,7 @@ if 'device' not in Base.metadata:
) )
reachable = Column(Boolean, default=True) reachable = Column(Boolean, default=True)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey, Float, String from sqlalchemy import Column, Integer, ForeignKey, Float, String
from platypush.common.db import Base from platypush.common.db import is_defined
from .devices import Device from .devices import Device
if 'dimmer' not in Base.metadata: if not is_defined('dimmer'):
class Dimmer(Device): class Dimmer(Device):
""" """
@ -24,6 +24,7 @@ if 'dimmer' not in Base.metadata:
value = Column(Float) value = Column(Float)
unit = Column(String) unit = Column(String)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'distance_sensor' not in Base.metadata: if not is_defined('distance_sensor'):
class DistanceSensor(NumericSensor): class DistanceSensor(NumericSensor):
""" """
@ -18,6 +18,7 @@ if 'distance_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'power_sensor' not in Base.metadata: if not is_defined('power_sensor'):
class PowerSensor(NumericSensor): class PowerSensor(NumericSensor):
__tablename__ = 'power_sensor' __tablename__ = 'power_sensor'
@ -14,12 +14,13 @@ if 'power_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'current_sensor' not in Base.metadata: if not is_defined('current_sensor'):
class CurrentSensor(NumericSensor): class CurrentSensor(NumericSensor):
__tablename__ = 'current_sensor' __tablename__ = 'current_sensor'
@ -28,12 +29,13 @@ if 'current_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'voltage_sensor' not in Base.metadata: if not is_defined('voltage_sensor'):
class VoltageSensor(NumericSensor): class VoltageSensor(NumericSensor):
__tablename__ = 'voltage_sensor' __tablename__ = 'voltage_sensor'
@ -42,12 +44,13 @@ if 'voltage_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'energy_sensor' not in Base.metadata: if not is_defined('energy_sensor'):
class EnergySensor(NumericSensor): class EnergySensor(NumericSensor):
__tablename__ = 'energy_sensor' __tablename__ = 'energy_sensor'
@ -56,6 +59,7 @@ if 'energy_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'heart_rate_sensor' not in Base.metadata: if not is_defined('heart_rate_sensor'):
class HeartRateSensor(NumericSensor): class HeartRateSensor(NumericSensor):
""" """
@ -18,6 +18,7 @@ if 'heart_rate_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'humidity_sensor' not in Base.metadata: if not is_defined('humidity_sensor'):
class HumiditySensor(NumericSensor): class HumiditySensor(NumericSensor):
""" """
@ -18,12 +18,13 @@ if 'humidity_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'dew_point_sensor' not in Base.metadata: if not is_defined('dew_point_sensor'):
class DewPointSensor(NumericSensor): class DewPointSensor(NumericSensor):
""" """
@ -36,6 +37,7 @@ if 'dew_point_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'illuminance_sensor' not in Base.metadata: if not is_defined('illuminance_sensor'):
class IlluminanceSensor(NumericSensor): class IlluminanceSensor(NumericSensor):
__tablename__ = 'illuminance_sensor' __tablename__ = 'illuminance_sensor'
@ -14,6 +14,7 @@ if 'illuminance_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, Float from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, Float
from platypush.common.db import Base from platypush.common.db import is_defined
from .devices import Device from .devices import Device
if 'light' not in Base.metadata: if not is_defined('light'):
class Light(Device): class Light(Device):
__tablename__ = 'light' __tablename__ = 'light'
@ -34,6 +34,7 @@ if 'light' not in Base.metadata:
temperature_min = Column(Float) temperature_min = Column(Float)
temperature_max = Column(Float) temperature_max = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'link_quality' not in Base.metadata: if not is_defined('link_quality'):
class LinkQuality(NumericSensor): class LinkQuality(NumericSensor):
__tablename__ = 'link_quality' __tablename__ = 'link_quality'
@ -19,6 +19,7 @@ if 'link_quality' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .three_axis import ThreeAxisSensor from .three_axis import ThreeAxisSensor
if 'magnetometer' not in Base.metadata: if not is_defined('magnetometer'):
class Magnetometer(ThreeAxisSensor): class Magnetometer(ThreeAxisSensor):
""" """
@ -20,6 +20,7 @@ if 'magnetometer' not in Base.metadata:
primary_key=True, primary_key=True,
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import BinarySensor from .sensors import BinarySensor
if 'motion_sensor' not in Base.metadata: if not is_defined('motion_sensor'):
class MotionSensor(BinarySensor): class MotionSensor(BinarySensor):
__tablename__ = 'motion_sensor' __tablename__ = 'motion_sensor'
@ -14,6 +14,7 @@ if 'motion_sensor' not in Base.metadata:
Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import BinarySensor from .sensors import BinarySensor
if 'presence_sensor' not in Base.metadata: if not is_defined('presence_sensor'):
class PresenceSensor(BinarySensor): class PresenceSensor(BinarySensor):
""" """
@ -18,6 +18,7 @@ if 'presence_sensor' not in Base.metadata:
Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'pressure_sensor' not in Base.metadata: if not is_defined('pressure_sensor'):
class PressureSensor(NumericSensor): class PressureSensor(NumericSensor):
""" """
@ -18,6 +18,7 @@ if 'pressure_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -12,7 +12,7 @@ from sqlalchemy import (
String, String,
) )
from platypush.common.db import Base from platypush.common.db import is_defined
from .devices import Device from .devices import Device
@ -32,7 +32,7 @@ class Sensor(Device):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if 'raw_sensor' not in Base.metadata: if not is_defined('raw_sensor'):
class RawSensor(Sensor): class RawSensor(Sensor):
""" """
@ -86,12 +86,13 @@ if 'raw_sensor' not in Base.metadata:
self.is_binary = False self.is_binary = False
self.is_json = False self.is_json = False
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'numeric_sensor' not in Base.metadata and 'percent_sensor' not in Base.metadata: if not is_defined('numeric_sensor') and not is_defined('percent_sensor'):
class NumericSensor(Sensor): class NumericSensor(Sensor):
""" """
@ -109,6 +110,7 @@ if 'numeric_sensor' not in Base.metadata and 'percent_sensor' not in Base.metada
max = Column(Float) max = Column(Float)
unit = Column(String) unit = Column(String)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
@ -124,6 +126,7 @@ if 'numeric_sensor' not in Base.metadata and 'percent_sensor' not in Base.metada
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
@ -135,7 +138,7 @@ if 'numeric_sensor' not in Base.metadata and 'percent_sensor' not in Base.metada
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if 'binary_sensor' not in Base.metadata: if not is_defined('binary_sensor'):
class BinarySensor(Sensor): class BinarySensor(Sensor):
""" """
@ -163,12 +166,13 @@ if 'binary_sensor' not in Base.metadata:
) )
value = Column(Boolean) value = Column(Boolean)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'enum_sensor' not in Base.metadata: if not is_defined('enum_sensor'):
class EnumSensor(Sensor): class EnumSensor(Sensor):
""" """
@ -184,12 +188,13 @@ if 'enum_sensor' not in Base.metadata:
values = Column(JSON) values = Column(JSON)
""" Possible values for the sensor, as a JSON array. """ """ Possible values for the sensor, as a JSON array. """
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'composite_sensor' not in Base.metadata: if not is_defined('composite_sensor'):
class CompositeSensor(Sensor): class CompositeSensor(Sensor):
""" """
@ -204,6 +209,7 @@ if 'composite_sensor' not in Base.metadata:
) )
value = Column(JSON) value = Column(JSON)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'steps_sensor' not in Base.metadata: if not is_defined('steps_sensor'):
class StepsSensor(NumericSensor): class StepsSensor(NumericSensor):
""" """
@ -18,6 +18,7 @@ if 'steps_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey, Boolean, String, JSON from sqlalchemy import Column, Integer, ForeignKey, Boolean, String, JSON
from platypush.common.db import Base from platypush.common.db import is_defined
from .devices import Device from .devices import Device
if 'switch' not in Base.metadata: if not is_defined('switch'):
class Switch(Device): class Switch(Device):
__tablename__ = 'switch' __tablename__ = 'switch'
@ -15,12 +15,13 @@ if 'switch' not in Base.metadata:
) )
state = Column(Boolean) state = Column(Boolean)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'enum_switch' not in Base.metadata: if not is_defined('enum_switch'):
class EnumSwitch(Device): class EnumSwitch(Device):
__tablename__ = 'enum_switch' __tablename__ = 'enum_switch'
@ -31,6 +32,7 @@ if 'enum_switch' not in Base.metadata:
value = Column(String) value = Column(String)
values = Column(JSON) values = Column(JSON)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,6 +1,6 @@
from sqlalchemy import Boolean, Column, Float, ForeignKey, Integer, JSON, String from sqlalchemy import Boolean, Column, Float, ForeignKey, Integer, JSON, String
from platypush.common.db import Base from platypush.common.db import is_defined
from . import Entity from . import Entity
from .devices import Device from .devices import Device
@ -8,7 +8,7 @@ from .sensors import NumericSensor, PercentSensor
from .temperature import TemperatureSensor from .temperature import TemperatureSensor
if 'cpu' not in Base.metadata: if not is_defined('cpu'):
class Cpu(Entity): class Cpu(Entity):
""" """
@ -23,12 +23,13 @@ if 'cpu' not in Base.metadata:
percent = Column(Float) percent = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'cpu_info' not in Base.metadata: if not is_defined('cpu_info'):
class CpuInfo(Entity): class CpuInfo(Entity):
""" """
@ -54,12 +55,13 @@ if 'cpu_info' not in Base.metadata:
l2_cache_size = Column(Integer) l2_cache_size = Column(Integer)
l3_cache_size = Column(Integer) l3_cache_size = Column(Integer)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'cpu_times' not in Base.metadata: if not is_defined('cpu_times'):
class CpuTimes(Entity): class CpuTimes(Entity):
""" """
@ -72,12 +74,13 @@ if 'cpu_times' not in Base.metadata:
Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'cpu_stats' not in Base.metadata: if not is_defined('cpu_stats'):
class CpuStats(Entity): class CpuStats(Entity):
""" """
@ -90,12 +93,13 @@ if 'cpu_stats' not in Base.metadata:
Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'memory_stats' not in Base.metadata: if not is_defined('memory_stats'):
class MemoryStats(Entity): class MemoryStats(Entity):
""" """
@ -119,12 +123,13 @@ if 'memory_stats' not in Base.metadata:
shared = Column(Integer) shared = Column(Integer)
percent = Column(Float) percent = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'swap_stats' not in Base.metadata: if not is_defined('swap_stats'):
class SwapStats(Entity): class SwapStats(Entity):
""" """
@ -142,12 +147,13 @@ if 'swap_stats' not in Base.metadata:
free = Column(Integer) free = Column(Integer)
percent = Column(Float) percent = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'disk' not in Base.metadata: if not is_defined('disk'):
class Disk(Entity): class Disk(Entity):
""" """
@ -175,12 +181,13 @@ if 'disk' not in Base.metadata:
write_time = Column(Float) write_time = Column(Float)
busy_time = Column(Float) busy_time = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'network_interface' not in Base.metadata: if not is_defined('network_interface'):
class NetworkInterface(Device): class NetworkInterface(Device):
""" """
@ -207,12 +214,13 @@ if 'network_interface' not in Base.metadata:
duplex = Column(String) duplex = Column(String)
flags = Column(JSON) flags = Column(JSON)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'system_temperature' not in Base.metadata: if not is_defined('system_temperature'):
class SystemTemperature(TemperatureSensor): class SystemTemperature(TemperatureSensor):
""" """
@ -230,12 +238,13 @@ if 'system_temperature' not in Base.metadata:
high = Column(Float) high = Column(Float)
critical = Column(Float) critical = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'system_fan' not in Base.metadata: if not is_defined('system_fan'):
class SystemFan(NumericSensor): class SystemFan(NumericSensor):
""" """
@ -250,12 +259,13 @@ if 'system_fan' not in Base.metadata:
primary_key=True, primary_key=True,
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
if 'system_battery' not in Base.metadata: if not is_defined('system_battery'):
class SystemBattery(PercentSensor): class SystemBattery(PercentSensor):
""" """
@ -273,6 +283,7 @@ if 'system_battery' not in Base.metadata:
seconds_left = Column(Float) seconds_left = Column(Float)
power_plugged = Column(Boolean) power_plugged = Column(Boolean)
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'temperature_sensor' not in Base.metadata: if not is_defined('temperature_sensor'):
class TemperatureSensor(NumericSensor): class TemperatureSensor(NumericSensor):
__tablename__ = 'temperature_sensor' __tablename__ = 'temperature_sensor'
@ -14,6 +14,7 @@ if 'temperature_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,13 +1,13 @@
from typing import Iterable, Mapping, Optional, Union from typing import Iterable, Mapping, Optional, Union
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from platypush.common.sensors import Numeric from platypush.common.sensors import Numeric
from .sensors import RawSensor from .sensors import RawSensor
if 'three_axis_sensor' not in Base.metadata: if not is_defined('three_axis_sensor'):
class ThreeAxisSensor(RawSensor): class ThreeAxisSensor(RawSensor):
""" """
@ -20,6 +20,7 @@ if 'three_axis_sensor' not in Base.metadata:
Integer, ForeignKey(RawSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(RawSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'time_duration' not in Base.metadata: if not is_defined('time_duration'):
class TimeDuration(NumericSensor): class TimeDuration(NumericSensor):
""" """
@ -18,6 +18,7 @@ if 'time_duration' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }

View file

@ -2,14 +2,14 @@ import logging
from sqlalchemy import Column, ForeignKey, Integer, String from sqlalchemy import Column, ForeignKey, Integer, String
from platypush.common.db import Base from platypush.common.db import is_defined
from . import Entity from . import Entity
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
if 'variable' not in Base.metadata: if not is_defined('variable'):
class Variable(Entity): class Variable(Entity):
""" """

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base from platypush.common.db import is_defined
from .sensors import NumericSensor from .sensors import NumericSensor
if 'weight_sensor' not in Base.metadata: if not is_defined('weight_sensor'):
class WeightSensor(NumericSensor): class WeightSensor(NumericSensor):
""" """
@ -18,6 +18,7 @@ if 'weight_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
) )
__table_args__ = {'extend_existing': True}
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }