JavaRush /Java Blog /Random EN /We add everything related to the database. (Part 1) - "Ja...

We add everything related to the database. (Part 1) - "Java project from A to Z"

Published in the Random EN group
Step by step we are moving towards our goal. "Java project from A to Z": Adding everything related to the database.  Part 1 - 1Today we need to solve many tasks:
  1. Add Flyway. Here we will configure the deployment of the database for the project.
  2. Add a database schema.
  3. Add a repository layer.
  4. Add a command for bot statistics.
  5. Write tests, after all.
So sit back, sit back, there will be a long read. We have already learned how to deploy our application using docker and docker-compose. It was not easy, and congratulations to those who managed to do it. If you have any questions about the implementation, you can look at the code in this pull request .

Adding Flyway

To add Flyway, you need to have a database, which we will add as another service in docker-compose. Last time I said that docker-compose has a services section, which is precisely responsible for adding and managing a certain infrastructure - that is, a set of applications/databases/messengers, etc.
I have already talked about what Flyway is and how to add it to a SpringBoot application . I advise you to refresh your memory before reading this article. You can also read the README.md file of the project with a demonstration of the article about Flyway + SpringBoot.
The first thing we do is update our main branch in the project (git pull) and create a new one with the name of our task - STEP_5_JRTB-1 (git checkout -b STEP_5_JRTB-1). Let me remind you: we renamed the branches so that it was possible to sort the sequence of tasks in the project only by name. This will allow you to quickly move from one step to another. Now you can work in peace. We need to add three dependencies:
<dependency>
  <groupId>org.flywaydb</groupId>
  <artifactId>flyway-core</artifactId>
</dependency>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
The first dependency is responsible for adding FlyWay to the project, the second adds the ability to connect to the MySQL database. The third is SpringBoot starter, which launches all the magic of using a database in Spring. And let’s also add a lombok so as not to write getters and setters:
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
</dependency>
Now we need a database for development. We will create it for now ( until I come up with a better idea ) as a separate docker-compose file and a separate file for application.properties. There is such a thing in development (and in the Spring ecosystem in particular) as profiles. Depending on the profile, different settings are launched for operation. I think in the future we will combine docker-compose into one file and make sure that only one database is launched for development on the settings that we need. For now we'll do this:
  1. Let's create a file in the root of the project docker-compose-test.yml, in which only the database with its own settings will be launched.
  2. Let's create an application-test.properties file with settings that match the settings in the docker-compose-test.yml file.
To work with MySQL, I took the official image from DockerHub : "Java project from A to Z": Adding everything related to the database.  Part 1 - 2After quickly scrolling through, I found the necessary variables: MYSQL_DATABASE - database name MYSQL_USER - database user name MYSQL_PASSWORD - database user password And how they need to be written correctly:
jrtb-db:
   image: mysql
   restart: always
   container_name: dev-jrtb-db
   ports:
     - "3306:3306"
   environment:
     MYSQL_DATABASE: "dev_jrtb_db"
     MYSQL_USER: root
     MYSQL_PASSWORD: root
After that, I create docker-compose-test.yml in the project root:
version: '3.1'

services:
 jrtb-db:
   image: mysql
   restart: always
   container_name: dev-jrtb-db
   ports:
     - "3306:3306"
   environment:
     MYSQL_DATABASE: "dev_jrtb_db"
     MYSQL_USER: root
     MYSQL_PASSWORD: root
To run this particular file to work with docker-compose, you need to use the -f flag in the request . It means that the name of the file that is launched will be provided, rather than the standard docker-compose.yml being taken. Therefore, the request will look like this: docker-compose -f docker-compose-test.yml up -d It turns out that first we say which file to take ( -f docker-compose-test.yml ), and then we say that we need to raise services and do this in daemon mode ( up -d ) - that is, so that the process runs outside the terminal session in which this command was launched. We launch and try to connect to the database to make sure that it is up and can be used. To do this, open the familiar MySQL Workbench ( here we talked about it ) and create a new connection: "Java project from A to Z": Adding everything related to the database.  Part 1 - 3We need to fill in the following fields:
  • Connection name - connection name - optionally, DEV_JRTB_DB;
  • Hostname - here is the address where the database is deployed. In our case, everything will remain as it was: 127.0.0.1 , or localhost;
  • username - here will be root, as we have written in docker-compose;
  • password - also root.
