From 4be40dead22f1efa6c5b9b3ab8f7cc49a0ab73e1 Mon Sep 17 00:00:00 2001
From: Antoine Rey <antoine.rey@gmail.com>
Date: Sun, 23 Sep 2018 09:14:29 +0200
Subject: [PATCH] #65 Add some MySQL documentation (#87)

* #65 Add some MySQL documentation

* #65 Add some MySQL documentation : fixing typo
---
 README.md                                     | 31 ++++++++++++++++---
 db_readme.txt                                 | 13 --------
 petclinic_db_setup_mysql.txt                  | 24 --------------
 .../src/main/resources/application.properties |  3 --
 .../src/main/resources/db/mysql/data.sql      |  5 ---
 .../src/test/resources/application-test.yml   |  5 +++
 .../src/main/resources/application.properties |  6 +---
 .../src/test/resources/application-test.yml   |  5 +++
 .../src/main/resources/application.properties |  4 ---
 .../src/test/resources/application-test.yml   |  5 +++
 10 files changed, 43 insertions(+), 58 deletions(-)
 delete mode 100644 db_readme.txt
 delete mode 100644 petclinic_db_setup_mysql.txt

diff --git a/README.md b/README.md
index 5915bd66..21c6d7a9 100644
--- a/README.md
+++ b/README.md
@@ -43,21 +43,41 @@ 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. A similar setup is provided for MySql in case a persistent database configuration is needed.
-Note that whenever the database type is changed, the data-access.properties file needs to be updated and the mysql-connector-java artifact from the pom.xml needs to be uncommented.
+In its default configuration, Petclinic uses an in-memory database (HSQLDB) which gets populated at startup with data.
+A similar setup is provided for MySql in case a persistent database configuration is needed.
+Dependency for Connector/J, the MySQL JDBC driver is already included in the `pom.xml` files.
+
+### Start a MySql database
 
 You may start a MySql database with docker:
 
 ```
 docker run -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7.8
 ```
+or download and install the MySQL database (e.g., MySQL Community Server 5.7 GA), which can be found here: https://dev.mysql.com/downloads/
+
+### Use the Spring 'mysql' profile
+
+To use a MySQL database, you have to start 3 microservices (`visits-service`, `customers-service` and `vets-services`)
+with the `mysql` Spring profile. Add the `--spring.profiles.active=mysql` as programm argument.
+
+By default, at startup, database schema will be created and data will be populated.
+You may also manualy create the PetClinic database and data by executing the `"db/mysql/{schema,data}.sql"` scripts of each 3 microservices. 
+In the `application.yml` of the [Configuration repository], set the `initialization-mode` to `never`.
+
+If you are running the microservices with Docker, you have to add the `mysql` profile into the (Dockerfile)[docker/Dockerfile]:
+```
+ENV SPRING_PROFILES_ACTIVE docker,mysql
+```
+In the `mysql section` of the `application.yml` from the [Configuration repository], you have to change 
+the host and port of your MySQL JDBC connection string. 
+
 
 ## Looking for something in particular?
 
 | Spring Cloud components | Resources  |
 |-------------------------|------------|
