Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 2db718d

Browse files
committed
Add hints for PostgreSQL R2DBC connector
- Refactor the data-r2dbc sample to work with H2 and PostgreSQL Closes gh-1626
1 parent 3fc66c4 commit 2db718d

File tree

15 files changed

+125
-54
lines changed

15 files changed

+125
-54
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
clean_up () {
4+
ARG=$?
5+
docker-compose down --volumes
6+
exit $ARG
7+
}
8+
trap clean_up EXIT
9+
10+
docker-compose up -d --renew-anon-volumes
11+
12+
export SPRING_PROFILES_ACTIVE=h2
13+
echo "Testing with $SPRING_PROFILES_ACTIVE profile"
14+
${PWD%/*samples/*}/scripts/compileWithMaven.sh $* && ${PWD%/*samples/*}/scripts/test.sh $*

samples/data-r2dbc/build.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
#!/usr/bin/env bash
22

3-
${PWD%/*samples/*}/scripts/compileWithMaven.sh $* && ${PWD%/*samples/*}/scripts/test.sh $*
3+
set -e
4+
5+
clean_up () {
6+
ARG=$?
7+
docker-compose down --volumes
8+
exit $ARG
9+
}
10+
trap clean_up EXIT
11+
12+
docker-compose up -d --renew-anon-volumes
13+
14+
export SPRING_PROFILES_ACTIVE=h2
15+
echo "Testing with $SPRING_PROFILES_ACTIVE profile"
16+
${PWD%/*samples/*}/scripts/compileWithMaven.sh $* && ${PWD%/*samples/*}/scripts/test.sh $*
17+
18+
echo "Testing with $SPRING_PROFILES_ACTIVE profile"
19+
export SPRING_PROFILES_ACTIVE=postgresql
20+
${PWD%/*samples/*}/scripts/compileWithMaven.sh $* && ${PWD%/*samples/*}/scripts/test.sh $*
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: '3.1'
2+
services:
3+
postgresql:
4+
image: 'postgres:14'
5+
environment:
6+
POSTGRES_USER: 'postgres'
7+
POSTGRES_PASSWORD: 'postgres'
8+
POSTGRES_DB: 'spring-native'
9+
ports:
10+
- '5432:5432'

samples/data-r2dbc/pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
<dependency>
3030
<groupId>io.r2dbc</groupId>
3131
<artifactId>r2dbc-h2</artifactId>
32+
<scope>runtime</scope>
3233
</dependency>
3334
<dependency>
34-
<groupId>org.springframework.boot</groupId>
35-
<artifactId>spring-boot-starter-test</artifactId>
36-
<scope>test</scope>
35+
<groupId>org.postgresql</groupId>
36+
<artifactId>r2dbc-postgresql</artifactId>
37+
<scope>runtime</scope>
3738
</dependency>
3839
</dependencies>
3940

samples/data-r2dbc/src/main/java/com/example/webflux/WebfluxApplication.java renamed to samples/data-r2dbc/src/main/java/com/example/r2dbc/R2DBCApplication.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.example.webflux;
16+
package com.example.r2dbc;
17+
18+
import reactor.core.publisher.Mono;
1719

1820
import org.springframework.boot.ApplicationRunner;
1921
import org.springframework.boot.SpringApplication;
@@ -24,14 +26,13 @@
2426
import org.springframework.r2dbc.core.DatabaseClient;
2527
import org.springframework.web.reactive.function.server.RouterFunction;
2628
import org.springframework.web.reactive.function.server.ServerResponse;
27-
import reactor.core.publisher.Mono;
2829

29-
import static org.springframework.web.reactive.function.server.RouterFunctions.*;
30-
import static org.springframework.web.reactive.function.server.ServerResponse.*;
30+
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
31+
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
3132

3233
@SpringBootApplication
3334
@EnableR2dbcAuditing(auditorAwareRef = "fixedAuditor")
34-
public class WebfluxApplication {
35+
public class R2DBCApplication {
3536

3637
@Bean
3738
RouterFunction<ServerResponse> routes(ReservationRepository reservationRepository) {
@@ -51,6 +52,6 @@ ReactiveAuditorAware<String> fixedAuditor() {
5152
}
5253

5354
public static void main(String[] args) {
54-
SpringApplication.run(WebfluxApplication.class, args);
55+
SpringApplication.run(R2DBCApplication.class, args);
5556
}
5657
}

samples/data-r2dbc/src/main/java/com/example/webflux/Reservation.java renamed to samples/data-r2dbc/src/main/java/com/example/r2dbc/Reservation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.example.webflux;
16+
package com.example.r2dbc;
17+
18+
import java.time.Instant;
1719

1820
import org.springframework.data.annotation.CreatedBy;
1921
import org.springframework.data.annotation.CreatedDate;
2022
import org.springframework.data.annotation.Id;
2123
import org.springframework.data.annotation.LastModifiedBy;
2224
import org.springframework.data.annotation.LastModifiedDate;
2325

24-
import java.time.Instant;
25-
2626
public class Reservation {
2727

2828
@Id
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.example.webflux;
1+
package com.example.r2dbc;
22

33
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
44

55
interface ReservationRepository extends ReactiveCrudRepository<Reservation, Integer> {
6-
}
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.r2dbc.url=r2dbc:postgres://localhost/spring-native
2+
spring.r2dbc.username=postgres
3+
spring.r2dbc.password=postgres
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#spring.r2dbc.url=r2dbc:postgres://localhost/orders
2-
#spring.r2dbc.username=orders
3-
#spring.r2dbc.password=orders
4-
#spring.jpa.generate-ddl=true
51
spring.data.r2dbc.repositories.enable=true
6-
#spring.datasource.driver-class-name=org.postgresql.Driver
2+
spring.sql.init.mode=always
3+
spring.r2dbc.url=r2dbc:h2:mem:///spring-native;DB_CLOSE_DELAY=-1
4+
spring.r2dbc.username=h2
5+
spring.r2dbc.password=h2
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
INSERT INTO reservation(name) VALUES ('Andy');
2-
INSERT INTO reservation(name) VALUES ('Sebastien');
2+
INSERT INTO reservation(name) VALUES ('Sebastien');

0 commit comments

Comments
 (0)