diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..caa9b49b8 --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/readme.md b/readme.md index e1e59d68d..3d8265995 100644 --- a/readme.md +++ b/readme.md @@ -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/). diff --git a/src/main/resources/application-hsqldb.properties b/src/main/resources/application-hsqldb.properties index 5a9156628..d65ff9f50 100644 --- a/src/main/resources/application-hsqldb.properties +++ b/src/main/resources/application-hsqldb.properties @@ -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 diff --git a/src/main/resources/application-mysql.properties b/src/main/resources/application-mysql.properties index daffa4ab3..e23dfa605 100644 --- a/src/main/resources/application-mysql.properties +++ b/src/main/resources/application-mysql.properties @@ -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 diff --git a/src/main/resources/application-postgres.properties b/src/main/resources/application-postgres.properties new file mode 100644 index 000000000..60889b43c --- /dev/null +++ b/src/main/resources/application-postgres.properties @@ -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 diff --git a/src/main/resources/application-postgresql.properties b/src/main/resources/application-postgresql.properties deleted file mode 100644 index 456b590e4..000000000 --- a/src/main/resources/application-postgresql.properties +++ /dev/null @@ -1,16 +0,0 @@ -# uncomment for init database (first start) -#spring.sql.init.mode=always -#spring.sql.init.schema-locations=classpath*:db/postgresql/initDB.sql -#spring.sql.init.data-locations=classpath*:db/postgresql/populateDB.sql - -# PostgreSQL config start -#---------------------------------------------------------------- -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 -#---------------------------------------------------------------- -# PostgreSQL config end diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 27f427499..460f9914a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 @@ -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 diff --git a/src/main/resources/db/hsqldb/populateDB.sql b/src/main/resources/db/hsqldb/data.sql similarity index 100% rename from src/main/resources/db/hsqldb/populateDB.sql rename to src/main/resources/db/hsqldb/data.sql diff --git a/src/main/resources/db/hsqldb/initDB.sql b/src/main/resources/db/hsqldb/schema.sql similarity index 100% rename from src/main/resources/db/hsqldb/initDB.sql rename to src/main/resources/db/hsqldb/schema.sql diff --git a/src/main/resources/db/mysql/populateDB.sql b/src/main/resources/db/mysql/data.sql similarity index 100% rename from src/main/resources/db/mysql/populateDB.sql rename to src/main/resources/db/mysql/data.sql diff --git a/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt b/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt index 49aa57e74..21213b4a0 100644 --- a/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt +++ b/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt @@ -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. \ No newline at end of file +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. diff --git a/src/main/resources/db/mysql/initDB.sql b/src/main/resources/db/mysql/schema.sql similarity index 90% rename from src/main/resources/db/mysql/initDB.sql rename to src/main/resources/db/mysql/schema.sql index b7ab4c82d..109d56698 100644 --- a/src/main/resources/db/mysql/initDB.sql +++ b/src/main/resources/db/mysql/schema.sql @@ -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), diff --git a/src/main/resources/db/postgresql/populateDB.sql b/src/main/resources/db/postgres/data.sql similarity index 100% rename from src/main/resources/db/postgresql/populateDB.sql rename to src/main/resources/db/postgres/data.sql diff --git a/src/main/resources/db/postgres/petclinic_db_setup_postgres.txt b/src/main/resources/db/postgres/petclinic_db_setup_postgres.txt new file mode 100644 index 000000000..981e2b0d8 --- /dev/null +++ b/src/main/resources/db/postgres/petclinic_db_setup_postgres.txt @@ -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. diff --git a/src/main/resources/db/postgresql/initDB.sql b/src/main/resources/db/postgres/schema.sql similarity index 100% rename from src/main/resources/db/postgresql/initDB.sql rename to src/main/resources/db/postgres/schema.sql diff --git a/src/main/resources/db/postgresql/petclinic_db_setup_postgresql.txt b/src/main/resources/db/postgresql/petclinic_db_setup_postgresql.txt deleted file mode 100644 index e07e1ecc7..000000000 --- a/src/main/resources/db/postgresql/petclinic_db_setup_postgresql.txt +++ /dev/null @@ -1,27 +0,0 @@ -================================================================================ -=== Spring PetClinic sample application - PostgreSQL Configuration === -================================================================================ - -@author Vitaliy Fedoriv -@autor Antoine Rey - --------------------------------------------------------------------------------- - -1) Download and install the PostgreSQL database, - which can be found here: https://www.postgresql.org/download/ - - -2) Open psql and create the PetClinic database: - - CREATE DATABASE petclinic - WITH OWNER = postgres - ENCODING = 'UTF8' - TABLESPACE = pg_default - CONNECTION LIMIT = -1; - -3) Create the PetClinic tables by executing the "db/postgresql/initDB.sql". -Then execute "db/mysql/populateDB.sql" script. -Or uncomment strings -#spring.datasource.schema=classpath*:db/postgresql/initDB.sql -#spring.datasource.data=classpath*:db/postgresql/populateDB.sql - in application-postgresql.properties file at the first time you run the app. \ No newline at end of file diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 31f3b8080..f57025fb5 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -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 @@ -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