diff --git a/pom.xml b/pom.xml index cf47f8aa6..8cf4db093 100644 --- a/pom.xml +++ b/pom.xml @@ -68,6 +68,10 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-docker-compose + @@ -118,11 +122,6 @@ spring-boot-testcontainers test - - org.springframework.boot - spring-boot-docker-compose - test - org.testcontainers junit-jupiter @@ -220,7 +219,7 @@ spring-boot-maven-plugin - build-info @@ -378,7 +377,7 @@ - org.eclipse.m2e @@ -436,4 +435,4 @@ - \ No newline at end of file + diff --git a/readme.md b/readme.md index 79b0fb692..0f57d9f5b 100644 --- a/readme.md +++ b/readme.md @@ -47,35 +47,23 @@ 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:` 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. 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. +A similar setup is provided for MySQL and PostgreSQL if a persistent database configuration is needed using docker compose. -You can start MySQL or PostgreSQL locally with whatever installer works for your OS or use docker: +You only need to pass the Spring Boot profile at the time of running the application: ```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 +./mvnw spring-boot:run -Dspring-boot.run.profiles=mysql ``` or ```bash -docker run -e POSTGRES_USER=petclinic -e POSTGRES_PASSWORD=petclinic -e POSTGRES_DB=petclinic -p 5432:5432 postgres:16.1 +./mvnw spring-boot:run -Dspring-boot.run.profiles=postgres ``` 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 At development time we recommend you use the test applications set up as `main()` methods in `PetClinicIntegrationTests` (using the default H2 database and also adding Spring Boot Devtools), `MySqlTestApplication` and `PostgresIntegrationTests`. These are set up so that you can run the apps in your IDE to get fast feedback and also run the same classes as integration tests against the respective database. The MySql integration tests use Testcontainers to start the database in a Docker container, and the Postgres tests use Docker Compose to do the same thing. diff --git a/src/main/resources/application-mysql.properties b/src/main/resources/application-mysql.properties index e23dfa605..2b476bc66 100644 --- a/src/main/resources/application-mysql.properties +++ b/src/main/resources/application-mysql.properties @@ -5,3 +5,5 @@ 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 diff --git a/src/main/resources/application-postgres.properties b/src/main/resources/application-postgres.properties index 60889b43c..8a6cf45b2 100644 --- a/src/main/resources/application-postgres.properties +++ b/src/main/resources/application-postgres.properties @@ -4,3 +4,5 @@ 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