Flyway
Setup
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
Migration File
-- V1__create_products_table.sql
CREATE TABLE products (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
Key Points
- Understanding Flyway and Liquibase is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Liquibase
Changelog
<databaseChangeLog>
<changeSet id="1" author="dev">
<createTable tableName="products">
<column name="id" type="BIGINT" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
Flyway vs Liquibase
| Aspect | Flyway | Liquibase |
|---|---|---|
| Format | SQL | SQL/XML/YAML |
| Rollback | Manual | Built-in |
Key Points
- Understanding Flyway and Liquibase is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Flyway and Liquibase in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Flyway and Liquibase implementation
// Key aspects: validation, error handling, logging, testing
public class FlywayandLiquibase {
// Production-ready implementation
} Identify and handle edge cases for Flyway and Liquibase. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotency Write a testing strategy for Flyway and Liquibase. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection Quiz
1. Which supports multiple formats?
2. Flyway baseline-on-migrate?
3. What is a common mistake when implementing Flyway and Liquibase?
Flashcards
Question
Flyway format?
Click to reveal answer
Answer
SQL only
Question
Liquibase advantage?
Click to reveal answer
Answer
Multiple formats, built-in rollback
Question
Flyway and Liquibase best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1. Flyway: SQL only, simple
- 2. Liquibase: multiple formats, rollback
- 3. Both track applied migrations
Interview Tips
- • Compare Flyway vs Liquibase
- • Know when to use each
Cheat Sheet
Flyway vs Liquibase
- Flyway: SQL only, V1__name.sql
- Liquibase: XML/YAML/SQL, rollback
- Flyway: simpler
- Liquibase: more features