Migration¶
Migration is a process of migrating models into the database. In Flask, the extension Flask-Migrate handles SQLAlchemy database migrations for Flask applications using Alembic.
SQLAlchemy¶
SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.
To create a table, we need to have a SQLAlchemy function. The function can be updated and changed.
See the code below
Define a table and a class to hold stac harvester the class has a get method that returns a list of stac harvester there is also a mapper to map relationships.
StacHarvester ¶
Bases: StatefulObjectMixin
, DomainObject
StacHarvester model.
Source code in ckanext/saeoss/model/stac_harvester.py
Migrate a model¶
In SAEOSS-Portal, after adding or updating a model. To apply a migration, run the following command:
docker exec -it saeoss_ckan-web_1 poetry run alembic -c /home/appuser/app/ckanext/saeoss/migration/saeoss/alembic.ini revision -m "Add stac harvest table"
After running the above command, a new file would have been created in the directory: ckanext/saeoss/migration/saeoss/alembic/versions
In each file in the path, there is a feature called upgrade and/or downgrade to create or update the table.
See the code below
Add stac harvest table
Revision ID: 796cac900039 Revises: cf3438c769b2 Create Date: 2023-10-11 11:37:26.280343
downgrade ¶
upgrade ¶
Create table function.