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-legacytag and GitHub Release.
- Versioned SQL Migrations: Author schema changes in plain Liquibase-formatted SQL (
--liquibase formatted sql) with explicit--changesetand--rollbackdirectives. - 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 todev. - Automated Rollbacks: Revert the last N applied changesets using Liquibase's
rollbackCounttask. - Zero-Install Gradle Wrapper: Includes
gradlewandgradlew.batpinned to Gradle 8.11.1.
- Liquibase Core
4.30.0& Liquibase Gradle Plugin2.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
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
- Java Development Kit (JDK) 17 or higher
- MySQL server accessible from your machine (default
devactivity connects tolocalhost:3306)
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()
}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()
}# Linux / macOS
./gradlew update
# Windows
gradlew.bat updatePass -Penv=<name> matching an activity defined in build.gradle:
./gradlew update -Penv=preprodRevert the last N applied changesets using rollbackCount:
./gradlew rollbackCount -PliquibaseCommandValue=2For 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.
- Create a new formatted SQL file in
sql/(e.g.,sql/changelog-v2.sql) starting with--liquibase formatted sql, followed by--changeset author:idand--rollbackstatements. Seesql/changelog-v1.sqlfor an example. - 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: trueThis project is licensed under the MIT License — see the LICENSE file for details.