From d33d760361375b8f00c069342f1c7fdeaa3ac6b1 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 24 Apr 2023 23:21:39 +0200 Subject: [PATCH] Better way to import `declarative_base` from SQLAlchemy. Import `declarative_base` in a way that is compatible with any SQLAlchemy version between 1.3 and 2.x. --- platypush/backend/covid19/__init__.py | 3 ++- platypush/backend/github/__init__.py | 3 ++- platypush/backend/http/request/rss/__init__.py | 3 ++- platypush/backend/mail/__init__.py | 4 +++- platypush/common/db.py | 9 ++++++++- platypush/plugins/media/search/local.py | 3 ++- 6 files changed, 19 insertions(+), 6 deletions(-) diff --git a/platypush/backend/covid19/__init__.py b/platypush/backend/covid19/__init__.py index 1be1db016..5f7444970 100644 --- a/platypush/backend/covid19/__init__.py +++ b/platypush/backend/covid19/__init__.py @@ -3,9 +3,10 @@ import os from typing import Optional, Union, List, Dict, Any from sqlalchemy import create_engine, Column, Integer, String, DateTime -from sqlalchemy.orm import sessionmaker, scoped_session, declarative_base +from sqlalchemy.orm import sessionmaker, scoped_session from platypush.backend import Backend +from platypush.common.db import declarative_base from platypush.config import Config from platypush.context import get_plugin from platypush.message.event.covid19 import Covid19UpdateEvent diff --git a/platypush/backend/github/__init__.py b/platypush/backend/github/__init__.py index 0a1bc3e67..6922db391 100644 --- a/platypush/backend/github/__init__.py +++ b/platypush/backend/github/__init__.py @@ -6,9 +6,10 @@ from typing import Optional, List import requests from sqlalchemy import create_engine, Column, String, DateTime -from sqlalchemy.orm import sessionmaker, scoped_session, declarative_base +from sqlalchemy.orm import sessionmaker, scoped_session from platypush.backend import Backend +from platypush.common.db import declarative_base from platypush.config import Config from platypush.message.event.github import ( GithubPushEvent, diff --git a/platypush/backend/http/request/rss/__init__.py b/platypush/backend/http/request/rss/__init__.py index 7ca6d9c64..6c624ae49 100644 --- a/platypush/backend/http/request/rss/__init__.py +++ b/platypush/backend/http/request/rss/__init__.py @@ -12,10 +12,11 @@ from sqlalchemy import ( ForeignKey, ) -from sqlalchemy.orm import sessionmaker, scoped_session, declarative_base +from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy.sql.expression import func from platypush.backend.http.request import HttpRequest +from platypush.common.db import declarative_base from platypush.config import Config from platypush.context import get_plugin from platypush.message.event.http.rss import NewFeedEvent diff --git a/platypush/backend/mail/__init__.py b/platypush/backend/mail/__init__.py index 84f1f8ab6..5dd4474fe 100644 --- a/platypush/backend/mail/__init__.py +++ b/platypush/backend/mail/__init__.py @@ -9,9 +9,10 @@ from threading import Thread, RLock from typing import List, Dict, Any, Optional, Tuple from sqlalchemy import engine, create_engine, Column, Integer, String, DateTime -from sqlalchemy.orm import sessionmaker, scoped_session, declarative_base +from sqlalchemy.orm import sessionmaker, scoped_session from platypush.backend import Backend +from platypush.common.db import declarative_base from platypush.config import Config from platypush.context import get_plugin from platypush.message.event.mail import ( @@ -40,6 +41,7 @@ class MailboxStatus(Base): # + # @dataclass class Mailbox: diff --git a/platypush/common/db.py b/platypush/common/db.py index 59be70308..f1979e3b1 100644 --- a/platypush/common/db.py +++ b/platypush/common/db.py @@ -1,3 +1,10 @@ -from sqlalchemy.orm import declarative_base +from sqlalchemy import __version__ + +sa_version = tuple(map(int, __version__.split('.'))) + +if sa_version >= (1, 4, 0): + from sqlalchemy.orm import declarative_base +else: + from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() diff --git a/platypush/plugins/media/search/local.py b/platypush/plugins/media/search/local.py index 76868aa4f..21603dc1b 100644 --- a/platypush/plugins/media/search/local.py +++ b/platypush/plugins/media/search/local.py @@ -12,9 +12,10 @@ from sqlalchemy import ( PrimaryKeyConstraint, ForeignKey, ) -from sqlalchemy.orm import sessionmaker, scoped_session, declarative_base +from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy.sql.expression import func +from platypush.common.db import declarative_base from platypush.config import Config from platypush.plugins.media import MediaPlugin from platypush.plugins.media.search import MediaSearcher