From d872835093e29d23d96a881f8ecd835e27277ecf Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 4 Oct 2023 09:50:10 +0200 Subject: [PATCH] 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. --- platypush/common/db.py | 38 ++++++++++++++++++++++++ platypush/entities/_base.py | 4 +-- platypush/entities/acceleration.py | 5 ++-- platypush/entities/audio.py | 8 +++-- platypush/entities/batteries.py | 5 ++-- platypush/entities/bluetooth/_device.py | 5 ++-- platypush/entities/bluetooth/_service.py | 5 ++-- platypush/entities/buttons.py | 5 ++-- platypush/entities/cloud.py | 5 ++-- platypush/entities/contact.py | 5 ++-- platypush/entities/devices.py | 5 ++-- platypush/entities/dimmers.py | 5 ++-- platypush/entities/distance.py | 5 ++-- platypush/entities/electricity.py | 14 +++++---- platypush/entities/heart.py | 5 ++-- platypush/entities/humidity.py | 8 +++-- platypush/entities/illuminance.py | 5 ++-- platypush/entities/lights.py | 5 ++-- platypush/entities/linkquality.py | 5 ++-- platypush/entities/magnetism.py | 5 ++-- platypush/entities/motion.py | 5 ++-- platypush/entities/presence.py | 5 ++-- platypush/entities/pressure.py | 5 ++-- platypush/entities/sensors.py | 18 +++++++---- platypush/entities/steps.py | 5 ++-- platypush/entities/switches.py | 8 +++-- platypush/entities/system.py | 35 ++++++++++++++-------- platypush/entities/temperature.py | 5 ++-- platypush/entities/three_axis.py | 5 ++-- platypush/entities/time.py | 5 ++-- platypush/entities/variables.py | 4 +-- platypush/entities/weight.py | 5 ++-- 32 files changed, 170 insertions(+), 82 deletions(-) diff --git a/platypush/common/db.py b/platypush/common/db.py index f1979e3b1..ef948b345 100644 --- a/platypush/common/db.py +++ b/platypush/common/db.py @@ -1,3 +1,6 @@ +from contextlib import contextmanager +from dataclasses import dataclass + from sqlalchemy import __version__ sa_version = tuple(map(int, __version__.split('.'))) @@ -8,3 +11,38 @@ else: from sqlalchemy.ext.declarative import 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 diff --git a/platypush/entities/_base.py b/platypush/entities/_base.py index cc76450f3..661fa636d 100644 --- a/platypush/entities/_base.py +++ b/platypush/entities/_base.py @@ -30,7 +30,7 @@ from sqlalchemy.orm.exc import ObjectDeletedError import platypush 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 EntityRegistryType = Dict[str, Type['Entity']] @@ -52,7 +52,7 @@ fail. logger = logging.getLogger(__name__) -if 'entity' not in Base.metadata: +if not is_defined('entity'): class Entity(Base): """ diff --git a/platypush/entities/acceleration.py b/platypush/entities/acceleration.py index c7112ae78..b41897f55 100644 --- a/platypush/entities/acceleration.py +++ b/platypush/entities/acceleration.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .three_axis import ThreeAxisSensor -if 'accelerometer' not in Base.metadata: +if not is_defined('accelerometer'): class Accelerometer(ThreeAxisSensor): """ @@ -20,6 +20,7 @@ if 'accelerometer' not in Base.metadata: primary_key=True, ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/audio.py b/platypush/entities/audio.py index 44dae75a4..46d29adea 100644 --- a/platypush/entities/audio.py +++ b/platypush/entities/audio.py @@ -1,12 +1,12 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .dimmers import Dimmer from .switches import Switch -if 'volume' not in Base.metadata: +if not is_defined('volume'): class Volume(Dimmer): __tablename__ = 'volume' @@ -15,12 +15,13 @@ if 'volume' not in Base.metadata: Integer, ForeignKey(Dimmer.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'muted' not in Base.metadata: +if not is_defined('muted'): class Muted(Switch): __tablename__ = 'muted' @@ -29,6 +30,7 @@ if 'muted' not in Base.metadata: Integer, ForeignKey(Switch.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/batteries.py b/platypush/entities/batteries.py index 828796933..6b1cb9360 100644 --- a/platypush/entities/batteries.py +++ b/platypush/entities/batteries.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'battery' not in Base.metadata: +if not is_defined('battery'): class Battery(NumericSensor): __tablename__ = 'battery' @@ -19,6 +19,7 @@ if 'battery' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/bluetooth/_device.py b/platypush/entities/bluetooth/_device.py index a2e9d8a85..0f45121b4 100644 --- a/platypush/entities/bluetooth/_device.py +++ b/platypush/entities/bluetooth/_device.py @@ -9,13 +9,13 @@ from sqlalchemy import ( String, ) -from platypush.common.db import Base +from platypush.common.db import is_defined from ..devices import Device from ._service import BluetoothService -if 'bluetooth_device' not in Base.metadata: +if not is_defined('bluetooth_device'): class BluetoothDevice(Device): """ @@ -68,6 +68,7 @@ if 'bluetooth_device' not in Base.metadata: model_id = Column(String, default=None) """ Device model ID. """ + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/bluetooth/_service.py b/platypush/entities/bluetooth/_service.py index 5b2bf1eb2..d00dbae91 100644 --- a/platypush/entities/bluetooth/_service.py +++ b/platypush/entities/bluetooth/_service.py @@ -8,10 +8,10 @@ from sqlalchemy import ( String, ) -from platypush.common.db import Base +from platypush.common.db import is_defined from platypush.entities import Entity -if 'bluetooth_service' not in Base.metadata: +if not is_defined('bluetooth_service'): class BluetoothService(Entity): """ @@ -44,6 +44,7 @@ if 'bluetooth_service' not in Base.metadata: connected = Column(Boolean, default=False) """ Whether an active connection exists to this service. """ + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/buttons.py b/platypush/entities/buttons.py index 3909d2340..917c358c6 100644 --- a/platypush/entities/buttons.py +++ b/platypush/entities/buttons.py @@ -6,14 +6,14 @@ from sqlalchemy import ( Integer, ) -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import EnumSensor logger = logging.getLogger(__name__) -if 'button' not in Base.metadata: +if not is_defined('button'): class Button(EnumSensor): __tablename__ = 'button' @@ -22,6 +22,7 @@ if 'button' not in Base.metadata: Integer, ForeignKey(EnumSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/cloud.py b/platypush/entities/cloud.py index 070665f16..f98a95408 100644 --- a/platypush/entities/cloud.py +++ b/platypush/entities/cloud.py @@ -6,12 +6,12 @@ from sqlalchemy import ( String, ) -from platypush.common.db import Base +from platypush.common.db import is_defined from .devices import Device -if 'cloud_instance' not in Base.metadata: +if not is_defined('cloud_instance'): class CloudInstance(Device): """ @@ -38,6 +38,7 @@ if 'cloud_instance' not in Base.metadata: alerts = Column(JSON) backups = Column(JSON) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/contact.py b/platypush/entities/contact.py index 00b502b58..aae0a796d 100644 --- a/platypush/entities/contact.py +++ b/platypush/entities/contact.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import BinarySensor -if 'contact_sensor' not in Base.metadata: +if not is_defined('contact_sensor'): class ContactSensor(BinarySensor): """ @@ -18,6 +18,7 @@ if 'contact_sensor' not in Base.metadata: Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/devices.py b/platypush/entities/devices.py index a65e50b38..fb8d5d601 100644 --- a/platypush/entities/devices.py +++ b/platypush/entities/devices.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, Boolean, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from ._base import Entity -if 'device' not in Base.metadata: +if not is_defined('device'): class Device(Entity): """ @@ -19,6 +19,7 @@ if 'device' not in Base.metadata: ) reachable = Column(Boolean, default=True) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/dimmers.py b/platypush/entities/dimmers.py index bdfa1d934..9c7791924 100644 --- a/platypush/entities/dimmers.py +++ b/platypush/entities/dimmers.py @@ -1,11 +1,11 @@ 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 -if 'dimmer' not in Base.metadata: +if not is_defined('dimmer'): class Dimmer(Device): """ @@ -24,6 +24,7 @@ if 'dimmer' not in Base.metadata: value = Column(Float) unit = Column(String) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/distance.py b/platypush/entities/distance.py index 1fe0193a4..c54bd20e3 100644 --- a/platypush/entities/distance.py +++ b/platypush/entities/distance.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'distance_sensor' not in Base.metadata: +if not is_defined('distance_sensor'): class DistanceSensor(NumericSensor): """ @@ -18,6 +18,7 @@ if 'distance_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/electricity.py b/platypush/entities/electricity.py index faa3d81c8..118c86d64 100644 --- a/platypush/entities/electricity.py +++ b/platypush/entities/electricity.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'power_sensor' not in Base.metadata: +if not is_defined('power_sensor'): class PowerSensor(NumericSensor): __tablename__ = 'power_sensor' @@ -14,12 +14,13 @@ if 'power_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'current_sensor' not in Base.metadata: +if not is_defined('current_sensor'): class CurrentSensor(NumericSensor): __tablename__ = 'current_sensor' @@ -28,12 +29,13 @@ if 'current_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'voltage_sensor' not in Base.metadata: +if not is_defined('voltage_sensor'): class VoltageSensor(NumericSensor): __tablename__ = 'voltage_sensor' @@ -42,12 +44,13 @@ if 'voltage_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'energy_sensor' not in Base.metadata: +if not is_defined('energy_sensor'): class EnergySensor(NumericSensor): __tablename__ = 'energy_sensor' @@ -56,6 +59,7 @@ if 'energy_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/heart.py b/platypush/entities/heart.py index 3c9e3067c..03edbe4da 100644 --- a/platypush/entities/heart.py +++ b/platypush/entities/heart.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'heart_rate_sensor' not in Base.metadata: +if not is_defined('heart_rate_sensor'): class HeartRateSensor(NumericSensor): """ @@ -18,6 +18,7 @@ if 'heart_rate_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/humidity.py b/platypush/entities/humidity.py index 8672d8fd3..0fcc37837 100644 --- a/platypush/entities/humidity.py +++ b/platypush/entities/humidity.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'humidity_sensor' not in Base.metadata: +if not is_defined('humidity_sensor'): class HumiditySensor(NumericSensor): """ @@ -18,12 +18,13 @@ if 'humidity_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'dew_point_sensor' not in Base.metadata: +if not is_defined('dew_point_sensor'): class DewPointSensor(NumericSensor): """ @@ -36,6 +37,7 @@ if 'dew_point_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/illuminance.py b/platypush/entities/illuminance.py index 7d37575e8..7bf99863d 100644 --- a/platypush/entities/illuminance.py +++ b/platypush/entities/illuminance.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'illuminance_sensor' not in Base.metadata: +if not is_defined('illuminance_sensor'): class IlluminanceSensor(NumericSensor): __tablename__ = 'illuminance_sensor' @@ -14,6 +14,7 @@ if 'illuminance_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/lights.py b/platypush/entities/lights.py index 4ccb122cb..a9a673078 100644 --- a/platypush/entities/lights.py +++ b/platypush/entities/lights.py @@ -1,11 +1,11 @@ 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 -if 'light' not in Base.metadata: +if not is_defined('light'): class Light(Device): __tablename__ = 'light' @@ -34,6 +34,7 @@ if 'light' not in Base.metadata: temperature_min = Column(Float) temperature_max = Column(Float) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/linkquality.py b/platypush/entities/linkquality.py index 6dc4169d8..7a25da520 100644 --- a/platypush/entities/linkquality.py +++ b/platypush/entities/linkquality.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'link_quality' not in Base.metadata: +if not is_defined('link_quality'): class LinkQuality(NumericSensor): __tablename__ = 'link_quality' @@ -19,6 +19,7 @@ if 'link_quality' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/magnetism.py b/platypush/entities/magnetism.py index 1e464ba98..0f9265f25 100644 --- a/platypush/entities/magnetism.py +++ b/platypush/entities/magnetism.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .three_axis import ThreeAxisSensor -if 'magnetometer' not in Base.metadata: +if not is_defined('magnetometer'): class Magnetometer(ThreeAxisSensor): """ @@ -20,6 +20,7 @@ if 'magnetometer' not in Base.metadata: primary_key=True, ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/motion.py b/platypush/entities/motion.py index 05a88eef8..70c87563a 100644 --- a/platypush/entities/motion.py +++ b/platypush/entities/motion.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import BinarySensor -if 'motion_sensor' not in Base.metadata: +if not is_defined('motion_sensor'): class MotionSensor(BinarySensor): __tablename__ = 'motion_sensor' @@ -14,6 +14,7 @@ if 'motion_sensor' not in Base.metadata: Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/presence.py b/platypush/entities/presence.py index 334bc4c43..9739d9ce2 100644 --- a/platypush/entities/presence.py +++ b/platypush/entities/presence.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import BinarySensor -if 'presence_sensor' not in Base.metadata: +if not is_defined('presence_sensor'): class PresenceSensor(BinarySensor): """ @@ -18,6 +18,7 @@ if 'presence_sensor' not in Base.metadata: Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/pressure.py b/platypush/entities/pressure.py index d52767b89..d65c77426 100644 --- a/platypush/entities/pressure.py +++ b/platypush/entities/pressure.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'pressure_sensor' not in Base.metadata: +if not is_defined('pressure_sensor'): class PressureSensor(NumericSensor): """ @@ -18,6 +18,7 @@ if 'pressure_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/sensors.py b/platypush/entities/sensors.py index 0542c01f1..3103a87ca 100644 --- a/platypush/entities/sensors.py +++ b/platypush/entities/sensors.py @@ -12,7 +12,7 @@ from sqlalchemy import ( String, ) -from platypush.common.db import Base +from platypush.common.db import is_defined from .devices import Device @@ -32,7 +32,7 @@ class Sensor(Device): super().__init__(*args, **kwargs) -if 'raw_sensor' not in Base.metadata: +if not is_defined('raw_sensor'): class RawSensor(Sensor): """ @@ -86,12 +86,13 @@ if 'raw_sensor' not in Base.metadata: self.is_binary = False self.is_json = False + __table_args__ = {'extend_existing': True} __mapper_args__ = { '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): """ @@ -109,6 +110,7 @@ if 'numeric_sensor' not in Base.metadata and 'percent_sensor' not in Base.metada max = Column(Float) unit = Column(String) + __table_args__ = {'extend_existing': True} __mapper_args__ = { '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 ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { '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) -if 'binary_sensor' not in Base.metadata: +if not is_defined('binary_sensor'): class BinarySensor(Sensor): """ @@ -163,12 +166,13 @@ if 'binary_sensor' not in Base.metadata: ) value = Column(Boolean) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'enum_sensor' not in Base.metadata: +if not is_defined('enum_sensor'): class EnumSensor(Sensor): """ @@ -184,12 +188,13 @@ if 'enum_sensor' not in Base.metadata: values = Column(JSON) """ Possible values for the sensor, as a JSON array. """ + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'composite_sensor' not in Base.metadata: +if not is_defined('composite_sensor'): class CompositeSensor(Sensor): """ @@ -204,6 +209,7 @@ if 'composite_sensor' not in Base.metadata: ) value = Column(JSON) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/steps.py b/platypush/entities/steps.py index ff536e336..183f8deda 100644 --- a/platypush/entities/steps.py +++ b/platypush/entities/steps.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'steps_sensor' not in Base.metadata: +if not is_defined('steps_sensor'): class StepsSensor(NumericSensor): """ @@ -18,6 +18,7 @@ if 'steps_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/switches.py b/platypush/entities/switches.py index 316e4cadc..1f2df58f0 100644 --- a/platypush/entities/switches.py +++ b/platypush/entities/switches.py @@ -1,11 +1,11 @@ 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 -if 'switch' not in Base.metadata: +if not is_defined('switch'): class Switch(Device): __tablename__ = 'switch' @@ -15,12 +15,13 @@ if 'switch' not in Base.metadata: ) state = Column(Boolean) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'enum_switch' not in Base.metadata: +if not is_defined('enum_switch'): class EnumSwitch(Device): __tablename__ = 'enum_switch' @@ -31,6 +32,7 @@ if 'enum_switch' not in Base.metadata: value = Column(String) values = Column(JSON) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/system.py b/platypush/entities/system.py index 82744b517..0421399ad 100644 --- a/platypush/entities/system.py +++ b/platypush/entities/system.py @@ -1,6 +1,6 @@ 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 .devices import Device @@ -8,7 +8,7 @@ from .sensors import NumericSensor, PercentSensor from .temperature import TemperatureSensor -if 'cpu' not in Base.metadata: +if not is_defined('cpu'): class Cpu(Entity): """ @@ -23,12 +23,13 @@ if 'cpu' not in Base.metadata: percent = Column(Float) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'cpu_info' not in Base.metadata: +if not is_defined('cpu_info'): class CpuInfo(Entity): """ @@ -54,12 +55,13 @@ if 'cpu_info' not in Base.metadata: l2_cache_size = Column(Integer) l3_cache_size = Column(Integer) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'cpu_times' not in Base.metadata: +if not is_defined('cpu_times'): class CpuTimes(Entity): """ @@ -72,12 +74,13 @@ if 'cpu_times' not in Base.metadata: Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'cpu_stats' not in Base.metadata: +if not is_defined('cpu_stats'): class CpuStats(Entity): """ @@ -90,12 +93,13 @@ if 'cpu_stats' not in Base.metadata: Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'memory_stats' not in Base.metadata: +if not is_defined('memory_stats'): class MemoryStats(Entity): """ @@ -119,12 +123,13 @@ if 'memory_stats' not in Base.metadata: shared = Column(Integer) percent = Column(Float) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'swap_stats' not in Base.metadata: +if not is_defined('swap_stats'): class SwapStats(Entity): """ @@ -142,12 +147,13 @@ if 'swap_stats' not in Base.metadata: free = Column(Integer) percent = Column(Float) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'disk' not in Base.metadata: +if not is_defined('disk'): class Disk(Entity): """ @@ -175,12 +181,13 @@ if 'disk' not in Base.metadata: write_time = Column(Float) busy_time = Column(Float) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'network_interface' not in Base.metadata: +if not is_defined('network_interface'): class NetworkInterface(Device): """ @@ -207,12 +214,13 @@ if 'network_interface' not in Base.metadata: duplex = Column(String) flags = Column(JSON) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'system_temperature' not in Base.metadata: +if not is_defined('system_temperature'): class SystemTemperature(TemperatureSensor): """ @@ -230,12 +238,13 @@ if 'system_temperature' not in Base.metadata: high = Column(Float) critical = Column(Float) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'system_fan' not in Base.metadata: +if not is_defined('system_fan'): class SystemFan(NumericSensor): """ @@ -250,12 +259,13 @@ if 'system_fan' not in Base.metadata: primary_key=True, ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } -if 'system_battery' not in Base.metadata: +if not is_defined('system_battery'): class SystemBattery(PercentSensor): """ @@ -273,6 +283,7 @@ if 'system_battery' not in Base.metadata: seconds_left = Column(Float) power_plugged = Column(Boolean) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/temperature.py b/platypush/entities/temperature.py index 2b378d47d..08824f145 100644 --- a/platypush/entities/temperature.py +++ b/platypush/entities/temperature.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'temperature_sensor' not in Base.metadata: +if not is_defined('temperature_sensor'): class TemperatureSensor(NumericSensor): __tablename__ = 'temperature_sensor' @@ -14,6 +14,7 @@ if 'temperature_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/three_axis.py b/platypush/entities/three_axis.py index 0789539eb..553b0bb0e 100644 --- a/platypush/entities/three_axis.py +++ b/platypush/entities/three_axis.py @@ -1,13 +1,13 @@ from typing import Iterable, Mapping, Optional, Union 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 .sensors import RawSensor -if 'three_axis_sensor' not in Base.metadata: +if not is_defined('three_axis_sensor'): class ThreeAxisSensor(RawSensor): """ @@ -20,6 +20,7 @@ if 'three_axis_sensor' not in Base.metadata: Integer, ForeignKey(RawSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/time.py b/platypush/entities/time.py index f5cf6e885..f72aab52c 100644 --- a/platypush/entities/time.py +++ b/platypush/entities/time.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'time_duration' not in Base.metadata: +if not is_defined('time_duration'): class TimeDuration(NumericSensor): """ @@ -18,6 +18,7 @@ if 'time_duration' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, } diff --git a/platypush/entities/variables.py b/platypush/entities/variables.py index 39c7a8ff7..41d383eec 100644 --- a/platypush/entities/variables.py +++ b/platypush/entities/variables.py @@ -2,14 +2,14 @@ import logging from sqlalchemy import Column, ForeignKey, Integer, String -from platypush.common.db import Base +from platypush.common.db import is_defined from . import Entity logger = logging.getLogger(__name__) -if 'variable' not in Base.metadata: +if not is_defined('variable'): class Variable(Entity): """ diff --git a/platypush/entities/weight.py b/platypush/entities/weight.py index 37cbe4f45..6fee1f24f 100644 --- a/platypush/entities/weight.py +++ b/platypush/entities/weight.py @@ -1,11 +1,11 @@ from sqlalchemy import Column, Integer, ForeignKey -from platypush.common.db import Base +from platypush.common.db import is_defined from .sensors import NumericSensor -if 'weight_sensor' not in Base.metadata: +if not is_defined('weight_sensor'): class WeightSensor(NumericSensor): """ @@ -18,6 +18,7 @@ if 'weight_sensor' not in Base.metadata: Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True ) + __table_args__ = {'extend_existing': True} __mapper_args__ = { 'polymorphic_identity': __tablename__, }