Written by Matthias Osswald - September
2022
SchemaHero provides a great way to
do GitOps with database tables. The current state of your database is
always displayed in Git while an operator handles the migrations for
you.
In Python one of the most commonly used ORM is SQLAlchemy.
Basically one could manually write the YAML files for the tables for an app using SQLAlchemy. But why do the work if there is a simpler solution?
That’s when I started writing a small library to generate the YAMLs automatically using the declarative base from SQLAlchemy.
sqlalchemy-hero
is the library providing a way to generate all your table YAMLs for
SchemaHero. In the following example an instance of HeroGenerator is
created with the SQLAlchemy base, the database type you want to use, the
namespace the resource should be applied in and the database name for
the tables.
Additionally you can specify the api version or custom type
mappings.
The files will be generated and saved to the out path.
hero_generator = HeroGenerator(
base=Base,
db_type=HeroDatabase.postgres,
namespace="hero-ns",
database="hero-db",
)
hero_generator.to_yaml_files(out_path=Path("./out"))