Skip to content

Commit 94f672e

Browse files
author
Joern Wellniak
committed
ADR
1 parent 393fd38 commit 94f672e

3 files changed

Lines changed: 140 additions & 0 deletions

File tree

File renamed without changes.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
````
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# ADR-012: Use Flyway Scripts for Database Initialization in Integration Tests
2+
3+
Date: 2025-10-24
4+
5+
## Status
6+
7+
Accepted
8+
9+
## Context
10+
11+
Integration tests (IT) require a consistent and reliable database state to ensure accurate and
12+
reproducible results. Currently, database initialization for integration tests is managed manually
13+
or through ad-hoc scripts, which can lead to inconsistencies and errors. Using the same Flyway
14+
scripts for both production and integration tests ensures that the database schema and data are
15+
consistent across environments.
16+
17+
## Decision
18+
19+
Flyway scripts will be used to initialize the database for integration tests. This ensures that the
20+
database schema and data are consistent with the production environment, reducing the risk of
21+
discrepancies and improving test reliability.
22+
23+
## Consequences
24+
25+
- **Positive**:
26+
- Ensures consistency between production and test environments.
27+
- Reduces duplication of effort in maintaining separate initialization scripts.
28+
- Simplifies debugging by using the same schema and data definitions.
29+
30+
- **Negative**:
31+
- May increase test setup time due to Flyway migrations.
32+
- Requires integration tests to handle potential migration errors.
33+
34+
## Implementation
35+
36+
1. Configure Flyway in the `application-test.properties` file:
37+
```properties
38+
spring.flyway.enabled=true
39+
spring.flyway.locations=classpath:db/migration
40+
spring.flyway.clean-on-validation-error=true
41+
spring.flyway.baseline-on-migrate=true
42+
```
43+
44+
2. Ensure that the integration test database is cleaned and migrated before each test run:
45+
- Use the `@BeforeEach` or `@BeforeAll` lifecycle methods to trigger Flyway migrations.
46+
- Example:
47+
```java
48+
@SpringBootTest
49+
public class ExampleIntegrationTest {
50+
51+
@Autowired
52+
private Flyway flyway;
53+
54+
@BeforeEach
55+
public void setupDatabase() {
56+
flyway.clean();
57+
flyway.migrate();
58+
}
59+
60+
@Test
61+
public void testExample() {
62+
// Test logic here
63+
}
64+
}
65+
```

0 commit comments

Comments
 (0)