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

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": Add everything related to the database.  Part 1 - 1Today we need to solve many tasks:
  1. Add flyway. Here we will also configure the database deployment for the project.
  2. Add a database schema.
  3. Add repository layer.
  4. Add command for bot statistics.
  5. Write tests, after all.
Therefore, sit back comfortably: there will be a longread. 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 there are any questions about the implementation, you can look at the code in this pull request .

Add 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 responsible for adding and managing some kind of 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 the information in your memory before reading this article. You can also read the README.md file of the project with a demonstration of an 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 one adds the ability to connect to the MySQL database. The third is SpringBoot starter, which starts all the magic of using the database in Spring. And let's 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 development database. We will create it for now ( until I figured it out better ) 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 for work are launched. 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, let's do this:
  1. Let's create a file at the root of the project docker-compose-test.yml, which will run only the database with its own settings.
  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": Add everything related to the database.  Part 1 - 2Quickly scrolling through, I found the necessary variables: MYSQL_DATABASE - database name MYSQL_USER - database username MYSQL_PASSWORD - database user password And how to write them 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 root of the project:
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 exactly this 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 runs will be provided, rather than the default docker-compose.yml. Therefore, the request will look like this: docker-compose -f docker-compose-test.yml up -d It turns out that at first we say which file to take ( -f docker-compose-test.yml ), and then we say that we need to raise the services and do it in daemon mode ( up -d ) — that is, so that the process runs outside the terminal session in which this command was launched. We start and try to connect to the database to make sure that it has risen 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": Add 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 , well, or localhost;
  • username - this will be root, as we have written in docker-compose;
  • password is also root.
After that, you need to check if the connection will be. To do this, click Test Connection : "Java project from A to Z": Add everything related to the database.  Part 1 - 4As a result, we get: Failed to Connect to MySQL ... Why? We go into the terminal and see what happened to the docker container. Let's write in the terminal: "Java project from A to Z": Add 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. We go in the terminal to the root of the project 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 is clear that the database did not start with our settings. The error is the same, so we isolate it from the list:
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 will 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 user and password and added only the password for the root user. This means that the root user will now have the root password. At least we assume so. Now let's run 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
We 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 more errors, so you can see if there will be access. But, unfortunately, there is no access again ... Nothing: we will continue to understand! "Java project from A to Z": Add everything related to the database.  Part 1 - 6To terminate docker-compose and remove all containers, you need to write: docker-compose -f docker-compose-test.yml down Here we have specified the file to be removed. What to do? Let's google: maybe someone managed to do better. The simplest query: “example of mysql in docker-compose” helps us find a reference to the medium resource. There is an example there:
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 to access the database again through MySQLWorkBench: "Java project from A to Z": Add everything related to the database.  Part 1 - 7Finally, we have access. But now I wonder what influenced it. I sin on this part:
expose:
   # Opens port 3306 on the container
   - '3306'
It's easy to check: without it, 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 Trying to enter the database through workbench: everything is fine. Then the only option is the DB version. 5.7 is clearly indicated here, that is, if you change it to the latest, it should not work. Restarted - and it really doesn’t work ... "Java project from A to Z": Add 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 the database we need is dev_jrtb_db. To do this, run Query in MySQL Workbench with the SHOW DATABASES command :"Java project from A to Z": Add everything related to the database.  Part 1 - 9Yes, it is in the output. Great, you can move on. Next, let's add our first migration. Based on the tasks from the article "Project planning: measure seven times - 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 exactly in that folder and such a name - described in an 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 user table that has only a chat ID (a unique element that can be used as an ID in the table) and its state is active or inactive. So, if the bot is stopped, the subscription information will remain, and it can be used if the bot wants to use it again. No other information about the user is needed yet. Now the main question: how to check it? We need to run our application. So far, there is no connection to the database in the main docker-compose.yml file, and we also need to learn how to debug the application along with the database. So let's run the application with Intellij IDEA. To test this, we will need to run our main method in the JavarushTelegramBotApplication. To separate deployment and testing work, we will use a different property file −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 starting SpringBoot: it will be written in the first lines). This is how 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 a little 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.codegym_community_bot
bot.token=1375780501:AAE4A6Rz0BSnIGzeu896OjQnjzsMEG6_uso
The essential difference is in the spring.datasource.url field: in the test profile, we have a URL to localhost . That is, it is expected that the application will be launched 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 in the application.properties URL another is already specified - jrtb-db. This will be the name of our database in docker-compose so that our docker application can access the database. Why? Therefore, they will run 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 run the profile on startup? To do this, you can configure the launch of the main method in Intellij IDEA. To do this, there is Edit Configurations : go to the main JavarushTelegramBotApplication class, click on the green arrow opposite the main method declaration and select Modify Run Configuration :"Java project from A to Z": Add everything related to the database.  Part 1 - 10We enter the necessary variables and configure the profile. In Intellij IDEA, the interface of this configuration window has changed somewhat, so I advise everyone to update IDEA to the latest version so that we see the same picture: "Java project from A to Z": Add everything related to the database.  Part 1 - 11In the Environment variables field, we need to add the bot name and token variables according to this pattern: variable_name1=value1; variable_name2=value2 . That is, key=value is separated by a semicolon. In our case, it will be like this: bot.token=1375780501:AAHLzsf4WhcjSdPguDwtggRS1IMu5l8;bot.username=codegym_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 : We write:"Java project from A to Z": Add everything related to the database.  Part 1 - 12-Dspring.profiles.active=test - this will tell SpringBoot to use the test profile, which means that the properties will be taken from application-test.properties. As a result, it will look like this: "Java project from A to Z": Add everything related to the database.  Part 1 - 13Now we launch our test database: docker-compose -f docker-compose-test.yml up And just so that 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: "Java project from A to Z": Add everything related to the database.  Part 1 - 14The 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 up and everything works (although this can be seen from the application launch logs, it’s definitely worth checking):
  1. use dev_jrtb_db;
  2. show tables;
Result: "Java project from A to Z": Add 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 with database added. Now new migrations will go like clockwork. The most difficult thing is the first setup and start 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."Java project from A to Z": Add everything related to the database.  Part 1 - 15

List of all materials in the series at the beginning of this article.

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