After this, you need to check if there is a connection. To do this, click Test Connection : "Java project from A to Z": Adding everything related to the database.  Part 1 - 4As a result, we get: Failed to Connect to MySQL... Why? We go to the terminal and see what happened to the docker container. Let's write in the terminal: "Java project from A to Z": Adding everything related to the database.  Part 1 - 5We see that the terminal was created 59 minutes ago, and the restart was 55 seconds... why? It’s not clear, you need to look at the logs. Go to the project root in the terminal and run the following command: docker-compose -f docker-compose-test.yml logs
We get:

dev-jrtb-db | 2021-02-28 10:14:28+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:28+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' dev-jrtb-db | 2021-02-28 10:14:28+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:28+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified dev-jrtb-db | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD dev-jrtb-db | 2021-02-28 10:14:29+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:29+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' dev-jrtb-db | 2021-02-28 10:14:29+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:29+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified dev-jrtb-db | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD dev-jrtb-db | 2021-02-28 10:14:29+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:29+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' dev-jrtb-db | 2021-02-28 10:14:29+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:29+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified dev-jrtb-db | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD dev-jrtb-db | 2021-02-28 10:14:30+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:30+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' dev-jrtb-db | 2021-02-28 10:14:30+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:30+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified dev-jrtb-db | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD dev-jrtb-db | 2021-02-28 10:14:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:32+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' dev-jrtb-db | 2021-02-28 10:14:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:32+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified dev-jrtb-db | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD dev-jrtb-db | 2021-02-28 10:14:34+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:34+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' dev-jrtb-db | 2021-02-28 10:14:34+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:34+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified dev-jrtb-db | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD dev-jrtb-db | 2021-02-28 10:14:37+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:37+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' dev-jrtb-db | 2021-02-28 10:14:37+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:37+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified dev-jrtb-db | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD dev-jrtb-db | 2021-02-28 10:14:44+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:44+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' dev-jrtb-db | 2021-02-28 10:14:44+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:44+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified dev-jrtb-db | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD dev-jrtb-db | 2021-02-28 10:14:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:58+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' dev-jrtb-db | 2021-02-28 10:14:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 10:14:58+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified dev-jrtb-db | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD

Now it’s clear that the database never started with our settings. The error is the same, so let’s isolate it from the sheet:
2021-02-28 11:03:37+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified dev-jrtb-db | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
That is, he needs to determine the password for the root user - the main user. Okay, now we'll update our file:
version: '3.1'

services:
 jrtb-db:
   image: mysql
   restart: always
   container_name: dev-jrtb-db
   ports:
     - "3306:3306"
   environment:
     MYSQL_DATABASE: "dev_jrtb_db"
     MYSQL_ROOT_PASSWORD: root
We removed those variables for the user and password and added only the password for the root user. This means that the root user will now have a root password. At least that's what we assume. Now let’s run it and see what happens: docker-compose -f docker-compose-test.yml up -d And immediately see what the logs tell us: docker-compose -f docker-compose-test.yml logs
Let's look:

Attaching to dev-jrtb-db dev-jrtb-db | 2021-02-28 11:20:40+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 11:20:41+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' dev-jrtb-db | 2021-02-28 11:20:41+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started. dev-jrtb-db | 2021-02-28 11:20:41+00:00 [Note] [Entrypoint]: Initializing database files dev-jrtb-db | 2021-02-28T11:20:41.083803Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.23) initializing of server in progress as process 43 dev-jrtb-db | 2021-02-28T11:20:41.088457Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. dev-jrtb-db | 2021-02-28T11:20:41.447990Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. dev-jrtb-db | 2021-02-28T11:20:42.672093Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. dev-jrtb-db | 2021-02-28 11:20:45+00:00 [Note] [Entrypoint]: Database files initialized dev-jrtb-db | 2021-02-28 11:20:45+00:00 [Note] [Entrypoint]: Starting temporary server dev-jrtb-db | 2021-02-28T11:20:45.893664Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 88 dev-jrtb-db | 2021-02-28T11:20:45.921695Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. dev-jrtb-db | 2021-02-28T11:20:46.088339Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. dev-jrtb-db | 2021-02-28T11:20:46.207499Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock dev-jrtb-db | 2021-02-28T11:20:46.369170Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. dev-jrtb-db | 2021-02-28T11:20:46.369357Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel. dev-jrtb-db | 2021-02-28T11:20:46.371501Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. dev-jrtb-db | 2021-02-28T11:20:46.402227Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server - GPL. dev-jrtb-db | 2021-02-28 11:20:46+00:00 [Note] [Entrypoint]: Temporary server started. dev-jrtb-db | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it. dev-jrtb-db | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it. dev-jrtb-db | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it. dev-jrtb-db | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it. dev-jrtb-db | 2021-02-28 11:20:49+00:00 [Note] [Entrypoint]: Creating database dev_jrtb_db dev-jrtb-db | dev-jrtb-db | 2021-02-28 11:20:49+00:00 [Note] [Entrypoint]: Stopping temporary server dev-jrtb-db | 2021-02-28T11:20:49.187996Z 11 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.23). dev-jrtb-db | 2021-02-28T11:20:50.925075Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23) MySQL Community Server - GPL. dev-jrtb-db | 2021-02-28 11:20:51+00:00 [Note] [Entrypoint]: Temporary server stopped dev-jrtb-db | dev-jrtb-db | 2021-02-28 11:20:51+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up. dev-jrtb-db | dev-jrtb-db | 2021-02-28T11:20:51.420058Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 1 dev-jrtb-db | 2021-02-28T11:20:51.427384Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. dev-jrtb-db | 2021-02-28T11:20:51.577244Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. dev-jrtb-db | 2021-02-28T11:20:51.677659Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock dev-jrtb-db | 2021-02-28T11:20:51.787156Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. dev-jrtb-db | 2021-02-28T11:20:51.787325Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel. dev-jrtb-db | 2021-02-28T11:20:51.789742Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. dev-jrtb-db | 2021-02-28T11:20:51.812100Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.

