Search code examples
mysqldockerspring-bootflyway

Unable to run the flyway scripts from Spring Boot application with database as Mysql in docker container


I have the spring boot application which has so many flyway scripts. This scripts will run automatically as soon as boot app brings up. they are running properly in without docker. when I put this application in docker container application.yml

server:
  port:8080
  context-path:/tms-server

http:
  mappers:
     jsonPrettyPrint:true
security:
  basic:
     enabled:false
  cors:
     enabled:true

flyway:
   enabled:true
  clean-on-validation-error:false
   validate-on-migrate:false
   url:jdbc:mysql://mysql-docker-container:3306/synfioo_poc?useSSL=false
   user:app_user
  password:test123
  schemas:synfioo_poc
  locations:db/migration/mysql
spring:
   profiles:
     active:mysql

flyway Sql file is located in classpath:db/migration/mysql

db/migration/mysql/V0001__R001_Create_schema.sql:

CREATE TABLE synfioo_poc.Binary_Object (
  id BIGINT NOT NULL AUTO_INCREMENT,
  modification_counter INTEGER NOT NULL,
  data BLOB(2147483647),
  size BIGINT NOT NULL,
  mime_Type VARCHAR(255),
  PRIMARY KEY (ID)
)

my application-mysql.yml Looks like

spring:
  jpa:
     database:mysql
     database-platform:org.hibernate.dialect.MySQL5Dialect
  datasource:
      url:jdbc:mysql://mysql-docker-container:3306/synfioo_poc?useSSL=false
      username:app_user
      password:test123
      driver-class-name:com.mysql.jdbc.Driver

After docker build I'm linking my Spring Boot with Mysql Db like this
assume that I have created mysql image properly and it running able to create the table from mysql client but flyway scripts are not running from java program also created the docker file properly

docker run -t --name synfioo-poc-container --link  mysql-docker-container:mysql -p 8080:8080  docker-synfioo-core:latest

The exception:

2018-05-31 13:28:45.746  INFO 1 --- [           main] o.f.core.internal.util.VersionPrinter    : Flyway 3.2.1 by Boxfuse
2018-05-31 13:28:46.783  INFO 1 --- [           main] o.f.c.i.dbsupport.DbSupportFactory       : Database: jdbc:h2:mem:testdb (H2 1.4)
2018-05-31 13:28:47.457  INFO 1 --- [           main] o.f.core.internal.command.DbValidate     : Validated 18 migrations (execution time 00:00.506s)
2018-05-31 13:28:47.502  INFO 1 --- [           main] o.f.c.i.metadatatable.MetaDataTableImpl  : Creating Metadata table: "PUBLIC"."schema_version"
2018-05-31 13:28:47.609  INFO 1 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema "PUBLIC": << Empty Schema >>
2018-05-31 13:28:47.615  INFO 1 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "PUBLIC" to version 0001 - R001 Create schema
2018-05-31 13:28:47.638 ERROR 1 --- [           main] o.f.core.internal.command.DbMigrate      : Migration of schema "PUBLIC" to version 0001 failed! Please restore back
ups and roll back database and code!
2018-05-31 13:28:47.670  WARN 1 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh
 attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'beansBatchConfig': Unsatisfied dependency expressed through me
thod 'setTransactionManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager'
 defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Initialization of bean failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoc
onfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException:
Migration V0001__R001_Create_schema.sql failed
----------------------------------------------
SQL State  : 90079
Error Code : 90079
Message    : Schema "SYNFIOO_POC" not found; SQL statement:
CREATE TABLE synfioo_poc.Binary_Object (
  id BIGINT NOT NULL AUTO_INCREMENT,
  modification_counter INTEGER NOT NULL,
  data BLOB(2147483647),
  size BIGINT NOT NULL,
  mime_Type VARCHAR(255),
  PRIMARY KEY (ID)
) [90079-193]
Location   : db/migration/mysql/V0001__R001_Create_schema.sql (/file:/synfioo-core-0.0.1-SNAPSHOT.jar!/db/migration/mysql/V0001__R001_Create_schema.sq
Line       : 8
Statement  : CREATE TABLE synfioo_poc.Binary_Object (
  id BIGINT NOT NULL AUTO_INCREMENT,
  modification_counter INTEGER NOT NULL,
  data BLOB(2147483647),
  size BIGINT NOT NULL,
  mime_Type VARCHAR(255),
  PRIMARY KEY (ID)
)

Can anyone help on this issue.


Solution

  • Message    : Schema "SYNFIOO_POC" not found; SQL statement:
    CREATE TABLE synfioo_poc.Binary_Object (
    ...
    

    Looks like the synfioo_poc schema / database referenced in V0001__R001_Create_schema.sql does not exist in the mysql-docker-container instance.

    Try adding createDatabaseIfNotExist=true to the jdbc url for mysql to automatically create it, if necessary:

    url:jdbc:mysql://mysql-docker-container:3306/synfioo_poc?useSSL=false&createDatabaseIfNotExist=true