chore: make it optional

This commit is contained in:
Josep Maria Formentí Serra 2024-05-20 08:15:39 +02:00
parent 5aedb47310
commit a9bcd80b61
9 changed files with 136 additions and 15 deletions

24
pom.xml
View file

@ -434,6 +434,30 @@
</pluginManagement>
</build>
</profile>
<profile>
<id>mysql</id>
<properties>
<spring-boot.run.profiles>mysql,docker-compose</spring-boot.run.profiles>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>postgres</id>
<properties>
<spring-boot.run.profiles>postgres,docker-compose</spring-boot.run.profiles>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
</project>

View file

@ -47,18 +47,51 @@ In its default configuration, Petclinic uses an in-memory database (H2) which
gets populated at startup with data. The h2 console is exposed at `http://localhost:8080/h2-console`,
and it is possible to inspect the content of the database using the `jdbc:h2:mem:<uuid>` URL. The UUID is printed at startup to the console.
A similar setup is provided for MySQL and PostgreSQL if a persistent database configuration is needed using docker compose.
A similar setup is provided for MySQL and PostgreSQL if a persistent database configuration is needed. Two options are available:
You only need to pass the Spring Boot profile at the time of running the application:
### Enable database via maven profile
Pass the profile for the corresponding database as an argument:
```bash
./mvnw spring-boot:test-run -Dstart-class=org.springframework.samples.petclinic.MysqlTestApplication
./mvnw spring-boot:run -Pmysql
```
or
```bash
./mvnw spring-boot:test-run -Dstart-class=org.springframework.samples.petclinic.PostgresIntegrationTests
./mvnw spring-boot:run -Ppostgres
```
### Manual configuration
Note that whenever the database type changes, the app needs to run with a different profile: `spring.profiles.active=mysql` for MySQL or `spring.profiles.active=postgres` for PostgreSQL.
You can start MySQL or PostgreSQL locally with whatever installer works for your OS or use docker:
```bash
docker run -e MYSQL_USER=petclinic -e MYSQL_PASSWORD=petclinic -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:8.2
```
or
```bash
docker run -e POSTGRES_USER=petclinic -e POSTGRES_PASSWORD=petclinic -e POSTGRES_DB=petclinic -p 5432:5432 postgres:16.1
```
Further documentation is provided for [MySQL](https://github.com/spring-projects/spring-petclinic/blob/main/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt)
and [PostgreSQL](https://github.com/spring-projects/spring-petclinic/blob/main/src/main/resources/db/postgres/petclinic_db_setup_postgres.txt).
Instead of vanilla `docker` you can also use the provided `docker-compose.yml` file to start the database containers. Each one has a profile just like the Spring profile:
```bash
docker-compose --profile mysql up
```
or
```bash
docker-compose --profile postgres up
```
## Test Applications

View file

@ -0,0 +1 @@
spring.docker.compose.profiles.active=${database}

View file

@ -5,5 +5,3 @@ spring.datasource.username=${MYSQL_USER:petclinic}
spring.datasource.password=${MYSQL_PASS:petclinic}
# SQL is written to be idempotent so this is safe
spring.sql.init.mode=always
# docker compose
spring.docker.compose.profiles.active=mysql

View file

@ -4,5 +4,3 @@ spring.datasource.username=${POSTGRES_USER:petclinic}
spring.datasource.password=${POSTGRES_PASS:petclinic}
# SQL is written to be idempotent so this is safe
spring.sql.init.mode=always
# docker compose
spring.docker.compose.profiles.active=postgres

View file

@ -0,0 +1,36 @@
================================================================================
=== Spring PetClinic sample application - MySQL Configuration ===
================================================================================
@author Sam Brannen
@author Costin Leau
@author Dave Syer
--------------------------------------------------------------------------------
1) Download and install the MySQL database (e.g., MySQL Community Server 5.1.x),
which can be found here: https://dev.mysql.com/downloads/. Or run the
"docker-compose.yml" from the root of the project (if you have docker installed
locally):
$ docker-compose up
...
mysql_1_eedb4818d817 | MySQL init process done. Ready for start up.
...
2) (Once only) create the PetClinic database and user by executing the "db/mysql/user.sql"
scripts. You can connect to the database running in the docker container using
`mysql -u root -h localhost --protocol tcp`, but you don't need to run the script there
because the petclinic user is already set up if you use the provided `docker-compose.yml`.
3) Run the app with `spring.profiles.active=mysql` (e.g. as a System property via the command
line, but any way that sets that property in a Spring Boot app should work). For example use
mvn spring-boot:run -Dspring-boot.run.profiles=mysql
To activate the profile on the command line.
N.B. the "petclinic" database has to exist for the app to work with the JDBC URL value
as it is configured by default. This condition is taken care of automatically by the
docker-compose configuration provided, or by the `user.sql` script if you run that as
root.

View file

@ -0,0 +1,19 @@
===============================================================================
=== Spring PetClinic sample application - PostgreSQL Configuration ===
===============================================================================
--------------------------------------------------------------------------------
1) Run the "docker-compose.yml" from the root of the project:
$ docker-compose up
...
spring-petclinic-postgres-1 | The files belonging to this database system will be owned by user "postgres".
...
2) Run the app with `spring.profiles.active=postgres` (e.g. as a System property via the command
line, but any way that sets that property in a Spring Boot app should work). For example use
mvn spring-boot:run -Dspring-boot.run.profiles=postgres
To activate the profile on the command line.

View file

@ -16,8 +16,12 @@
package org.springframework.samples.petclinic;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.testcontainers.containers.MySQLContainer;
/**
* PetClinic Spring Boot Application.
@ -28,10 +32,15 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class MysqlTestApplication {
@ServiceConnection
@Profile("mysql")
@Bean
static MySQLContainer<?> container() {
return new MySQLContainer<>("mysql:8.2");
}
public static void main(String[] args) {
new SpringApplicationBuilder(PetClinicApplication.class) //
.profiles("mysql") //
.run(args);
SpringApplication.run(PetClinicApplication.class, "--spring.profiles.active=mysql");
}
}

View file

@ -70,6 +70,9 @@ public class PostgresIntegrationTests {
public static void main(String[] args) {
new SpringApplicationBuilder(PetClinicApplication.class) //
.profiles("postgres") //
.properties( //
"spring.docker.compose.profiles.active=postgres" //
) //
.listeners(new PropertiesLogger()) //
.run(args);
}