Home > Net >  Can spring create a new schema (using flyway) at startup and then connect to it via default datasour
Can spring create a new schema (using flyway) at startup and then connect to it via default datasour

Time:12-01

My spring-boot app has following properties set,

spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/my-schema
spring.datasource.username=root
spring.datasource.password=*****
spring.flyway.check-location=false
spring.flyway.createSchemas=true
spring.flyway.schemas=my-schema

The schema 'my-schema' does not pre-exist and I would want for it to be created by flyway and then be used by spring-boot app to sping up HikarCP datasource.

If I run the application with the above configurations I get the following error upon startup:

Caused by: org.flywaydb.core.internal.exception.FlywaySqlException:
Unable to obtain connection from database: Unknown database 'my-schema'

Now, if I change,

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/

The application starts up perfectly and creates the schema. However, when it tries to query any table the thrown exception is:

java.sql.SQLException: No database selected

CodePudding user response:

You can configure Flyway with a URL that's used purely for migrations and then configure your app to use a different URL. Something like this:

spring.flyway.url=jdbc:mysql://127.0.0.1:3306
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/my-schema

You'll also need to provide credentials for the Flyway-specific connection to the database using spring.flyway.user and spring.flyway.password.

  • Related