-| Configuration server    | [Config server properties](spring-petclinic-config-server/src/main/resources/application.yml) and [Configuration repository](https://github.com/spring-petclinic/spring-petclinic-microservices-config) |
+| Configuration server    | [Config server properties](spring-petclinic-config-server/src/main/resources/application.yml) and [Configuration repository] |
 | Service Discovery       | [Eureka server](spring-petclinic-discovery-server) and [Service discovery client](spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/VetsServiceApplication.java) |
 | API Gateway             | [Zuul reverse proxy](spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/ApiGatewayApplication.java) and [Routing configuration](https://github.com/spring-petclinic/spring-petclinic-microservices-config/blob/master/api-gateway.yml) |
 | Docker Compose          | [Spring Boot with Docker guide](https://spring.io/guides/gs/spring-boot-docker/) and [docker-compose file](docker-compose.yml) |
@@ -78,3 +98,6 @@ docker run -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:
 The [issue tracker](https://github.com/spring-petclinic/spring-petclinic-microservices/issues) is the preferred channel for bug reports, features requests and submitting pull requests.
 
 For pull requests, editor preferences are available in the [editor config](.editorconfig) for easy use in common text editors. Read more and download plugins at <http://editorconfig.org>.
+
+
+[Configuration repository]: https://github.com/spring-petclinic/spring-petclinic-microservices-config
diff --git a/db_readme.txt b/db_readme.txt
deleted file mode 100644
index 68e3f245..00000000
--- a/db_readme.txt
+++ /dev/null
@@ -1,13 +0,0 @@
-================================================================================
-===        Spring PetClinic sample application - Database Configuration      ===
-================================================================================
-
-@author Costin Leau
-
---------------------------------------------------------------------------------
-
-In its default configuration, Petclinic uses an in-memory database (HSQLDB) which
-gets populated at startup with data. A similar setup is provided for Mysql in case
-a persistent database configuration is needed.
-Note that whenever the database type is changed, the data-access.properties file needs to
-be updated.
diff --git a/petclinic_db_setup_mysql.txt b/petclinic_db_setup_mysql.txt
deleted file mode 100644
index 765711ea..00000000
--- a/petclinic_db_setup_mysql.txt
+++ /dev/null
@@ -1,24 +0,0 @@
-================================================================================
-===        Spring PetClinic sample application - MySQL Configuration         ===
-================================================================================
-
-@author Sam Brannen
-@author Costin Leau
-
---------------------------------------------------------------------------------
-
-1) Download and install the MySQL database (e.g., MySQL Community Server 5.1.x),
-   which can be found here: http://dev.mysql.com/downloads/
-
-2) Download Connector/J, the MySQL JDBC driver (e.g., Connector/J 5.1.x), which
-   can be found here: http://dev.mysql.com/downloads/connector/j/
-   Copy the Connector/J JAR file (e.g., mysql-connector-java-5.1.5-bin.jar) into
-   the db/mysql directory. Alternatively, uncomment the mysql-connector from the
-   Petclinic pom.
-
-3) Create the PetClinic database and user by executing the "db/mysql/createDB.txt"
-   script.
-
-4) Open "src/main/resources/spring/data-access.properties"; comment out all properties in the
-   "HSQL Settings" section; uncomment all properties in the "MySQL Settings"
-   section.
diff --git a/spring-petclinic-customers-service/src/main/resources/application.properties b/spring-petclinic-customers-service/src/main/resources/application.properties
index 00b397d0..e6bce2eb 100644
--- a/spring-petclinic-customers-service/src/main/resources/application.properties
+++ b/spring-petclinic-customers-service/src/main/resources/application.properties
@@ -1,5 +1,2 @@
 server.port=0
-petclinic.database=hsqldb
-spring.datasource.schema=classpath*:db/${petclinic.database}/schema.sql
-spring.datasource.data=classpath*:db/${petclinic.database}/data.sql
 logging.level.org.springframework=INFO
diff --git a/spring-petclinic-customers-service/src/main/resources/db/mysql/data.sql b/spring-petclinic-customers-service/src/main/resources/db/mysql/data.sql
index cbc917a4..d6e91b1b 100644
--- a/spring-petclinic-customers-service/src/main/resources/db/mysql/data.sql
+++ b/spring-petclinic-customers-service/src/main/resources/db/mysql/data.sql
@@ -29,8 +29,3 @@ INSERT IGNORE INTO pets VALUES (10, 'Mulligan', '1997-02-24', 2, 8);
 INSERT IGNORE INTO pets VALUES (11, 'Freddy', '2000-03-09', 5, 9);
 INSERT IGNORE INTO pets VALUES (12, 'Lucky', '2000-06-24', 2, 10);
 INSERT IGNORE INTO pets VALUES (13, 'Sly', '2002-06-08', 1, 10);
