Skip to content

Commit dc2126e

Browse files
[Liquibase]: Spring Liquibase application example (#24)
1 parent 88d6f6f commit dc2126e

File tree

9 files changed

+339
-0
lines changed

9 files changed

+339
-0
lines changed

jdbc/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
</activation>
4949
<modules>
5050
<module>spring-data-jpa</module>
51+
<module>spring-liquibase-app</module>
5152
</modules>
5253
</profile>
5354
</profiles>

jdbc/spring-liquibase-app/pom.xml

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>tech.ydb.jdbc.examples</groupId>
5+
<artifactId>ydb-jdbc-examples</artifactId>
6+
<version>1.1.0-SNAPSHOT</version>
7+
</parent>
8+
9+
<artifactId>spring-liquibase-app</artifactId>
10+
<name>Spring Liquibase Example</name>
11+
<description>Basic example for SpringBoot3 and Liquibase</description>
12+
<properties>
13+
<maven.compiler.release>17</maven.compiler.release>
14+
<kotlin.version>1.9.22</kotlin.version>
15+
<hibernate.ydb.dialect.version>0.9.1</hibernate.ydb.dialect.version>
16+
<spring.boot.version>3.2.1</spring.boot.version>
17+
<liquibase.ydb.dialect.version>0.9.1</liquibase.ydb.dialect.version>
18+
<liquibase.core.version>4.24.0</liquibase.core.version>
19+
</properties>
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-data-jpa</artifactId>
24+
<version>${spring.boot.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.data</groupId>
28+
<artifactId>spring-data-commons</artifactId>
29+
<version>${spring.boot.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.jetbrains.kotlin</groupId>
33+
<artifactId>kotlin-reflect</artifactId>
34+
<version>${kotlin.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.jetbrains.kotlin</groupId>
38+
<artifactId>kotlin-stdlib</artifactId>
39+
<version>${kotlin.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>tech.ydb.dialects</groupId>
43+
<artifactId>hibernate-ydb-dialect</artifactId>
44+
<version>${hibernate.ydb.dialect.version}</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>tech.ydb.jdbc</groupId>
48+
<artifactId>ydb-jdbc-driver-shaded</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>tech.ydb.dialects</groupId>
52+
<artifactId>liquibase-ydb-dialect</artifactId>
53+
<version>${liquibase.ydb.dialect.version}</version>
54+
<exclusions>
55+
<exclusion>
56+
<groupId>org.slf4j</groupId>
57+
<artifactId>slf4j-simple</artifactId>
58+
</exclusion>
59+
</exclusions>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.liquibase</groupId>
63+
<artifactId>liquibase-core</artifactId>
64+
<version>${liquibase.core.version}</version>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-test</artifactId>
70+
<version>${spring.boot.version}</version>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>tech.ydb.test</groupId>
75+
<artifactId>ydb-junit5-support</artifactId>
76+
<scope>test</scope>
77+
<exclusions>
78+
<exclusion>
79+
<groupId>org.junit.jupiter</groupId>
80+
<artifactId>junit-jupiter-api</artifactId>
81+
</exclusion>
82+
</exclusions>
83+
</dependency>
84+
</dependencies>
85+
<build>
86+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
87+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
88+
<plugins>
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-surefire-plugin</artifactId>
92+
<configuration>
93+
<environmentVariables>
94+
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
95+
</environmentVariables>
96+
</configuration>
97+
</plugin>
98+
<plugin>
99+
<groupId>org.springframework.boot</groupId>
100+
<artifactId>spring-boot-maven-plugin</artifactId>
101+
<version>${spring.boot.version}</version>
102+
</plugin>
103+
<plugin>
104+
<groupId>org.jetbrains.kotlin</groupId>
105+
<artifactId>kotlin-maven-plugin</artifactId>
106+
<version>${kotlin.version}</version>
107+
<executions>
108+
<execution>
109+
<id>compile</id>
110+
<phase>compile</phase>
111+
<goals>
112+
<goal>compile</goal>
113+
</goals>
114+
</execution>
115+
<execution>
116+
<id>test-compile</id>
117+
<goals>
118+
<goal>test-compile</goal>
119+
</goals>
120+
</execution>
121+
</executions>
122+
<configuration>
123+
<args>
124+
<arg>-Xjsr305=strict</arg>
125+
</args>
126+
<compilerPlugins>
127+
<plugin>spring</plugin>
128+
<plugin>jpa</plugin>
129+
</compilerPlugins>
130+
</configuration>
131+
<dependencies>
132+
<dependency>
133+
<groupId>org.jetbrains.kotlin</groupId>
134+
<artifactId>kotlin-maven-allopen</artifactId>
135+
<version>${kotlin.version}</version>
136+
</dependency>
137+
<dependency>
138+
<groupId>org.jetbrains.kotlin</groupId>
139+
<artifactId>kotlin-maven-noarg</artifactId>
140+
<version>${kotlin.version}</version>
141+
</dependency>
142+
</dependencies>
143+
</plugin>
144+
</plugins>
145+
</build>
146+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tech.ydb.liquibase
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
5+
/**
6+
* @author Kirill Kurdyukov
7+
*/
8+
@SpringBootApplication
9+
class LiquibaseApplication
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package tech.ydb.liquibase.model
2+
3+
import jakarta.persistence.Column
4+
import jakarta.persistence.Entity
5+
import jakarta.persistence.Id
6+
import jakarta.persistence.Table
7+
import java.time.LocalDate
8+
import java.time.LocalDateTime
9+
10+
@Entity
11+
@Table(name = "employee")
12+
data class Employee(
13+
@Id
14+
val id: Long,
15+
16+
@Column(name = "full_name")
17+
val fullName: String,
18+
19+
@Column
20+
val email: String,
21+
22+
@Column(name = "hire_date")
23+
val hireDate: LocalDate,
24+
25+
@Column
26+
val salary: java.math.BigDecimal,
27+
28+
@Column(name = "is_active")
29+
val isActive: Boolean,
30+
31+
@Column
32+
val department: String,
33+
34+
@Column
35+
val age: Int,
36+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package tech.ydb.liquibase.repository
2+
3+
import org.springframework.data.repository.CrudRepository
4+
import tech.ydb.liquibase.model.Employee
5+
6+
interface EmployeeRepository : CrudRepository<Employee, Long>
7+
8+
fun EmployeeRepository.findByIdOrNull(id: Long): Employee? = this.findById(id).orElse(null)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spring.jpa.properties.hibernate.dialect=tech.ydb.hibernate.dialect.YdbDialect
2+
3+
spring.datasource.driver-class-name=tech.ydb.jdbc.YdbDriver
4+
spring.datasource.url=jdbc:ydb:grpc://localhost:2136/local
5+
6+
spring.liquibase.change-log=classpath:changelog.yaml
7+
8+
logging.level.liquibase=DEBUG
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
databaseChangeLog:
2+
- changeSet:
3+
id: "20230904-1"
4+
author: "yourName"
5+
changes:
6+
- createTable:
7+
tableName: employee
8+
columns:
9+
- column:
10+
name: id
11+
type: bigint
12+
constraints:
13+
primaryKey: true
14+
nullable: false
15+
- column:
16+
name: full_name
17+
type: varchar
18+
- column:
19+
name: email
20+
type: varchar
21+
- column:
22+
name: hire_date
23+
type: date
24+
- column:
25+
name: salary
26+
type: decimal(22,9)
27+
- column:
28+
name: is_active
29+
type: boolean
30+
- column:
31+
name: department
32+
type: varchar
33+
- column:
34+
name: age
35+
type: int
36+
- column:
37+
name: limit_date_password
38+
type: datetime
39+
- createIndex:
40+
indexName: idx_employee_email
41+
tableName: employee
42+
unique: false
43+
columns:
44+
- column:
45+
name: email
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package tech.ydb.liquibase
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Assertions.assertNull
5+
import org.junit.jupiter.api.Test
6+
import org.junit.jupiter.api.extension.RegisterExtension
7+
import org.springframework.beans.factory.annotation.Autowired
8+
import org.springframework.boot.test.context.SpringBootTest
9+
import org.springframework.test.context.DynamicPropertyRegistry
10+
import org.springframework.test.context.DynamicPropertySource
11+
import tech.ydb.liquibase.model.Employee
12+
import tech.ydb.liquibase.repository.EmployeeRepository
13+
import tech.ydb.liquibase.repository.findByIdOrNull
14+
import tech.ydb.test.junit5.YdbHelperExtension
15+
import java.math.BigDecimal
16+
import java.time.LocalDate
17+
import java.time.ZoneId
18+
import java.util.*
19+
20+
/**
21+
* @author Kirill Kurdyukov
22+
*/
23+
@SpringBootTest
24+
class YdbLiquibaseTest {
25+
26+
companion object {
27+
@JvmField
28+
@RegisterExtension
29+
val ydb = YdbHelperExtension()
30+
31+
@JvmStatic
32+
@DynamicPropertySource
33+
fun propertySource(registry: DynamicPropertyRegistry) {
34+
registry.add("spring.datasource.url") {
35+
"jdbc:ydb:${if (ydb.useTls()) "grpcs://" else "grpc://"}" +
36+
"${ydb.endpoint()}${ydb.database()}${ydb.authToken()?.let { "?token=$it" } ?: ""}"
37+
}
38+
}
39+
}
40+
41+
@Autowired
42+
lateinit var employeeRepository: EmployeeRepository
43+
44+
@Test
45+
fun `migration liquibase and CRUD actions`() {
46+
TimeZone.setDefault(TimeZone.getTimeZone(ZoneId.of("UTC")))
47+
48+
val employee = Employee(
49+
1,
50+
"Kirill Kurdyukov",
51+
52+
LocalDate.parse("2023-12-20"),
53+
BigDecimal("500000.000000000"),
54+
true,
55+
"YDB AppTeam",
56+
23
57+
)
58+
59+
employeeRepository.save(employee)
60+
61+
assertEquals(employee, employeeRepository.findByIdOrNull(employee.id))
62+
63+
employeeRepository.delete(employee)
64+
65+
assertNull(employeeRepository.findByIdOrNull(employee.id))
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<!-- https://www.testcontainers.org/supported_docker_environment/logging_config/ -->
10+
<logger name="org.testcontainers" level="warn" />
11+
<logger name="com.github.dockerjava" level="warn"/>
12+
<logger name="com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire" level="off"/>
13+
14+
<logger name="org.hibernate.SQL" additivity="true" level="debug" />
15+
16+
<root level="info">
17+
<appender-ref ref="console" />
18+
</root>
19+
</configuration>

0 commit comments

Comments
 (0)