๐ From Chaos to Order: How Mongock & Friends Keep Your DB in Shape Automatically!
☁️ The “Cloudyrock” Mystery
You might have seen this in a Spring Boot project:
@ChangeLog
public class DatabaseChangelog {
@ChangeSet(order = "001", id = "createIndex", author = "anand")
public void createIndex(MongoTemplate mongoTemplate) {
mongoTemplate.indexOps("users")
.ensureIndex(new Index().on("email", Sort.Direction.ASC).unique());
}
}
And wondered:
That’s Mongock in action — made by the Cloudyrock team.
It’s basically the database time machine for Java projects.
No flux capacitors, just clever scanning, execution, and bookkeeping.
๐ How It Works (From Start to End)
1️⃣ Scan Your Change Logs
You specify in application.properties
:
mongock.change-logs-scan-package=com.javabeanbag.changelog
Mongock starts with Spring Boot and scans this package for classes annotated with @ChangeLog
.
2️⃣ Run Only What’s New
Inside each @ChangeLog
, you define @ChangeSet
methods.
- id → Unique name for the change
- order → Execution order
- author → Who wrote it
Mongock stores the executed changes in a special collection/table (e.g., mongockChangeLog
in MongoDB).
“Is this
id
already in my history table?If yes → skip
If no → run and record it.”
This prevents duplicate execution and “index already exists” errors.
3️⃣ Apply Changes
- For MongoDB → Uses
MongoTemplate
orMongoDatabase
to run commands - For MySQL/PostgreSQL (JDBC mode) → Runs SQL via JDBC
✅ Yes — Mongock can work with MySQL, but for SQL databases, Liquibase/Flyway are more common.
๐ Example — Adding an Index in MongoDB
@ChangeLog(order = "001")
public class UserIndexes {
@ChangeSet(order = "001", id = "createEmailIndex", author = "anand")
public void createEmailIndex(MongoTemplate mongoTemplate) {
mongoTemplate.indexOps("users")
.ensureIndex(new Index().on("email", Sort.Direction.ASC).unique());
}
}
๐ What Happens in the DB
After the first startup, Mongock creates a special collection in MongoDB:
mongockChangeLog
{
"_id": ObjectId("66bb98f4134a6e24f0e91234"),
"changeId": "createEmailIndex",
"author": "anand",
"timestamp": ISODate("2025-08-13T10:20:00Z"),
"state": "EXECUTED",
"executionMillis": 120,
"executionHostname": "dev-machine",
"executionId": "f27b81f2-2341-4d8f-8d4b-23bb53f1c9ae"
}
๐ Next Startup: Mongock checks changeId
→ “Already exists” → Skips execution.
๐ก Example — Seeding Initial Data in MySQL
@ChangeLog(order = "002")
public class InitialData {
@ChangeSet(order = "001", id = "insertAdmin", author = "anand")
public void insertAdmin(Connection connection) throws SQLException {
try (PreparedStatement stmt = connection.prepareStatement(
"INSERT INTO users (id, username, role) VALUES (1, 'admin', 'ADMIN')")) {
stmt.executeUpdate();
}
}
}
⚔️ Mongock vs Liquibase vs Flyway — Quick Battle Card
Feature | Mongock | Liquibase | Flyway |
---|---|---|---|
MongoDB Support | ✅ Native | ❌ | ❌ |
MySQL/Postgres | ✅ (JDBC) | ✅ | ✅ |
Java Code Changes | ✅ | ❌ (SQL/XML) | ❌ (SQL only) |
Microservices Ready | ✅ | ✅ | ✅ |
Best For | Mixed NoSQL+SQL | Complex DB | Simple SQL |
๐ Life Without Mongock
- ๐ง Remembering which DB got which index
- ๐ Writing “DB update” docs nobody reads
- ๐♂️ Running manual scripts at midnight before deployment
- ๐ Accidentally applying the same change twice
☕ Life With Mongock: Drink coffee → Deploy → Mongock: “Relax, I got this.”
๐ Wrapping Up ☕
When working in microservices, keeping DB schemas in sync is like keeping cats in a bathtub — chaos ๐ฑ๐ฆ.
Mongock (or Liquibase/Flyway) brings order:
- Tracks applied changes
- Avoids duplicates
- Works across environments without manual steps
MongoDB → Go Mongock
Pure SQL → Use Liquibase or Flyway
Comments
Post a Comment