There seem to be no errors anymore, so you can see if there will be access. But, unfortunately, there is no access again... Nothing: we’ll figure it out further! "Java project from A to Z": Adding everything related to the database.  Part 1 - 6To terminate docker-compose and delete all containers, you need to write: docker-compose -f docker-compose-test.yml down Here we specified the file to delete. What to do? Let's google it: maybe someone has done it better. The simplest query: “example of mysql in docker-compose” helps us find a link to a medium resource. They give this example:
version: '3.3'
services:
 db:
   image: mysql:5.7
   restart: always
   environment:
     MYSQL_DATABASE: 'db'
     # So you don't have to use root, but you can if you like
     MYSQL_USER: 'user'
     # You can use whatever password you like
     MYSQL_PASSWORD: 'password'
     # Password for root access
     MYSQL_ROOT_PASSWORD: 'password'
   ports:
     # <Port exposed> : < MySQL Port running inside container>
     - '3306:3306'
   expose:
     # Opens port 3306 on the container
     - '3306'
     # Where our data will be persisted
   volumes:
     - my-db:/var/lib/mysql
# Names our volume
volumes:
 my-db:
We adapt it to our needs and get:
version: '3.1'

services:
 jrtb-db:
   image: mysql:5.7
   restart: always
   environment:
     MYSQL_DATABASE: 'dev_jrtb_db'
     # So you don't have to use root, but you can if you like
     MYSQL_USER: 'dev_jrtb_db_user'
     # You can use whatever password you like
     MYSQL_PASSWORD: 'dev_jrtb_db_password'
     # Password for root access
     MYSQL_ROOT_PASSWORD: 'root'
   ports:
     # <Port exposed> : < MySQL Port running inside container>
     - '3306:3306'
   expose:
       # Opens port 3306 on the container
       - '3306'
We launch this option with the command already known to us: docker-compose -f docker-compose-test.yml up -d We try again to access the database through MySQLWorkBench: "Java project from A to Z": Adding everything related to the database.  Part 1 - 7Finally, access has appeared. But now I wonder what influenced this. I'm sinning on this part:
expose:
   # Opens port 3306 on the container
   - '3306'
It's easy to check: without this, nothing should work :D. Let's check: delete this part, restart docker-compose: docker-compose -f docker-compose-test.yml down docker-compose -f docker-compose-test.yml up We try to log into the database through workbench: everything is fine. Then the only option is the database version. It clearly states 5.7, that is, if you change it to the latest, it should not work. I restarted it - and it really doesn’t work... "Java project from A to Z": Adding everything related to the database.  Part 1 - 8Apparently, they have some problems with the docker image on the latest version. Okay, no problem. This often happens with free software. We will use version 5.7. Let's see if there is a database that we need - dev_jrtb_db. To do this, run the Query command in MySQL Workbench SHOW DATABASES : "Java project from A to Z": Adding everything related to the database.  Part 1 - 9Yes, it is present in the output. Great, we can move on. Next, let's add our first migration. Based on the tasks from the article “Project planning: measure twice, cut once,” we will create our first table tg_user. All migrations will be located in the projectDir/src/main/resources/db/migration folder , and the name will be: V00001__add_tg_user_table.sql . Why there is such a name in that folder - I described it in the article about Spring + flyway. This is what our first migration will look like: V00001__add_tg_user_table.sql:
-- ensure that the table with this name is removed before creating a new one.
DROP TABLE IF EXISTS tg_user;

