Featured image of post How to Use Database Migrations to Evolve Schemas Smoothly

How to Use Database Migrations to Evolve Schemas Smoothly

Flyway, Liquibase, Entity Framework Migrations, and Custom Scripts

Ah, database migrations. The art of changing your database schema without breaking everything like a Jenga tower on a wobbly table.

If you’ve ever been tasked with modifying a database structure in a live application, you know the fear that comes with it.

But fear not!

Tools like Flyway, Liquibase, Entity Framework Migrations, and even custom scripts can help us evolve database schemas smoothly.


1. Flyway - The Lightweight Migration Champ

Flyway is a simple and effective migration tool that works with raw SQL scripts. It’s great when you want a no-nonsense approach to database migrations.

Installation

To install Flyway, grab it via SDKMAN or manually download it:

1
2
3
4
# Install via SDKMAN
sdk install flyway

# Or download manually from Flyway's website

Creating a Migration

Flyway expects SQL migration files in a db/migration folder:

1
2
3
4
5
6
-- V1__Create_Users_Table.sql
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL
);

Then run:

1
flyway migrate

And boom! Your schema is updated.


2. Liquibase - The XML/JSON/YAML SQL Guru

Liquibase is another powerful migration tool that allows you to define migrations in XML, JSON, YAML, or raw SQL.

Installation

Grab Liquibase with:

1
2
brew install liquibase  # On Mac
choco install liquibase  # On Windows

Writing a Migration

Here’s a sample Liquibase changelog file using YAML:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
databaseChangeLog:
  - changeSet:
      id: 1
      author: developer
      changes:
        - createTable:
            tableName: users
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
              - column:
                  name: name
                  type: varchar(100)
              - column:
                  name: email
                  type: varchar(100)
                  constraints:
                    unique: true

Apply it with:

1
liquibase update

Nice and structured!


3. Entity Framework Migrations - The .NET Dev’s Best Friend

If you’re working in .NET, Entity Framework Migrations is a fantastic way to handle schema changes.

Adding a Migration

1
2
dotnet ef migrations add CreateUsersTable
dotnet ef database update

Example Migration

EF generates a migration file in C#:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public partial class CreateUsersTable : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Users",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                Name = table.Column<string>(maxLength: 100, nullable: false),
                Email = table.Column<string>(maxLength: 100, nullable: false, unique: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Users", x => x.Id);
            }
        );
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable("Users");
    }
}

C# magic at its finest!


4. Custom Scripts - The DIY Migration Approach

If you like living on the edge (or your project has specific needs), writing custom migration scripts is always an option.

Example Bash Script

1
2
#!/bin/bash
psql -d mydatabase -U myuser -c "CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) UNIQUE);"

Run it:

1
bash migrate.sh

You now have full control—just don’t mess it up!


Conclusion

Migrations are essential for evolving database schemas without chaos. Depending on your stack, you can use:

  • Flyway for simple SQL-based migrations
  • Liquibase for structured migrations in XML, JSON, YAML, or SQL
  • Entity Framework Migrations for C#-based schema evolution
  • Custom scripts if you like to roll up your sleeves

Choose the right tool for your project, and may your schemas evolve smoothly without breaking production! 🚀


Key Ideas

ConceptDescription
FlywaySimple SQL-based migration tool
LiquibaseSupports structured migrations in XML, JSON, YAML, and SQL
Entity Framework Migrations.NET-based migration system
Custom ScriptsDIY approach for schema evolution
Migration ToolsHelp apply schema changes without downtime

References

  1. Flyway Documentation
  2. Liquibase Documentation
  3. Entity Framework Migrations
  4. PostgreSQL PSQL Guide