File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11POSTGRES_USER = your_postgres_user
22POSTGRES_PASSWORD = your_postgres_password
33POSTGRES_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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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 >
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 >
Original file line number Diff line number Diff line change @@ -12,10 +12,7 @@ spring.datasource.username=${POSTGRES_USER}
1212spring.datasource.password =${POSTGRES_PASSWORD}
1313spring.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}
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+ databaseChangeLog :
2+ - include :
3+ file : db/changelog/changes/001-create-users-table.yaml
You can’t perform that action at this time.
0 commit comments