Skip to content

Commit 7c231e7

Browse files
committed
JDBC examples added
Dependencies updated
1 parent b09f291 commit 7c231e7

File tree

30 files changed

+334
-68
lines changed

30 files changed

+334
-68
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
id "com.diffplug.spotless" version "6.19.0"
3+
id "com.asarkar.gradle.build-time-tracker" version "4.3.0"
34
}
45

56
group = groupId
@@ -13,6 +14,7 @@ repositories {
1314
subprojects {
1415
apply plugin: "java"
1516
apply plugin: "com.diffplug.spotless"
17+
apply plugin: "com.asarkar.gradle.build-time-tracker"
1618

1719
group = groupId
1820
version = koraVersion

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
groupId=ru.tinkoff.kora
2-
koraVersion=1.0.4
2+
koraVersion=1.0.6
33

44

55
##### GRADLE #####

kora-java-cache-redis/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dependencies {
3333
implementation "ru.tinkoff.kora:config-hocon"
3434

3535
testImplementation "ru.tinkoff.kora:test-junit5"
36-
testImplementation "io.goodforgod:testcontainers-extensions-redis:0.9.4"
36+
testImplementation "io.goodforgod:testcontainers-extensions-redis:0.9.6"
3737
testImplementation "redis.clients:jedis:4.4.3"
3838
}
3939

kora-java-crud/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ dependencies {
5858

5959
testImplementation "org.mockito:mockito-core:5.6.0"
6060
testImplementation "ru.tinkoff.kora:test-junit5"
61-
testImplementation "io.goodforgod:testcontainers-extensions-postgres:0.9.4"
61+
testImplementation "io.goodforgod:testcontainers-extensions-postgres:0.9.6"
6262
testImplementation "org.testcontainers:junit-jupiter:1.17.6"
6363
}
6464

kora-java-crud/src/main/resources/application.conf

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,20 @@ openapi {
3535

3636

3737
resilient {
38-
circuitbreaker {
39-
pet {
40-
slidingWindowSize = 2
41-
minimumRequiredCalls = 2
42-
failureRateThreshold = 100
43-
permittedCallsInHalfOpenState = 1
44-
waitDurationInOpenState = 15s
45-
}
38+
circuitbreaker.pet {
39+
slidingWindowSize = 50
40+
minimumRequiredCalls = 25
41+
failureRateThreshold = 50
42+
waitDurationInOpenState = "25s"
43+
permittedCallsInHalfOpenState = 10
4644
}
47-
timeout {
48-
pet {
49-
duration = 5000ms
50-
}
45+
timeout.pet {
46+
duration = "5000ms"
5147
}
52-
retry {
53-
pet {
54-
delay = 100ms
55-
attempts = 2
56-
}
48+
retry.pet {
49+
delay = "500ms"
50+
delayStep = "5s"
51+
attempts = 3
5752
}
5853
}
5954

kora-java-database-cassandra/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dependencies {
3434
implementation "ru.tinkoff.kora:config-hocon"
3535

3636
testImplementation "ru.tinkoff.kora:test-junit5"
37-
testImplementation "io.goodforgod:testcontainers-extensions-cassandra:0.9.4"
37+
testImplementation "io.goodforgod:testcontainers-extensions-cassandra:0.9.6"
3838
}
3939

4040
//noinspection GroovyAssignabilityCheck

kora-java-database-jdbc/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependencies {
3636
implementation "ru.tinkoff.kora:config-hocon"
3737

3838
testImplementation "ru.tinkoff.kora:test-junit5"
39-
testImplementation "io.goodforgod:testcontainers-extensions-postgres:0.9.4"
39+
testImplementation "io.goodforgod:testcontainers-extensions-postgres:0.9.6"
4040
}
4141

4242
//noinspection GroovyAssignabilityCheck
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ru.tinkoff.kora.example.jdbc;
2+
3+
import jakarta.annotation.Nullable;
4+
import java.util.List;
5+
import java.util.UUID;
6+
import ru.tinkoff.kora.database.common.UpdateCount;
7+
import ru.tinkoff.kora.database.common.annotation.*;
8+
import ru.tinkoff.kora.database.jdbc.JdbcRepository;
9+
10+
@Repository
11+
public interface JdbcIdRandomCompositeRepository extends JdbcRepository {
12+
13+
record Entity(@Id @Embedded EntityId id,
14+
@Column("name") String name) {
15+
16+
public Entity(String name) {
17+
this(new EntityId(UUID.randomUUID(), UUID.randomUUID()), name);
18+
}
19+
20+
public record EntityId(UUID a, UUID b) {}
21+
}
22+
23+
@Query("SELECT * FROM entities_composite_uuid WHERE a = :id.a AND b = :id.b")
24+
@Nullable
25+
Entity findById(Entity.EntityId id);
26+
27+
@Query("""
28+
INSERT INTO entities_composite_uuid(a, b, name)
29+
VALUES (:entity.id.a, :entity.id.b, :entity.name)
30+
""")
31+
UpdateCount insert(Entity entity);
32+
33+
@Query("""
34+
INSERT INTO entities_composite_uuid(a, b, name)
35+
VALUES (:entity.id.a, :entity.id.b, :entity.name)
36+
""")
37+
UpdateCount insert(@Batch List<Entity> entity);
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package ru.tinkoff.kora.example.jdbc;
2+
3+
import jakarta.annotation.Nullable;
4+
import java.util.List;
5+
import ru.tinkoff.kora.database.common.UpdateCount;
6+
import ru.tinkoff.kora.database.common.annotation.*;
7+
import ru.tinkoff.kora.database.jdbc.JdbcRepository;
8+
9+
@Repository
10+
public interface JdbcIdSequenceCompositeRepository extends JdbcRepository {
11+
12+
record Entity(@Id @Embedded EntityId id,
13+
@Column("name") String name) {
14+
15+
public Entity(String name) {
16+
this(null, name);
17+
}
18+
19+
public record EntityId(Long a, Long b) {}
20+
}
21+
22+
@Query("SELECT * FROM entities_composite WHERE a = :id.a AND b = :id.b")
23+
@Nullable
24+
Entity findById(Entity.EntityId id);
25+
26+
@Query("""
27+
INSERT INTO entities_composite(name)
28+
VALUES (:entity.name)
29+
""")
30+
UpdateCount insert(Entity entity);
31+
32+
@Query("""
33+
INSERT INTO entities_composite(name)
34+
VALUES (:entity.name)
35+
""")
36+
@Id
37+
Entity.EntityId insertGenerated(Entity entity);
38+
39+
@Query("""
40+
INSERT INTO entities_composite(name)
41+
VALUES (:entity.name)
42+
""")
43+
@Id
44+
List<Entity.EntityId> insertGenerated(@Batch List<Entity> entity);
45+
}

kora-java-database-jdbc/src/main/java/ru/tinkoff/kora/example/jdbc/JdbcIdSequenceRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@Repository
99
public interface JdbcIdSequenceRepository extends JdbcRepository {
1010

11-
record Entity(Long id,
11+
record Entity(@Id Long id,
1212
@Column("name") String name) {
1313

1414
public Entity(String name) {
@@ -32,12 +32,12 @@ INSERT INTO entities_sequence(name)
3232
VALUES (:entity.name)
3333
""")
3434
@Id
35-
Long createGenerated(Entity entity);
35+
Long insertGenerated(Entity entity);
3636

3737
@Query("""
3838
INSERT INTO entities_sequence(name)
3939
VALUES (:entity.name)
4040
""")
4141
@Id
42-
List<Long> createGenerated(@Batch List<Entity> entity);
42+
List<Long> insertGenerated(@Batch List<Entity> entity);
4343
}

kora-java-database-jdbc/src/main/java/ru/tinkoff/kora/example/jdbc/RootService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public final class RootService {
1515
private final JdbcMapperParameterRepository jdbcMapperParameterRepository;
1616
private final JdbcIdSequenceRepository jdbcIdSequenceRepository;
1717
private final JdbcIdRandomRepository jdbcIdRandomRepository;
18+
private final JdbcIdSequenceCompositeRepository jdbcIdSequenceCompositeRepository;
19+
private final JdbcIdRandomCompositeRepository jdbcIdRandomCompositeRepository;
1820
private final JdbcJsonbRepository jdbcJsonbRepository;
1921
private final JdbcCrudMacrosRepository jdbcCrudMacrosRepository;
2022

@@ -26,6 +28,8 @@ public RootService(JdbcCrudSyncRepository jdbcCrudSyncRepository,
2628
JdbcMapperParameterRepository jdbcMapperParameterRepository,
2729
JdbcIdSequenceRepository jdbcIdSequenceRepository,
2830
JdbcIdRandomRepository jdbcIdRandomRepository,
31+
JdbcIdSequenceCompositeRepository jdbcIdSequenceCompositeRepository,
32+
JdbcIdRandomCompositeRepository jdbcIdRandomCompositeRepository,
2933
JdbcJsonbRepository jdbcJsonbRepository,
3034
JdbcCrudMacrosRepository jdbcCrudMacrosRepository) {
3135
this.jdbcCrudSyncRepository = jdbcCrudSyncRepository;
@@ -36,6 +40,8 @@ public RootService(JdbcCrudSyncRepository jdbcCrudSyncRepository,
3640
this.jdbcMapperParameterRepository = jdbcMapperParameterRepository;
3741
this.jdbcIdSequenceRepository = jdbcIdSequenceRepository;
3842
this.jdbcIdRandomRepository = jdbcIdRandomRepository;
43+
this.jdbcIdSequenceCompositeRepository = jdbcIdSequenceCompositeRepository;
44+
this.jdbcIdRandomCompositeRepository = jdbcIdRandomCompositeRepository;
3945
this.jdbcJsonbRepository = jdbcJsonbRepository;
4046
this.jdbcCrudMacrosRepository = jdbcCrudMacrosRepository;
4147
}

kora-java-database-jdbc/src/main/resources/db/migration/V1__setup-tables.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ CREATE TABLE IF NOT EXISTS entities_uuid
2525
);
2626

2727

28+
CREATE TABLE IF NOT EXISTS entities_composite
29+
(
30+
a BIGINT GENERATED ALWAYS AS IDENTITY,
31+
b BIGINT GENERATED ALWAYS AS IDENTITY,
32+
name VARCHAR NOT NULL,
33+
PRIMARY KEY (a, b)
34+
);
35+
36+
37+
CREATE TABLE IF NOT EXISTS entities_composite_uuid
38+
(
39+
a UUID NOT NULL,
40+
b UUID NOT NULL,
41+
name VARCHAR NOT NULL,
42+
PRIMARY KEY (a, b)
43+
);
44+
45+
2846
CREATE TABLE IF NOT EXISTS entities_jsonb
2947
(
3048
id UUID NOT NULL,

kora-java-database-jdbc/src/test/java/ru/tinkoff/kora/example/jdbc/JdbcCrudMacrosTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public KoraConfigModification config() {
3939
}
4040

4141
@Test
42-
void syncSingleSuccess() {
42+
void syncSingle() {
4343
// given
4444
var entityCreate = new JdbcCrudMacrosRepository.Entity("1", 1, "2", null);
4545
repository.insert(entityCreate);
@@ -68,7 +68,7 @@ void syncSingleSuccess() {
6868
}
6969

7070
@Test
71-
void syncBatchSuccess() {
71+
void syncBatch() {
7272
// given
7373
var entityCreate1 = new JdbcCrudMacrosRepository.Entity("1", 1, "2", null);
7474
var entityCreate2 = new JdbcCrudMacrosRepository.Entity("2", 3, "4", null);

kora-java-database-jdbc/src/test/java/ru/tinkoff/kora/example/jdbc/JdbcCrudReactorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public KoraConfigModification config() {
4040
}
4141

4242
@Test
43-
void monoSingleSuccess() {
43+
void monoSingle() {
4444
// given
4545
var entityCreate = new JdbcCrudReactorRepository.Entity("1", 1, "2", null);
4646
repository.insert(entityCreate).block(Duration.ofMinutes(1));
@@ -69,7 +69,7 @@ void monoSingleSuccess() {
6969
}
7070

7171
@Test
72-
void monoBatchSuccess() {
72+
void monoBatch() {
7373
// given
7474
var entityCreate1 = new JdbcCrudReactorRepository.Entity("1", 1, "2", null);
7575
var entityCreate2 = new JdbcCrudReactorRepository.Entity("2", 3, "4", null);

kora-java-database-jdbc/src/test/java/ru/tinkoff/kora/example/jdbc/JdbcCrudSyncTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public KoraConfigModification config() {
3939
}
4040

4141
@Test
42-
void syncSingleSuccess() {
42+
void syncSingle() {
4343
// given
4444
var entityCreate = new JdbcCrudSyncRepository.Entity("1", 1, "2", null);
4545
repository.insert(entityCreate);
@@ -68,7 +68,7 @@ void syncSingleSuccess() {
6868
}
6969

7070
@Test
71-
void syncBatchSuccess() {
71+
void syncBatch() {
7272
// given
7373
var entityCreate1 = new JdbcCrudSyncRepository.Entity("1", 1, "2", null);
7474
var entityCreate2 = new JdbcCrudSyncRepository.Entity("2", 3, "4", null);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package ru.tinkoff.kora.example.jdbc;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import io.goodforgod.testcontainers.extensions.ContainerMode;
6+
import io.goodforgod.testcontainers.extensions.jdbc.ContainerPostgresConnection;
7+
import io.goodforgod.testcontainers.extensions.jdbc.JdbcConnection;
8+
import io.goodforgod.testcontainers.extensions.jdbc.Migration;
9+
import io.goodforgod.testcontainers.extensions.jdbc.TestcontainersPostgres;
10+
import java.util.List;
11+
import org.jetbrains.annotations.NotNull;
12+
import org.junit.jupiter.api.Test;
13+
import ru.tinkoff.kora.database.common.UpdateCount;
14+
import ru.tinkoff.kora.example.jdbc.JdbcIdRandomCompositeRepository.Entity;
15+
import ru.tinkoff.kora.test.extension.junit5.KoraAppTest;
16+
import ru.tinkoff.kora.test.extension.junit5.KoraAppTestConfigModifier;
17+
import ru.tinkoff.kora.test.extension.junit5.KoraConfigModification;
18+
import ru.tinkoff.kora.test.extension.junit5.TestComponent;
19+
20+
@TestcontainersPostgres(
21+
mode = ContainerMode.PER_RUN,
22+
migration = @Migration(
23+
engine = Migration.Engines.FLYWAY,
24+
apply = Migration.Mode.PER_METHOD,
25+
drop = Migration.Mode.PER_METHOD))
26+
@KoraAppTest(Application.class)
27+
class JdbcIdRandomCompositeTests implements KoraAppTestConfigModifier {
28+
29+
@ContainerPostgresConnection
30+
private JdbcConnection connection;
31+
32+
@TestComponent
33+
private JdbcIdRandomCompositeRepository repository;
34+
35+
@NotNull
36+
@Override
37+
public KoraConfigModification config() {
38+
return KoraConfigModification.ofSystemProperty("POSTGRES_JDBC_URL", connection.params().jdbcUrl())
39+
.withSystemProperty("POSTGRES_USER", connection.params().username())
40+
.withSystemProperty("POSTGRES_PASS", connection.params().password());
41+
}
42+
43+
@Test
44+
void insertOne() {
45+
// given
46+
var entityCreate = new Entity("Bob");
47+
48+
// when
49+
UpdateCount count = repository.insert(entityCreate);
50+
assertEquals(1, count.value());
51+
52+
var foundCreated = repository.findById(entityCreate.id());
53+
assertNotNull(foundCreated);
54+
assertEquals(entityCreate.name(), foundCreated.name());
55+
}
56+
57+
@Test
58+
void insertMany() {
59+
// given
60+
var entities = List.of(
61+
new Entity("b1"),
62+
new Entity("b2"));
63+
64+
// when
65+
var countMany = repository.insert(entities);
66+
assertEquals(2, countMany.value());
67+
68+
var foundCreated = repository.findById(entities.get(0).id());
69+
assertNotNull(foundCreated);
70+
assertEquals(entities.get(0).name(), foundCreated.name());
71+
}
72+
}

0 commit comments

Comments
 (0)