Urgent.News

What's breaking now, across thousands of outlets.

Tech

Generated SQL migrations for Python, TypeScript and Go projects

Migrax started as a tool for Java: it reads JPA entities and writes the SQL migrations that bring the database in line with them. But the idea doesn't depend on Java. Every ORM knows the tables it expects, and almost every ORM can print them as CREATE TABLE statements. That SQL is all Migrax needs. Keep it in a schema file, and Migrax compares it with what the existing migrations build, writes…

Migrax was initially designed for Java, generating SQL migrations based on JPA entities. However, the concept applies to any ORM, as they all know the expected tables and can generate CREATE TABLE statements. The tool reads a schema file, compares it with the existing migrations, writes the next migration with its rollback script, and applies it. This article demonstrates the process using a Python project, Drizzle in TypeScript, and GORM in Go.

Exporting the schema involves creating a script that compiles the ORM models into SQL CREATE TABLE statements. This script is then used to generate the initial migration. Migrax reads its settings from environment variables, including the database URL, user, and password, as well as the locations for the migrations and schema file.

During the first migration, Migrax sets up a throwaway PostgreSQL database in Docker, reads the schema file, and creates two tables, customers and orders, along with an index and a foreign key constraint. The rollback script is also generated. Migrax then runs the migration on the throwaway database, which validates the SQL before applying it to the production server.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Multi-Cloud Failover Patterns: AWS vs Azure

Multi-Cloud Failover Patterns: AWS vs Azure Introdução Neste artigo, analisamos Multi-Cloud Failover Patterns: AWS vs Azure com foco prático em serviços gerenciados da AWS e Azure .

Migrax vs Flyway: generated vs hand-written migrations

Flyway and Migrax both keep a database schema in numbered SQL files, apply them in order and record what ran. The difference is where the SQL comes from.

  • Flyway requires manual SQL file writing for each migration
  • Migrax automatically generates migrations from JPA entities
  • Migrax provides automated rollback scripts for destructive changes

More from Saturday 31 October →