cloudfoundry/credhub

Postgresql database migrations failed with a not empty database

max-soe opened this issue · 4 comments

What version of the credhub server you are using?

We using the credhub bosh release with the version 2.12.0
As database we use a postgres server with version 11.10

What did you expect to happen?

That flyway execute all migrations in a new postgres server instance on the first start of credhub even if there is already data in the database. We creating some views in our database for our monitoring during the setup of the database. But afterwards the deployment of credhub failed

What was the actual behavior?

Credhub failed with the following error. If the database is empty everything works fine...

2022-04-29T13:30:51.358Z [background-preinit] ....  INFO --- Version: HV000001: Hibernate Validator 6.1.7.Final
2022-04-29T13:30:51.692Z [main] ....  INFO --- CredHubApp: The following profiles are active: prod
2022-04-29T13:30:57.922Z [main] ....  INFO --- TomcatWebServer: Tomcat initialized with port(s): 8844 (https)
2022-04-29T13:30:57.950Z [main] ....  INFO --- StandardEngine: Starting Servlet engine: [Apache Tomcat/9.0.53]
2022-04-29T13:30:58.061Z [main] ....  INFO --- [/]: Initializing Spring embedded WebApplicationContext
2022-04-29T13:30:59.658Z [main] ....  INFO --- HikariDataSource: HikariPool-1 - Starting...
2022-04-29T13:30:59.863Z [main] ....  INFO --- FlywayMigrationStrategyConfiguration: Validating database state...
2022-04-29T13:31:00.002Z [main] ....  INFO --- VersionPrinter: Flyway Community Edition 7.15.0 by Redgate
Detected resolved migration not applied to database: 3. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 4. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 5. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 6. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 11. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 12. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 13. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 14. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 17. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 18. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 19. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 21. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 24. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 25. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 28. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 29. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 30. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 31. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 32. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 33. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 34. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 35. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 36. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 37. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 38. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 39. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 40. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 41. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 43. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 44. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
Detected resolved migration not applied to database: 48. To fix this error, either run migrate, or set -ignorePendingMigrations=true.
2022-04-29T13:31:00.585Z [main] .... FATAL --- FlywayMigrationStrategyConfiguration: FlyWay migration failed. Crashing.
2022-04-29T13:31:00.587Z [main] ....  INFO --- HikariDataSource: HikariPool-1 - Shutdown initiated...
2022-04-29T13:31:00.595Z [main] ....  INFO --- HikariDataSource: HikariPool-1 - Shutdown completed.
2022-04-29T13:31:00.604Z [main] ....  INFO --- StandardService: Stopping service [Tomcat]
2022-04-29T13:31:00.651Z [main] ....  INFO --- ConditionEvaluationReportLoggingListener:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-04-29T13:31:00.692Z [main] .... ERROR --- SpringApplication: Application run failed
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.10.jar!/:5.3.10]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.10.jar!/:5.3.10]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.10.jar!/:5.3.10]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.10.jar!/:5.3.10]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.11.jar!/:2.4.11]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:771) ~[spring-boot-2.4.11.jar!/:2.4.11]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:763) [spring-boot-2.4.11.jar!/:2.4.11]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:438) [spring-boot-2.4.11.jar!/:2.4.11]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:339) [spring-boot-2.4.11.jar!/:2.4.11]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329) [spring-boot-2.4.11.jar!/:2.4.11]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1318) [spring-boot-2.4.11.jar!/:2.4.11]
        at org.cloudfoundry.credhub.CredHubApp.main(CredHubApp.java:20) [classes!/:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_322]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_322]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_322]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_322]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) [credhub.jar:?]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) [credhub.jar:?]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) [credhub.jar:?]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) [credhub.jar:?]
Caused by: org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "public" but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
        at org.flywaydb.core.Flyway$1.execute(Flyway.java:200) ~[flyway-core-7.15.0.jar!/:?]
        at org.flywaydb.core.Flyway$1.execute(Flyway.java:170) ~[flyway-core-7.15.0.jar!/:?]
        at org.flywaydb.core.Flyway.execute(Flyway.java:586) ~[flyway-core-7.15.0.jar!/:?]
        at org.flywaydb.core.Flyway.migrate(Flyway.java:170) ~[flyway-core-7.15.0.jar!/:?]
        at org.cloudfoundry.credhub.config.FlywayMigrationStrategyConfiguration.runMigration(FlywayMigrationStrategyConfiguration.java:75) ~[classes!/:?]
        at org.cloudfoundry.credhub.config.FlywayMigrationStrategyConfiguration.lambda$repairBeforeMigration$0(FlywayMigrationStrategyConfiguration.java:26) ~[classes!/:?]
        at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:62) ~[spring-boot-autoconfigure-2.4.11.jar!/:2.4.11]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.10.jar!/:5.3.10]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.10.jar!/:5.3.10]
        ... 27 more

Manual workaround:

We found a way to execute the migrations by setting the following config parameters in the application/spring.yml config:

spring:
  flyway:
     baselineOnMigrate: true
     baselineVersion: 0

@max-soe Could you describe how you set up your new views where you had some CredHub data from another instance but didn't have the fly_schema_history table?

The workaround you gave overrides a safety feature that detects when CredHub is pointed toward the wrong type of database, so I don't think we'd want to make that the default. (https://flywaydb.org/documentation/configuration/parameters/baselineOnMigrate)

Hi @swalchemist,

We created a database and created with psql some view (https://www.postgresql.org/docs/current/sql-createview.html) to improve our monitoring. Afterwards we tried to deploy credhub but that failed because the database was not empty.

We meanwhile have a workaround and this issue is not that important for us. Feel free to close it or add maybe an information to your documentation that the provided database for credhub must be empty. Otherwise the database migrations will fail.

Closing this as a non-standard usage and will investigate documentaiton clarifications regarding this.