|
| 1 | + |
| 2 | +# ADR-011: Use Flyway for All DDL Operations |
| 3 | + |
| 4 | +Date: 2025-10-24 |
| 5 | + |
| 6 | +## Status |
| 7 | + |
| 8 | +Accepted |
| 9 | + |
| 10 | +## Context |
| 11 | + |
| 12 | +Database schema changes (DDL operations) are currently managed manually or through ad-hoc scripts. |
| 13 | +This approach can lead to inconsistencies, difficulties in tracking changes, and challenges in |
| 14 | +maintaining a reliable migration history. Flyway provides a structured and version-controlled way to |
| 15 | +manage database migrations, ensuring consistency and traceability. |
| 16 | + |
| 17 | +## Decision |
| 18 | + |
| 19 | +All DDL operations will be managed using Flyway scripts. Each table will have its own dedicated |
| 20 | +Flyway script to ensure modularity and clarity. This approach will standardize database migrations |
| 21 | +and simplify collaboration among developers. |
| 22 | + |
| 23 | +## Consequences |
| 24 | + |
| 25 | +- **Positive**: |
| 26 | + - Ensures a consistent and version-controlled approach to database schema changes. |
| 27 | + - Simplifies tracking and auditing of database changes. |
| 28 | + - Reduces the risk of conflicts and errors during migrations. |
| 29 | + - Modular scripts improve clarity and maintainability. |
| 30 | + |
| 31 | +- **Negative**: |
| 32 | + - Requires developers to learn and adopt Flyway conventions. |
| 33 | + - Initial setup effort to migrate existing schema changes into Flyway scripts. |
| 34 | + |
| 35 | +## Implementation |
| 36 | + |
| 37 | +1. Add Flyway as a dependency in the `pom.xml`: |
| 38 | + ```xml |
| 39 | + <dependency> |
| 40 | + <groupId>org.flywaydb</groupId> |
| 41 | + <artifactId>flyway-core</artifactId> |
| 42 | + <version>9.0.0</version> |
| 43 | + </dependency> |
| 44 | + ``` |
| 45 | + |
| 46 | +2. Configure Flyway in the `application.properties` file: |
| 47 | + ```properties |
| 48 | + spring.flyway.enabled=true |
| 49 | + spring.flyway.locations=classpath:db/migration |
| 50 | + spring.flyway.baseline-on-migrate=true |
| 51 | + ``` |
| 52 | + |
| 53 | +3. Create a new Flyway script for each table: |
| 54 | + - Scripts should follow the naming convention `V<version>__<description>.sql`. |
| 55 | + - Example for a `users` table: |
| 56 | + ```sql |
| 57 | + -- File: V1__Create_users_table.sql |
| 58 | + CREATE TABLE users ( |
| 59 | + id BIGINT PRIMARY KEY, |
| 60 | + username VARCHAR(255) NOT NULL, |
| 61 | + password VARCHAR(255) NOT NULL, |
| 62 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 63 | + ); |
| 64 | + |
| 65 | + ``` |
| 66 | + |
| 67 | +the table and the columns in the table should be described using SQL comments. |
| 68 | + |
| 69 | +Use varchar without limitation instead of varchar(xxx) |
| 70 | + |
| 71 | +4. Store all Flyway scripts in the `src/main/resources/db/migration` directory. |
| 72 | + |
| 73 | +5. Document this decision in the project's development guidelines to ensure all new DDL operations |
| 74 | + are added as Flyway scripts. |
| 75 | +```` |
0 commit comments