Skip to content

Commit d612d74

Browse files
authored
feat(liquibase): integrate Liquibase for database migrations and upda… (#46)
* feat(liquibase): integrate Liquibase for database migrations and update configuration * feat(liquibase): update Liquibase output changelog path and add version to plugin * feat(env): update environment variable placeholders for Liquibase configuration
1 parent 928bd24 commit d612d74

8 files changed

Lines changed: 105 additions & 11 deletions

File tree

.env.template

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
POSTGRES_USER=your_postgres_user
22
POSTGRES_PASSWORD=your_postgres_password
33
POSTGRES_DB=your_postgres_db
4-
POSTGRES_PORT=5433
4+
POSTGRES_PORT=5432
5+
6+
# Liquibase configuration
7+
LB_CHANGELOG=your_changelog_file.yaml
8+
LB_OUTPUT_CHANGELOG=your_output_changelog.yaml
9+
LB_DRIVER=your_jdbc_driver_class
10+
LB_SCHEMA=your_database_schema

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,23 @@ src/main/java/com/xpeho/spring_boot_java_random_user/
207207
| `picture` | VARCHAR(500) | Avatar/picture URL |
208208
| `nat` | VARCHAR(10) | Nationality code |
209209

210-
**Schema auto-created on startup via `schema.sql`**
210+
### Liquibase Migrations
211+
212+
Database schema is managed with **Liquibase**. Migrations are applied automatically on application startup.
213+
214+
```
215+
src/main/resources/db/changelog/
216+
├── db.changelog-master.yaml ← Index file
217+
└── changes/
218+
└── 001-create-users-table.yaml ← Table creation
219+
```
220+
221+
**CLI Commands:**
222+
```bash
223+
./mvnw liquibase:status # View pending changes
224+
./mvnw liquibase:update # Apply migrations
225+
./mvnw liquibase:rollback -Dliquibase.rollbackCount=1 # Rollback last change
226+
```
211227

212228
---
213229

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ services:
1212
- "${POSTGRES_PORT}:5432"
1313
volumes:
1414
- postgres_data:/var/lib/postgresql/data
15-
- ./src/main/resources/schema.sql:/docker-entrypoint-initdb.d/01-schema.sql
1615
healthcheck:
1716
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
1817
interval: 10s

mvnw

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@
122122
<artifactId>spring-boot-starter-test</artifactId>
123123
<scope>test</scope>
124124
</dependency>
125-
125+
<dependency>
126+
<groupId>org.liquibase</groupId>
127+
<artifactId>liquibase-core</artifactId>
128+
</dependency>
126129
</dependencies>
127130

128131
<build>
@@ -131,6 +134,20 @@
131134
<groupId>org.springframework.boot</groupId>
132135
<artifactId>spring-boot-maven-plugin</artifactId>
133136
</plugin>
137+
<plugin>
138+
<groupId>org.liquibase</groupId>
139+
<artifactId>liquibase-maven-plugin</artifactId>
140+
<version>5.0.0</version>
141+
<configuration>
142+
<changeLogFile>${env.LB_CHANGELOG}</changeLogFile>
143+
<outputChangeLogFile>${env.LB_OUTPUT_CHANGELOG}</outputChangeLogFile>
144+
<url>jdbc:postgresql://localhost:${env.POSTGRES_PORT}/${env.POSTGRES_DB}</url>
145+
<username>${env.POSTGRES_USER}</username>
146+
<password>${env.POSTGRES_PASSWORD}</password>
147+
<driver>${env.LB_DRIVER}</driver>
148+
<defaultSchemaName>${env.LB_SCHEMA}</defaultSchemaName>
149+
</configuration>
150+
</plugin>
134151
<plugin>
135152
<groupId>org.apache.maven.plugins</groupId>
136153
<artifactId>maven-surefire-plugin</artifactId>

src/main/resources/application.properties

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ spring.datasource.username=${POSTGRES_USER}
1212
spring.datasource.password=${POSTGRES_PASSWORD}
1313
spring.datasource.driver-class-name=org.postgresql.Driver
1414

15-
16-
# Spring Data JDBC - Initialize database
17-
spring.sql.init.mode=always
18-
spring.sql.init.schema-locations=classpath:schema.sql
19-
spring.sql.init.data-locations=
20-
spring.sql.init.platform=postgresql
21-
spring.sql.init.continue-on-error=true
15+
# Liquibase configuration
16+
spring.liquibase.enabled=${SPRING_LIQUIBASE_ENABLED:true}
17+
spring.liquibase.change-log=classpath:${LB_CHANGELOG}
18+
spring.liquibase.default-schema=${LB_SCHEMA}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
databaseChangeLog:
2+
- changeSet:
3+
id: 001-create-users-table
4+
author: xpeho
5+
changes:
6+
- createTable:
7+
tableName: users
8+
columns:
9+
- column:
10+
name: id
11+
type: SERIAL
12+
autoIncrement: true
13+
constraints:
14+
primaryKey: true
15+
nullable: false
16+
- column:
17+
name: gender
18+
type: VARCHAR(20)
19+
- column:
20+
name: firstname
21+
type: VARCHAR(100)
22+
- column:
23+
name: lastname
24+
type: VARCHAR(100)
25+
- column:
26+
name: civility
27+
type: VARCHAR(20)
28+
- column:
29+
name: email
30+
type: VARCHAR(255)
31+
- column:
32+
name: phone
33+
type: VARCHAR(50)
34+
- column:
35+
name: picture
36+
type: VARCHAR(500)
37+
- column:
38+
name: nationality
39+
type: VARCHAR(10)
40+
rollback:
41+
- dropTable:
42+
tableName: users
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
databaseChangeLog:
2+
- include:
3+
file: db/changelog/changes/001-create-users-table.yaml

0 commit comments

Comments
 (0)