Skip to content

Commit

Permalink
Bump MySQL support from 5.7 to 8.4 and align spring profile with the …
Browse files Browse the repository at this point in the history
…official Spring Petclinic version (#142)
  • Loading branch information
arey authored Aug 24, 2024
1 parent a745565 commit 99291bd
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 117 deletions.
26 changes: 26 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
services:
mysql:
image: mysql:8.4
command: --mysql-native-password=ON
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_USER=petclinic
- MYSQL_PASSWORD=petclinic
- MYSQL_DATABASE=petclinic
volumes:
- "./conf.d:/etc/mysql/conf.d:ro"
profiles:
- mysql
postgres:
image: postgres:16.3
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=petclinic
- POSTGRES_USER=petclinic
- POSTGRES_DB=petclinic
profiles:
- postgres
53 changes: 23 additions & 30 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,52 +53,45 @@ Our issue tracker is available here: https://github.com/spring-petclinic/spring-
## Database configuration

In its default configuration, Petclinic uses an in-memory database (HSQLDB) which gets populated at startup with data.
Similar setups are provided for MySql and PostgreSQL in case a persistent database configuration is needed. However, to populate them with sample data, you should follow additional instructions from either `src/main/resources/db/mysql/petclinic_db_setup_mysql.txt` (for MySQL) or `src/main/resources/db/postgresql/petclinic_db_setup_postgresql.txt` (for PostgreSQL) file.

To run Petclinic locally using persistent database, it is also needed to change profile defined in `application.properties` file.
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.
See the [Spring Boot documentation](https://docs.spring.io/spring-boot/how-to/properties-and-configuration.html#howto.properties-and-configuration.set-active-spring-profiles) for more detail on how to set the active profile.
You can also change profile defined in the `application.properties` file.
For MySQL database, it is needed to change param `hsqldb` to `mysql` in the following line of `application.properies` file:
```properties
spring.profiles.active=hsqldb,spring-data-jpa
```
Before doing this, it would be good to check properties defined in `application-mysql.properties` file:

```properties
spring.datasource.url = jdbc:mysql://localhost:3306/petclinic?useUnicode=true
spring.datasource.username=pc
spring.datasource.password=petclinic
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=none
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.4
```

You may also start a MySql database with Docker:
or

```sh
docker run --name mysql-petclinic -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7.8
```bash
docker run -e POSTGRES_USER=petclinic -e POSTGRES_PASSWORD=petclinic -e POSTGRES_DB=petclinic -p 5432:5432 postgres:16.3
```

For PostgeSQL database, it is needed to change param `hsqldb` to `postgresql` in the following line of `application.properties` file:
```properties
spring.profiles.active=hsqldb,spring-data-jpa
```
Before doing this, it would be good to check properties defined in `application-postgresql.properties` file:
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).

```properties
spring.datasource.url=jdbc:postgresql://localhost:5432/petclinic
spring.datasource.username=postgres
spring.datasource.password=petclinic
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database=POSTGRESQL
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
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
```
You may also start a Postgres database with Docker:

```sh
docker run --name postgres-petclinic -e POSTGRES_PASSWORD=petclinic -e POSTGRES_DB=petclinic -p 5432:5432 -d postgres:9.6.0
or

```bash
docker-compose --profile postgres up
```


## API First Approach

This API is built following some [API First approach principles](https://swagger.io/resources/articles/adopting-an-api-first-approach/).
Expand Down
8 changes: 1 addition & 7 deletions src/main/resources/application-hsqldb.properties
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
# HSQLDB config start
#----------------------------------------------------------------

spring.sql.init.schema-locations=classpath*:db/hsqldb/initDB.sql
spring.sql.init.data-locations=classpath*:db/hsqldb/populateDB.sql

spring.datasource.url=jdbc:hsqldb:mem:petclinic
spring.datasource.username=sa
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database=HSQL
spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
spring.jpa.hibernate.ddl-auto=none
#----------------------------------------------------------------
# HSQLDB config end
23 changes: 7 additions & 16 deletions src/main/resources/application-mysql.properties
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
# uncomment for init database (first start)
#spring.sql.init.mode=always
#spring.sql.init.schema-locations=classpath*:db/mysql/initDB.sql
#spring.sql.init.data-locations=classpath*:db/mysql/populateDB.sql

# MySQL config start
#----------------------------------------------------------------
spring.datasource.url = jdbc:mysql://localhost:3306/petclinic?useUnicode=true
spring.datasource.username=pc
spring.datasource.password=petclinic
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=none
#----------------------------------------------------------------
# MySQL config end
# database init, supports mysql too
database=mysql
spring.datasource.url=${MYSQL_URL:jdbc:mysql://localhost/petclinic}
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
6 changes: 6 additions & 0 deletions src/main/resources/application-postgres.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
database=postgres
spring.datasource.url=${POSTGRES_URL:jdbc:postgresql://localhost/petclinic}
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
16 changes: 0 additions & 16 deletions src/main/resources/application-postgresql.properties

This file was deleted.

8 changes: 7 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# ------------------------------------------------
# When using HSQL, use: hsqldb
# When using MySQL, use: mysql
# When using PostgeSQL, use: postgresql
# When using PostgeSQL, use: postgres
# ------------------------------------------------
#
# one for select repository layer
Expand All @@ -23,6 +23,12 @@ spring.profiles.active=hsqldb,spring-data-jpa
server.port=9966
server.servlet.context-path=/petclinic/

# database init, supports mysql and postgres too
database=hsqldb
spring.sql.init.schema-locations=classpath*:db/${database}/schema.sql
spring.sql.init.data-locations=classpath*:db/${database}/data.sql


spring.messages.basename=messages/messages
spring.jpa.open-in-view=false

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 25 additions & 9 deletions src/main/resources/db/mysql/petclinic_db_setup_mysql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@

@author Sam Brannen
@author Costin Leau
@author Dave Syer

--------------------------------------------------------------------------------

1) Download and install the MySQL database (e.g., MySQL Community Server 5.7.x),
which can be found here: http://dev.mysql.com/downloads/mysql/
Alternatively, you may use the official MySQL docker image. Refer to the
README.md for the Docker command line.
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) Create the PetClinic database and user by executing the "db/mysql/initDB.sql"
and "db/mysql/populateDB.sql" scripts. Or uncomment strings
#spring.datasource.schema=classpath*:db/mysql/initDB.sql
#spring.datasource.data=classpath*:db/mysql/populateDB.sql
in application-mysql.properties file at the first time you run the app.
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.
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
CREATE DATABASE IF NOT EXISTS petclinic;

ALTER DATABASE petclinic
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;

GRANT ALL PRIVILEGES ON petclinic.* TO pc@localhost IDENTIFIED BY 'pc';

USE petclinic;

CREATE TABLE IF NOT EXISTS vets (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30),
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions src/main/resources/db/postgres/petclinic_db_setup_postgres.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
================================================================================
=== Spring PetClinic sample application - PostgreSQL Configuration ===
================================================================================

@author Vitaliy Fedoriv
@autor Antoine Rey

--------------------------------------------------------------------------------

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.
File renamed without changes.
27 changes: 0 additions & 27 deletions src/main/resources/db/postgresql/petclinic_db_setup_postgresql.txt

This file was deleted.

6 changes: 5 additions & 1 deletion src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# ------------------------------------------------
# When using HSQL, use: hsqldb
# When using MySQL, use: mysql
# When using PostgeSQL, use: postgresql
# When using PostgeSQL, use: postgres
# ------------------------------------------------
#
# one - for select database
Expand All @@ -24,6 +24,10 @@ server.port=9966
server.servlet.context-path=/petclinic/
spring.jpa.open-in-view=false

# database init
spring.sql.init.schema-locations=classpath*:db/hsqldb/schema.sql
spring.sql.init.data-locations=classpath*:db/hsqldb/data.sql

spring.messages.basename=messages/messages
logging.level.org.springframework=INFO
#logging.level.org.springframework=DEBUG
Expand Down

0 comments on commit 99291bd

Please sign in to comment.