Home > database >  Spring/Hibernate app in docker doesn't remove columns
Spring/Hibernate app in docker doesn't remove columns

Time:07-05

I am new to docker and it is easy to get confused about some things. Here is my question. I am working on my Spring Boot app and was creating entities for DB. I found out that when I remove a column from the entity after rebuilding the container (docker-compose up --build) this column isn't removed. But when I add a new column after rebuilding a container new column is created.

After that, I tried to remove all unused images and containers by running docker system prune. And surprisingly after once again running docker-compose up --build column was removed from db.

Is this expected or it can be changed somehow?

I'm gonna add my docker files. Maybe the problem is somewhere there.

Dockerfile:

FROM maven:3.5-jdk-11 AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package -DskipTests


FROM adoptopenjdk:11-jre-hotspot
ARG JAR_FILE=*.jar
COPY --from=build /usr/src/app/target/restaurant-microservices.jar /usr/app/restaurant-microservices.jar
ENTRYPOINT ["java","-jar","/usr/app/restaurant-microservices.jar"]

docker-compose.yml:

version: '3'
services:
  db:
    image: 'postgres:13'
    container_name: db
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=restaurant
    ports:
      - "5432:5432"
  app:
    build: .
    container_name: app
    ports:
      - "8080:8080"
    depends_on:
      - db
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/restaurant
      - SPRING_DATASOURCE_USERNAME=postgres
      - SPRING_DATASOURCE_PASSWORD=password
      - SPRING_JPA_HIBERNATE_DDL_AUTO=update

Upd: I tried the same thing with controllers and it creates and removes controllers without any issues

CodePudding user response:

I found the problem. It is all about this line of code in docker-compose.yml(and application.properties)

   SPRING_JPA_HIBERNATE_DDL_AUTO=update

The update operation for example, will attempt to add new columns, constraints, etc. but will never remove a column or constraint that may have existed previously but no longer does as part of the object model from a prior run (found here How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?) So when I changed update to create-drop (not sure if it is optimal option) it started working as expected

CodePudding user response:

oh, I also didn't know that. thank you for your problem solving.

It is something weird, because 'update' means something changed.

  • Related