-- Create tg_user table
CREATE TABLE tg_user (
   chat_id VARCHAR(100),
   active BOOLEAN
);
That is, here we simply create a table of a user who has only a chat ID (a unique element that can be used to work as an ID in the table) and its state - active or inactive. So, if the bot is stopped, the subscription information will remain and can be used if they want to use the bot again. No other information about the user is needed for now. Now the main question: how to check this? We need to launch our application. So far, the main docker-compose.yml file has no connection with the database, and we also need to learn how to debug the application together with the database. Therefore, let's launch the application with Intellij IDEA. To test this we will need to run our main method in JavarushTelegramBotApplication. To separate the work of deployment and testing, we will use another file with properties - application-test.properties . With this template we let Spring know that this file will be used for the test profile . When we do not specify a profile, the default profile is used (check this in your logs when running SpringBoot: it will be written about this in the first lines). This is what it will look like:
# MySQL configurations:
spring.datasource.url=jdbc:mysql://localhost:3306/dev_jrtb_db
spring.datasource.username=dev_jrtb_db_user
spring.datasource.password=dev_jrtb_db_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# TelegramBot configurations:
bot.username=tes
bot.token=1375780501:AAE4A6Rz0BSnIGzeu896OjQnjzsMEG6_uso
And the main application.properties will look slightly different:
# MySQL configurations:
spring.datasource.url=jdbc:mysql://jrtb-db:3306/jrtb_db
spring.datasource.username=jrtb_db_user
spring.datasource.password=jrtb_db_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# TelegramBot configurations:
bot.username=test.javarush_community_bot
bot.token=1375780501:AAE4A6Rz0BSnIGzeu896OjQnjzsMEG6_uso
The significant difference is in the spring.datasource.url field: in the test profile we have a URL on localhost . That is, the application is expected to run outside of Docker, which is true, because when debugging we will run our project in Intellij IDEA and expect that we will have access to the database from the local machine. But the application.properties URL already contains a different one - jrtb-db . This will be the name of our database in docker-compose, and this way our docker application will be able to reach the database. Why? Therefore, they will launch within the same docker-compose and will know about each other in this way. This is necessary so that we have one closed infrastructure for the project. But how to launch the profile at startup? To do this, you can configure the launch of the main method in Intellij IDEA. For this, there is Edit Configurations : go to the main class of JavarushTelegramBotApplication, click on the green arrow next to the declaration of the main method and select Modify Run Configuration : "Java project from A to Z": Adding everything related to the database.  Part 1 - 10Enter the necessary variables and configure the profile. In Intellij IDEA, the interface of this configuration window has changed slightly, so I advise everyone to update IDEA to the latest version so that we see the same picture: "Java project from A to Z": Adding everything related to the database.  Part 1 - 11In the Environment variables field, we need to add the bot name and token variables using the following template: variable_name1=value1; variable_name2=value2 . That is, key=value are separated by semicolons. In our case it will be like this: bot.token=1375780501:AAHLzsf4WhcjSdPguDwtggRS1IMu5l8;bot.username=javarush_community_bot (you will have a different name and token for the bot) Next, select the Add VM options field in the Modify options button : Write: -Dspring.profiles.active =test - this will tell SpringBoot to use the test profile, which means the properties will be taken from application-test.properties. As a result, it will look like this: Now we launch our test database: docker-compose -f docker-compose-test.yml up And exactly this way, without the -d prefix, so that the logs can be immediately seen in the terminal, if any are needed. And the last step is to simply run the main method. If you do everything in the same way as I described, you will end up with: The second line of the logs will be a description of what the test profile has earned. We go through MySQLWorkbench and run two commands to check that the migration has rolled out and everything is working (although this is visible from the application launch logs, it’s definitely worth checking):"Java project from A to Z": Adding everything related to the database.  Part 1 - 12"Java project from A to Z": Adding everything related to the database.  Part 1 - 13"Java project from A to Z": Adding everything related to the database.  Part 1 - 14
  1. use dev_jrtb_db;
  2. show tables;
Result: "Java project from A to Z": Adding everything related to the database.  Part 1 - 15As we expected, we got two tables. The first is technical for accounting for migrations, the second is our table for Telegram users. All: Flyway along with the database has been added. Now new migrations will go like clockwork. The most difficult thing is the first setup and launch of the process. In continuation , we are completing work on this global task: adding databases to docker-compose.yml, adding a Repository layer, statistics for the bot, writing and updating tests and documentation.

A list of all materials in the series is at the beginning of this article.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION