diff --git a/maubot/db.py b/maubot/db.py index 6a4f82b..6210bee 100644 --- a/maubot/db.py +++ b/maubot/db.py @@ -13,40 +13,15 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from typing import Type -from sqlalchemy import (Column, String, Boolean, ForeignKey, Text, TypeDecorator) +from sqlalchemy import Column, String, Boolean, ForeignKey, Text from sqlalchemy.orm import Query, scoped_session from sqlalchemy.ext.declarative import declarative_base -import json from mautrix.types import UserID, FilterID, SyncToken, ContentURI -from mautrix.client.api.types.util import Serializable - -from .command_spec import CommandSpec Base: declarative_base = declarative_base() -def make_serializable_alchemy(serializable_type: Type[Serializable]): - class SerializableAlchemy(TypeDecorator): - impl = Text - - @property - def python_type(self): - return serializable_type - - def process_literal_param(self, value: Serializable, _) -> str: - return json.dumps(value.serialize()) if value is not None else None - - def process_bind_param(self, value: Serializable, _) -> str: - return json.dumps(value.serialize()) if value is not None else None - - def process_result_value(self, value: str, _) -> serializable_type: - return serializable_type.deserialize(json.loads(value)) if value is not None else None - - return SerializableAlchemy - - class DBPlugin(Base): query: Query __tablename__ = "plugin" @@ -78,20 +53,6 @@ class DBClient(Base): avatar_url: ContentURI = Column(String(255), nullable=False, default="") -class DBCommandSpec(Base): - query: Query - __tablename__ = "command_spec" - - plugin: str = Column(String(255), - ForeignKey("plugin.id", onupdate="CASCADE", ondelete="CASCADE"), - primary_key=True) - client: UserID = Column(String(255), - ForeignKey("client.id", onupdate="CASCADE", ondelete="CASCADE"), - primary_key=True) - spec: CommandSpec = Column(make_serializable_alchemy(CommandSpec), nullable=False) - - def init(session: scoped_session) -> None: DBPlugin.query = session.query_property() DBClient.query = session.query_property() - DBCommandSpec.query = session.query_property()