Skip to content

About

Lightweight Gradle and Liquibase SQL migration project for managing MySQL database schemas, multi-environment deployments, and rollbacks.

Topics

Resources

Stars

3 stars

Watchers

1 watching

Forks

Repository files navigation

Foo Support SQL

License: MIT Java Liquibase Gradle MySQL

A lightweight Gradle and Liquibase SQL database migration project for managing MySQL schema changes, multi-environment deployments, and rollbacks using YAML master changelogs and formatted SQL scripts.

Note: The original legacy version (Gradle 6.3 / Liquibase 3.8.9) is archived under the v0.1.0-legacy tag and GitHub Release.

Key Features

  • Versioned SQL Migrations: Author schema changes in plain Liquibase-formatted SQL (--liquibase formatted sql) with explicit --changeset and --rollback directives.
  • YAML Master Changelog: Organize and include ordered SQL changelog files from a single entry point (config/db.changelog-master.yaml).
  • Multi-Environment Targeting: Switch target database environments via Gradle properties (-Penv=<environment>), defaulting to dev.
  • Automated Rollbacks: Revert the last N applied changesets using Liquibase's rollbackCount task.
  • Zero-Install Gradle Wrapper: Includes gradlew and gradlew.bat pinned to Gradle 8.11.1.

Tech Stack

  • Liquibase Core 4.30.0 & Liquibase Gradle Plugin 2.2.2 (org.liquibase.gradle)
  • MySQL Connector/J 9.1.0 (com.mysql:mysql-connector-j)
  • SnakeYAML 2.3
  • Picocli 4.7.6
  • Gradle Wrapper 8.11.1

Project Structure

foo-support-sql/
├── build.gradle                         # Liquibase plugin, JDBC driver, and environment activities
├── settings.gradle                      # Gradle project configuration (foo-liquibase-sql)
├── gradlew / gradlew.bat                # Gradle Wrapper scripts
├── config/
│   └── db.changelog-master.yaml         # Master YAML changelog including SQL migration files
├── sql/
│   └── changelog-v1.sql                 # Example formatted SQL changeset with rollback
└── src/main/resources/
    └── application.properties           # Application resource placeholder

Prerequisites

  • Java Development Kit (JDK) 17 or higher
  • MySQL server accessible from your machine (default dev activity connects to localhost:3306)

Configuration

Database environments are defined as Liquibase activities in build.gradle. By default, a dev activity is configured and automatically creates the foo_test database if it does not exist:

liquibase {
    activities {
        dev {
            arguments = [
                changelogFile: changeLog,
                url          : 'jdbc:mysql://localhost:3306/foo_test?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8',
                username     : 'root',
                password     : 'root'
            ]
        }
    }
    runList = env.toString().isEmpty() ? 'dev' : env.toString()
}

Adding Another Environment

Add additional activity blocks (such as preprod or prod) inside liquibase.activities in build.gradle:

liquibase {
    activities {
        dev {
            arguments = [
                changelogFile: changeLog,
                url          : 'jdbc:mysql://localhost:3306/foo_test?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8',
                username     : 'root',
                password     : 'root'
            ]
        }
        preprod {
            arguments = [
                changelogFile: changeLog,
                url          : 'jdbc:mysql://mysql-server:3306/my_database?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8',
                username     : 'mysqlusername',
                password     : 'mysqlpass'
            ]
        }
    }
    runList = env.toString().isEmpty() ? 'dev' : env.toString()
}

Usage

Run Migrations (Default dev Environment)

# Linux / macOS
./gradlew update

# Windows
gradlew.bat update

Run Migrations for a Specific Environment

Pass -Penv=<name> matching an activity defined in build.gradle:

./gradlew update -Penv=preprod

Roll Back Changesets

Revert the last N applied changesets using rollbackCount:

./gradlew rollbackCount -PliquibaseCommandValue=2

For example, if the latest applied changeset ID is 5, running with -PliquibaseCommandValue=2 rolls back the two most recent changesets back to 3. You can also combine it with -Penv=<environment> to target a specific environment.

Adding New SQL Migrations

  1. Create a new formatted SQL file in sql/ (e.g., sql/changelog-v2.sql) starting with --liquibase formatted sql, followed by --changeset author:id and --rollback statements. See sql/changelog-v1.sql for an example.
  2. Register the new file in config/db.changelog-master.yaml:
databaseChangeLog:
  - logicalFilePath: config/db.changelog-master.yaml
  - include:
      file: ../sql/changelog-v1.sql
      relativeToChangelogFile: true
  - include:
      file: ../sql/changelog-v2.sql
      relativeToChangelogFile: true

License

This project is licensed under the MIT License — see the LICENSE file for details.

About

Lightweight Gradle and Liquibase SQL migration project for managing MySQL database schemas, multi-environment deployments, and rollbacks.

Topics

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors