Skip to content

Commit

Permalink
[Flyway]: Spring flyway app (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillKurdyukov authored Apr 12, 2024
1 parent 92bf516 commit ed0079c
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 14 deletions.
1 change: 1 addition & 0 deletions jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
</activation>
<modules>
<module>spring-data-jpa</module>
<module>spring-flyway-app</module>
<module>spring-liquibase-app</module>
</modules>
</profile>
Expand Down
6 changes: 0 additions & 6 deletions jdbc/spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@
<groupId>tech.ydb.test</groupId>
<artifactId>ydb-junit5-support</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
Expand Down
135 changes: 135 additions & 0 deletions jdbc/spring-flyway-app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>tech.ydb.jdbc.examples</groupId>
<artifactId>ydb-jdbc-examples</artifactId>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>spring-flyway-app</artifactId>
<name>Spring Flyway Example</name>
<description>Basic example for SpringBoot3 and Flyway</description>
<properties>
<maven.compiler.release>17</maven.compiler.release>
<kotlin.version>1.9.22</kotlin.version>
<hibernate.ydb.dialect.version>0.9.2</hibernate.ydb.dialect.version>
<spring.boot.version>3.2.1</spring.boot.version>
<flyway.ydb.dialect.version>1.0.0-RC0</flyway.ydb.dialect.version>
<flyway.core.version>10.7.1</flyway.core.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>tech.ydb.dialects</groupId>
<artifactId>hibernate-ydb-dialect</artifactId>
<version>${hibernate.ydb.dialect.version}</version>
</dependency>
<dependency>
<groupId>tech.ydb.jdbc</groupId>
<artifactId>ydb-jdbc-driver-shaded</artifactId>
</dependency>
<dependency>
<groupId>tech.ydb.dialects</groupId>
<artifactId>flyway-ydb-dialect</artifactId>
<version>${flyway.ydb.dialect.version}</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>10.7.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>tech.ydb.test</groupId>
<artifactId>ydb-junit5-support</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
</environmentVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tech.ydb.liquibase

import org.springframework.boot.autoconfigure.SpringBootApplication

/**
* @author Kirill Kurdyukov
*/
@SpringBootApplication
class FlywayApplication
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tech.ydb.liquibase.model

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Table
import java.time.LocalDate
import java.time.LocalDateTime

@Entity
@Table(name = "employee")
data class Employee(
@Id
val id: Long,

@Column(name = "full_name")
val fullName: String,

@Column
val email: String,

@Column(name = "hire_date")
val hireDate: LocalDate,

@Column
val salary: java.math.BigDecimal,

@Column(name = "is_active")
val isActive: Boolean,

@Column
val department: String,

@Column
val age: Int,

@Column(name = "limit_date_password")
val limitDatePassword: LocalDateTime,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tech.ydb.liquibase.repository

import org.springframework.data.repository.CrudRepository
import tech.ydb.liquibase.model.Employee

interface EmployeeRepository : CrudRepository<Employee, Long>

fun EmployeeRepository.findByIdOrNull(id: Long): Employee? = this.findById(id).orElse(null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring.jpa.properties.hibernate.dialect=tech.ydb.hibernate.dialect.YdbDialect

spring.datasource.driver-class-name=tech.ydb.jdbc.YdbDriver
spring.datasource.url=jdbc:ydb:grpc://localhost:2136/local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE employee ADD INDEX email_employee_index GLOBAL ON (email)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE employee
(
id Int64 NOT NULL,
full_name Text,
email Text,
hire_date Date,
salary Decimal(22, 9),
is_active Bool,
department Text,
age Int32,
limit_date_password Datetime,

PRIMARY KEY (id)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package tech.ydb.liquibase

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import tech.ydb.liquibase.model.Employee
import tech.ydb.liquibase.repository.EmployeeRepository
import tech.ydb.liquibase.repository.findByIdOrNull
import tech.ydb.test.junit5.YdbHelperExtension
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.Month
import java.time.ZoneId
import java.util.*

/**
* @author Kirill Kurdyukov
*/
@SpringBootTest
class YdbFlywayTest {

companion object {
@JvmField
@RegisterExtension
val ydb = YdbHelperExtension()

@JvmStatic
@DynamicPropertySource
fun propertySource(registry: DynamicPropertyRegistry) {
registry.add("spring.datasource.url") {
"jdbc:ydb:${if (ydb.useTls()) "grpcs://" else "grpc://"}" +
"${ydb.endpoint()}${ydb.database()}${ydb.authToken()?.let { "?token=$it" } ?: ""}"
}
}
}

@Autowired
lateinit var employeeRepository: EmployeeRepository

@Test
fun `migration flyway and CRUD actions`() {
TimeZone.setDefault(TimeZone.getTimeZone(ZoneId.of("UTC")))

val employee = Employee(
1,
"Kirill Kurdyukov",
"[email protected]",
LocalDate.parse("2023-12-20"),
BigDecimal("500000.000000000"),
true,
"YDB AppTeam",
23,
LocalDateTime.of(2021, Month.JUNE, 2, 12, 12),
)

employeeRepository.save(employee)

assertEquals(employee, employeeRepository.findByIdOrNull(employee.id))

employeeRepository.delete(employee)

assertNull(employeeRepository.findByIdOrNull(employee.id))
}
}
19 changes: 19 additions & 0 deletions jdbc/spring-flyway-app/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
</encoder>
</appender>

<!-- https://www.testcontainers.org/supported_docker_environment/logging_config/ -->
<logger name="org.testcontainers" level="warn" />
<logger name="com.github.dockerjava" level="warn"/>
<logger name="com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire" level="off"/>

<logger name="org.hibernate.SQL" additivity="true" level="debug" />

<root level="info">
<appender-ref ref="console" />
</root>
</configuration>
8 changes: 1 addition & 7 deletions jdbc/spring-liquibase-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<properties>
<maven.compiler.release>17</maven.compiler.release>
<kotlin.version>1.9.22</kotlin.version>
<hibernate.ydb.dialect.version>0.9.1</hibernate.ydb.dialect.version>
<hibernate.ydb.dialect.version>0.9.2</hibernate.ydb.dialect.version>
<spring.boot.version>3.2.1</spring.boot.version>
<liquibase.ydb.dialect.version>0.9.1</liquibase.ydb.dialect.version>
<liquibase.core.version>4.24.0</liquibase.core.version>
Expand Down Expand Up @@ -68,12 +68,6 @@
<groupId>tech.ydb.test</groupId>
<artifactId>ydb-junit5-support</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ data class Employee(

@Column
val age: Int,

@Column(name = "limit_date_password")
val limitDatePassword: LocalDateTime,
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import tech.ydb.liquibase.repository.findByIdOrNull
import tech.ydb.test.junit5.YdbHelperExtension
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.Month
import java.time.ZoneId
import java.util.*

Expand Down Expand Up @@ -53,7 +55,8 @@ class YdbLiquibaseTest {
BigDecimal("500000.000000000"),
true,
"YDB AppTeam",
23
23,
LocalDateTime.of(2021, Month.JUNE, 2, 12, 12),
)

employeeRepository.save(employee)
Expand Down

0 comments on commit ed0079c

Please sign in to comment.