Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restarting with external database configured re-runs the database init scripts #182

Open
davosian opened this issue Dec 10, 2024 · 2 comments · May be fixed by #183
Open

Restarting with external database configured re-runs the database init scripts #182

davosian opened this issue Dec 10, 2024 · 2 comments · May be fixed by #183
Labels

Comments

@davosian
Copy link

When starting the application with the following docker-compose definition, restarting the application fails because it is trying to initialize the (then existing) database again:

services:
  backend:
    image: springcommunity/spring-petclinic-rest:3.2.1
    ports:
      - "9966:9966"
    environment:
      SPRING_PROFILES_ACTIVE: postgres,spring-data-jpa
      SPRING_DATASOURCE_URL: jdbc:postgresql://database:5432/petclinic
      SPRING_DATASOURCE_USERNAME: petclinic
      SPRING_DATASOURCE_PASSWORD: petclinic
    depends_on:
      - database

  database:
    image: postgres:latest
    environment:
      POSTGRES_DB: petclinic
      POSTGRES_USER: petclinic
      POSTGRES_PASSWORD: petclinic
    ports:
      - "5432:5432"
    volumes:
      - dbdata:/var/lib/postgresql/data

volumes:
  dbdata:

Is this a bug or is there a way for the application to detect that the database has been initialized already?

When adding SPRING_SQL_INIT_MODE: never after the first run, the application works fine since this forces the application to skip the database initialization.

@arey arey added the bug label Dec 10, 2024
@arey
Copy link
Member

arey commented Dec 10, 2024

Hi @davosian did you have the same error?

backend-1 | Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
backend-1 | ERROR SpringApplication - Application run failed
backend-1 | org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Failed to initialize dependency 'dataSourceScriptDatabaseInitializer' of LoadTimeWeaverAware bean 'entityManagerFactory': Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Failed to execute SQL script statement #21 of file [/app/resources/db/postgres/schema.sql]: ALTER TABLE roles ADD CONSTRAINT uni_username_role UNIQUE (role,username)

If so we could try to fix the schema.sql script in order to be reentrant.

@davosian
Copy link
Author

Yes, this would work. I was hoping that I was simply missing something in my docker configuration but it looks this is not the case. Submitted a PR for fixing the script: #183.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants