Persistence

To store user data like points the bot needs some way of persisting data to a database. For that purpose, an Object Relational Mapper is used.

An ORM provides a higher level of abstraction over SQL and a DBMS, ensuring security against injection, DBMS-independent code and makes storing and retrieving data a easier and less error-prone.

SQLAlchemy

The bot uses SQLAlchemy 1.4 as ORM, which connects to a Postgres server running in a docker compose container.

The hashtablebot.persistence package contains the code that handles persisting and retrieving data, and hashtablebot.entity defines the Python objects to be persisted and their corresponding table structures.

Alembic

Alembic is used to create migrations for changes in the database, such as new tables and columns.

As long as the target_metadata variable in env.py includes all the tables, the migrations can be autogenerated with:

alembic revision --autogenerate

Then run the migration with:

alembic upgrade head