This page will describe how Matar facilitates communication between the app and its databases.
Matar makes use of Alembic which is a database version management system using SQL Alchemy as the underlying engine. SQL Alchemy is a library that manages communication between Python programs and databases.
With Alembic you can define a schema via a sqlalchemy model, and have the database reflect those changes. Alembic can also migrate schemas via SQLalchemy models. no work has to be done manually which reduces chances of error.
Let's take a sample sqlalchemy model from matar code repo ,alembic will create a table reflecting this schema in our database:
SQLAlchemy Model
In a file db.py , we have the following SQLAlchemy model for an organization in matar app :
In the code above `class organization(Base)` uses a SQLalchemy base class that comes with a number of features including metadata that contains information about all schemas.
Alembic uses the metadata information from Base to create database tables. The alembic components with database configuration are located inside the migrations folder of the Matar repo.
Migrations directory
Here is a preview of migrations folder of the matar repo:
versions - Directory for storage of migration files.
alembic.ini - Alembic configuration file.
README - Instructions on how to use alembic.
env.py - Commands and configurations for alembic migrations.
script.py.mako - This is a Mako template file which is used to generate new migration scripts. Whatever is here is used to generate new files within versions/.
Here is an alembic migration file related to organization SQLalchemy model defined on the starting of the page:
An alembic migration file contains two functions: upgrade and downgrade. These functions contain the necessary operations to implement the changes that you made to your SQLAlchemy models (upgrade) and to undo the changes once they've been implemented (downgrade).
Another thing to notice is the down_revision variable. This is how Alembic knows the correct order in which to apply migrations. When we create the next revision, the new file’s down_revision identifier would point to this one:
Now if we want to modify or add another column to the organization table. We need to perform another migration where alembicrevise '4640e9af0234' and create a new revision.
"""Add an organization ID column to Organization Database Table. Revision ID: 01ea0e75088aRevises: 4640e9af0234Create Date: 2023-04-28 02:27:59.044441"""from alembic import opimport sqlalchemy as sa# revision identifiers, used by Alembic.revision ='01ea0e75088a'down_revision ='4640e9af0234'branch_labels =Nonedepends_on =Nonedefupgrade():# ### commands auto generated by Alembic - please adjust! ###with op.batch_alter_table('post_activity', schema=None)as batch_op: batch_op.add_column(sa.Column('organization_id', sa.String(), nullable=True))# ### end Alembic commands ###defdowngrade():# ### commands auto generated by Alembic - please adjust! ###with op.batch_alter_table('post_activity', schema=None)as batch_op: batch_op.drop_column('organization_id')# ### end Alembic commands ###
Listing SQLAlchemy Models
A detailed list of all relevant SQLAlchemy models is provided below: