diff --git a/jdbc/pom.xml b/jdbc/pom.xml index 9984f39..b3ef6b1 100644 --- a/jdbc/pom.xml +++ b/jdbc/pom.xml @@ -48,6 +48,7 @@ spring-data-jpa + spring-liquibase-app diff --git a/jdbc/spring-data-jpa-v5/pom.xml b/jdbc/spring-data-jpa-v5/pom.xml index 9217fa4..d72b3dc 100644 --- a/jdbc/spring-data-jpa-v5/pom.xml +++ b/jdbc/spring-data-jpa-v5/pom.xml @@ -11,7 +11,7 @@ Basic example for SpringBoot3 and Hibernate 6 1.9.22 - 0.9.1 + 0.9.2 2.5.7 diff --git a/jdbc/spring-data-jpa/pom.xml b/jdbc/spring-data-jpa/pom.xml index 30294b5..d883b09 100644 --- a/jdbc/spring-data-jpa/pom.xml +++ b/jdbc/spring-data-jpa/pom.xml @@ -12,7 +12,7 @@ 17 1.9.22 - 0.9.1 + 0.9.2 3.2.1 diff --git a/jdbc/spring-liquibase-app/pom.xml b/jdbc/spring-liquibase-app/pom.xml new file mode 100644 index 0000000..af75ac1 --- /dev/null +++ b/jdbc/spring-liquibase-app/pom.xml @@ -0,0 +1,140 @@ + + 4.0.0 + + tech.ydb.jdbc.examples + ydb-jdbc-examples + 1.1.0-SNAPSHOT + + + spring-liquibase-app + Spring Liquibase Example + Basic example for SpringBoot3 and Liquibase + + 17 + 1.9.22 + 0.9.1 + 3.2.1 + 0.9.1 + 4.24.0 + + + + org.springframework.boot + spring-boot-starter-data-jpa + ${spring.boot.version} + + + org.springframework.data + spring-data-commons + ${spring.boot.version} + + + org.jetbrains.kotlin + kotlin-reflect + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + tech.ydb.dialects + hibernate-ydb-dialect + ${hibernate.ydb.dialect.version} + + + tech.ydb.jdbc + ydb-jdbc-driver-shaded + + + tech.ydb.dialects + liquibase-ydb-dialect + 0.9.7 + + + org.liquibase + liquibase-core + ${liquibase.core.version} + + + + org.springframework.boot + spring-boot-starter-test + ${spring.boot.version} + test + + + tech.ydb.test + ydb-junit5-support + test + + + org.junit.jupiter + junit-jupiter-api + + + + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/test/kotlin + + + org.apache.maven.plugins + maven-surefire-plugin + + + true + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring.boot.version} + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + + test-compile + + + + + + -Xjsr305=strict + + + spring + jpa + + + + + org.jetbrains.kotlin + kotlin-maven-allopen + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-maven-noarg + ${kotlin.version} + + + + + + \ No newline at end of file diff --git a/jdbc/spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/LiquibaseApplication.kt b/jdbc/spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/LiquibaseApplication.kt new file mode 100644 index 0000000..3f6db9a --- /dev/null +++ b/jdbc/spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/LiquibaseApplication.kt @@ -0,0 +1,9 @@ +package tech.ydb.liquibase + +import org.springframework.boot.autoconfigure.SpringBootApplication + +/** + * @author Kirill Kurdyukov + */ +@SpringBootApplication +class LiquibaseApplication \ No newline at end of file diff --git a/jdbc/spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/model/Employee.kt b/jdbc/spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/model/Employee.kt new file mode 100644 index 0000000..1cba0d9 --- /dev/null +++ b/jdbc/spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/model/Employee.kt @@ -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, +) \ No newline at end of file diff --git a/jdbc/spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/repository/EmployeeRepository.kt b/jdbc/spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/repository/EmployeeRepository.kt new file mode 100644 index 0000000..2a8b173 --- /dev/null +++ b/jdbc/spring-liquibase-app/src/main/kotlin/tech/ydb/liquibase/repository/EmployeeRepository.kt @@ -0,0 +1,8 @@ +package tech.ydb.liquibase.repository + +import org.springframework.data.repository.CrudRepository +import tech.ydb.liquibase.model.Employee + +interface EmployeeRepository : CrudRepository + +fun EmployeeRepository.findByIdOrNull(id: Long): Employee? = this.findById(id).orElse(null) diff --git a/jdbc/spring-liquibase-app/src/main/resources/application.properties b/jdbc/spring-liquibase-app/src/main/resources/application.properties new file mode 100644 index 0000000..2c5403c --- /dev/null +++ b/jdbc/spring-liquibase-app/src/main/resources/application.properties @@ -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 diff --git a/jdbc/spring-liquibase-app/src/main/resources/changelog.yaml b/jdbc/spring-liquibase-app/src/main/resources/changelog.yaml new file mode 100644 index 0000000..7d04696 --- /dev/null +++ b/jdbc/spring-liquibase-app/src/main/resources/changelog.yaml @@ -0,0 +1,45 @@ +databaseChangeLog: + - changeSet: + id: "employee" + author: "kurdyukov-kir" + 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 diff --git a/jdbc/spring-liquibase-app/src/test/kotlin/tech/ydb/liquibase/YdbLiquibaseTest.kt b/jdbc/spring-liquibase-app/src/test/kotlin/tech/ydb/liquibase/YdbLiquibaseTest.kt new file mode 100644 index 0000000..46e4308 --- /dev/null +++ b/jdbc/spring-liquibase-app/src/test/kotlin/tech/ydb/liquibase/YdbLiquibaseTest.kt @@ -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", + "kurdyukov-kir@ydb.tech", + 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)) + } +} \ No newline at end of file diff --git a/jdbc/spring-liquibase-app/src/test/resources/logback.xml b/jdbc/spring-liquibase-app/src/test/resources/logback.xml new file mode 100644 index 0000000..5a56f30 --- /dev/null +++ b/jdbc/spring-liquibase-app/src/test/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + %d %5p %40.40c:%4L - %m%n + + + + + + + + + + + + + + \ No newline at end of file