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

Flyway config should take into account if the DB has already some tables #558

Closed
icougil opened this issue Dec 1, 2018 · 2 comments
Closed

Comments

@icougil
Copy link

icougil commented Dec 1, 2018

Describe the bug
alfio fails when at booting up pointing to a database that may have a table in their schema, even if you try to add the properties or the environment properties that should make Flyway take this into account.
Just to add, some times it is mandatory to respect the actual status of the database and depending on the cloud provider, some databases will have their own extensions installed in order to provide some features, like geospatial support. Example for PostgreSQL here and here

To Reproduce
Steps to reproduce the behavior:

  1. Deploy alf.io in a database with some tables in the same schema
  2. Setup the properties that should make Flyway respect the actual database structure via adding the properties flyway.baselineOnMigrate=true or defining the environment variable FLYWAY_BASELINE_ON_MIGRATE=true
  3. Boot up alf.io
  4. You will see an stacktrace like this:
    09:58:47.927 [main] WARN org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminReservationManager' defined in URL [jar:file:/.../alfio-boot.war!/WEB-INF/classes!/alfio/manager/AdminReservationManager.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eventManager' defined in URL [jar:file:/.../alfio-boot.war!/WEB-INF/classes!/alfio/manager/EventManager.class]: Unsatisfied dependency expressed through constructor parameter 14; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'migrator' defined in alfio.config.DataSourceConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'migrator' threw exception; nested exception is org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "public" without metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table.

Expected behavior
What I should expect is to respect the actual DB and add the corresponding tables required to boot alfio

Screenshots
N/A
Desktop (please complete the following information):
N/A
Smartphone (please complete the following information):
N/A

Additional context
Branch 1.x-maintenance

I think I found a solution for this, and I'll lunch a MR in order set the proper Flyway environment variables in order to skip this behaviour.

PS: There is another issue after solving this which is that the schema_version table will be created on the 1st bootup but incorrectly. This is what you will see the next stacktrace after fixing the previous error:

21:52:54.384 [main] ERROR org.flywaydb.core.internal.command.DbMigrate - Migration of schema "public" to version 2 - ADD CONFIRMATION TS failed! Changes successfully rolled back.
2018-12-01T22:52:54+01:00 21:52:54.390 [main] WARN org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminReservationManager' defined in URL [jar:file:/.../alfio-boot.war!/WEB-INF/classes!/alfio/manager/AdminReservationManager.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eventManager' defined in URL [jar:file:/.../alfio-boot.war!/WEB-INF/classes!/alfio/manager/EventManager.class]: Unsatisfied dependency expressed through constructor parameter 14; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'migrator' defined in alfio.config.DataSourceConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'migrator' threw exception; nested exception is org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateSqlException:
2018-12-01T22:52:54+01:00 Migration V2__ADD_CONFIRMATION_TS.sql failed
2018-12-01T22:52:54+01:00 --------------------------------------------
2018-12-01T22:52:54+01:00 SQL State : 42P01
2018-12-01T22:52:54+01:00 Error Code : 0
2018-12-01T22:52:54+01:00 Message : ERROR: relation "tickets_reservation" does not exist
2018-12-01T22:52:54+01:00 Location : alfio/db/PGSQL/V2__ADD_CONFIRMATION_TS.sql (/..../alfio-boot.war!/WEB-INF/classes!/alfio/db/PGSQL/V2__ADD_CONFIRMATION_TS.sql)
2018-12-01T22:52:54+01:00 Line : 18
2018-12-01T22:52:54+01:00 Statement : alter table tickets_reservation add column confirmation_ts timestamp with time zone
2018-12-01T22:52:54+01:00 21:52:54.433 [main] ERROR org.springframework.boot.SpringApplication - Application startup failed

But if you connect to the database, delete the existing rows of this table and restarts alf.io, finally you will get the system up & running ;-)

@syjer
Copy link
Member

syjer commented Dec 10, 2018

hi @cougil , we have not forgotten this bug, we will check and accept the PR :)

@cbellone
Copy link
Member

implemented. Thanks @cougil!

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

No branches or pull requests

3 participants