-
-INSERT IGNORE INTO visits VALUES (1, 7, '2010-03-04', 'rabies shot');
-INSERT IGNORE INTO visits VALUES (2, 8, '2011-03-04', 'rabies shot');
-INSERT IGNORE INTO visits VALUES (3, 8, '2009-06-04', 'neutered');
-INSERT IGNORE INTO visits VALUES (4, 7, '2008-09-04', 'spayed');
diff --git a/spring-petclinic-customers-service/src/test/resources/application-test.yml b/spring-petclinic-customers-service/src/test/resources/application-test.yml
index 0a71b59e..0abdac44 100644
--- a/spring-petclinic-customers-service/src/test/resources/application-test.yml
+++ b/spring-petclinic-customers-service/src/test/resources/application-test.yml
@@ -1,4 +1,9 @@
 spring.jpa.hibernate.ddl-auto: none
 
+spring:
+  datasource:
+    schema: classpath*:db/hsqldb/schema.sql
+    data: classpath*:db/hsqldb/data.sql
+
 logging.level.org.springframework: INFO
 
diff --git a/spring-petclinic-vets-service/src/main/resources/application.properties b/spring-petclinic-vets-service/src/main/resources/application.properties
index 52d0b2a2..50072a6e 100644
--- a/spring-petclinic-vets-service/src/main/resources/application.properties
+++ b/spring-petclinic-vets-service/src/main/resources/application.properties
@@ -1,7 +1,3 @@
 server.port=0
-petclinic.database=hsqldb
-spring.datasource.schema=classpath*:db/${petclinic.database}/schema.sql
-spring.datasource.data=classpath*:db/${petclinic.database}/data.sql
-logging.level.org.springframework=INFO
 spring.profiles.active=production
-spring.cache.cache-names=vets
\ No newline at end of file
+spring.cache.cache-names=vets
diff --git a/spring-petclinic-vets-service/src/test/resources/application-test.yml b/spring-petclinic-vets-service/src/test/resources/application-test.yml
index 77d3ecb3..2d18cfde 100644
--- a/spring-petclinic-vets-service/src/test/resources/application-test.yml
+++ b/spring-petclinic-vets-service/src/test/resources/application-test.yml
@@ -1,5 +1,10 @@
 spring.jpa.hibernate.ddl-auto: none
 
+spring:
+  datasource:
+    schema: classpath*:db/hsqldb/schema.sql
+    data: classpath*:db/hsqldb/data.sql
+
 vets:
   cache:
     ttl: 10
diff --git a/spring-petclinic-visits-service/src/main/resources/application.properties b/spring-petclinic-visits-service/src/main/resources/application.properties
index 00b397d0..cbe617ef 100644
--- a/spring-petclinic-visits-service/src/main/resources/application.properties
+++ b/spring-petclinic-visits-service/src/main/resources/application.properties
@@ -1,5 +1 @@
 server.port=0
-petclinic.database=hsqldb
-spring.datasource.schema=classpath*:db/${petclinic.database}/schema.sql
-spring.datasource.data=classpath*:db/${petclinic.database}/data.sql
-logging.level.org.springframework=INFO
diff --git a/spring-petclinic-visits-service/src/test/resources/application-test.yml b/spring-petclinic-visits-service/src/test/resources/application-test.yml
index 0a71b59e..0abdac44 100644
--- a/spring-petclinic-visits-service/src/test/resources/application-test.yml
+++ b/spring-petclinic-visits-service/src/test/resources/application-test.yml
@@ -1,4 +1,9 @@
 spring.jpa.hibernate.ddl-auto: none
 
+spring:
+  datasource:
+    schema: classpath*:db/hsqldb/schema.sql
+    data: classpath*:db/hsqldb/data.sql
+
 logging.level.org.springframework: INFO
 
-- 
GitLab