Database Test with Spring Boot

0

Personally I have an error, when I run my application with the profile test, Spring Boot does not create the same database in the local profile. Any suggestion?

Here's my application-local.properties file:

# Hibernate
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

spring.datasource.username=rw_itau
spring.datasource.password=rw_itau
spring.datasource.url=jdbc\:postgresql\://localhost/rw_itau
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.hibernate.ddl-auto=create-drop

And the application-test.properties file:

# Hibernate
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

spring.datasource.username=rw_itau
spring.datasource.password=rw_itau
spring.datasource.url=jdbc\:postgresql\://localhost/rw_itau_teste
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.hibernate.ddl-auto=create-drop

In addition I have several classes as the initial class of SpringBoot:

package br.com.zup.rw.itau;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

@SpringBootApplication
@Controller
public class Boot
{

   public static void main(String[] args)
   {
      SpringApplication.run(Boot.class, args);
   }

}

I'm using Maven and Docker with Postgres Bank the error generated is this:

  

org.postgresql.util.PSQLException: FATAL: database "rw_itau_teste"   does not exist

I run the project with this argument:

-Dspring.profiles.active=test

But when I run with the local argument, the database is created normally.

-Dspring.profiles.active=local

When I run the second error profile when connecting to the bank.

    
asked by anonymous 17.11.2017 / 15:54

1 answer

1

Good Afternoon

  spring.jpa.hibernate.ddl-auto=create-drop

This property does not create the database, only the database tables. Manually create in Postgres, and you'll see that it irr rows and create tables.

link

Solution: you can create in the main method a JDBC connection by running PreparedStatemente.executeQuery ("create database rw_itau_teste").

Use flywayDb as in the link above, using migration;

I hope I have helped.

    
17.11.2017 / 20:26