Skip to content

Commit ed0079c

Browse files
[Flyway]: Spring flyway app (#31)
1 parent 92bf516 commit ed0079c

File tree

14 files changed

+308
-14
lines changed

14 files changed

+308
-14
lines changed

jdbc/pom.xml

Lines changed: 1 addition & 0 deletions
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-flyway-app</module>
5152
<module>spring-liquibase-app</module>
5253
</modules>
5354
</profile>

jdbc/spring-data-jpa/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@
7777
<groupId>tech.ydb.test</groupId>
7878
<artifactId>ydb-junit5-support</artifactId>
7979
<scope>test</scope>
80-
<exclusions>
81-
<exclusion>
82-
<groupId>org.junit.jupiter</groupId>
83-
<artifactId>junit-jupiter-api</artifactId>
84-
</exclusion>
85-
</exclusions>
8680
</dependency>
8781
</dependencies>
8882
<build>

jdbc/spring-flyway-app/pom.xml

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>tech.ydb.jdbc.examples</groupId>
6+
<artifactId>ydb-jdbc-examples</artifactId>
7+
<version>1.1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>spring-flyway-app</artifactId>
11+
<name>Spring Flyway Example</name>
12+
<description>Basic example for SpringBoot3 and Flyway</description>
13+
<properties>
14+
<maven.compiler.release>17</maven.compiler.release>
15+
<kotlin.version>1.9.22</kotlin.version>
16+
<hibernate.ydb.dialect.version>0.9.2</hibernate.ydb.dialect.version>
17+
<spring.boot.version>3.2.1</spring.boot.version>
18+
<flyway.ydb.dialect.version>1.0.0-RC0</flyway.ydb.dialect.version>
19+
<flyway.core.version>10.7.1</flyway.core.version>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-data-jpa</artifactId>
25+
<version>${spring.boot.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.data</groupId>
29+
<artifactId>spring-data-commons</artifactId>
30+
<version>${spring.boot.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.jetbrains.kotlin</groupId>
34+
<artifactId>kotlin-reflect</artifactId>
35+
<version>${kotlin.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.jetbrains.kotlin</groupId>
39+
<artifactId>kotlin-stdlib</artifactId>
40+
<version>${kotlin.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>tech.ydb.dialects</groupId>
44+
<artifactId>hibernate-ydb-dialect</artifactId>
45+
<version>${hibernate.ydb.dialect.version}</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>tech.ydb.jdbc</groupId>
49+
<artifactId>ydb-jdbc-driver-shaded</artifactId>
50+
</dependency>
51+
<dependency>
52+
<groupId>tech.ydb.dialects</groupId>
53+
<artifactId>flyway-ydb-dialect</artifactId>
54+
<version>${flyway.ydb.dialect.version}</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.flywaydb</groupId>
58+
<artifactId>flyway-core</artifactId>
59+
<version>10.7.1</version>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-test</artifactId>
65+
<version>${spring.boot.version}</version>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>tech.ydb.test</groupId>
70+
<artifactId>ydb-junit5-support</artifactId>
71+
<scope>test</scope>
72+
</dependency>
73+
</dependencies>
74+
<build>
75+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
76+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-surefire-plugin</artifactId>
81+
<configuration>
82+
<environmentVariables>
83+
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
84+
</environmentVariables>
85+
</configuration>
86+
</plugin>
87+
<plugin>
88+
<groupId>org.springframework.boot</groupId>
89+
<artifactId>spring-boot-maven-plugin</artifactId>
90+
<version>${spring.boot.version}</version>
91+
</plugin>
92+
<plugin>
93+
<groupId>org.jetbrains.kotlin</groupId>
94+
<artifactId>kotlin-maven-plugin</artifactId>
95+
<version>${kotlin.version}</version>
96+
<executions>
97+
<execution>
98+
<id>compile</id>
99+
<phase>compile</phase>
100+
<goals>
101+
<goal>compile</goal>
102+
</goals>
103+
</execution>
104+
<execution>
105+
<id>test-compile</id>
106+
<goals>
107+
<goal>test-compile</goal>
108+
</goals>
109+
</execution>
110+
</executions>
111+
<configuration>
112+
<args>
113+
<arg>-Xjsr305=strict</arg>
114+
</args>
115+
<compilerPlugins>
116+
<plugin>spring</plugin>
117+
<plugin>jpa</plugin>
118+
</compilerPlugins>
119+
</configuration>
120+
<dependencies>
121+
<dependency>
122+
<groupId>org.jetbrains.kotlin</groupId>
123+
<artifactId>kotlin-maven-allopen</artifactId>
124+
<version>${kotlin.version}</version>
125+
</dependency>
126+
<dependency>
127+
<groupId>org.jetbrains.kotlin</groupId>
128+
<artifactId>kotlin-maven-noarg</artifactId>
129+
<version>${kotlin.version}</version>
130+
</dependency>
131+
</dependencies>
132+
</plugin>
133+
</plugins>
134+
</build>
135+
</project>
Lines changed: 9 additions & 0 deletions
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 FlywayApplication
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
37+
@Column(name = "limit_date_password")
38+
val limitDatePassword: LocalDateTime,
39+
)
Lines changed: 8 additions & 0 deletions
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)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE employee ADD INDEX email_employee_index GLOBAL ON (email)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CREATE TABLE employee
2+
(
3+
id Int64 NOT NULL,
4+
full_name Text,
5+
email Text,
6+
hire_date Date,
7+
salary Decimal(22, 9),
8+
is_active Bool,
9+
department Text,
10+
age Int32,
11+
limit_date_password Datetime,
12+
13+
PRIMARY KEY (id)
14+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.LocalDateTime
18+
import java.time.Month
19+
import java.time.ZoneId
20+
import java.util.*
21+
22+
/**
23+
* @author Kirill Kurdyukov
24+
*/
25+
@SpringBootTest
26+
class YdbFlywayTest {
27+
28+
companion object {
29+
@JvmField
30+
@RegisterExtension
31+
val ydb = YdbHelperExtension()
32+
33+
@JvmStatic
34+
@DynamicPropertySource
35+
fun propertySource(registry: DynamicPropertyRegistry) {
36+
registry.add("spring.datasource.url") {
37+
"jdbc:ydb:${if (ydb.useTls()) "grpcs://" else "grpc://"}" +
38+
"${ydb.endpoint()}${ydb.database()}${ydb.authToken()?.let { "?token=$it" } ?: ""}"
39+
}
40+
}
41+
}
42+
43+
@Autowired
44+
lateinit var employeeRepository: EmployeeRepository
45+
46+
@Test
47+
fun `migration flyway and CRUD actions`() {
48+
TimeZone.setDefault(TimeZone.getTimeZone(ZoneId.of("UTC")))
49+
50+
val employee = Employee(
51+
1,
52+
"Kirill Kurdyukov",
53+
54+
LocalDate.parse("2023-12-20"),
55+
BigDecimal("500000.000000000"),
56+
true,
57+
"YDB AppTeam",
58+
23,
59+
LocalDateTime.of(2021, Month.JUNE, 2, 12, 12),
60+
)
61+
62+
employeeRepository.save(employee)
63+
64+
assertEquals(employee, employeeRepository.findByIdOrNull(employee.id))
65+
66+
employeeRepository.delete(employee)
67+
68+
assertNull(employeeRepository.findByIdOrNull(employee.id))
69+
}
70+
}

0 commit comments

Comments
 (0)