-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Liquibase]: Spring Liquibase application example (#24)
- Loading branch information
1 parent
88d6f6f
commit dc2126e
Showing
9 changed files
with
339 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<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-liquibase-app</artifactId> | ||
<name>Spring Liquibase Example</name> | ||
<description>Basic example for SpringBoot3 and Liquibase</description> | ||
<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> | ||
<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> | ||
</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>liquibase-ydb-dialect</artifactId> | ||
<version>${liquibase.ydb.dialect.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.liquibase</groupId> | ||
<artifactId>liquibase-core</artifactId> | ||
<version>${liquibase.core.version}</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> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</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> |
9 changes: 9 additions & 0 deletions
9
jdbc/spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/LiquibaseApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 LiquibaseApplication |
36 changes: 36 additions & 0 deletions
36
jdbc/spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/model/Employee.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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, | ||
) |
8 changes: 8 additions & 0 deletions
8
.../spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/repository/EmployeeRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
8 changes: 8 additions & 0 deletions
8
jdbc/spring-liquibase-app/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
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 | ||
|
||
spring.liquibase.change-log=classpath:changelog.yaml | ||
|
||
logging.level.liquibase=DEBUG |
45 changes: 45 additions & 0 deletions
45
jdbc/spring-liquibase-app/src/main/resources/changelog.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
databaseChangeLog: | ||
- changeSet: | ||
id: "20230904-1" | ||
author: "yourName" | ||
changes: | ||
- createTable: | ||
tableName: employee | ||
columns: | ||
- column: | ||
name: id | ||
type: bigint | ||
constraints: | ||
primaryKey: true | ||
nullable: false | ||
- column: | ||
name: full_name | ||
type: varchar | ||
- column: | ||
name: email | ||
type: varchar | ||
- column: | ||
name: hire_date | ||
type: date | ||
- column: | ||
name: salary | ||
type: decimal(22,9) | ||
- column: | ||
name: is_active | ||
type: boolean | ||
- column: | ||
name: department | ||
type: varchar | ||
- column: | ||
name: age | ||
type: int | ||
- column: | ||
name: limit_date_password | ||
type: datetime | ||
- createIndex: | ||
indexName: idx_employee_email | ||
tableName: employee | ||
unique: false | ||
columns: | ||
- column: | ||
name: email |
67 changes: 67 additions & 0 deletions
67
jdbc/spring-liquibase-app/src/test/kotlin/tech/ydb/liquibase/YdbLiquibaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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.ZoneId | ||
import java.util.* | ||
|
||
/** | ||
* @author Kirill Kurdyukov | ||
*/ | ||
@SpringBootTest | ||
class YdbLiquibaseTest { | ||
|
||
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 liquibase 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 | ||
) | ||
|
||
employeeRepository.save(employee) | ||
|
||
assertEquals(employee, employeeRepository.findByIdOrNull(employee.id)) | ||
|
||
employeeRepository.delete(employee) | ||
|
||
assertNull(employeeRepository.findByIdOrNull(employee.id)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |