python-telegram-bot/ptbcontrib

[FEATURE] Support MySQL in PTB_sqlalchemy_persistence

kolja-wagner opened this issue · 2 comments

PTB_sqlalchemy_persistence

Because other data in the project is already stored in mysql i've tried to adapt the code for using mysql instead of postgres. (And never run postgres myself, maybe this is already the source of the bug.)

Steps to reproduce

  1. Altering the test for postgres-uri in line 71.
  2. Use sqlalchemy-scoped-session bound to a mysql-database.
  3. start the updater

Actual behaviour

File "persistence.py", line 108, in __load_database:
self._chat_data = defaultdict(dict, self._key_mapper(data.get("chat_data", {}), int))
AttributeError: 'str' object has no attribute 'get'

For some reasons 'data' is of type 'str'. ("{}" after calling __init_database())

Solution

adding two lines of code at 105:

if isinstance(data, str):
    data = json.loads(data)

Version of Python, python-telegram-bot & dependencies:

$ python -m telegram
python-telegram-bot 13.11
Bot API 5.7
certifi 2021.10.08
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)]

mysql 8.0
sqlalchemy 1.4.27
conda 4.11.0
Windows 11

I thought somebody could find this interessting. Apart from this minor fix could this module named more general.

Hi, first of all thanks for your feedback, tbh this is not really a "bug" since it doesn't mention anywhere that it works with MySQL too though having support for MySQL along with postgresql is really good idea! Would you like to create a PR with this change?

I might try, though i'am verry new to contributing to projects other than my own or from my commrades.